Main MRPT website > C++ reference
MRPT logo

CSickLaserSerial.h

Go to the documentation of this file.
00001 /* +---------------------------------------------------------------------------+
00002    |          The Mobile Robot Programming Toolkit (MRPT) C++ library          |
00003    |                                                                           |
00004    |                   http://mrpt.sourceforge.net/                            |
00005    |                                                                           |
00006    |   Copyright (C) 2005-2011  University of Malaga                           |
00007    |                                                                           |
00008    |    This software was written by the Machine Perception and Intelligent    |
00009    |      Robotics Lab, University of Malaga (Spain).                          |
00010    |    Contact: Jose-Luis Blanco  <jlblanco@ctima.uma.es>                     |
00011    |                                                                           |
00012    |  This file is part of the MRPT project.                                   |
00013    |                                                                           |
00014    |     MRPT is free software: you can redistribute it and/or modify          |
00015    |     it under the terms of the GNU General Public License as published by  |
00016    |     the Free Software Foundation, either version 3 of the License, or     |
00017    |     (at your option) any later version.                                   |
00018    |                                                                           |
00019    |   MRPT is distributed in the hope that it will be useful,                 |
00020    |     but WITHOUT ANY WARRANTY; without even the implied warranty of        |
00021    |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         |
00022    |     GNU General Public License for more details.                          |
00023    |                                                                           |
00024    |     You should have received a copy of the GNU General Public License     |
00025    |     along with MRPT.  If not, see <http://www.gnu.org/licenses/>.         |
00026    |                                                                           |
00027    +---------------------------------------------------------------------------+ */
00028 #ifndef CSickLaserSerial_H
00029 #define CSickLaserSerial_H
00030 
00031 #include <mrpt/hwdrivers/C2DRangeFinderAbstract.h>
00032 #include <mrpt/hwdrivers/CSerialPort.h>
00033 
00034 namespace mrpt
00035 {
00036         namespace hwdrivers
00037         {
00038                 /** This "software driver" implements the communication protocol for interfacing a SICK LMS 2XX laser scanners through a standard RS232 serial port (or a USB2SERIAL converter).
00039                   *   The serial port is opened upon the first call to "doProcess" or "initialize", so you must call "loadConfig" before
00040                   *   this, or manually call "setSerialPort". Another alternative is to call the base class method C2DRangeFinderAbstract::bindIO,
00041                   *   but the "setSerialPort" interface is probably much simpler to use.
00042                   *
00043                   *   For an example of usage see the example in "samples/SICK_laser_serial_test".
00044                   *   See also the example configuration file for rawlog-grabber in "share/mrpt/config_files/rawlog-grabber".
00045                   *
00046                   *  \code
00047                   *  PARAMETERS IN THE ".INI"-LIKE CONFIGURATION STRINGS:
00048                   * -------------------------------------------------------
00049                   *   [supplied_section_name]
00050                   *   COM_port_WIN = COM1   // Serial port to connect to
00051                   *   COM_port_LIN = ttyS0
00052                   *
00053                   *   COM_baudRate = 38400 // Possible values: 9600 (default), 38400, 5000000
00054                   *   mm_mode      = 1/0   // 1: millimeter mode, 0:centimeter mode (Default=0)
00055                   *   FOV          = 180   // Field of view: 100 or 180 degrees (Default=180)
00056                   *   resolution   =  50   // Scanning resolution, in units of 1/100 degree. Valid values: 25,50,100 (Default=50)
00057                   *
00058                   *
00059                   *   pose_x=0.21       // Laser range scaner 3D position in the robot (meters)
00060                   *   pose_y=0
00061                   *   pose_z=0.34
00062                   *   pose_yaw=0        // Angles in degrees
00063                   *   pose_pitch=0
00064                   *   pose_roll=0
00065                   *  \endcode
00066                   *
00067                   * \sa C2DRangeFinderAbstract
00068                   */
00069                 class HWDRIVERS_IMPEXP CSickLaserSerial : public C2DRangeFinderAbstract
00070                 {
00071                         DEFINE_GENERIC_SENSOR(CSickLaserSerial)
00072 
00073                 private:
00074                         bool    m_mm_mode;
00075                         int             m_scans_FOV; //!< 100 or 180 deg
00076                         int             m_scans_res; //!< 1/100th of deg: 100, 50 or 25
00077 
00078                         /** The sensor 6D pose: */
00079                         poses::TPose3D          m_sensorPose;
00080 
00081                         static int                      CRC16_GEN_POL;
00082 
00083 
00084                         bool    tryToOpenComms(std::string *err_msg=NULL);      //!< Tries to open the com port and setup all the LMS protocol. Returns true if OK or already open.
00085                         bool    waitContinuousSampleFrame( std::vector<float> &ranges, unsigned char &LMS_status, bool &is_mm_mode );
00086 
00087 
00088                         bool LMS_setupSerialComms();    //!< Assures laser is connected and operating at 38400, in its case returns true.
00089                         bool LMS_setupBaudrate(int baud);       //!< Send a command to change the LMS comms baudrate, return true if ACK is OK. baud can be: 9600, 19200, 38400, 500000
00090                         bool LMS_statusQuery(); //!< Send a status query and wait for the answer. Return true on OK.
00091                         bool LMS_waitACK(uint16_t timeout_ms); //!< Returns false if timeout
00092                         bool LMS_waitIncomingFrame(uint16_t timeout); //!< Returns false if timeout
00093                         bool LMS_sendMeasuringMode_cm_mm();  //!< Returns false on error
00094                         bool LMS_startContinuousMode();
00095                         bool LMS_endContinuousMode();
00096 
00097             bool SendCommandToSICK(const uint8_t *cmd,const uint16_t cmd_len); //!< Send header+command-data+crc and waits for ACK. Return false on error.
00098 
00099                         uint8_t                 m_received_frame_buffer[2000];
00100 
00101                         std::string             m_com_port;             //!< If set to non-empty, the serial port will be attempted to be opened automatically when this class is first used to request data from the laser.
00102                         CSerialPort             *m_mySerialPort; //!< Will be !=NULL only if I created it, so I must destroy it at the end.
00103                         int             m_com_baudRate; //!< Baudrate: 9600, 38400, 500000
00104                         unsigned int    m_nTries_connect; //!< Default = 1
00105                         unsigned int    m_nTries_current;
00106 
00107                 protected:
00108                         /** Loads specific configuration for the device from a given source of configuration parameters, for example, an ".ini" file, loading from the section "[iniSection]" (see utils::CConfigFileBase and derived classes)
00109                           *  See hwdrivers::CSickLaserSerial for the possible parameters
00110                           */
00111                         void  loadConfig_sensorSpecific(
00112                                 const mrpt::utils::CConfigFileBase &configSource,
00113                                 const std::string         &iniSection );
00114 
00115                 public:
00116                         /** Constructor  */
00117                         CSickLaserSerial();
00118 
00119                         /** Destructor  */
00120                         virtual ~CSickLaserSerial();
00121 
00122                         /** Changes the serial port to connect to (call prior to 'doProcess'), for example "COM1" or "ttyS0".
00123                           *  This is not needed if the configuration is loaded with "loadConfig".
00124                           */
00125                         void  setSerialPort(const std::string &port) { m_com_port = port; }
00126 
00127                         /** \sa setSerialPort */
00128                         std::string getSerialPort() const { return m_com_port; }
00129 
00130                         /** Changes the serial port baud rate (call prior to 'doProcess'); valid values are 9600,38400 and 500000.
00131                           *  This is not needed if the configuration is loaded with "loadConfig".
00132                           *  \sa getBaudRate */
00133                         void setBaudRate(int baud) {
00134                                         m_com_baudRate = baud;
00135                         }
00136                         /** \sa setBaudRate */
00137                         int getBaudRate() const { return m_com_baudRate; }
00138 
00139 
00140                         /** Enables/Disables the millimeter mode, with a greater accuracy but a shorter range (default=false)
00141                           *  (call prior to 'doProcess') This is not needed if the configuration is loaded with "loadConfig".
00142                           */
00143                         void setMillimeterMode(bool mm_mode=true) { m_mm_mode = mm_mode; }
00144 
00145                         /** Set the scanning field of view - possible values are 100 or 180 (default)
00146                           *  (call prior to 'doProcess') This is not needed if the configuration is loaded with "loadConfig".
00147                           */
00148                         void setScanFOV(int fov_degrees) { m_scans_FOV = fov_degrees; }
00149                         int getScanFOV() const { return m_scans_FOV; }
00150 
00151                         /** Set the scanning resolution, in units of 1/100 degree - Possible values are 25, 50 and 100, for 0.25, 0.5 (default) and 1 deg.
00152                           *  (call prior to 'doProcess') This is not needed if the configuration is loaded with "loadConfig".
00153                           */
00154                         void setScanResolution(int res_1_100th_degree) { m_scans_res=res_1_100th_degree; }
00155                         int getScanResolution() const { return m_scans_res; }
00156 
00157             /** If performing several tries in ::initialize(), this is the current try loop number. */
00158                         unsigned int getCurrentConnectTry() const { return m_nTries_current; }
00159 
00160 
00161                         /** Specific laser scanner "software drivers" must process here new data from the I/O stream, and, if a whole scan has arrived, return it.
00162                           *  This method will be typically called in a different thread than other methods, and will be called in a timely fashion.
00163                           */
00164                         void  doProcessSimple(
00165                                 bool                                                    &outThereIsObservation,
00166                                 mrpt::slam::CObservation2DRangeScan     &outObservation,
00167                                 bool                                                    &hardwareError );
00168 
00169 
00170                         /** Set-up communication with the laser.
00171                           *  Called automatically by rawlog-grabber.
00172                           *  If used manually, call after "loadConfig" and before "doProcess".
00173                           *
00174                           *  In this class this method does nothing, since the communications are setup at the first try from "doProcess" or "doProcessSimple".
00175                           */
00176                         void initialize();
00177 
00178                         /** Enables the scanning mode (in this class this has no effect).
00179                           * \return If everything works "true", or "false" if there is any error.
00180                           */
00181                         bool  turnOn();
00182 
00183                         /** Disables the scanning mode (in this class this has no effect).
00184                           * \return If everything works "true", or "false" if there is any error.
00185                           */
00186                         bool  turnOff();
00187 
00188                 };      // End of class
00189 
00190         } // End of namespace
00191 } // End of namespace
00192 
00193 #endif



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN: at Sat Mar 26 06:16:28 UTC 2011