c++/drivers/lms/sicklms-1.0/SickLMSMessage.hh

Go to the documentation of this file.
00001 /*!
00002  * \file SickLMSMessage.hh
00003  * \brief Definition of class SickLMSMessage.
00004  *
00005  * Code by Jason C. Derenick and Thomas H. Miller.
00006  * Contact derenick(at)lehigh(dot)edu
00007  *
00008  * The Sick LIDAR Matlab/C++ Toolbox
00009  * Copyright (c) 2008, Jason C. Derenick and Thomas H. Miller
00010  * All rights reserved.
00011  *
00012  * This software is released under a BSD Open-Source License.
00013  * See http://sicktoolbox.sourceforge.net
00014  */
00015 
00016 #ifndef SICK_LMS_MESSAGE_HH
00017 #define SICK_LMS_MESSAGE_HH
00018 
00019 /* Auto-generated header */
00020 #include "SickConfig.hh"
00021 
00022 /* Definition dependencies */
00023 #include <string.h>
00024 #include <netinet/in.h>   
00025 #include "SickMessage.hh"
00026 #include "SickException.hh"
00027 
00028 #define CRC16_GEN_POL 0x8005                    ///< Used to compute CRCs
00029 
00030 /** Makes a "short" in little endian */
00031 #define MKSHORT(a,b) ((unsigned short) (a) | ((unsigned short)(b) << 8))
00032 
00033 #define SICK_LMS_MSG_HEADER_LEN            (4)  ///< Sick LMS message length in bytes
00034 #define SICK_LMS_MSG_PAYLOAD_MAX_LEN     (812)  ///< Sick LMS max payload length in bytes
00035 #define SICK_LMS_MSG_TRAILER_LEN           (2)  ///< Sick LMS message trailer length in bytes 
00036 
00037 /* Associate the namespace */
00038 namespace SickToolbox {
00039 
00040   /**
00041    * \brief A class to represent all messages sent to and from the Sick LMS 2xx
00042    *
00043    * This class helps to construct messages to be sent to the Sick. It also
00044    * provides a container for received messages to be parsed into.
00045    */
00046   class SickLMSMessage : public SickMessage< SICK_LMS_MSG_HEADER_LEN, SICK_LMS_MSG_PAYLOAD_MAX_LEN, SICK_LMS_MSG_TRAILER_LEN >
00047   {
00048 
00049   public:
00050   
00051     /** Default constructor. Constructs an empty message (not well-formed!). */
00052     SickLMSMessage( );
00053 
00054     /** Constructs a frame by using BuildMessage(). */
00055     SickLMSMessage( const uint8_t dest_address, const uint8_t * const payload_buffer, const unsigned int payload_length );
00056 
00057     /** Constructs a frame using ParseMessage(). */
00058     SickLMSMessage( uint8_t * const message_buffer );
00059 
00060     /** Constructs a well-formed raw frame from input fields. */
00061     void BuildMessage( uint8_t dest_address, const uint8_t * const payload_buffer,
00062                        const unsigned int payload_length );
00063     
00064     /** Populates fields from a (well-formed) raw frame. */
00065     void ParseMessage( const uint8_t * const message_buffer );
00066 
00067     /** Gets the address of the frame. */
00068     uint8_t GetDestAddress( ) const { return _message_buffer[1]; }
00069 
00070     /** Gets the command code associated with the message */
00071     uint8_t GetCommandCode( ) const { return _message_buffer[MESSAGE_HEADER_LENGTH]; }
00072 
00073     /** Gets the status byte from an LMS response message (NOTE: only applies to Sick LMS response telegrams!) */
00074     uint8_t GetStatusByte( ) const { return _message_buffer[MESSAGE_HEADER_LENGTH+_payload_length-1]; }
00075     
00076     /** Gets the checksum for the message. */
00077     uint16_t GetChecksum( ) const { return _checksum; }
00078     
00079     /** Reset the data associated with this message (for initialization purposes) */
00080     void Clear( );
00081     
00082     /** A debugging function that prints the contents of the message. */
00083     void Print( ) const;
00084 
00085     /** Destructor */
00086     ~SickLMSMessage();
00087 
00088   protected:
00089 
00090     /** The checksum (CRC16) */
00091     uint16_t _checksum;
00092     
00093   private:
00094 
00095     /** Computes the checksum of the frame. */
00096     uint16_t _computeCRC( uint8_t * data, unsigned int data_length ) const;
00097 
00098   };
00099 
00100 } /* namespace SickToolbox */
00101   
00102 #endif //SICK_LMS_MESSAGE_HH

Generated on Thu Mar 20 09:41:42 2008 for sicktoolbox-1.0 by  doxygen 1.5.1