MRPT  2.0.2
CCANBusReader.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
11 #include <mrpt/comms/CSerialPort.h>
15 
16 namespace mrpt::hwdrivers
17 {
18 /** This "software driver" implements the communication protocol for interfacing
19  * a SICK LMS 2XX laser scanners through a standard RS232 serial port (or a
20  * USB2SERIAL converter).
21  * The serial port is opened upon the first call to "doProcess" or
22  * "initialize", so you must call "loadConfig" before
23  * this, or manually call "setSerialPort". Another alternative is to call the
24  * base class method C2DRangeFinderAbstract::bindIO,
25  * but the "setSerialPort" interface is probably much simpler to use.
26  *
27  * For an example of usage see the example in
28  * "samples/SICK_laser_serial_test".
29  * See also the example configuration file for rawlog-grabber in
30  * "share/mrpt/config_files/rawlog-grabber".
31  *
32  * \code
33  * PARAMETERS IN THE ".INI"-LIKE CONFIGURATION STRINGS:
34  * -------------------------------------------------------
35  * [supplied_section_name]
36  * COM_port_WIN = COM1 // Serial port to connect to
37  * COM_port_LIN = ttyS0
38  *
39  * COM_baudRate = 38400 // Possible values: 9600 (default), 38400, 5000000
40  * mm_mode = 1/0 // 1: millimeter mode, 0:centimeter mode (Default=0)
41  * FOV = 180 // Field of view: 100 or 180 degrees (Default=180)
42  * resolution = 50 // Scanning resolution, in units of 1/100 degree.
43  * Valid values: 25,50,100 (Default=50)
44  *
45  *
46  * pose_x=0.21 // Laser range scaner 3D position in the robot (meters)
47  * pose_y=0
48  * pose_z=0.34
49  * pose_yaw=0 // Angles in degrees
50  * pose_pitch=0
51  * pose_roll=0
52  * \endcode
53  *
54  * \sa C2DRangeFinderAbstract
55  * \ingroup mrpt_hwdrivers_grp
56  */
58 {
60 
61  private:
62  /** Tries to open the com port and setup all the LMS protocol. Returns true
63  * if OK or already open. */
64  bool tryToOpenComms(std::string* err_msg = nullptr);
66  uint8_t& out_prio, uint8_t& out_pdu_format, uint8_t& out_pdu_spec,
67  uint8_t& out_src_address, uint8_t& out_data_length, uint16_t& out_pgn,
68  std::vector<uint8_t>& out_data, std::vector<char>& out_raw_frame);
69 
70  /** Sends the specified speed to the CAN Converter. */
71  bool sendCANBusReaderSpeed();
72  /** Opens the CAN Channel */
73  bool CANBusOpenChannel();
74  /** Closes the CAN Channel */
75  bool CANBusCloseChannel();
76  bool CANBusAutoPoll();
77  bool CANBusPoll();
78  bool CANBusX1();
79  bool setupSerialComms();
80  bool queryVersion(bool printOutVersion = false);
81  bool waitACK(uint16_t timeout_ms);
82  bool waitForVersion(uint16_t timeout, bool printOutVersion = false);
83  bool waitIncomingFrame(uint16_t timeout);
84 
86  const uint8_t* cmd, const uint16_t cmd_len, bool wait = true);
87 
88  uint8_t m_received_frame_buffer[2000];
89 
90  /** If set to non-empty, the serial port will be attempted to be opened
91  * automatically when this class is first used to request data from the
92  * laser. */
93  std::string m_com_port;
94  /** Will be !=nullptr only if I created it, so I must destroy it at the end.
95  */
97  /** Baudrate: 9600, 38400, 500000 */
98  int m_com_baudRate{57600};
99  /** Default = 1 */
100  unsigned int m_nTries_connect{1};
101  unsigned int m_nTries_current{0};
102  int m_canbus_speed{250000};
103  bool m_canreader_timestamp{false}; // for future work
105  false}; // if the can bus channel is open or not
106 
107  protected:
108  /** See the class documentation at the top for expected parameters */
110  const mrpt::config::CConfigFileBase& configSource,
111  const std::string& iniSection) override;
112 
113  public:
114  /** Constructor */
115  CCANBusReader();
116 
117  /** Destructor */
118  ~CCANBusReader() override;
119 
120  /** Changes the serial port to connect to (call prior to 'doProcess'), for
121  * example "COM1" or "ttyS0".
122  * This is not needed if the configuration is loaded with "loadConfig".
123  */
124  void setSerialPort(const std::string& port) { m_com_port = port; }
125  /** \sa setSerialPort */
126  std::string getSerialPort() const { return m_com_port; }
127  /** Changes the serial port baud rate (call prior to 'doProcess'); valid
128  * values are 9600,38400 and 500000.
129  * This is not needed if the configuration is loaded with "loadConfig".
130  * \sa getBaudRate */
131  void setBaudRate(int baud) { m_com_baudRate = baud; }
132  /** \sa setBaudRate */
133  int getBaudRate() const { return m_com_baudRate; }
134  /** Enables/Disables the addition of a timestamp according to the arrival
135  * time to the converter (default=false)
136  * (call prior to 'doProcess') This is not needed if the configuration is
137  * loaded with "loadConfig".
138  */
139  void setCANReaderTimeStamping(bool setTimestamp = false)
140  {
141  m_canreader_timestamp = setTimestamp;
142  }
144  /** Sets the CAN reader speed when connecting to the CAN Bus
145  */
146  void setCANReaderSpeed(const unsigned int speed) { m_canbus_speed = speed; }
147  unsigned int getCANReaderSpeed() { return m_canbus_speed; }
148  /** If performing several tries in ::initialize(), this is the current try
149  * loop number. */
150  unsigned int getCurrentConnectTry() const { return m_nTries_current; }
151  /** Specific laser scanner "software drivers" must process here new data
152  * from the I/O stream, and, if a whole scan has arrived, return it.
153  * This method will be typically called in a different thread than other
154  * methods, and will be called in a timely fashion.
155  */
156  void doProcessSimple(
157  bool& outThereIsObservation,
158  mrpt::obs::CObservationCANBusJ1939& outObservation,
159  bool& hardwareError);
160 
161  /** Set-up communication with the laser.
162  * Called automatically by rawlog-grabber.
163  * If used manually, call after "loadConfig" and before "doProcess".
164  *
165  * In this class this method does nothing, since the communications are
166  * setup at the first try from "doProcess" or "doProcessSimple".
167  */
168  void initialize() override;
169 
170  void doProcess() override;
171 
172 }; // End of class
173 
174 } // namespace mrpt::hwdrivers
A generic interface for a wide-variety of sensors designed to be used in the application RawLogGrabbe...
bool sendCANBusReaderSpeed()
Sends the specified speed to the CAN Converter.
void setCANReaderTimeStamping(bool setTimestamp=false)
Enables/Disables the addition of a timestamp according to the arrival time to the converter (default=...
A communications serial port built as an implementation of a utils::CStream.
Definition: CSerialPort.h:41
bool waitACK(uint16_t timeout_ms)
bool waitContinuousSampleFrame(uint8_t &out_prio, uint8_t &out_pdu_format, uint8_t &out_pdu_spec, uint8_t &out_src_address, uint8_t &out_data_length, uint16_t &out_pgn, std::vector< uint8_t > &out_data, std::vector< char > &out_raw_frame)
This class stores a message from a CAN BUS with the protocol J1939.
void doProcessSimple(bool &outThereIsObservation, mrpt::obs::CObservationCANBusJ1939 &outObservation, bool &hardwareError)
Specific laser scanner "software drivers" must process here new data from the I/O stream...
Contains classes for various device interfaces.
std::string getSerialPort() const
bool waitIncomingFrame(uint16_t timeout)
void setBaudRate(int baud)
Changes the serial port baud rate (call prior to &#39;doProcess&#39;); valid values are 9600,38400 and 500000.
bool CANBusCloseChannel()
Closes the CAN Channel.
This class allows loading and storing values and vectors of different types from a configuration text...
bool waitForVersion(uint16_t timeout, bool printOutVersion=false)
Versatile class for consistent logging and management of output messages.
This "software driver" implements the communication protocol for interfacing a SICK LMS 2XX laser sca...
Definition: CCANBusReader.h:57
void initialize() override
Set-up communication with the laser.
uint8_t m_received_frame_buffer[2000]
Definition: CCANBusReader.h:88
bool tryToOpenComms(std::string *err_msg=nullptr)
Tries to open the com port and setup all the LMS protocol.
void setCANReaderSpeed(const unsigned int speed)
Sets the CAN reader speed when connecting to the CAN Bus.
void setSerialPort(const std::string &port)
Changes the serial port to connect to (call prior to &#39;doProcess&#39;), for example "COM1" or "ttyS0"...
#define DEFINE_GENERIC_SENSOR(class_name)
This declaration must be inserted in all CGenericSensor classes definition, within the class declarat...
~CCANBusReader() override
Destructor.
unsigned int getCurrentConnectTry() const
If performing several tries in initialize(), this is the current try loop number. ...
bool sendCommandToCANReader(const uint8_t *cmd, const uint16_t cmd_len, bool wait=true)
mrpt::comms::CSerialPort * m_mySerialPort
Will be !=nullptr only if I created it, so I must destroy it at the end.
Definition: CCANBusReader.h:96
void loadConfig_sensorSpecific(const mrpt::config::CConfigFileBase &configSource, const std::string &iniSection) override
See the class documentation at the top for expected parameters.
std::string m_com_port
If set to non-empty, the serial port will be attempted to be opened automatically when this class is ...
Definition: CCANBusReader.h:93
int m_com_baudRate
Baudrate: 9600, 38400, 500000.
Definition: CCANBusReader.h:98
void doProcess() override
This method will be invoked at a minimum rate of "process_rate" (Hz)
bool CANBusOpenChannel()
Opens the CAN Channel.
bool queryVersion(bool printOutVersion=false)
unsigned int m_nTries_connect
Default = 1.



Page generated by Doxygen 1.8.14 for MRPT 2.0.2 Git: 9b4fd2465 Mon May 4 16:59:08 2020 +0200 at lun may 4 17:26:07 CEST 2020