| 1 | /* |
| 2 | * Slamtec LIDAR SDK |
| 3 | * |
| 4 | * Copyright (c) 2014 - 2023 Shanghai Slamtec Co., Ltd. |
| 5 | * http://www.slamtec.com |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | /* |
| 10 | * Sample Data Unpacker System |
| 11 | * Capsule Style Sample Node Handlers |
| 12 | */ |
| 13 | |
| 14 | /* |
| 15 | * Redistribution and use in source and binary forms, with or without |
| 16 | * modification, are permitted provided that the following conditions are met: |
| 17 | * |
| 18 | * 1. Redistributions of source code must retain the above copyright notice, |
| 19 | * this list of conditions and the following disclaimer. |
| 20 | * |
| 21 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 22 | * this list of conditions and the following disclaimer in the documentation |
| 23 | * and/or other materials provided with the distribution. |
| 24 | * |
| 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 27 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 28 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 29 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 30 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 31 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 32 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 33 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 34 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 35 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 36 | * |
| 37 | */ |
| 38 | |
| 39 | #include "../dataunnpacker_commondef.h" |
| 40 | #include "../dataunpacker.h" |
| 41 | #include "../dataunnpacker_internal.h" |
| 42 | |
| 43 | |
| 44 | |
| 45 | #include "handler_capsules.h" |
| 46 | |
| 47 | BEGIN_DATAUNPACKER_NS() |
| 48 | |
| 49 | namespace unpacker{ |
| 50 | |
| 51 | |
| 52 | // UnpackerHandler_CapsuleNode |
| 53 | /////////////////////////////////////////////////////////////////////////////////// |
| 54 | |
| 55 | static _u64 _getSampleDelayOffsetInExpressMode(const SlamtecLidarTimingDesc& timing, int sampleIdx) |
| 56 | { |
| 57 | // FIXME: to eval |
| 58 | // |
| 59 | // guess channel baudrate by LIDAR model .... |
| 60 | const _u64 channelBaudRate = timing.native_baudrate? timing.native_baudrate:115200; |
| 61 | |
| 62 | _u64 tranmissionDelay = 1000000ULL * sizeof(rplidar_response_capsule_measurement_nodes_t) * 10 / channelBaudRate; |
| 63 | |
| 64 | if (timing.native_interface_type == LIDARInterfaceType::LIDAR_INTERFACE_ETHERNET) |
| 65 | { |
| 66 | tranmissionDelay = 100; //dummy value |
| 67 | } |
| 68 | |
| 69 | // center of the sample duration |
| 70 | const _u64 sampleDelay = (timing.sample_duration_uS >> 1); |
| 71 | const _u64 sampleFilterDelay = timing.sample_duration_uS; |
| 72 | const _u64 groupingDelay = (31 - sampleIdx) * timing.sample_duration_uS; |
| 73 | |
| 74 | |
| 75 | return sampleFilterDelay + sampleDelay + tranmissionDelay + timing.linkage_delay_uS + groupingDelay; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | UnpackerHandler_CapsuleNode::UnpackerHandler_CapsuleNode() |
| 80 | : _cached_scan_node_buf_pos(0) |
| 81 | , _is_previous_capsuledataRdy(false) |
| 82 | , _cached_last_data_timestamp_us(0) |
| 83 | { |
| 84 | _cached_scan_node_buf.resize(sizeof(rplidar_response_capsule_measurement_nodes_t)); |
| 85 | memset(&_cachedTimingDesc, 0, sizeof(_cachedTimingDesc)); |
| 86 | } |
| 87 | |
| 88 | UnpackerHandler_CapsuleNode::~UnpackerHandler_CapsuleNode() |
| 89 | { |
| 90 | |
| 91 | } |
| 92 | |
| 93 | void UnpackerHandler_CapsuleNode::onUnpackerContextSet(LIDARSampleDataUnpacker::UnpackerContextType type, const void* data, size_t size) |
| 94 | { |
| 95 | if (type == LIDARSampleDataUnpacker::UNPACKER_CONTEXT_TYPE_LIDAR_TIMING) { |
| 96 | assert(size == sizeof(_cachedTimingDesc)); |
| 97 | _cachedTimingDesc = *reinterpret_cast<const SlamtecLidarTimingDesc*>(data); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | |
| 102 | _u8 UnpackerHandler_CapsuleNode::getSampleAnswerType() const |
| 103 | { |
| 104 | return RPLIDAR_ANS_TYPE_MEASUREMENT_CAPSULED; |
| 105 | } |
| 106 | |
| 107 | void UnpackerHandler_CapsuleNode::onData(LIDARSampleDataUnpackerInner* engine, const _u8* data, size_t cnt) |
| 108 | { |
| 109 | for (size_t pos = 0; pos < cnt; ++pos) { |
| 110 | _u8 current_data = data[pos]; |
| 111 | switch (_cached_scan_node_buf_pos) { |
| 112 | case 0: // expect the sync bit 1 |
| 113 | { |
| 114 | _u8 tmp = (current_data >> 4); |
| 115 | if (tmp == RPLIDAR_RESP_MEASUREMENT_EXP_SYNC_1) { |
| 116 | // pass |
| 117 | } |
| 118 | else { |
| 119 | _is_previous_capsuledataRdy = false; |
| 120 | continue; |
| 121 | } |
| 122 | |
| 123 | } |
| 124 | break; |
| 125 | case 1: // expect the sync bit 2 |
| 126 | { |
| 127 | _u8 tmp = (current_data >> 4); |
| 128 | if (tmp == RPLIDAR_RESP_MEASUREMENT_EXP_SYNC_2) { |
| 129 | // pass |
| 130 | } |
| 131 | else { |
| 132 | _cached_scan_node_buf_pos = 0; |
| 133 | _is_previous_capsuledataRdy = false; |
| 134 | continue; |
| 135 | } |
| 136 | } |
| 137 | break; |
| 138 | |
| 139 | case sizeof(rplidar_response_capsule_measurement_nodes_t) - 1: // new data ready |
| 140 | { |
| 141 | _cached_scan_node_buf[sizeof(rplidar_response_capsule_measurement_nodes_t) - 1] = current_data; |
| 142 | _cached_scan_node_buf_pos = 0; |
| 143 | |
| 144 | rplidar_response_capsule_measurement_nodes_t* node = reinterpret_cast<rplidar_response_capsule_measurement_nodes_t*>(&_cached_scan_node_buf[0]); |
| 145 | |
| 146 | // calc the checksum ... |
| 147 | _u8 checksum = 0; |
| 148 | _u8 recvChecksum = ((node->s_checksum_1 & 0xF) | (node->s_checksum_2 << 4)); |
| 149 | for (size_t cpos = offsetof(rplidar_response_capsule_measurement_nodes_t, start_angle_sync_q6); |
| 150 | cpos < sizeof(rplidar_response_capsule_measurement_nodes_t); ++cpos) |
| 151 | { |
| 152 | checksum ^= _cached_scan_node_buf[cpos]; |
| 153 | } |
| 154 | |
| 155 | if (recvChecksum == checksum) |
| 156 | { |
| 157 | // only consider vaild if the checksum matches... |
| 158 | |
| 159 | // perform data endianess convertion if necessary |
| 160 | #ifdef _CPU_ENDIAN_BIG |
| 161 | node->start_angle_sync_q6 = le16_to_cpu(node->start_angle_sync_q6); |
| 162 | for (size_t cpos = 0; cpos < _countof(node->cabins); ++cpos) { |
| 163 | node->cabins[cpos].distance_angle_1 = le16_to_cpu(node->cabins[cpos].distance_angle_1); |
| 164 | node->cabins[cpos].distance_angle_2 = le16_to_cpu(node->cabins[cpos].distance_angle_2); |
| 165 | } |
| 166 | #endif |
| 167 | if (node->start_angle_sync_q6 & RPLIDAR_RESP_MEASUREMENT_EXP_SYNCBIT) |
| 168 | { |
| 169 | if (_is_previous_capsuledataRdy) { |
| 170 | engine->publishDecodingErrorMsg(LIDARSampleDataUnpacker::ERR_EVENT_ON_EXP_ENCODER_RESET |
| 171 | , RPLIDAR_ANS_TYPE_MEASUREMENT_CAPSULED, node, sizeof(*node)); |
| 172 | } |
| 173 | // this is the first capsule frame in logic, discard the previous cached data... |
| 174 | _is_previous_capsuledataRdy = false; |
| 175 | engine->publishNewScanReset(); |
| 176 | |
| 177 | |
| 178 | } |
| 179 | _onScanNodeCapsuleData(*node, engine); |
| 180 | } |
| 181 | else { |
| 182 | _is_previous_capsuledataRdy = false; |
| 183 | |
| 184 | |
| 185 | engine->publishDecodingErrorMsg(LIDARSampleDataUnpacker::ERR_EVENT_ON_EXP_CHECKSUM_ERR |
| 186 | , RPLIDAR_ANS_TYPE_MEASUREMENT_CAPSULED, node, sizeof(*node)); |
| 187 | |
| 188 | } |
| 189 | continue; |
| 190 | } |
| 191 | break; |
| 192 | |
| 193 | } |
| 194 | _cached_scan_node_buf[_cached_scan_node_buf_pos++] = current_data; |
| 195 | } |
| 196 | |
| 197 | } |
| 198 | |
| 199 | void UnpackerHandler_CapsuleNode::reset() |
| 200 | { |
| 201 | _cached_scan_node_buf_pos = 0; |
| 202 | _is_previous_capsuledataRdy = false; |
| 203 | _cached_last_data_timestamp_us = 0; |
| 204 | } |
| 205 | |
| 206 | void UnpackerHandler_CapsuleNode::_onScanNodeCapsuleData(rplidar_response_capsule_measurement_nodes_t& capsule, LIDARSampleDataUnpackerInner* engine) |
| 207 | { |
| 208 | _u64 currentTS = engine->getCurrentTimestamp_uS(); |
| 209 | if (_is_previous_capsuledataRdy) { |
| 210 | int diffAngle_q8; |
| 211 | int currentStartAngle_q8 = ((capsule.start_angle_sync_q6 & 0x7FFF) << 2); |
| 212 | int prevStartAngle_q8 = ((_cached_previous_capsuledata.start_angle_sync_q6 & 0x7FFF) << 2); |
| 213 | |
| 214 | diffAngle_q8 = (currentStartAngle_q8)-(prevStartAngle_q8); |
| 215 | if (prevStartAngle_q8 > currentStartAngle_q8) { |
| 216 | diffAngle_q8 += (360 << 8); |
| 217 | } |
| 218 | |
| 219 | int angleInc_q16 = (diffAngle_q8 << 3); |
| 220 | int currentAngle_raw_q16 = (prevStartAngle_q8 << 8); |
| 221 | for (int pos = 0; pos < (int)_countof(_cached_previous_capsuledata.cabins); ++pos) |
| 222 | { |
| 223 | int dist_q2[2]; |
| 224 | int angle_q6[2]; |
| 225 | int syncBit[2]; |
| 226 | |
| 227 | dist_q2[0] = (_cached_previous_capsuledata.cabins[pos].distance_angle_1 & 0xFFFC); |
| 228 | dist_q2[1] = (_cached_previous_capsuledata.cabins[pos].distance_angle_2 & 0xFFFC); |
| 229 | |
| 230 | int angle_offset1_q3 = ((_cached_previous_capsuledata.cabins[pos].offset_angles_q3 & 0xF) | ((_cached_previous_capsuledata.cabins[pos].distance_angle_1 & 0x3) << 4)); |
| 231 | int angle_offset2_q3 = ((_cached_previous_capsuledata.cabins[pos].offset_angles_q3 >> 4) | ((_cached_previous_capsuledata.cabins[pos].distance_angle_2 & 0x3) << 4)); |
| 232 | |
| 233 | angle_q6[0] = ((currentAngle_raw_q16 - (angle_offset1_q3 << 13)) >> 10); |
| 234 | syncBit[0] = (((currentAngle_raw_q16 + angleInc_q16) % (360 << 16)) < angleInc_q16) ? 1 : 0; |
| 235 | currentAngle_raw_q16 += angleInc_q16; |
| 236 | |
| 237 | |
| 238 | angle_q6[1] = ((currentAngle_raw_q16 - (angle_offset2_q3 << 13)) >> 10); |
| 239 | syncBit[1] = (((currentAngle_raw_q16 + angleInc_q16) % (360 << 16)) < angleInc_q16) ? 1 : 0; |
| 240 | currentAngle_raw_q16 += angleInc_q16; |
| 241 | |
| 242 | for (int cpos = 0; cpos < 2; ++cpos) { |
| 243 | |
| 244 | if (angle_q6[cpos] < 0) angle_q6[cpos] += (360 << 6); |
| 245 | if (angle_q6[cpos] >= (360 << 6)) angle_q6[cpos] -= (360 << 6); |
| 246 | |
| 247 | rplidar_response_measurement_node_hq_t hqNode; |
| 248 | |
| 249 | |
| 250 | hqNode.flag = (syncBit[cpos] | ((!syncBit[cpos]) << 1)); |
| 251 | hqNode.quality = dist_q2[cpos] ? (0x2F << RPLIDAR_RESP_MEASUREMENT_QUALITY_SHIFT) : 0; |
| 252 | |
| 253 | hqNode.angle_z_q14 = (angle_q6[cpos] << 8) / 90; |
| 254 | hqNode.dist_mm_q2 = dist_q2[cpos]; |
| 255 | |
| 256 | engine->publishHQNode(_cached_last_data_timestamp_us - _getSampleDelayOffsetInExpressMode(_cachedTimingDesc, pos * 2 + cpos), &hqNode); |
| 257 | } |
| 258 | |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | _cached_previous_capsuledata = capsule; |
| 263 | _is_previous_capsuledataRdy = true; |
| 264 | _cached_last_data_timestamp_us = currentTS; |
| 265 | |
| 266 | } |
| 267 | |
| 268 | |
| 269 | // UnpackerHandler_UltraCapsuleNode |
| 270 | /////////////////////////////////////////////////////////////////////////////////// |
| 271 | |
| 272 | static _u64 _getSampleDelayOffsetInUltraBoostMode(const SlamtecLidarTimingDesc& timing, int sampleIdx) |
| 273 | { |
| 274 | // FIXME: to eval |
| 275 | // |
| 276 | // guess channel baudrate by LIDAR model .... |
| 277 | const _u64 channelBaudRate = timing.native_baudrate ? timing.native_baudrate : 256000; |
| 278 | |
| 279 | _u64 tranmissionDelay = 1000000ULL * sizeof(rplidar_response_ultra_capsule_measurement_nodes_t) * 10 / channelBaudRate; |
| 280 | |
| 281 | if (timing.native_interface_type == LIDARInterfaceType::LIDAR_INTERFACE_ETHERNET) |
| 282 | { |
| 283 | tranmissionDelay = 100; //dummy value |
| 284 | } |
| 285 | |
| 286 | // center of the sample duration |
| 287 | const _u64 sampleDelay = (timing.sample_duration_uS >> 1); |
| 288 | const _u64 sampleFilterDelay = timing.sample_duration_uS; |
| 289 | const _u64 groupingDelay = ((32 * 3 - 1) - sampleIdx) * timing.sample_duration_uS; |
| 290 | |
| 291 | |
| 292 | return sampleFilterDelay + sampleDelay + tranmissionDelay + timing.linkage_delay_uS + groupingDelay; |
| 293 | } |
| 294 | |
| 295 | |
| 296 | UnpackerHandler_UltraCapsuleNode::UnpackerHandler_UltraCapsuleNode() |
| 297 | : _cached_scan_node_buf_pos(0) |
| 298 | , _is_previous_capsuledataRdy(false) |
| 299 | , _cached_last_data_timestamp_us(0) |
| 300 | { |
| 301 | _cached_scan_node_buf.resize(sizeof(rplidar_response_ultra_capsule_measurement_nodes_t)); |
| 302 | memset(&_cachedTimingDesc, 0, sizeof(_cachedTimingDesc)); |
| 303 | } |
| 304 | |
| 305 | UnpackerHandler_UltraCapsuleNode::~UnpackerHandler_UltraCapsuleNode() |
| 306 | { |
| 307 | |
| 308 | } |
| 309 | |
| 310 | void UnpackerHandler_UltraCapsuleNode::onUnpackerContextSet(LIDARSampleDataUnpacker::UnpackerContextType type, const void* data, size_t size) |
| 311 | { |
| 312 | if (type == LIDARSampleDataUnpacker::UNPACKER_CONTEXT_TYPE_LIDAR_TIMING) { |
| 313 | assert(size == sizeof(_cachedTimingDesc)); |
| 314 | _cachedTimingDesc = *reinterpret_cast<const SlamtecLidarTimingDesc*>(data); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | |
| 319 | _u8 UnpackerHandler_UltraCapsuleNode::getSampleAnswerType() const |
| 320 | { |
| 321 | return RPLIDAR_ANS_TYPE_MEASUREMENT_CAPSULED_ULTRA; |
| 322 | } |
| 323 | |
| 324 | void UnpackerHandler_UltraCapsuleNode::onData(LIDARSampleDataUnpackerInner* engine, const _u8* data, size_t cnt) |
| 325 | { |
| 326 | |
| 327 | for (size_t pos = 0; pos < cnt; ++pos) { |
| 328 | _u8 current_data = data[pos]; |
| 329 | switch (_cached_scan_node_buf_pos) { |
| 330 | case 0: // expect the sync bit 1 |
| 331 | { |
| 332 | _u8 tmp = (current_data >> 4); |
| 333 | if (tmp == RPLIDAR_RESP_MEASUREMENT_EXP_SYNC_1) { |
| 334 | // pass |
| 335 | } |
| 336 | else { |
| 337 | _is_previous_capsuledataRdy = false; |
| 338 | continue; |
| 339 | } |
| 340 | |
| 341 | } |
| 342 | break; |
| 343 | case 1: // expect the sync bit 2 |
| 344 | { |
| 345 | _u8 tmp = (current_data >> 4); |
| 346 | if (tmp == RPLIDAR_RESP_MEASUREMENT_EXP_SYNC_2) { |
| 347 | // pass |
| 348 | } |
| 349 | else { |
| 350 | _cached_scan_node_buf_pos = 0; |
| 351 | _is_previous_capsuledataRdy = false; |
| 352 | continue; |
| 353 | } |
| 354 | } |
| 355 | break; |
| 356 | |
| 357 | case sizeof(rplidar_response_ultra_capsule_measurement_nodes_t) - 1: // new data ready |
| 358 | { |
| 359 | _cached_scan_node_buf[sizeof(rplidar_response_ultra_capsule_measurement_nodes_t) - 1] = current_data; |
| 360 | _cached_scan_node_buf_pos = 0; |
| 361 | |
| 362 | rplidar_response_ultra_capsule_measurement_nodes_t* node = reinterpret_cast<rplidar_response_ultra_capsule_measurement_nodes_t*>(&_cached_scan_node_buf[0]); |
| 363 | |
| 364 | // calc the checksum ... |
| 365 | _u8 checksum = 0; |
| 366 | _u8 recvChecksum = ((node->s_checksum_1 & 0xF) | (node->s_checksum_2 << 4)); |
| 367 | for (size_t cpos = offsetof(rplidar_response_ultra_capsule_measurement_nodes_t, start_angle_sync_q6); |
| 368 | cpos < sizeof(rplidar_response_ultra_capsule_measurement_nodes_t); ++cpos) |
| 369 | { |
| 370 | checksum ^= _cached_scan_node_buf[cpos]; |
| 371 | } |
| 372 | |
| 373 | if (recvChecksum == checksum) |
| 374 | { |
| 375 | // only consider vaild if the checksum matches... |
| 376 | |
| 377 | // perform data endianess convertion if necessary |
| 378 | #ifdef _CPU_ENDIAN_BIG |
| 379 | node->start_angle_sync_q6 = le16_to_cpu(node->start_angle_sync_q6); |
| 380 | for (size_t cpos = 0; cpos < _countof(node->ultra_cabins); ++cpos) { |
| 381 | node->ultra_cabins[cpos].combined_x3 = le32_to_cpu(node->ultra_cabins[cpos].combined_x3); |
| 382 | } |
| 383 | #endif |
| 384 | if (node->start_angle_sync_q6 & RPLIDAR_RESP_MEASUREMENT_EXP_SYNCBIT) |
| 385 | { |
| 386 | if (_is_previous_capsuledataRdy) { |
| 387 | engine->publishDecodingErrorMsg(LIDARSampleDataUnpacker::ERR_EVENT_ON_EXP_ENCODER_RESET |
| 388 | , RPLIDAR_ANS_TYPE_MEASUREMENT_CAPSULED_ULTRA, node, sizeof(*node)); |
| 389 | |
| 390 | } |
| 391 | // this is the first capsule frame in logic, discard the previous cached data... |
| 392 | _is_previous_capsuledataRdy = false; |
| 393 | |
| 394 | engine->publishNewScanReset(); |
| 395 | |
| 396 | } |
| 397 | _onScanNodeUltraCapsuleData(*node, engine); |
| 398 | } |
| 399 | else { |
| 400 | _is_previous_capsuledataRdy = false; |
| 401 | |
| 402 | engine->publishDecodingErrorMsg(LIDARSampleDataUnpacker::ERR_EVENT_ON_EXP_CHECKSUM_ERR |
| 403 | , RPLIDAR_ANS_TYPE_MEASUREMENT_CAPSULED_ULTRA, node, sizeof(*node)); |
| 404 | |
| 405 | } |
| 406 | continue; |
| 407 | } |
| 408 | break; |
| 409 | |
| 410 | } |
| 411 | _cached_scan_node_buf[_cached_scan_node_buf_pos++] = current_data; |
| 412 | } |
| 413 | |
| 414 | } |
| 415 | |
| 416 | void UnpackerHandler_UltraCapsuleNode::reset() |
| 417 | { |
| 418 | _cached_scan_node_buf_pos = 0; |
| 419 | _is_previous_capsuledataRdy = false; |
| 420 | } |
| 421 | |
| 422 | static _u32 _varbitscale_decode(_u32 scaled, _u32& scaleLevel) |
| 423 | { |
| 424 | static const _u32 VBS_SCALED_BASE[] = { |
| 425 | RPLIDAR_VARBITSCALE_X16_DEST_VAL, |
| 426 | RPLIDAR_VARBITSCALE_X8_DEST_VAL, |
| 427 | RPLIDAR_VARBITSCALE_X4_DEST_VAL, |
| 428 | RPLIDAR_VARBITSCALE_X2_DEST_VAL, |
| 429 | 0, |
| 430 | }; |
| 431 | |
| 432 | static const _u32 VBS_SCALED_LVL[] = { |
| 433 | 4, |
| 434 | 3, |
| 435 | 2, |
| 436 | 1, |
| 437 | 0, |
| 438 | }; |
| 439 | |
| 440 | static const _u32 VBS_TARGET_BASE[] = { |
| 441 | (0x1 << RPLIDAR_VARBITSCALE_X16_SRC_BIT), |
| 442 | (0x1 << RPLIDAR_VARBITSCALE_X8_SRC_BIT), |
| 443 | (0x1 << RPLIDAR_VARBITSCALE_X4_SRC_BIT), |
| 444 | (0x1 << RPLIDAR_VARBITSCALE_X2_SRC_BIT), |
| 445 | 0, |
| 446 | }; |
| 447 | |
| 448 | for (size_t i = 0; i < _countof(VBS_SCALED_BASE); ++i) |
| 449 | { |
| 450 | int remain = ((int)scaled - (int)VBS_SCALED_BASE[i]); |
| 451 | if (remain >= 0) { |
| 452 | scaleLevel = VBS_SCALED_LVL[i]; |
| 453 | return VBS_TARGET_BASE[i] + (remain << scaleLevel); |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | return 0; |
| 458 | } |
| 459 | |
| 460 | void UnpackerHandler_UltraCapsuleNode::_onScanNodeUltraCapsuleData(rplidar_response_ultra_capsule_measurement_nodes_t& capsule, LIDARSampleDataUnpackerInner* engine) |
| 461 | { |
| 462 | _u64 currentTS = engine->getCurrentTimestamp_uS(); |
| 463 | if (_is_previous_capsuledataRdy) { |
| 464 | int diffAngle_q8; |
| 465 | int currentStartAngle_q8 = ((capsule.start_angle_sync_q6 & 0x7FFF) << 2); |
| 466 | int prevStartAngle_q8 = ((_cached_previous_ultracapsuledata.start_angle_sync_q6 & 0x7FFF) << 2); |
| 467 | |
| 468 | diffAngle_q8 = (currentStartAngle_q8)-(prevStartAngle_q8); |
| 469 | if (prevStartAngle_q8 > currentStartAngle_q8) { |
| 470 | diffAngle_q8 += (360 << 8); |
| 471 | } |
| 472 | |
| 473 | int angleInc_q16 = (diffAngle_q8 << 3) / 3; |
| 474 | int currentAngle_raw_q16 = (prevStartAngle_q8 << 8); |
| 475 | for (int pos = 0; pos < (int)_countof(_cached_previous_ultracapsuledata.ultra_cabins); ++pos) |
| 476 | { |
| 477 | int dist_q2[3]; |
| 478 | int angle_q6[3]; |
| 479 | int syncBit[3]; |
| 480 | |
| 481 | |
| 482 | _u32 combined_x3 = _cached_previous_ultracapsuledata.ultra_cabins[pos].combined_x3; |
| 483 | |
| 484 | // unpack ... |
| 485 | int dist_major = (combined_x3 & 0xFFF); |
| 486 | |
| 487 | // signed partical integer, using the magic shift here |
| 488 | // DO NOT TOUCH |
| 489 | |
| 490 | int dist_predict1 = (((int)(combined_x3 << 10)) >> 22); |
| 491 | int dist_predict2 = (((int)combined_x3) >> 22); |
| 492 | |
| 493 | int dist_major2; |
| 494 | |
| 495 | _u32 scalelvl1=0, scalelvl2 = 0; |
| 496 | |
| 497 | // prefetch next ... |
| 498 | if (pos == _countof(_cached_previous_ultracapsuledata.ultra_cabins) - 1) |
| 499 | { |
| 500 | dist_major2 = (capsule.ultra_cabins[0].combined_x3 & 0xFFF); |
| 501 | } |
| 502 | else { |
| 503 | dist_major2 = (_cached_previous_ultracapsuledata.ultra_cabins[pos + 1].combined_x3 & 0xFFF); |
| 504 | } |
| 505 | |
| 506 | // decode with the var bit scale ... |
| 507 | dist_major = _varbitscale_decode(dist_major, scalelvl1); |
| 508 | dist_major2 = _varbitscale_decode(dist_major2, scalelvl2); |
| 509 | |
| 510 | |
| 511 | int dist_base1 = dist_major; |
| 512 | int dist_base2 = dist_major2; |
| 513 | |
| 514 | if ((!dist_major) && dist_major2) { |
| 515 | dist_base1 = dist_major2; |
| 516 | scalelvl1 = scalelvl2; |
| 517 | } |
| 518 | |
| 519 | |
| 520 | dist_q2[0] = (dist_major << 2); |
| 521 | if (((_u32)dist_predict1 == 0xFFFFFE00) || ((_u32)dist_predict1 == 0x1FF)) { |
| 522 | dist_q2[1] = 0; |
| 523 | } |
| 524 | else { |
| 525 | dist_predict1 = (int)(dist_predict1 << scalelvl1); |
| 526 | dist_q2[1] = (dist_predict1 + dist_base1) << 2; |
| 527 | |
| 528 | } |
| 529 | |
| 530 | if (((_u32)dist_predict2 == 0xFFFFFE00) || ((_u32)dist_predict2 == 0x1FF)) { |
| 531 | dist_q2[2] = 0; |
| 532 | } |
| 533 | else { |
| 534 | dist_predict2 = (int)(dist_predict2 << scalelvl2); |
| 535 | dist_q2[2] = (dist_predict2 + dist_base2) << 2; |
| 536 | } |
| 537 | |
| 538 | for (int cpos = 0; cpos < 3; ++cpos) |
| 539 | { |
| 540 | |
| 541 | syncBit[cpos] = (((currentAngle_raw_q16 + angleInc_q16) % (360 << 16)) < angleInc_q16) ? 1 : 0; |
| 542 | |
| 543 | |
| 544 | rplidar_response_measurement_node_hq_t hqNode; |
| 545 | |
| 546 | |
| 547 | int offsetAngleMean_q16 = (int)(7.5 * 3.1415926535 * (1 << 16) / 180.0); |
| 548 | |
| 549 | if (dist_q2[cpos] >= (50 * 4)) |
| 550 | { |
| 551 | const int k1 = 98361; |
| 552 | const int k2 = int(k1 / dist_q2[cpos]); |
| 553 | |
| 554 | offsetAngleMean_q16 = (int)(8 * 3.1415926535 * (1 << 16) / 180) - (k2 << 6) - (k2 * k2 * k2) / 98304; |
| 555 | } |
| 556 | |
| 557 | angle_q6[cpos] = ((currentAngle_raw_q16 - int(offsetAngleMean_q16 * 180 / 3.14159265)) >> 10); |
| 558 | currentAngle_raw_q16 += angleInc_q16; |
| 559 | |
| 560 | if (angle_q6[cpos] < 0) angle_q6[cpos] += (360 << 6); |
| 561 | if (angle_q6[cpos] >= (360 << 6)) angle_q6[cpos] -= (360 << 6); |
| 562 | |
| 563 | |
| 564 | hqNode.flag = (syncBit[cpos] | ((!syncBit[cpos]) << 1)); |
| 565 | hqNode.quality = dist_q2[cpos] ? (0x2F << RPLIDAR_RESP_MEASUREMENT_QUALITY_SHIFT) : 0; |
| 566 | |
| 567 | hqNode.angle_z_q14 = (angle_q6[cpos] << 8) / 90; |
| 568 | hqNode.dist_mm_q2 = dist_q2[cpos]; |
| 569 | |
| 570 | engine->publishHQNode(_cached_last_data_timestamp_us - _getSampleDelayOffsetInUltraBoostMode(_cachedTimingDesc, pos * 3 + cpos), &hqNode); |
| 571 | } |
| 572 | |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | _cached_previous_ultracapsuledata = capsule; |
| 577 | _is_previous_capsuledataRdy = true; |
| 578 | _cached_last_data_timestamp_us = currentTS; |
| 579 | |
| 580 | } |
| 581 | |
| 582 | |
| 583 | // UnpackerHandler_DenseCapsuleNode |
| 584 | /////////////////////////////////////////////////////////////////////////////////// |
| 585 | |
| 586 | static _u64 _getSampleDelayOffsetInDenseMode(const SlamtecLidarTimingDesc& timing, int sampleIdx) |
| 587 | { |
| 588 | // FIXME: to eval |
| 589 | // |
| 590 | // guess channel baudrate by LIDAR model .... |
| 591 | const _u64 channelBaudRate = timing.native_baudrate ? timing.native_baudrate : 256000; |
| 592 | |
| 593 | _u64 tranmissionDelay = 1000000ULL * sizeof(rplidar_response_dense_capsule_measurement_nodes_t) * 10 / channelBaudRate; |
| 594 | |
| 595 | if (timing.native_interface_type == LIDARInterfaceType::LIDAR_INTERFACE_ETHERNET) |
| 596 | { |
| 597 | tranmissionDelay = 100; //dummy value |
| 598 | } |
| 599 | |
| 600 | // center of the sample duration |
| 601 | const _u64 sampleDelay = (timing.sample_duration_uS >> 1); |
| 602 | const _u64 sampleFilterDelay = timing.sample_duration_uS; |
| 603 | const _u64 groupingDelay = (39 - sampleIdx) * timing.sample_duration_uS; |
| 604 | |
| 605 | |
| 606 | return sampleFilterDelay + sampleDelay + tranmissionDelay + timing.linkage_delay_uS + groupingDelay; |
| 607 | } |
| 608 | |
| 609 | UnpackerHandler_DenseCapsuleNode::UnpackerHandler_DenseCapsuleNode() |
| 610 | : _cached_scan_node_buf_pos(0) |
| 611 | , _is_previous_capsuledataRdy(false) |
| 612 | , _cached_last_data_timestamp_us(0) |
| 613 | |
| 614 | { |
| 615 | _cached_scan_node_buf.resize(sizeof(rplidar_response_dense_capsule_measurement_nodes_t)); |
| 616 | memset(&_cachedTimingDesc, 0, sizeof(_cachedTimingDesc)); |
| 617 | } |
| 618 | |
| 619 | UnpackerHandler_DenseCapsuleNode::~UnpackerHandler_DenseCapsuleNode() |
| 620 | { |
| 621 | |
| 622 | } |
| 623 | |
| 624 | void UnpackerHandler_DenseCapsuleNode::onUnpackerContextSet(LIDARSampleDataUnpacker::UnpackerContextType type, const void* data, size_t size) |
| 625 | { |
| 626 | if (type == LIDARSampleDataUnpacker::UNPACKER_CONTEXT_TYPE_LIDAR_TIMING) { |
| 627 | assert(size == sizeof(_cachedTimingDesc)); |
| 628 | _cachedTimingDesc = *reinterpret_cast<const SlamtecLidarTimingDesc*>(data); |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | |
| 633 | _u8 UnpackerHandler_DenseCapsuleNode::getSampleAnswerType() const |
| 634 | { |
| 635 | return RPLIDAR_ANS_TYPE_MEASUREMENT_DENSE_CAPSULED; |
| 636 | } |
| 637 | |
| 638 | |
| 639 | void UnpackerHandler_DenseCapsuleNode::onData(LIDARSampleDataUnpackerInner* engine, const _u8* data, size_t cnt) |
| 640 | { |
| 641 | |
| 642 | for (size_t pos = 0; pos < cnt; ++pos) { |
| 643 | _u8 current_data = data[pos]; |
| 644 | switch (_cached_scan_node_buf_pos) { |
| 645 | case 0: // expect the sync bit 1 |
| 646 | { |
| 647 | _u8 tmp = (current_data >> 4); |
| 648 | if (tmp == RPLIDAR_RESP_MEASUREMENT_EXP_SYNC_1) { |
| 649 | // pass |
| 650 | } |
| 651 | else { |
| 652 | _is_previous_capsuledataRdy = false; |
| 653 | continue; |
| 654 | } |
| 655 | |
| 656 | } |
| 657 | break; |
| 658 | case 1: // expect the sync bit 2 |
| 659 | { |
| 660 | _u8 tmp = (current_data >> 4); |
| 661 | if (tmp == RPLIDAR_RESP_MEASUREMENT_EXP_SYNC_2) { |
| 662 | // pass |
| 663 | } |
| 664 | else { |
| 665 | _cached_scan_node_buf_pos = 0; |
| 666 | _is_previous_capsuledataRdy = false; |
| 667 | continue; |
| 668 | } |
| 669 | } |
| 670 | break; |
| 671 | |
| 672 | case sizeof(rplidar_response_dense_capsule_measurement_nodes_t) - 1: // new data ready |
| 673 | { |
| 674 | _cached_scan_node_buf[sizeof(rplidar_response_dense_capsule_measurement_nodes_t) - 1] = current_data; |
| 675 | _cached_scan_node_buf_pos = 0; |
| 676 | |
| 677 | rplidar_response_dense_capsule_measurement_nodes_t* node = reinterpret_cast<rplidar_response_dense_capsule_measurement_nodes_t*>(&_cached_scan_node_buf[0]); |
| 678 | |
| 679 | // calc the checksum ... |
| 680 | _u8 checksum = 0; |
| 681 | _u8 recvChecksum = ((node->s_checksum_1 & 0xF) | (node->s_checksum_2 << 4)); |
| 682 | for (size_t cpos = offsetof(rplidar_response_dense_capsule_measurement_nodes_t, start_angle_sync_q6); |
| 683 | cpos < sizeof(rplidar_response_dense_capsule_measurement_nodes_t); ++cpos) |
| 684 | { |
| 685 | checksum ^= _cached_scan_node_buf[cpos]; |
| 686 | } |
| 687 | |
| 688 | if (recvChecksum == checksum) |
| 689 | { |
| 690 | // only consider vaild if the checksum matches... |
| 691 | |
| 692 | // perform data endianess convertion if necessary |
| 693 | #ifdef _CPU_ENDIAN_BIG |
| 694 | node->start_angle_sync_q6 = le16_to_cpu(node->start_angle_sync_q6); |
| 695 | for (size_t cpos = 0; cpos < _countof(node->cabins); ++cpos) { |
| 696 | node->cabins[cpos].distance_angle_1 = le16_to_cpu(node->cabins[cpos].distance_angle_1); |
| 697 | node->cabins[cpos].distance_angle_2 = le16_to_cpu(node->cabins[cpos].distance_angle_2); |
| 698 | } |
| 699 | #endif |
| 700 | if (node->start_angle_sync_q6 & RPLIDAR_RESP_MEASUREMENT_EXP_SYNCBIT) |
| 701 | { |
| 702 | if (_is_previous_capsuledataRdy) { |
| 703 | engine->publishDecodingErrorMsg(LIDARSampleDataUnpacker::ERR_EVENT_ON_EXP_ENCODER_RESET |
| 704 | , RPLIDAR_ANS_TYPE_MEASUREMENT_DENSE_CAPSULED, node, sizeof(*node)); |
| 705 | } |
| 706 | // this is the first capsule frame in logic, discard the previous cached data... |
| 707 | _is_previous_capsuledataRdy = false; |
| 708 | engine->publishNewScanReset(); |
| 709 | |
| 710 | |
| 711 | } |
| 712 | _onScanNodeDenseCapsuleData(*node, engine); |
| 713 | } |
| 714 | else { |
| 715 | _is_previous_capsuledataRdy = false; |
| 716 | |
| 717 | engine->publishDecodingErrorMsg(LIDARSampleDataUnpacker::ERR_EVENT_ON_EXP_CHECKSUM_ERR |
| 718 | , RPLIDAR_ANS_TYPE_MEASUREMENT_DENSE_CAPSULED, node, sizeof(*node)); |
| 719 | |
| 720 | } |
| 721 | continue; |
| 722 | } |
| 723 | break; |
| 724 | |
| 725 | } |
| 726 | _cached_scan_node_buf[_cached_scan_node_buf_pos++] = current_data; |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | void UnpackerHandler_DenseCapsuleNode::reset() |
| 731 | { |
| 732 | _cached_scan_node_buf_pos = 0; |
| 733 | _cached_last_data_timestamp_us = 0; |
| 734 | } |
| 735 | |
| 736 | void UnpackerHandler_DenseCapsuleNode::_onScanNodeDenseCapsuleData(rplidar_response_dense_capsule_measurement_nodes_t& dense_capsule, LIDARSampleDataUnpackerInner* engine) |
| 737 | { |
| 738 | static int lastNodeSyncBit = 0; |
| 739 | _u64 currentTs = engine->getCurrentTimestamp_uS(); |
| 740 | |
| 741 | if (_is_previous_capsuledataRdy) { |
| 742 | int diffAngle_q8; |
| 743 | int currentStartAngle_q8 = ((dense_capsule.start_angle_sync_q6 & 0x7FFF) << 2); |
| 744 | int prevStartAngle_q8 = ((_cached_previous_dense_capsuledata.start_angle_sync_q6 & 0x7FFF) << 2); |
| 745 | |
| 746 | diffAngle_q8 = (currentStartAngle_q8)-(prevStartAngle_q8); |
| 747 | if (prevStartAngle_q8 > currentStartAngle_q8) { |
| 748 | diffAngle_q8 += (360 << 8); |
| 749 | } |
| 750 | int maxDiffAngleThreshold_q8 = (360/* 360 degree */ * 100 /*100Hz*/ * _countof(dense_capsule.cabins) /*40 points per capsule*/ / (1000000 / _cachedTimingDesc.sample_duration_uS)) << 8; |
| 751 | if (diffAngle_q8 > maxDiffAngleThreshold_q8) {//discard |
| 752 | _cached_previous_dense_capsuledata = dense_capsule; |
| 753 | return; |
| 754 | } |
| 755 | |
| 756 | int angleInc_q16 = (diffAngle_q8 << 8) / 40; |
| 757 | int currentAngle_raw_q16 = (prevStartAngle_q8 << 8); |
| 758 | for (int pos = 0; pos < (int)_countof(_cached_previous_dense_capsuledata.cabins); ++pos) |
| 759 | { |
| 760 | int dist_q2; |
| 761 | int angle_q6; |
| 762 | int syncBit; |
| 763 | const int dist = static_cast<int>(_cached_previous_dense_capsuledata.cabins[pos].distance); |
| 764 | dist_q2 = dist << 2; |
| 765 | angle_q6 = (currentAngle_raw_q16 >> 10); |
| 766 | syncBit = (((currentAngle_raw_q16 + angleInc_q16) % (360 << 16)) < (angleInc_q16 << 1)) ? 1 : 0; |
| 767 | syncBit = (syncBit ^ lastNodeSyncBit) & syncBit;//Ensure that syncBit is exactly detected |
| 768 | |
| 769 | currentAngle_raw_q16 += angleInc_q16; |
| 770 | |
| 771 | if (angle_q6 < 0) angle_q6 += (360 << 6); |
| 772 | if (angle_q6 >= (360 << 6)) angle_q6 -= (360 << 6); |
| 773 | |
| 774 | rplidar_response_measurement_node_hq_t hqNode; |
| 775 | |
| 776 | |
| 777 | hqNode.flag = (syncBit | ((!syncBit) << 1)); |
| 778 | hqNode.quality = dist_q2 ? (0x2F << RPLIDAR_RESP_MEASUREMENT_QUALITY_SHIFT) : 0; |
| 779 | hqNode.angle_z_q14 = (angle_q6 << 8) / 90; |
| 780 | hqNode.dist_mm_q2 = dist_q2; |
| 781 | engine->publishHQNode(currentTs - _getSampleDelayOffsetInDenseMode(_cachedTimingDesc, pos), &hqNode); |
| 782 | |
| 783 | lastNodeSyncBit = syncBit; |
| 784 | |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | _cached_previous_dense_capsuledata = dense_capsule; |
| 789 | _is_previous_capsuledataRdy = true; |
| 790 | |
| 791 | } |
| 792 | |
| 793 | // UnpackerHandler_UltraDenseCapsuleNode |
| 794 | /////////////////////////////////////////////////////////////////////////////////// |
| 795 | |
| 796 | static _u64 _getSampleDelayOffsetInUltraDenseMode(const SlamtecLidarTimingDesc& timing, int sampleIdx) |
| 797 | { |
| 798 | // FIXME: to eval |
| 799 | // |
| 800 | // guess channel baudrate by LIDAR model .... |
| 801 | const _u64 channelBaudRate = timing.native_baudrate ? timing.native_baudrate : 1000000; |
| 802 | |
| 803 | _u64 tranmissionDelay = 1000000ULL * sizeof(sl_lidar_response_ultra_dense_capsule_measurement_nodes_t) * 10 / channelBaudRate; |
| 804 | |
| 805 | if (timing.native_interface_type == LIDARInterfaceType::LIDAR_INTERFACE_ETHERNET) |
| 806 | { |
| 807 | tranmissionDelay = 100; //dummy value |
| 808 | } |
| 809 | |
| 810 | // center of the sample duration |
| 811 | const _u64 sampleDelay = (timing.sample_duration_uS >> 1); |
| 812 | const _u64 sampleFilterDelay = timing.sample_duration_uS; |
| 813 | const _u64 groupingDelay = (31 - sampleIdx) * timing.sample_duration_uS; |
| 814 | |
| 815 | |
| 816 | return sampleFilterDelay + sampleDelay + tranmissionDelay + timing.linkage_delay_uS + groupingDelay; |
| 817 | } |
| 818 | |
| 819 | |
| 820 | UnpackerHandler_UltraDenseCapsuleNode::UnpackerHandler_UltraDenseCapsuleNode() |
| 821 | : _cached_scan_node_buf_pos(0) |
| 822 | , _is_previous_capsuledataRdy(false) |
| 823 | , _cached_last_data_timestamp_us(0) |
| 824 | , _last_node_sync_bit(0) |
| 825 | , _last_dist_q2(0) |
| 826 | |
| 827 | { |
| 828 | _cached_scan_node_buf.resize(sizeof(rplidar_response_ultra_dense_capsule_measurement_nodes_t)); |
| 829 | memset(&_cachedTimingDesc, 0, sizeof(_cachedTimingDesc)); |
| 830 | } |
| 831 | |
| 832 | UnpackerHandler_UltraDenseCapsuleNode::~UnpackerHandler_UltraDenseCapsuleNode() |
| 833 | { |
| 834 | |
| 835 | } |
| 836 | |
| 837 | |
| 838 | void UnpackerHandler_UltraDenseCapsuleNode::onUnpackerContextSet(LIDARSampleDataUnpacker::UnpackerContextType type, const void* data, size_t size) |
| 839 | { |
| 840 | if (type == LIDARSampleDataUnpacker::UNPACKER_CONTEXT_TYPE_LIDAR_TIMING) { |
| 841 | assert(size == sizeof(_cachedTimingDesc)); |
| 842 | _cachedTimingDesc = *reinterpret_cast<const SlamtecLidarTimingDesc*>(data); |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | |
| 847 | _u8 UnpackerHandler_UltraDenseCapsuleNode::getSampleAnswerType() const |
| 848 | { |
| 849 | return RPLIDAR_ANS_TYPE_MEASUREMENT_ULTRA_DENSE_CAPSULED; |
| 850 | } |
| 851 | |
| 852 | void UnpackerHandler_UltraDenseCapsuleNode::onData(LIDARSampleDataUnpackerInner* engine, const _u8* data, size_t cnt) |
| 853 | { |
| 854 | for (size_t pos = 0; pos < cnt; ++pos) { |
| 855 | _u8 current_data = data[pos]; |
| 856 | switch (_cached_scan_node_buf_pos) { |
| 857 | case 0: // expect the sync bit 1 |
| 858 | { |
| 859 | _u8 tmp = (current_data >> 4); |
| 860 | if (tmp == RPLIDAR_RESP_MEASUREMENT_EXP_SYNC_1) { |
| 861 | // pass |
| 862 | } |
| 863 | else { |
| 864 | _is_previous_capsuledataRdy = false; |
| 865 | continue; |
| 866 | } |
| 867 | |
| 868 | } |
| 869 | break; |
| 870 | case 1: // expect the sync bit 2 |
| 871 | { |
| 872 | _u8 tmp = (current_data >> 4); |
| 873 | if (tmp == RPLIDAR_RESP_MEASUREMENT_EXP_SYNC_2) { |
| 874 | // pass |
| 875 | } |
| 876 | else { |
| 877 | _cached_scan_node_buf_pos = 0; |
| 878 | _is_previous_capsuledataRdy = false; |
| 879 | continue; |
| 880 | } |
| 881 | } |
| 882 | break; |
| 883 | |
| 884 | case sizeof(rplidar_response_ultra_dense_capsule_measurement_nodes_t) - 1: // new data ready |
| 885 | { |
| 886 | _cached_scan_node_buf[sizeof(rplidar_response_ultra_dense_capsule_measurement_nodes_t) - 1] = current_data; |
| 887 | _cached_scan_node_buf_pos = 0; |
| 888 | |
| 889 | rplidar_response_ultra_dense_capsule_measurement_nodes_t* node = reinterpret_cast<rplidar_response_ultra_dense_capsule_measurement_nodes_t*>(&_cached_scan_node_buf[0]); |
| 890 | |
| 891 | // calc the checksum ... |
| 892 | _u8 checksum = 0; |
| 893 | _u8 recvChecksum = ((node->s_checksum_1 & 0xF) | (node->s_checksum_2 << 4)); |
| 894 | for (size_t cpos = offsetof(rplidar_response_ultra_dense_capsule_measurement_nodes_t, time_stamp); |
| 895 | cpos < sizeof(rplidar_response_ultra_dense_capsule_measurement_nodes_t); ++cpos) |
| 896 | { |
| 897 | checksum ^= _cached_scan_node_buf[cpos]; |
| 898 | } |
| 899 | |
| 900 | if (recvChecksum == checksum) |
| 901 | { |
| 902 | // only consider vaild if the checksum matches... |
| 903 | |
| 904 | // perform data endianess convertion if necessary |
| 905 | #ifdef _CPU_ENDIAN_BIG |
| 906 | node->start_angle_sync_q6 = le16_to_cpu(node->start_angle_sync_q6); |
| 907 | for (size_t cpos = 0; cpos < _countof(node->cabins); ++cpos) { |
| 908 | node->cabins[cpos].qualityl_distance_scale[0] = le16_to_cpu(node->cabins[cpos].qualityl_distance_scale[0]); |
| 909 | node->cabins[cpos].qualityl_distance_scale[1] = le16_to_cpu(node->cabins[cpos].qualityl_distance_scale[1]); |
| 910 | } |
| 911 | #endif |
| 912 | if (node->start_angle_sync_q6 & RPLIDAR_RESP_MEASUREMENT_EXP_SYNCBIT) |
| 913 | { |
| 914 | if (_is_previous_capsuledataRdy) { |
| 915 | engine->publishDecodingErrorMsg(LIDARSampleDataUnpacker::ERR_EVENT_ON_EXP_ENCODER_RESET |
| 916 | , RPLIDAR_ANS_TYPE_MEASUREMENT_ULTRA_DENSE_CAPSULED, node, sizeof(*node)); |
| 917 | |
| 918 | } |
| 919 | // this is the first capsule frame in logic, discard the previous cached data... |
| 920 | _is_previous_capsuledataRdy = false; |
| 921 | engine->publishNewScanReset(); |
| 922 | |
| 923 | } |
| 924 | _onScanNodeUltraDenseCapsuleData(*node, engine); |
| 925 | } |
| 926 | else { |
| 927 | _is_previous_capsuledataRdy = false; |
| 928 | |
| 929 | engine->publishDecodingErrorMsg(LIDARSampleDataUnpacker::ERR_EVENT_ON_EXP_CHECKSUM_ERR |
| 930 | , RPLIDAR_ANS_TYPE_MEASUREMENT_ULTRA_DENSE_CAPSULED, node, sizeof(*node)); |
| 931 | |
| 932 | } |
| 933 | continue; |
| 934 | } |
| 935 | break; |
| 936 | |
| 937 | } |
| 938 | _cached_scan_node_buf[_cached_scan_node_buf_pos++] = current_data; |
| 939 | } |
| 940 | |
| 941 | } |
| 942 | |
| 943 | void UnpackerHandler_UltraDenseCapsuleNode::reset() |
| 944 | { |
| 945 | _cached_scan_node_buf_pos = 0; |
| 946 | _cached_last_data_timestamp_us = 0; |
| 947 | _last_node_sync_bit = 0; |
| 948 | _last_dist_q2 = 0; |
| 949 | } |
| 950 | |
| 951 | void UnpackerHandler_UltraDenseCapsuleNode::_onScanNodeUltraDenseCapsuleData(rplidar_response_ultra_dense_capsule_measurement_nodes_t& capsule, LIDARSampleDataUnpackerInner* engine) |
| 952 | { |
| 953 | _u64 currentTimestamp = engine->getCurrentTimestamp_uS(); |
| 954 | |
| 955 | const rplidar_response_ultra_dense_capsule_measurement_nodes_t* ultra_dense_capsule = reinterpret_cast<const rplidar_response_ultra_dense_capsule_measurement_nodes_t*>(&capsule); |
| 956 | if (_is_previous_capsuledataRdy) { |
| 957 | int diffAngle_q8; |
| 958 | int currentStartAngle_q8 = ((ultra_dense_capsule->start_angle_sync_q6 & 0x7FFF) << 2); |
| 959 | int prevStartAngle_q8 = ((_cached_previous_ultra_dense_capsuledata.start_angle_sync_q6 & 0x7FFF) << 2); |
| 960 | |
| 961 | |
| 962 | |
| 963 | diffAngle_q8 = (currentStartAngle_q8)-(prevStartAngle_q8); |
| 964 | if (prevStartAngle_q8 > currentStartAngle_q8) { |
| 965 | diffAngle_q8 += (360 << 8); |
| 966 | } |
| 967 | |
| 968 | int maxDiffAngleThreshold_q8 = (360/* 360 degree */ * 100 /*100Hz*/ * _countof(ultra_dense_capsule->cabins) /*64 points per capsule*/ / (1000000 / _cachedTimingDesc.sample_duration_uS)) << 8; |
| 969 | if (diffAngle_q8 > maxDiffAngleThreshold_q8) {//discard |
| 970 | _cached_previous_ultra_dense_capsuledata = *ultra_dense_capsule; |
| 971 | return; |
| 972 | } |
| 973 | #define DISTANCE_THRESHOLD_TO_SCALE_1 2046 // (2^10 - 1)*2 mm |
| 974 | #define DISTANCE_THRESHOLD_TO_SCALE_2 8187 // (2^11 - 1)*3 + 2046 mm |
| 975 | #define DISTANCE_THRESHOLD_TO_SCALE_3 24567 // (2^12 - 1)*4 + 8187 mm |
| 976 | int angleInc_q16 = (diffAngle_q8 << 8) / 64; |
| 977 | int currentAngle_raw_q16 = (prevStartAngle_q8 << 8); |
| 978 | for (int pos = 0; pos < (int)_countof(_cached_previous_ultra_dense_capsuledata.cabins) * 2; ++pos) |
| 979 | { |
| 980 | int angle_q6; |
| 981 | int syncBit; |
| 982 | size_t cabin_idx = pos >> 1; |
| 983 | _u32 quality_dist_scale; |
| 984 | if (!(pos & 0x1)) { |
| 985 | quality_dist_scale = _cached_previous_ultra_dense_capsuledata.cabins[cabin_idx].qualityl_distance_scale[0] | ((_cached_previous_ultra_dense_capsuledata.cabins[cabin_idx].qualityh_array & 0x0F) << 16); |
| 986 | } |
| 987 | else { |
| 988 | quality_dist_scale = _cached_previous_ultra_dense_capsuledata.cabins[cabin_idx].qualityl_distance_scale[1] | ((_cached_previous_ultra_dense_capsuledata.cabins[cabin_idx].qualityh_array >> 4) << 16); |
| 989 | } |
| 990 | |
| 991 | _u8 scale = quality_dist_scale & 0x3; |
| 992 | _u8 quality = 0; |
| 993 | int dist_q2 = 0; |
| 994 | |
| 995 | switch (scale) { |
| 996 | case 0: |
| 997 | quality = quality_dist_scale >> 12; |
| 998 | dist_q2 = (quality_dist_scale & 0xFFC) * 2; |
| 999 | if (_last_dist_q2) { |
| 1000 | if (abs(dist_q2 - _last_dist_q2) <= 8/*2mm *2*/) { |
| 1001 | dist_q2 = (dist_q2 + _last_dist_q2) >> 1; |
| 1002 | } |
| 1003 | } |
| 1004 | break; |
| 1005 | case 1: |
| 1006 | quality = (quality_dist_scale >> 13) << 1; |
| 1007 | dist_q2 = (quality_dist_scale & 0x1FFC) * 3 + (DISTANCE_THRESHOLD_TO_SCALE_1 << 2); |
| 1008 | break; |
| 1009 | case 2: |
| 1010 | quality = (quality_dist_scale >> 14) << 2; |
| 1011 | dist_q2 = (quality_dist_scale & 0x3FFC) * 4 + (DISTANCE_THRESHOLD_TO_SCALE_2 << 2); |
| 1012 | break; |
| 1013 | case 3: |
| 1014 | quality = (quality_dist_scale >> 15) << 3; |
| 1015 | dist_q2 = (quality_dist_scale & 0x7FFC) * 5 + (DISTANCE_THRESHOLD_TO_SCALE_3 << 2); |
| 1016 | break; |
| 1017 | } |
| 1018 | _last_dist_q2 = dist_q2; |
| 1019 | angle_q6 = (currentAngle_raw_q16 >> 10); |
| 1020 | syncBit = (((currentAngle_raw_q16 + angleInc_q16) % (360 << 16)) < (angleInc_q16 << 1)) ? 1 : 0; |
| 1021 | syncBit = (syncBit ^ _last_node_sync_bit) & syncBit;//Ensure that syncBit is exactly detected |
| 1022 | |
| 1023 | currentAngle_raw_q16 += angleInc_q16; |
| 1024 | |
| 1025 | if (angle_q6 < 0) angle_q6 += (360 << 6); |
| 1026 | if (angle_q6 >= (360 << 6)) angle_q6 -= (360 << 6); |
| 1027 | |
| 1028 | |
| 1029 | rplidar_response_measurement_node_hq_t hqNode; |
| 1030 | |
| 1031 | |
| 1032 | |
| 1033 | hqNode.flag = (syncBit | ((!syncBit) << 1)); |
| 1034 | hqNode.quality = quality; |
| 1035 | hqNode.angle_z_q14 = (angle_q6 << 8) / 90; |
| 1036 | hqNode.dist_mm_q2 = dist_q2; |
| 1037 | engine->publishHQNode(currentTimestamp - _getSampleDelayOffsetInUltraDenseMode(_cachedTimingDesc, pos), &hqNode); |
| 1038 | |
| 1039 | _last_node_sync_bit = syncBit; |
| 1040 | |
| 1041 | } |
| 1042 | } |
| 1043 | |
| 1044 | _cached_previous_ultra_dense_capsuledata = *ultra_dense_capsule; |
| 1045 | _is_previous_capsuledataRdy = true; |
| 1046 | |
| 1047 | } |
| 1048 | |
| 1049 | |
| 1050 | |
| 1051 | } |
| 1052 | |
| 1053 | |
| 1054 | END_DATAUNPACKER_NS() |