| 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 | * HQNode Sample Node Handler |
| 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 | #ifdef CONF_NO_BOOST_CRC_SUPPORT |
| 44 | #include "sl_crc.h" |
| 45 | #endif |
| 46 | |
| 47 | #include "handler_hqnode.h" |
| 48 | |
| 49 | BEGIN_DATAUNPACKER_NS() |
| 50 | |
| 51 | namespace unpacker{ |
| 52 | |
| 53 | |
| 54 | static _u64 _getSampleDelayOffsetInHQMode(const SlamtecLidarTimingDesc& timing) |
| 55 | { |
| 56 | // FIXME: to eval |
| 57 | // |
| 58 | // guess channel baudrate by LIDAR model .... |
| 59 | const _u64 channelBaudRate = timing.native_baudrate? timing.native_baudrate:1000000; |
| 60 | |
| 61 | _u64 tranmissionDelay = 1000000ULL * sizeof(rplidar_response_measurement_node_hq_t) * 10 / channelBaudRate; |
| 62 | |
| 63 | if (timing.native_interface_type == LIDARInterfaceType::LIDAR_INTERFACE_ETHERNET) |
| 64 | { |
| 65 | tranmissionDelay = 100; //dummy value |
| 66 | } |
| 67 | |
| 68 | // center of the sample duration |
| 69 | const _u64 sampleDelay = (timing.sample_duration_uS >> 1); |
| 70 | const _u64 sampleFilterDelay = timing.sample_duration_uS; |
| 71 | |
| 72 | return sampleFilterDelay + sampleDelay + tranmissionDelay + timing.linkage_delay_uS; |
| 73 | } |
| 74 | |
| 75 | UnpackerHandler_HQNode::UnpackerHandler_HQNode() |
| 76 | : _cached_scan_node_buf_pos(0) |
| 77 | { |
| 78 | _cached_scan_node_buf.resize(sizeof(rplidar_response_hq_capsule_measurement_nodes_t)); |
| 79 | memset(&_cachedTimingDesc, 0, sizeof(_cachedTimingDesc)); |
| 80 | } |
| 81 | |
| 82 | UnpackerHandler_HQNode::~UnpackerHandler_HQNode() |
| 83 | { |
| 84 | |
| 85 | } |
| 86 | |
| 87 | _u8 UnpackerHandler_HQNode::getSampleAnswerType() const |
| 88 | { |
| 89 | return RPLIDAR_ANS_TYPE_MEASUREMENT_HQ; |
| 90 | } |
| 91 | |
| 92 | void UnpackerHandler_HQNode::onData(LIDARSampleDataUnpackerInner* engine, const _u8* data, size_t cnt) |
| 93 | { |
| 94 | |
| 95 | for (size_t pos = 0; pos < cnt; ++pos) |
| 96 | { |
| 97 | _u8 current_data = data[pos]; |
| 98 | |
| 99 | switch (_cached_scan_node_buf_pos) |
| 100 | { |
| 101 | case 0: // expect the sync byte |
| 102 | { |
| 103 | if (current_data == RPLIDAR_RESP_MEASUREMENT_HQ_SYNC) { |
| 104 | // pass |
| 105 | } |
| 106 | else { |
| 107 | continue; |
| 108 | } |
| 109 | } |
| 110 | break; |
| 111 | |
| 112 | case sizeof(rplidar_response_hq_capsule_measurement_nodes_t) - 1 - 4: // get bytes to calculate crc ready |
| 113 | { |
| 114 | |
| 115 | } |
| 116 | break; |
| 117 | |
| 118 | case sizeof(rplidar_response_hq_capsule_measurement_nodes_t) - 1: // new data ready |
| 119 | { |
| 120 | _cached_scan_node_buf[sizeof(rplidar_response_hq_capsule_measurement_nodes_t) - 1] = current_data; |
| 121 | _cached_scan_node_buf_pos = 0; |
| 122 | rplidar_response_hq_capsule_measurement_nodes_t* nodesData = reinterpret_cast<rplidar_response_hq_capsule_measurement_nodes_t*>(&_cached_scan_node_buf[0]); |
| 123 | |
| 124 | #ifdef CONF_NO_BOOST_CRC_SUPPORT |
| 125 | _u32 crcCalc = crc32::getResult(&_cached_scan_node_buf[0], sizeof(sl_lidar_response_hq_capsule_measurement_nodes_t) - 4); |
| 126 | |
| 127 | |
| 128 | #else |
| 129 | // calculate crc with boost crc method |
| 130 | boost::crc_optimal<32, 0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF, true, true> mycrc; |
| 131 | std::vector<_u8> crcInputData; |
| 132 | crcInputData.resize(sizeof(rplidar_response_hq_capsule_measurement_nodes_t) - 4); |
| 133 | memcpy(&crcInputData[0], nodesData, sizeof(rplidar_response_hq_capsule_measurement_nodes_t) - 4); |
| 134 | //supplement crcInputData to mutiples of 4 |
| 135 | int leftBytes = 4 - (crcInputData.size() & 3); |
| 136 | for (int i = 0; i < leftBytes; i++) |
| 137 | crcInputData.push_back(0); |
| 138 | mycrc.process_bytes(&crcInputData[0], crcInputData.size()); |
| 139 | _u32 crcCalc = mycrc.checksum(); |
| 140 | |
| 141 | #endif |
| 142 | |
| 143 | _u32 recvCRC = nodesData->crc32; |
| 144 | #ifdef _CPU_ENDIAN_BIG |
| 145 | recvCRC = le32_to_cpu(recvCRC); |
| 146 | nodesData->time_stamp = le64_to_cpu(nodesData->time_stamp); |
| 147 | #endif |
| 148 | if (recvCRC == crcCalc) |
| 149 | { |
| 150 | for (size_t p = 0; p < _countof(nodesData->node_hq); ++p) |
| 151 | { |
| 152 | rplidar_response_measurement_node_hq_t hqNode = nodesData->node_hq[p]; |
| 153 | #ifdef _CPU_ENDIAN_BIG |
| 154 | hqNode.angle_z_q14 = le16_to_cpu(hqNode.angle_z_q14); |
| 155 | hqNode.dist_mm_q2 = le32_to_cpu(hqNode.dist_mm_q2); |
| 156 | #endif |
| 157 | engine->publishHQNode(engine->getCurrentTimestamp_uS() - _getSampleDelayOffsetInHQMode(_cachedTimingDesc), &hqNode); |
| 158 | } |
| 159 | } |
| 160 | else //crc check not passed |
| 161 | { |
| 162 | engine->publishDecodingErrorMsg(LIDARSampleDataUnpacker::ERR_EVENT_ON_EXP_CHECKSUM_ERR |
| 163 | , RPLIDAR_ANS_TYPE_MEASUREMENT_HQ, nodesData, sizeof(*nodesData)); |
| 164 | } |
| 165 | continue; |
| 166 | } |
| 167 | break; |
| 168 | |
| 169 | |
| 170 | } |
| 171 | _cached_scan_node_buf[_cached_scan_node_buf_pos++] = current_data; |
| 172 | } |
| 173 | |
| 174 | } |
| 175 | |
| 176 | |
| 177 | void UnpackerHandler_HQNode::onUnpackerContextSet(LIDARSampleDataUnpacker::UnpackerContextType type, const void* data, size_t size) |
| 178 | { |
| 179 | if (type == LIDARSampleDataUnpacker::UNPACKER_CONTEXT_TYPE_LIDAR_TIMING) { |
| 180 | assert(size == sizeof(_cachedTimingDesc)); |
| 181 | _cachedTimingDesc = *reinterpret_cast<const SlamtecLidarTimingDesc*>(data); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | void UnpackerHandler_HQNode::reset() |
| 186 | { |
| 187 | _cached_scan_node_buf_pos = 0; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | |
| 192 | END_DATAUNPACKER_NS() |