76 if(1 != sscanf(msg_ptr,
"%x", value))
78 msg_ptr = strchr(msg_ptr,
' ');
79 while (msg_ptr && isspace(*msg_ptr))
104 if(useBinaryProtocol)
107 int dbg_telegram_length = receiveLength;
109 int msg_start_idx = 27;
110 int msg_parameter_length = receiveLength - msg_start_idx - 1;
111 if(msg_parameter_length < 8)
113 ROS_ERROR_STREAM(
"## ERROR SickScanMessages::parseLIDoutputstateMsg(): received " << receiveLength <<
" byte, expected at least 8 byte (" << __FILE__ <<
":" << __LINE__ <<
")");
119 bool parameter_format_ok =
false;
121 uint16_t timestamp_status = (receiveBuffer[receiveLength - 3] << 8) | (receiveBuffer[receiveLength - 2]);
122 int number_of_output_states = (msg_parameter_length - 6 - 2) / 5;
123 if(msg_parameter_length == 6 + number_of_output_states * 5 + 2 && timestamp_status == 0)
125 parameter_format_ok =
true;
126 ROS_DEBUG_STREAM(
"SickScanMessages::parseLIDoutputstateMsg(): " << number_of_output_states <<
" output states, no timestamp" );
130 timestamp_status = (receiveBuffer[receiveLength - 14] << 8) | (receiveBuffer[receiveLength - 13]);
131 number_of_output_states = (msg_parameter_length - 6 - 2 - 11) / 5;
132 if(msg_parameter_length == 6 + number_of_output_states * 5 + 2 + 11 && timestamp_status > 0)
134 parameter_format_ok =
true;
135 ROS_DEBUG_STREAM(
"SickScanMessages::parseLIDoutputstateMsg(): " << number_of_output_states <<
" output states and timestamp" );
138 if(!parameter_format_ok)
140 ROS_ERROR_STREAM(
"## ERROR SickScanMessages::parseLIDoutputstateMsg(): received " << receiveLength <<
" byte with invalid format (" << __FILE__ <<
":" << __LINE__ <<
"): "
145 receiveBuffer += msg_start_idx;
146 receiveLength -= msg_start_idx;
147 if( !
readBinaryBuffer(receiveBuffer, receiveLength, output_msg.version_number)
148 || !
readBinaryBuffer(receiveBuffer, receiveLength, output_msg.system_counter))
150 ROS_ERROR_STREAM(
"## ERROR SickScanMessages::parseLIDoutputstateMsg(): error parsing version_number and system_counter (" << __FILE__ <<
":" << __LINE__ <<
")");
154 output_msg.output_state.reserve(number_of_output_states);
155 output_msg.output_count.reserve(number_of_output_states);
156 for(
int state_cnt = 0; state_cnt < number_of_output_states; state_cnt++)
158 uint8_t output_state;
159 uint32_t output_count;
163 ROS_ERROR_STREAM(
"## ERROR SickScanMessages::parseLIDoutputstateMsg(): error parsing version_number and system_counter (" << __FILE__ <<
":" << __LINE__ <<
")");
166 if(output_state == 0 || output_state == 1)
168 output_msg.output_state.push_back(output_state);
169 output_msg.output_count.push_back(output_count);
175 ROS_ERROR_STREAM(
"## ERROR SickScanMessages::parseLIDoutputstateMsg(): error parsing time_state (" << __FILE__ <<
":" << __LINE__ <<
")");
178 if(output_msg.time_state != timestamp_status)
180 ROS_ERROR_STREAM(
"## ERROR SickScanMessages::parseLIDoutputstateMsg(): time_state mismatch, received " << (
int)output_msg.time_state <<
", expected " << (
int)timestamp_status <<
" (" << __FILE__ <<
":" << __LINE__ <<
")");
185 if(timestamp_status > 0)
195 ROS_ERROR_STREAM(
"## ERROR SickScanMessages::parseLIDoutputstateMsg(): error parsing timestamp (" << __FILE__ <<
":" << __LINE__ <<
")");
201 output_msg.header.frame_id = frame_id;
204 std::stringstream state_str;
205 assert(output_msg.output_state.size() == output_msg.output_state.size());
206 state_str << number_of_output_states <<
" output states received" << (timestamp_status?
" with":
", no") <<
" timestamp\n";
207 for(
int state_cnt = 0; state_cnt < output_msg.output_count.size(); state_cnt++)
208 state_str <<
"state[" << state_cnt <<
"]: " << (uint32_t)output_msg.output_state[state_cnt] <<
", count=" << (uint32_t)output_msg.output_count[state_cnt] <<
"\n";
210 << dbg_telegram_length <<
" byte telegram: " << dbg_telegram_hex_copy <<
"\n"
211 <<
"version_number: " << (uint32_t)output_msg.version_number <<
", system_counter: " << (uint32_t)output_msg.system_counter <<
"\n"
213 <<
"time state: " << (uint32_t)output_msg.time_state
214 <<
", 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
215 <<
", 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);
223 char* msg_ptr = (
char*)(msg_offset < receiveLength ? (receiveBuffer + msg_offset) : 0);
225 uint32_t version_number = 0, system_counter = 0;
228 output_msg.version_number = (uint16_t)(version_number & 0xFFFF);
229 output_msg.system_counter = system_counter;
230 for(
int state_cnt = 0; state_cnt < 7 && msg_ptr != 0; state_cnt++)
232 uint32_t output_state = 2, output_count = 0;
235 if (msg_ptr && (output_state == 0 || output_state == 1))
237 output_msg.output_state.push_back((uint8_t)(output_state & 0xFF));
238 output_msg.output_count.push_back(output_count);
241 for(
int state_cnt = 0; state_cnt < 8; state_cnt++)
245 uint32_t time_state = 0, year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0, microsecond = 0;
247 if (msg_ptr && time_state > 0)
257 output_msg.time_state = (uint16_t)time_state;
258 output_msg.year = (uint16_t)year;
259 output_msg.month = (uint8_t)month;
260 output_msg.day = (uint8_t)day;
261 output_msg.hour = (uint8_t)hour;
262 output_msg.minute = (uint8_t)minute;
263 output_msg.second = (uint8_t)second;
264 output_msg.microsecond = (uint32_t)microsecond;
267 output_msg.header.frame_id = frame_id;
269 std::stringstream state_str;
270 for(
int state_cnt = 0; state_cnt < output_msg.output_count.size(); state_cnt++)
271 state_str <<
"state[" << state_cnt <<
"]: " << (uint32_t)output_msg.output_state[state_cnt] <<
", count=" << (uint32_t)output_msg.output_count[state_cnt] <<
"\n";
273 << receiveLength <<
" byte telegram: " << std::string((
char*)receiveBuffer + 1, receiveLength - 2) <<
"\n"
274 <<
"version_number: " << (uint32_t)output_msg.version_number <<
", system_counter: " << (uint32_t)output_msg.system_counter <<
"\n"
276 <<
"time state: " << (uint32_t)output_msg.time_state
277 <<
", 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
278 <<
", 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);
310 if(useBinaryProtocol)
312 uint8_t* msg_receiveBuffer = receiveBuffer;
313 int msg_receiveLength = receiveLength;
316 int msg_start_idx = 19;
317 receiveBuffer += msg_start_idx;
318 receiveLength -= msg_start_idx;
320 output_msg.fields_number = 0;
321 if(!
readBinaryBuffer(receiveBuffer, receiveLength, output_msg.fields_number) || output_msg.fields_number <= 0)
323 ROS_ERROR_STREAM(
"## ERROR SickScanMessages::parseLFErecMsg(): parse error, fields number = " << output_msg.fields_number<<
" should be greater than 0 (" << __FILE__ <<
":" << __LINE__ <<
")");
327 output_msg.fields.reserve(output_msg.fields_number);
328 for(
int field_idx = 0; field_idx < output_msg.fields_number; field_idx++)
331 if( !
readBinaryBuffer(receiveBuffer, receiveLength, field_msg.version_number)
334 || !
readBinaryBuffer(receiveBuffer, receiveLength, field_msg.dist_scale_factor)
335 || !
readBinaryBuffer(receiveBuffer, receiveLength, field_msg.dist_scale_offset)
336 || !
readBinaryBuffer(receiveBuffer, receiveLength, field_msg.angle_scale_factor)
337 || !
readBinaryBuffer(receiveBuffer, receiveLength, field_msg.angle_scale_offset)
338 || !
readBinaryBuffer(receiveBuffer, receiveLength, field_msg.field_result_mrs))
340 ROS_ERROR_STREAM(
"## ERROR SickScanMessages::parseLFErecMsg(): parse error field " << field_idx <<
" (" << __FILE__ <<
":" << __LINE__ <<
")");
344 uint16_t dummy_byte[3] = {0};
349 ROS_ERROR_STREAM(
"## ERROR SickScanMessages::parseLFErecMsg(): parse error field " << field_idx <<
" (" << __FILE__ <<
":" << __LINE__ <<
")");
353 int additional_dummy_byte_length = 0;
357 additional_dummy_byte_length = (21 - 6);
358 receiveBuffer += additional_dummy_byte_length;
359 receiveLength -= additional_dummy_byte_length;
360 if(receiveLength < 0)
362 ROS_ERROR_STREAM(
"## ERROR SickScanMessages::parseLFErecMsg(): parse error field " << field_idx <<
" (" << __FILE__ <<
":" << __LINE__ <<
")");
368 ROS_ERROR_STREAM(
"## ERROR SickScanMessages::parseLFErecMsg(): parse error field " << field_idx <<
" (" << __FILE__ <<
":" << __LINE__ <<
")");
372 if(field_msg.time_state)
382 ROS_ERROR_STREAM(
"## ERROR SickScanMessages::parseLFErecMsg(): parse error field " << field_idx <<
" (" << __FILE__ <<
":" << __LINE__ <<
")");
393 field_msg.minute = 0;
394 field_msg.second = 0;
395 field_msg.microsecond = 0;
397 output_msg.fields.push_back(field_msg);
399 if(output_msg.fields.size() != output_msg.fields_number)
401 ROS_ERROR_STREAM(
"## ERROR SickScanMessages::parseLFErecMsg(): parse error, fields number = " << output_msg.fields_number <<
", but " << output_msg.fields.size() <<
" fields parsed (" << __FILE__ <<
":" << __LINE__ <<
")");
405 if(receiveLength != 1)
407 ROS_ERROR_STREAM(
"## ERROR SickScanMessages::parseLFErecMsg(): parse error, " << receiveLength <<
" bytes in buffer after decoding all fields, should be 0 (" << __FILE__ <<
":" << __LINE__ <<
")");
414 output_msg.header.frame_id = frame_id;
416 std::stringstream fields_str;
417 for(
int field_idx = 0; field_idx < output_msg.fields.size(); field_idx++)
422 fields_str <<
"field[" << field_idx <<
"]: idx=" << (uint32_t)field_msg.field_index
423 <<
", dist_scale_factor=" << field_msg.dist_scale_factor
424 <<
", dist_scale_offset=" << field_msg.dist_scale_offset
425 <<
", angle_scale_factor=" << field_msg.angle_scale_factor
426 <<
", angle_scale_offset=" << field_msg.angle_scale_offset
427 <<
", field_result_mrs=" << (uint32_t)field_msg.field_result_mrs
428 <<
", time state: " << (uint32_t)field_msg.time_state
429 <<
", 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
430 <<
", 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
431 <<
"." << std::setfill(
'0') << std::setw(6) << (uint32_t)field_msg.microsecond;
433 ROS_DEBUG_STREAM(
"SickScanMessages::parseLFErecMsg(): LFErec with " << output_msg.fields.size() <<
" fields:\n" << fields_str.str());
439 ROS_ERROR_STREAM(
"## ERROR SickScanMessages::parseLFErecMsg not implemented for Cola-A messages (" << __FILE__ <<
":" << __LINE__ <<
")");
449 const uint32_t binary_stx = 0x02020202;
450 bool requestIsBinary = (requestLength >=
sizeof(binary_stx) && memcmp(sopasRequest, &binary_stx,
sizeof(binary_stx)) == 0);
452 int keyword_start = 0, keyword_end = 0;
453 if(requestIsBinary && requestLength > 12)
455 else if(!requestIsBinary && requestLength > 5)
459 keyword_end = keyword_start;
460 while(keyword_end < requestLength-1 && sopasRequest[keyword_end] != 0x03 && !isspace(sopasRequest[keyword_end]))
462 std::string
keyword((
const char*)&sopasRequest[keyword_start], keyword_end - keyword_start);