46 if(
sizeof(value) > bufferlen)
48 ROS_ERROR_STREAM(
"## ERROR SickScanMessages::readBinaryBuffer(): bufferlen=" << bufferlen <<
" byte, " <<
sizeof(value) <<
" byte required.");
51 memcpy(&value, buffer,
sizeof(value));
52 swap_endian((
unsigned char *) &value,
sizeof(value));
53 buffer +=
sizeof(value);
54 bufferlen -= (int)
sizeof(value);
88 int dbg_telegram_length = receiveLength;
90 int msg_start_idx = 27;
91 int msg_parameter_length = receiveLength - msg_start_idx - 1;
92 if(msg_parameter_length < 8)
94 ROS_ERROR_STREAM(
"## ERROR SickScanMessages::parseLIDoutputstateMsg(): received " << receiveLength <<
" byte, expected at least 8 byte (" << __FILE__ <<
":" << __LINE__ <<
")");
100 bool parameter_format_ok =
false;
102 uint16_t timestamp_status = (receiveBuffer[receiveLength - 3] << 8) | (receiveBuffer[receiveLength - 2]);
103 int number_of_output_states = (msg_parameter_length - 6 - 2) / 5;
104 if(msg_parameter_length == 6 + number_of_output_states * 5 + 2 && timestamp_status == 0)
106 parameter_format_ok =
true;
107 ROS_DEBUG_STREAM(
"SickScanMessages::parseLIDoutputstateMsg(): " << number_of_output_states <<
" output states, no timestamp" );
111 timestamp_status = (receiveBuffer[receiveLength - 14] << 8) | (receiveBuffer[receiveLength - 13]);
112 number_of_output_states = (msg_parameter_length - 6 - 2 - 11) / 5;
113 if(msg_parameter_length == 6 + number_of_output_states * 5 + 2 + 11 && timestamp_status > 0)
115 parameter_format_ok =
true;
116 ROS_DEBUG_STREAM(
"SickScanMessages::parseLIDoutputstateMsg(): " << number_of_output_states <<
" output states and timestamp" );
119 if(!parameter_format_ok)
121 ROS_ERROR_STREAM(
"## ERROR SickScanMessages::parseLIDoutputstateMsg(): received " << receiveLength <<
" byte with invalid format (" << __FILE__ <<
":" << __LINE__ <<
"): " 126 receiveBuffer += msg_start_idx;
127 receiveLength -= msg_start_idx;
128 if( !
readBinaryBuffer(receiveBuffer, receiveLength, output_msg.version_number)
129 || !
readBinaryBuffer(receiveBuffer, receiveLength, output_msg.system_counter))
131 ROS_ERROR_STREAM(
"## ERROR SickScanMessages::parseLIDoutputstateMsg(): error parsing version_number and system_counter (" << __FILE__ <<
":" << __LINE__ <<
")");
135 output_msg.output_state.reserve(number_of_output_states);
136 output_msg.output_count.reserve(number_of_output_states);
137 for(
int state_cnt = 0; state_cnt < number_of_output_states; state_cnt++)
139 uint8_t output_state;
140 uint32_t output_count;
144 ROS_ERROR_STREAM(
"## ERROR SickScanMessages::parseLIDoutputstateMsg(): error parsing version_number and system_counter (" << __FILE__ <<
":" << __LINE__ <<
")");
147 if(output_state == 0 || output_state == 1)
149 output_msg.output_state.push_back(output_state);
150 output_msg.output_count.push_back(output_count);
156 ROS_ERROR_STREAM(
"## ERROR SickScanMessages::parseLIDoutputstateMsg(): error parsing time_state (" << __FILE__ <<
":" << __LINE__ <<
")");
159 if(output_msg.time_state != timestamp_status)
161 ROS_ERROR_STREAM(
"## ERROR SickScanMessages::parseLIDoutputstateMsg(): time_state mismatch, received " << (
int)output_msg.time_state <<
", expected " << (
int)timestamp_status <<
" (" << __FILE__ <<
":" << __LINE__ <<
")");
166 if(timestamp_status > 0)
176 ROS_ERROR_STREAM(
"## ERROR SickScanMessages::parseLIDoutputstateMsg(): error parsing timestamp (" << __FILE__ <<
":" << __LINE__ <<
")");
180 output_msg.header.stamp = timeStamp;
181 output_msg.header.seq = 0;
182 output_msg.header.frame_id = frame_id;
185 std::stringstream state_str;
186 assert(output_msg.output_state.size() == output_msg.output_state.size());
187 state_str << number_of_output_states <<
" output states received" << (timestamp_status?
" with":
", no") <<
" timestamp\n";
188 for(
int state_cnt = 0; state_cnt < output_msg.output_count.size(); state_cnt++)
189 state_str <<
"state[" << state_cnt <<
"]: " << (uint32_t)output_msg.output_state[state_cnt] <<
", count=" << (uint32_t)output_msg.output_count[state_cnt] <<
"\n";
191 << dbg_telegram_length <<
" byte telegram: " << dbg_telegram_hex_copy <<
"\n" 192 <<
"version_number: " << (uint32_t)output_msg.version_number <<
", system_counter: " << (uint32_t)output_msg.system_counter <<
"\n" 194 <<
"time state: " << (uint32_t)output_msg.time_state
195 <<
", date: " << std::setfill(
'0') << std::setw(4) << (uint32_t)output_msg.year <<
"-" << std::setfill(
'0') << std::setw(2) << (uint32_t)output_msg.month <<
"-" << std::setfill(
'0') << std::setw(2) << (uint32_t)output_msg.day
196 <<
", time: " << std::setfill(
'0') << std::setw(2) << (uint32_t)output_msg.hour <<
":" << std::setfill(
'0') << std::setw(2) << (uint32_t)output_msg.minute <<
":" << std::setfill(
'0') << std::setw(2) << (uint32_t)output_msg.second <<
"." << std::setfill(
'0') << std::setw(6) << (uint32_t)output_msg.microsecond);
202 ROS_ERROR_STREAM(
"## ERROR SickScanMessages::parseLIDoutputstateMsg not implemented for Cola-A messages(" << __FILE__ <<
":" << __LINE__ <<
")");
233 if(useBinaryProtocol)
235 uint8_t* msg_receiveBuffer = receiveBuffer;
236 int msg_receiveLength = receiveLength;
239 int msg_start_idx = 19;
240 receiveBuffer += msg_start_idx;
241 receiveLength -= msg_start_idx;
243 output_msg.fields_number = 0;
244 if(!
readBinaryBuffer(receiveBuffer, receiveLength, output_msg.fields_number) || output_msg.fields_number <= 0)
246 ROS_ERROR_STREAM(
"## ERROR SickScanMessages::parseLFErecMsg(): parse error, fields number = " << output_msg.fields_number<<
" should be greater 0 (" << __FILE__ <<
":" << __LINE__ <<
")");
250 output_msg.fields.reserve(output_msg.fields_number);
251 for(
int field_idx = 0; field_idx < output_msg.fields_number; field_idx++)
253 sick_scan::LFErecFieldMsg field_msg;
254 if( !
readBinaryBuffer(receiveBuffer, receiveLength, field_msg.version_number)
257 || !
readBinaryBuffer(receiveBuffer, receiveLength, field_msg.dist_scale_factor)
258 || !
readBinaryBuffer(receiveBuffer, receiveLength, field_msg.dist_scale_offset)
259 || !
readBinaryBuffer(receiveBuffer, receiveLength, field_msg.angle_scale_factor)
260 || !
readBinaryBuffer(receiveBuffer, receiveLength, field_msg.angle_scale_offset)
261 || !
readBinaryBuffer(receiveBuffer, receiveLength, field_msg.field_result_mrs))
263 ROS_ERROR_STREAM(
"## ERROR SickScanMessages::parseLFErecMsg(): parse error field " << field_idx <<
" (" << __FILE__ <<
":" << __LINE__ <<
")");
267 uint16_t dummy_byte[3] = {0};
272 ROS_ERROR_STREAM(
"## ERROR SickScanMessages::parseLFErecMsg(): parse error field " << field_idx <<
" (" << __FILE__ <<
":" << __LINE__ <<
")");
276 int additional_dummy_byte_length = 0;
280 additional_dummy_byte_length = (21 - 6);
281 receiveBuffer += additional_dummy_byte_length;
282 receiveLength -= additional_dummy_byte_length;
283 if(receiveLength < 0)
285 ROS_ERROR_STREAM(
"## ERROR SickScanMessages::parseLFErecMsg(): parse error field " << field_idx <<
" (" << __FILE__ <<
":" << __LINE__ <<
")");
291 ROS_ERROR_STREAM(
"## ERROR SickScanMessages::parseLFErecMsg(): parse error field " << field_idx <<
" (" << __FILE__ <<
":" << __LINE__ <<
")");
295 if(field_msg.time_state)
305 ROS_ERROR_STREAM(
"## ERROR SickScanMessages::parseLFErecMsg(): parse error field " << field_idx <<
" (" << __FILE__ <<
":" << __LINE__ <<
")");
316 field_msg.minute = 0;
317 field_msg.second = 0;
318 field_msg.microsecond = 0;
320 output_msg.fields.push_back(field_msg);
322 if(output_msg.fields.size() != output_msg.fields_number)
324 ROS_ERROR_STREAM(
"## ERROR SickScanMessages::parseLFErecMsg(): parse error, fields number = " << output_msg.fields_number <<
", but " << output_msg.fields.size() <<
" fields parsed (" << __FILE__ <<
":" << __LINE__ <<
")");
328 if(receiveLength != 1)
330 ROS_ERROR_STREAM(
"## ERROR SickScanMessages::parseLFErecMsg(): parse error, " << receiveLength <<
" bytes in buffer after decoding all fields, should be 0 (" << __FILE__ <<
":" << __LINE__ <<
")");
335 output_msg.header.stamp = timeStamp;
336 output_msg.header.seq = 0;
337 output_msg.header.frame_id = frame_id;
339 std::stringstream fields_str;
340 for(
int field_idx = 0; field_idx < output_msg.fields.size(); field_idx++)
342 sick_scan::LFErecFieldMsg& field_msg = output_msg.fields[field_idx];
345 fields_str <<
"field[" << field_idx <<
"]: idx=" << (uint32_t)field_msg.field_index
346 <<
", dist_scale_factor=" << field_msg.dist_scale_factor
347 <<
", dist_scale_offset=" << field_msg.dist_scale_offset
348 <<
", angle_scale_factor=" << field_msg.angle_scale_factor
349 <<
", angle_scale_offset=" << field_msg.angle_scale_offset
350 <<
", field_result_mrs=" << (uint32_t)field_msg.field_result_mrs
351 <<
", time state: " << (uint32_t)field_msg.time_state
352 <<
", date: " << std::setfill(
'0') << std::setw(4) << (uint32_t)field_msg.year <<
"-" << std::setfill(
'0') << std::setw(2) << (uint32_t)field_msg.month <<
"-" << std::setfill(
'0') << std::setw(2) << (uint32_t)field_msg.day
353 <<
", time: " << std::setfill(
'0') << std::setw(2) << (uint32_t)field_msg.hour <<
":" << std::setfill(
'0') << std::setw(2) << (uint32_t)field_msg.minute <<
":" << std::setfill(
'0') << std::setw(2) << (uint32_t)field_msg.second
354 <<
"." << std::setfill(
'0') << std::setw(6) << (uint32_t)field_msg.microsecond;
356 ROS_DEBUG_STREAM(
"SickScanMessages::parseLFErecMsg(): LFErec with " << output_msg.fields.size() <<
" fields:\n" << fields_str.str());
362 ROS_ERROR_STREAM(
"## ERROR SickScanMessages::parseLFErecMsg not implemented for Cola-A messages (" << __FILE__ <<
":" << __LINE__ <<
")");
static std::string binDataToAsciiString(const uint8_t *binary_data, int length)
static bool parseLIDoutputstateMsg(const ros::Time &timeStamp, uint8_t *receiveBuffer, int receiveLength, bool useBinaryProtocol, const std::string &frame_id, sick_scan::LIDoutputstateMsg &output_msg)
void swap_endian(unsigned char *ptr, int numBytes)
Universal swapping function.
SickScanMessages(ros::NodeHandle *nh=0)
static bool readBinaryBuffer(byte_ptr &buffer, int &bufferlen, T &value)
virtual ~SickScanMessages()
#define ROS_DEBUG_STREAM(args)
static bool parseLFErecMsg(const ros::Time &timeStamp, uint8_t *receiveBuffer, int receiveLength, bool useBinaryProtocol, EVAL_FIELD_SUPPORT eval_field_logic, const std::string &frame_id, sick_scan::LFErecMsg &output_msg)
#define ROS_ERROR_STREAM(args)