SickLMS2xxMessage.cc
Go to the documentation of this file.
1 
16 /* Auto-generated header */
18 
19 /* Implementation dependencies */
20 #include <iomanip>
21 #include <iostream>
22 
25 
26 /* Associate the namespace */
27 namespace SickToolbox {
28 
33 
34  /* Initialize the object */
35  Clear();
36  }
37 
44  SickLMS2xxMessage::SickLMS2xxMessage( const uint8_t dest_address, const uint8_t * const payload_buffer, const unsigned int payload_length ) :
46 
47  /* Build the message */
48  BuildMessage(dest_address,payload_buffer,payload_length);
49  }
50 
55  SickLMS2xxMessage::SickLMS2xxMessage( uint8_t * const message_buffer ) :
57 
58  /* Parse the byte sequence into a message object */
59  ParseMessage(message_buffer);
60  }
61 
68  void SickLMS2xxMessage::BuildMessage( const uint8_t dest_address, const uint8_t * const payload_buffer,
69  const unsigned int payload_length ) {
70 
71  /* Call the parent method!
72  * NOTE: The parent method resets the object and assigns _message_length, _payload_length,
73  * _populated and copies the payload into the message buffer at the correct position
74  */
76  ::BuildMessage(payload_buffer,payload_length);
77 
78  /*
79  * Set the message header!
80  */
81  _message_buffer[0] = 0x02; // Start of transmission (stx)
82  _message_buffer[1] = dest_address; // Sick LMS address
83 
84  /* Include the payload length in the header */
85  uint16_t payload_length_16 = host_to_sick_lms_2xx_byte_order((uint16_t)_payload_length);
86  memcpy(&_message_buffer[2],&payload_length_16,2);
87 
88  /*
89  * Set the message trailer!
90  */
92  memcpy(&_message_buffer[_payload_length+4],&_checksum,2);
93 
94  }
95 
100  void SickLMS2xxMessage::ParseMessage( const uint8_t * const message_buffer ) {
101 
102  /* Call the parent method!
103  * NOTE: This method resets the object and assigns _populated as true
104  */
106  ::ParseMessage(message_buffer);
107 
108  /* Extract the payload length */
109  uint16_t payload_length_16 = 0;
110  memcpy(&payload_length_16,&message_buffer[2],2);
111  _payload_length = (unsigned int)sick_lms_2xx_to_host_byte_order(payload_length_16);
112 
113  /* Compute the total message length */
115 
116  /* Copy the give message into the buffer */
117  memcpy(_message_buffer, message_buffer,_message_length);
118 
119  /* Extract the checksum from the frame */
120  memcpy(&_checksum,&_message_buffer[_payload_length+MESSAGE_HEADER_LENGTH],2);
122 
123  }
124 
129 
130  /* Call the parent method and clear out class' protected members */
132 
133  /* Reset the class' additional fields */
134  _checksum = 0;
135 
136  }
137 
141  void SickLMS2xxMessage::Print( ) const {
142 
143  std::cout.setf(std::ios::hex,std::ios::basefield);
144  std::cout << "Checksum: " << (unsigned int) GetChecksum() << std::endl;
145  std::cout << "Dest. Addr.: " << (unsigned int) GetDestAddress() << std::endl;
146  std::cout << "Command Code: " << (unsigned int) GetCommandCode() << std::endl;
147  std::cout << std::flush;
148 
149  /* Call parent's print function */
151  }
152 
159  uint16_t SickLMS2xxMessage::_computeCRC( uint8_t * data, unsigned int data_length ) const {
160 
161  uint16_t uCrc16;
162  uint8_t abData[2];
163  uCrc16 = abData[0] = 0;
164  while (data_length-- ) {
165  abData[1] = abData[0];
166  abData[0] = *data++;
167  if(uCrc16 & 0x8000) {
168  uCrc16 = (uCrc16 & 0x7fff) << 1;
169  uCrc16 ^= CRC16_GEN_POL;
170  }
171  else {
172  uCrc16 <<= 1;
173  }
174  uCrc16 ^= MKSHORT(abData[0],abData[1]);
175  }
176  return uCrc16;
177  }
178 
180 
181 } /* namespace SickToolbox */
virtual void ParseMessage(const uint8_t *const message_buffer)=0
Parses a sequence of bytes into a Sick message.
Definition: SickMessage.hh:133
#define SICK_LMS_2XX_MSG_PAYLOAD_MAX_LEN
Sick LMS max payload length in bytes.
uint16_t _computeCRC(uint8_t *data, unsigned int data_length) const
Computes the CRC16 of the given data buffer.
uint16_t host_to_sick_lms_2xx_byte_order(uint16_t value)
Converts host byte order (little-endian) to Sick LMS byte order (little-endian)
void BuildMessage(const uint8_t *const payload_buffer, const unsigned int payload_length)
Constructs a Sick message given the parameter values.
Definition: SickMessage.hh:111
#define MKSHORT(a, b)
Defines simple utility functions for working with the Sick LMS 2xx laser range finder units...
Definition of class SickLMS2xxMessage.
virtual void Print() const
Print data about this object.
Definition: SickMessage.hh:201
void Clear()
Reset all internal fields and buffers associated with the object.
void ParseMessage(const uint8_t *const message_buffer)
Parses a sequence of bytes into a well-formed message.
#define CRC16_GEN_POL
Used to compute CRCs.
virtual void Clear()
Reset all internal fields and buffers.
Definition: SickMessage.hh:185
#define SICK_LMS_2XX_MSG_TRAILER_LEN
Sick LMS message trailer length in bytes.
SickLMS2xxMessage()
A default constructor.
uint16_t sick_lms_2xx_to_host_byte_order(uint16_t value)
Converts Sick LMS byte order (little-endian) to host byte order (little-endian)
void BuildMessage(uint8_t dest_address, const uint8_t *const payload_buffer, const unsigned int payload_length)
Consructs a message object from given parameter values.
Provides an abstract parent for all Sick messages.
Definition: SickMessage.hh:32
void Print() const
Print the message contents.
Encapsulates the Sick LIDAR Matlab/C++ toolbox.
Definition: SickLD.cc:44
#define SICK_LMS_2XX_MSG_HEADER_LEN
Sick LMS message length in bytes.


sicktoolbox
Author(s): Jason Derenick , Thomas Miller
autogenerated on Tue Sep 10 2019 03:37:34