MRPT  2.0.0
CSickLaserSerial.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>
13 #include <mrpt/math/TPose3D.h>
14 
15 namespace mrpt::hwdrivers
16 {
17 /** This "software driver" implements the communication protocol for interfacing
18  * a SICK LMS 2XX laser scanners through a standard RS232 serial port (or a
19  * USB2SERIAL converter).
20  * The serial port is opened upon the first call to "doProcess" or
21  * "initialize", so you must call "loadConfig" before
22  * this, or manually call "setSerialPort". Another alternative is to call the
23  * base class method C2DRangeFinderAbstract::bindIO,
24  * but the "setSerialPort" interface is probably much simpler to use.
25  *
26  * For an example of usage see the example in
27  * "samples/SICK_laser_serial_test".
28  * See also the example configuration file for rawlog-grabber in
29  * "share/mrpt/config_files/rawlog-grabber".
30  *
31  * \code
32  * PARAMETERS IN THE ".INI"-LIKE CONFIGURATION STRINGS:
33  * -------------------------------------------------------
34  * [supplied_section_name]
35  * COM_port_WIN = COM1 // Serial port to connect to
36  * COM_port_LIN = ttyS0
37  *
38  * COM_baudRate = 38400 // Possible values: 9600 (default), 38400, 5000000
39  * mm_mode = 1/0 // 1: millimeter mode, 0:centimeter mode (Default=0)
40  * FOV = 180 // Field of view: 100 or 180 degrees (Default=180)
41  * resolution = 50 // Scanning resolution, in units of 1/100 degree.
42  * Valid values: 25,50,100 (Default=50)
43  * //skip_laser_config = true // (Default:false) If true, doesn't send the
44  * initialization commands to the laser and go straight to capturing
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  *
53  * //preview = true // Enable GUI visualization of captured data
54  *
55  * // Optional: Exclusion zones to avoid the robot seeing itself:
56  * //exclusionZone1_x = 0.20 0.30 0.30 0.20
57  * //exclusionZone1_y = 0.20 0.30 0.30 0.20
58  *
59  * // Optional: Exclusion zones to avoid the robot seeing itself:
60  * //exclusionAngles1_ini = 20 // Deg
61  * //exclusionAngles1_end = 25 // Deg
62  * \endcode
63  *
64  * \sa C2DRangeFinderAbstract
65  * \ingroup mrpt_hwdrivers_grp
66  */
68 {
70 
71  private:
72  bool m_mm_mode{false};
73  /** 100 or 180 deg */
74  int m_scans_FOV{180};
75  /** 1/100th of deg: 100, 50 or 25 */
76  int m_scans_res{50};
77 
78  /** The sensor 6D pose: */
80 
81  static int CRC16_GEN_POL;
82 
83  /** Tries to open the com port and setup all the LMS protocol. Returns true
84  * if OK or already open. */
85  bool tryToOpenComms(std::string* err_msg = nullptr);
87  std::vector<float>& ranges, unsigned char& LMS_status,
88  bool& is_mm_mode);
89 
90  /** Assures laser is connected and operating at 38400, in its case returns
91  * true. */
92  bool LMS_setupSerialComms();
93  /** Send a command to change the LMS comms baudrate, return true if ACK is
94  * OK. baud can be: 9600, 19200, 38400, 500000 */
95  bool LMS_setupBaudrate(int baud);
96  /** Send a status query and wait for the answer. Return true on OK. */
97  bool LMS_statusQuery();
98  /** Returns false if timeout */
99  bool LMS_waitACK(uint16_t timeout_ms);
100  /** Returns false if timeout */
101  bool LMS_waitIncomingFrame(uint16_t timeout);
102  /** Returns false on error */
105  bool LMS_endContinuousMode();
106 
107  /** Send header+command-data+crc and waits for ACK. Return false on error.
108  */
109  bool SendCommandToSICK(const uint8_t* cmd, const uint16_t cmd_len);
110 
111  uint8_t m_received_frame_buffer[2000];
112 
113  /** If set to non-empty, the serial port will be attempted to be opened
114  * automatically when this class is first used to request data from the
115  * laser. */
116  std::string m_com_port;
117  /** Will be !=nullptr only if I created it, so I must destroy it at the end.
118  */
120  /** Baudrate: 9600, 38400, 500000 */
121  int m_com_baudRate{38400};
122  /** Default = 1 */
123  unsigned int m_nTries_connect{1};
124  unsigned int m_nTries_current{0};
125  /** If true, doesn't send the initialization commands to the laser and go
126  * straight to capturing */
127  bool m_skip_laser_config{false};
128 
129  protected:
130  /** See the class documentation at the top for expected parameters */
132  const mrpt::config::CConfigFileBase& configSource,
133  const std::string& iniSection) override;
134 
135  public:
136  /** Constructor */
138 
139  /** Destructor */
140  ~CSickLaserSerial() override;
141 
142  /** Changes the serial port to connect to (call prior to 'doProcess'), for
143  * example "COM1" or "ttyS0".
144  * This is not needed if the configuration is loaded with "loadConfig".
145  */
146  void setSerialPort(const std::string& port) { m_com_port = port; }
147  /** \sa setSerialPort */
148  std::string getSerialPort() const { return m_com_port; }
149  /** Changes the serial port baud rate (call prior to 'doProcess'); valid
150  * values are 9600,38400 and 500000.
151  * This is not needed if the configuration is loaded with "loadConfig".
152  * \sa getBaudRate */
153  void setBaudRate(int baud) { m_com_baudRate = baud; }
154  /** \sa setBaudRate */
155  int getBaudRate() const { return m_com_baudRate; }
156  /** Enables/Disables the millimeter mode, with a greater accuracy but a
157  * shorter range (default=false)
158  * (call prior to 'doProcess') This is not needed if the configuration is
159  * loaded with "loadConfig".
160  */
161  void setMillimeterMode(bool mm_mode = true) { m_mm_mode = mm_mode; }
162  /** Set the scanning field of view - possible values are 100 or 180
163  * (default)
164  * (call prior to 'doProcess') This is not needed if the configuration is
165  * loaded with "loadConfig".
166  */
167  void setScanFOV(int fov_degrees) { m_scans_FOV = fov_degrees; }
168  int getScanFOV() const { return m_scans_FOV; }
169  /** Set the scanning resolution, in units of 1/100 degree - Possible values
170  * are 25, 50 and 100, for 0.25, 0.5 (default) and 1 deg.
171  * (call prior to 'doProcess') This is not needed if the configuration is
172  * loaded with "loadConfig".
173  */
174  void setScanResolution(int res_1_100th_degree)
175  {
176  m_scans_res = res_1_100th_degree;
177  }
178  int getScanResolution() const { return m_scans_res; }
179  /** If performing several tries in ::initialize(), this is the current try
180  * loop number. */
181  unsigned int getCurrentConnectTry() const { return m_nTries_current; }
182  /** Specific laser scanner "software drivers" must process here new data
183  * from the I/O stream, and, if a whole scan has arrived, return it.
184  * This method will be typically called in a different thread than other
185  * methods, and will be called in a timely fashion.
186  */
187  void doProcessSimple(
188  bool& outThereIsObservation,
189  mrpt::obs::CObservation2DRangeScan& outObservation,
190  bool& hardwareError) override;
191 
192  /** Set-up communication with the laser.
193  * Called automatically by rawlog-grabber.
194  * If used manually, call after "loadConfig" and before "doProcess".
195  *
196  * In this class this method does nothing, since the communications are
197  * setup at the first try from "doProcess" or "doProcessSimple".
198  */
199  void initialize() override;
200 
201  /** Enables the scanning mode (in this class this has no effect).
202  * \return If everything works "true", or "false" if there is any error.
203  */
204  bool turnOn() override;
205 
206  /** Disables the scanning mode (in this class this has no effect).
207  * \return If everything works "true", or "false" if there is any error.
208  */
209  bool turnOff() override;
210 
211 }; // End of class
212 
213 } // namespace mrpt::hwdrivers
void setMillimeterMode(bool mm_mode=true)
Enables/Disables the millimeter mode, with a greater accuracy but a shorter range (default=false) (ca...
std::shared_ptr< mrpt::comms::CSerialPort > m_mySerialPort
Will be !=nullptr only if I created it, so I must destroy it at the end.
bool LMS_sendMeasuringMode_cm_mm()
Returns false on error.
void setSerialPort(const std::string &port)
Changes the serial port to connect to (call prior to &#39;doProcess&#39;), for example "COM1" or "ttyS0"...
void initialize() override
Set-up communication with the laser.
bool LMS_statusQuery()
Send a status query and wait for the answer.
Contains classes for various device interfaces.
void loadConfig_sensorSpecific(const mrpt::config::CConfigFileBase &configSource, const std::string &iniSection) override
See the class documentation at the top for expected parameters.
int m_com_baudRate
Baudrate: 9600, 38400, 500000.
bool turnOn() override
Enables the scanning mode (in this class this has no effect).
bool LMS_setupSerialComms()
Assures laser is connected and operating at 38400, in its case returns true.
void setScanFOV(int fov_degrees)
Set the scanning field of view - possible values are 100 or 180 (default) (call prior to &#39;doProcess&#39;)...
bool LMS_setupBaudrate(int baud)
Send a command to change the LMS comms baudrate, return true if ACK is OK.
std::string m_com_port
If set to non-empty, the serial port will be attempted to be opened automatically when this class is ...
This class allows loading and storing values and vectors of different types from a configuration text...
bool m_skip_laser_config
If true, doesn&#39;t send the initialization commands to the laser and go straight to capturing...
bool waitContinuousSampleFrame(std::vector< float > &ranges, unsigned char &LMS_status, bool &is_mm_mode)
bool tryToOpenComms(std::string *err_msg=nullptr)
Tries to open the com port and setup all the LMS protocol.
bool LMS_waitIncomingFrame(uint16_t timeout)
Returns false if timeout.
void setScanResolution(int res_1_100th_degree)
Set the scanning resolution, in units of 1/100 degree - Possible values are 25, 50 and 100...
mrpt::math::TPose3D m_sensorPose
The sensor 6D pose:
#define DEFINE_GENERIC_SENSOR(class_name)
This declaration must be inserted in all CGenericSensor classes definition, within the class declarat...
int m_scans_res
1/100th of deg: 100, 50 or 25
This is the base, abstract class for "software drivers" interfaces to 2D scanners (laser range finder...
void setBaudRate(int baud)
Changes the serial port baud rate (call prior to &#39;doProcess&#39;); valid values are 9600,38400 and 500000.
bool SendCommandToSICK(const uint8_t *cmd, const uint16_t cmd_len)
Send header+command-data+crc and waits for ACK.
A "CObservation"-derived class that represents a 2D range scan measurement (typically from a laser sc...
This "software driver" implements the communication protocol for interfacing a SICK LMS 2XX laser sca...
bool turnOff() override
Disables the scanning mode (in this class this has no effect).
Lightweight 3D pose (three spatial coordinates, plus three angular coordinates).
Definition: TPose3D.h:24
unsigned int m_nTries_connect
Default = 1.
void doProcessSimple(bool &outThereIsObservation, mrpt::obs::CObservation2DRangeScan &outObservation, bool &hardwareError) override
Specific laser scanner "software drivers" must process here new data from the I/O stream...
bool LMS_waitACK(uint16_t timeout_ms)
Returns false if timeout.
~CSickLaserSerial() override
Destructor.
unsigned int getCurrentConnectTry() const
If performing several tries in initialize(), this is the current try loop number. ...



Page generated by Doxygen 1.8.14 for MRPT 2.0.0 Git: b38439d21 Tue Mar 31 19:58:06 2020 +0200 at miƩ abr 1 00:50:30 CEST 2020