40 #include <arpa/inet.h> 52 template <
class SICK_MONITOR_CLASS,
class SICK_MSG_CLASS >
102 void _sendMessage(
const SICK_MSG_CLASS &sick_message,
const unsigned int byte_interval )
const 110 const uint8_t *
const byte_sequence,
111 const unsigned int byte_sequence_length,
115 double _computeElapsedTime(
const struct timeval &beg_time,
const struct timeval &end_time )
const {
return ((end_time.tv_sec*1e6)+(end_time.tv_usec))-((beg_time.tv_sec*1e6)+beg_time.tv_usec); }
119 SICK_MSG_CLASS &recv_message,
120 const uint8_t *
const byte_sequence,
121 const unsigned int byte_sequence_length,
122 const unsigned int byte_interval,
123 const unsigned int timeout_value,
131 template<
class SICK_MONITOR_CLASS,
class SICK_MSG_CLASS >
139 catch ( std::bad_alloc &allocation_exception ) {
140 std::cerr <<
"SickLIDAR::SickLIDAR: Allocation error - " << allocation_exception.what() << std::endl;
148 template<
class SICK_MONITOR_CLASS,
class SICK_MSG_CLASS >
161 template<
class SICK_MONITOR_CLASS,
class SICK_MSG_CLASS >
170 catch(SickThreadException &sick_thread_exception) {
171 std::cerr << sick_thread_exception.
what() << std::endl;
177 std::cerr <<
"SickLIDAR::_startListening: Unknown exception!!!" << std::endl;
189 template<
class SICK_MONITOR_CLASS,
class SICK_MSG_CLASS >
198 catch(SickThreadException &sick_thread_exception) {
199 std::cerr << sick_thread_exception.
what() << std::endl;
205 std::cerr <<
"SickLIDAR::_stopListening: Unknown exception!!!" << std::endl;
217 template<
class SICK_MONITOR_CLASS,
class SICK_MSG_CLASS >
222 if((fd_flags = fcntl(
_sick_fd,F_GETFL)) < 0) {
223 throw SickIOException(
"SickLIDAR::_setNonBlocking: fcntl failed!");
227 if(fcntl(
_sick_fd,F_SETFL,fd_flags & (~O_NONBLOCK)) < 0) {
228 throw SickIOException(
"SickLIDAR::_setNonBlocking: fcntl failed!");
236 template<
class SICK_MONITOR_CLASS,
class SICK_MSG_CLASS >
241 if((fd_flags = fcntl(
_sick_fd,F_GETFL)) < 0) {
242 throw SickIOException(
"SickLIDAR::_setNonBlockingIO: fcntl failed!");
246 if(fcntl(
_sick_fd,F_SETFL,fd_flags | O_NONBLOCK) < 0) {
247 throw SickIOException(
"SickLIDAR::_setNonBlockingIO: fcntl failed!");
257 template<
class SICK_MONITOR_CLASS,
class SICK_MSG_CLASS >
261 uint8_t message_buffer[SICK_MSG_CLASS::MESSAGE_MAX_LENGTH] = {0};
264 sick_message.GetMessage(message_buffer);
265 unsigned int message_length = sick_message.GetMessageLength();
268 if (byte_interval == 0) {
271 if ((
unsigned int)write(
_sick_fd,message_buffer,message_length) != message_length) {
279 for (
unsigned int i = 0; i < message_length; i++) {
282 if (write(
_sick_fd,&message_buffer[i],1) != 1) {
287 usleep(byte_interval);
300 template<
class SICK_MONITOR_CLASS,
class SICK_MSG_CLASS >
305 struct timeval beg_time, end_time;
308 gettimeofday(&beg_time,NULL);
317 gettimeofday(&end_time,NULL);
336 template<
class SICK_MONITOR_CLASS,
class SICK_MSG_CLASS >
338 const uint8_t *
const byte_sequence,
339 const unsigned int byte_sequence_length,
343 uint8_t payload_buffer[SICK_MSG_CLASS::MESSAGE_PAYLOAD_MAX_LENGTH];
346 struct timeval beg_time, end_time;
349 SICK_MSG_CLASS curr_message;
352 gettimeofday(&beg_time,NULL);
362 curr_message.GetPayloadSubregion(payload_buffer,0,byte_sequence_length-1);
365 for (i=0; (i < byte_sequence_length) && (payload_buffer[i] == byte_sequence[i]); i++);
368 if (i == byte_sequence_length) {
369 sick_message = curr_message;
379 gettimeofday(&end_time,NULL);
395 template<
class SICK_MONITOR_CLASS,
class SICK_MSG_CLASS >
397 SICK_MSG_CLASS &recv_message,
398 const uint8_t *
const byte_sequence,
399 const unsigned int byte_sequence_length,
400 const unsigned int byte_interval,
401 const unsigned int timeout_value,
402 const unsigned int num_tries )
406 for(
unsigned int i = 0; i < num_tries; i++) {
414 _recvMessage(recv_message,byte_sequence,byte_sequence_length,timeout_value);
425 if (i == num_tries - 1) {
426 throw SickTimeoutException(
"SickLIDAR::_sendMessageAndGetReply: Attempted max number of tries w/o success!");
430 std::cerr << sick_timeout.
what() <<
" " << num_tries - i - 1 <<
" tries remaining" << std::endl;
436 std::cerr << sick_io_error.
what() << std::endl;
442 std::cerr <<
"SickLIDAR::_sendMessageAndGetReply: Unknown exception!!!" << std::endl;
Contains some simple exception classes.