SickPLSBufferMonitor.cc
Go to the documentation of this file.
1 
17 /* Auto-generated header */
18 //#include "SickConfig.hh"
19 
20 /* Implementation dependencies */
21 #include <iostream>
22 #include <termios.h>
23 
24 #include "SickPLS.hh"
25 #include "SickPLSBufferMonitor.hh"
26 #include "SickPLSMessage.hh"
27 #include "SickPLSUtility.hh"
28 #include "SickException.hh"
29 
30 /* Associate the namespace */
31 namespace SickToolbox
32 {
33 
38 
44 {
45 
46  uint8_t search_buffer[2] = {0};
47  uint8_t payload_length_buffer[2] = {0};
48  uint8_t payload_buffer[SickPLSMessage::MESSAGE_PAYLOAD_MAX_LENGTH] = {0};
49  uint8_t checksum_buffer[2] = {0};
50  uint16_t payload_length, checksum;
51 
52  try
53  {
54 
55  /* Drain the I/O buffers! */
56  if (tcdrain(_sick_fd) != 0)
57  {
58  throw SickIOException("SickPLSBufferMonitor::GetNextMessageFromDataStream: tcdrain failed!");
59  }
60 
61  /* Read until we get a valid message header */
62  unsigned int bytes_searched = 0;
63  while(search_buffer[0] != 0x02 || search_buffer[1] != DEFAULT_SICK_PLS_HOST_ADDRESS)
64  {
65 
66  /* Slide the search window */
67  search_buffer[0] = search_buffer[1];
68 
69  /* Attempt to read in another byte */
71 
72 
73  /* Header should be no more than max message length + header length bytes away */
75  {
76  throw SickTimeoutException("SickPLSBufferMonitor::GetNextMessageFromDataStream: header timeout!");
77  }
78 
79  /* Increment the number of bytes searched */
80  bytes_searched++;
81 
82  // std::cout.setf(std::ios::hex,std::ios::basefield);
83  // std::cout << "search_buffer[1]: " << search_buffer[1]<<std::endl;
84  // std::cout.setf(std::ios::dec,std::ios::basefield);
85  // std::cout << " bytes searched: " << bytes_searched<< std::endl;
86 
87 
88  }
89 
90  /* Read until we receive the payload length or we timeout */
91  _readBytes(payload_length_buffer,2,DEFAULT_SICK_PLS_SICK_BYTE_TIMEOUT);
92 
93 
94 
95  /* Extract the payload length */
96  memcpy(&payload_length,payload_length_buffer,2);
97  payload_length = sick_pls_to_host_byte_order(payload_length);
98 
99  /* Make sure the payload length is legitimate, otherwise disregard */
100  if (payload_length <= SickPLSMessage::MESSAGE_MAX_LENGTH)
101  {
102 
103  /* Read until we receive the payload or we timeout */
104  _readBytes(payload_buffer,payload_length,DEFAULT_SICK_PLS_SICK_BYTE_TIMEOUT);
105 
106  /* Read until we receive the checksum or we timeout */
108 
109  /* Copl;y into uint16_t so it can be used */
110  memcpy(&checksum,checksum_buffer,2);
111  checksum = sick_pls_to_host_byte_order(checksum);
112 
113  /* Build a frame and compute the crc */
114  sick_message.BuildMessage(DEFAULT_SICK_PLS_HOST_ADDRESS,payload_buffer,payload_length);
115 
116  /* See if the checksums match */
117  if(sick_message.GetChecksum() != checksum)
118  {
119  throw SickBadChecksumException("SickPLS::GetNextMessageFromDataStream: CRC16 failed!");
120  }
121 
122  }
123 
124  }
125 
126  catch(SickTimeoutException &sick_timeout_exception)
127  {
128  /* This is ok! as we're usually waiting for the laser to respond*/
129  //std::cout<<"is this really ok?: "<<sick_timeout_exception.what()<<std::endl;
130  }
131 
132  /* Handle a bad checksum! */
133  catch(SickBadChecksumException &sick_checksum_exception)
134  {
135  sick_message.Clear(); // Clear the message container
136  }
137 
138  /* Handle any serious IO exceptions */
139  catch(SickIOException &sick_io_exception)
140  {
141  throw;
142  }
143 
144  /* A sanity check */
145  catch (...)
146  {
147  throw;
148  }
149 
150 }
151 
156 
157 } /* namespace SickToolbox */
Defines simple utility functions for working with the Sick LMS 2xx laser range finder units...
Contains some simple exception classes.
void _readBytes(uint8_t *const dest_buffer, const int num_bytes_to_read, const unsigned int timeout_value=0) const
uint16_t sick_pls_to_host_byte_order(uint16_t value)
Converts Sick LMS byte order (little-endian) to host byte order (little-endian)
A class for monitoring the receive buffer when interfacing with a Sick PLS LIDAR. ...
Definition of class SickPLSMessage.
~SickPLSBufferMonitor()
A standard destructor.
SickPLSBufferMonitor()
A standard constructor.
#define DEFAULT_SICK_PLS_HOST_ADDRESS
Client/host default serial address.
Definition: SickPLS.hh:31
void GetNextMessageFromDataStream(SickPLSMessage &sick_message)
Acquires the next message from the SickPLS byte stream.
#define DEFAULT_SICK_PLS_SICK_BYTE_TIMEOUT
Max allowable time between consecutive bytes.
Defines a class for monitoring the receive buffer when interfacing w/ a Sick PLS laser range finder...
A class to represent all messages sent to and from the Sick PLS.
Definition of class SickPLS. Code by Jason C. Derenick and Thomas H. Miller. Contact derenick(at)lehi...
Thrown instance where the driver can&#39;t read,write,drain,flush,... the buffers.
Makes handling timeouts much easier.
Thrown when a received message has an invalid checksum.


asr_mild_base_laserscanner
Author(s): Aumann Florian, Borella Jocelyn, Dehmani Souheil, Marek Felix, Reckling Reno
autogenerated on Mon Jun 10 2019 12:41:37