|
| 1 | +/* |
| 2 | + This program is free software: you can redistribute it and/or modify |
| 3 | + it under the terms of the GNU General Public License as published by |
| 4 | + the Free Software Foundation, either version 3 of the License, or |
| 5 | + (at your option) any later version. |
| 6 | +
|
| 7 | + This program is distributed in the hope that it will be useful, |
| 8 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | + GNU General Public License for more details. |
| 11 | +
|
| 12 | + You should have received a copy of the GNU General Public License |
| 13 | + along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 14 | + */ |
| 15 | + |
| 16 | +#include "AP_RangeFinder_DTS6012M.h" |
| 17 | + |
| 18 | +#if AP_RANGEFINDER_DTS6012M_ENABLED |
| 19 | + |
| 20 | +#include <AP_HAL/AP_HAL.h> |
| 21 | + |
| 22 | +extern const AP_HAL::HAL& hal; |
| 23 | + |
| 24 | +/* |
| 25 | + CRC-16/MODBUS calculation |
| 26 | + Polynomial: 0x8005, Init: 0xFFFF, RefIn: true, RefOut: true, XorOut: 0x0000 |
| 27 | +*/ |
| 28 | +uint16_t AP_RangeFinder_DTS6012M::crc16_modbus(const uint8_t *data, uint16_t len) |
| 29 | +{ |
| 30 | + uint16_t crc = 0xFFFF; |
| 31 | + for (uint16_t i = 0; i < len; i++) { |
| 32 | + crc ^= data[i]; |
| 33 | + for (uint8_t j = 0; j < 8; j++) { |
| 34 | + if (crc & 0x0001) { |
| 35 | + crc = (crc >> 1) ^ 0xA001; // 0xA001 is the bit-reversed form of 0x8005 |
| 36 | + } else { |
| 37 | + crc >>= 1; |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + return crc; |
| 42 | +} |
| 43 | + |
| 44 | +/* |
| 45 | + send the start stream command (0x01) to begin periodic measurement output |
| 46 | + Frame: A5 03 20 01 00 00 00 CRC16_H CRC16_L |
| 47 | +*/ |
| 48 | +void AP_RangeFinder_DTS6012M::send_start_command() |
| 49 | +{ |
| 50 | + const uint8_t cmd[] = { |
| 51 | + DTS6012M_FRAME_HEADER, // 0xA5 |
| 52 | + DTS6012M_DEVICE_ID, // 0x03 |
| 53 | + DTS6012M_DEVICE_TYPE, // 0x20 |
| 54 | + DTS6012M_CMD_START_STREAM, // 0x01 |
| 55 | + 0x00, // reserved |
| 56 | + 0x00, 0x00 // length = 0 (no data) |
| 57 | + }; |
| 58 | + |
| 59 | + // calculate CRC over the command bytes |
| 60 | + const uint16_t crc = crc16_modbus(cmd, sizeof(cmd)); |
| 61 | + |
| 62 | + uart->write(cmd, sizeof(cmd)); |
| 63 | + // CRC is sent high byte first per protocol spec |
| 64 | + uart->write(uint8_t(crc >> 8)); |
| 65 | + uart->write(uint8_t(crc & 0xFF)); |
| 66 | +} |
| 67 | + |
| 68 | +/* |
| 69 | + read from the sensor and return distance in meters |
| 70 | + Protocol response frame (23 bytes total): |
| 71 | + Header: A5 03 20 01 00 00 0E (7 bytes) |
| 72 | + Data (14 bytes): |
| 73 | + [0-1] Secondary target distance (LE, mm) - 0xFFFF if invalid |
| 74 | + [2-3] Secondary target correction |
| 75 | + [4-5] Secondary target intensity |
| 76 | + [6-7] Primary target distance (LE, mm) |
| 77 | + [8-9] Primary target correction |
| 78 | + [10-11] Primary target intensity |
| 79 | + [12-13] Sunlight base |
| 80 | + CRC16: 2 bytes (big-endian) |
| 81 | +*/ |
| 82 | +bool AP_RangeFinder_DTS6012M::get_reading(float &reading_m) |
| 83 | +{ |
| 84 | + if (uart == nullptr) { |
| 85 | + return false; |
| 86 | + } |
| 87 | + |
| 88 | + // send start command on first call |
| 89 | + if (!start_cmd_sent) { |
| 90 | + send_start_command(); |
| 91 | + start_cmd_sent = true; |
| 92 | + } |
| 93 | + |
| 94 | + float sum_mm = 0; |
| 95 | + uint16_t count = 0; |
| 96 | + uint16_t count_out_of_range = 0; |
| 97 | + int32_t latest_intensity = -1; |
| 98 | + |
| 99 | + // read available bytes from the sensor |
| 100 | + for (auto i = 0; i < 8192; i++) { |
| 101 | + uint8_t c; |
| 102 | + if (!uart->read(c)) { |
| 103 | + break; |
| 104 | + } |
| 105 | + |
| 106 | + // waiting for frame header |
| 107 | + if (linebuf_len == 0) { |
| 108 | + if (c == DTS6012M_FRAME_HEADER) { |
| 109 | + linebuf[linebuf_len++] = c; |
| 110 | + } |
| 111 | + continue; |
| 112 | + } |
| 113 | + |
| 114 | + // accumulate bytes into buffer |
| 115 | + linebuf[linebuf_len++] = c; |
| 116 | + |
| 117 | + // validate header fields as they arrive |
| 118 | + if (linebuf_len == 2 && c != DTS6012M_DEVICE_ID) { |
| 119 | + linebuf_len = 0; |
| 120 | + continue; |
| 121 | + } |
| 122 | + if (linebuf_len == 3 && c != DTS6012M_DEVICE_TYPE) { |
| 123 | + linebuf_len = 0; |
| 124 | + continue; |
| 125 | + } |
| 126 | + if (linebuf_len == 4 && c != DTS6012M_CMD_START_STREAM) { |
| 127 | + // not a measurement frame, discard |
| 128 | + linebuf_len = 0; |
| 129 | + continue; |
| 130 | + } |
| 131 | + |
| 132 | + // wait for complete frame |
| 133 | + if (linebuf_len < DTS6012M_FRAME_LEN) { |
| 134 | + continue; |
| 135 | + } |
| 136 | + |
| 137 | + // frame complete - verify length field |
| 138 | + const uint16_t data_len = (uint16_t(linebuf[5]) << 8) | linebuf[6]; |
| 139 | + if (data_len != DTS6012M_DATA_LEN) { |
| 140 | + linebuf_len = 0; |
| 141 | + continue; |
| 142 | + } |
| 143 | + |
| 144 | + // verify CRC-16 over header + data (all bytes except last 2) |
| 145 | + const uint16_t crc_calc = crc16_modbus(linebuf, DTS6012M_FRAME_LEN - DTS6012M_CRC_LEN); |
| 146 | + const uint16_t crc_recv = (uint16_t(linebuf[DTS6012M_FRAME_LEN - 2]) << 8) | linebuf[DTS6012M_FRAME_LEN - 1]; |
| 147 | + |
| 148 | + if (crc_calc != crc_recv) { |
| 149 | + linebuf_len = 0; |
| 150 | + continue; |
| 151 | + } |
| 152 | + |
| 153 | + // extract primary target distance (data bytes 6-7, little-endian, in mm) |
| 154 | + const uint8_t *data = &linebuf[DTS6012M_HEADER_LEN]; |
| 155 | + const uint16_t dist_mm = uint16_t(data[7]) << 8 | data[6]; |
| 156 | + |
| 157 | + if (dist_mm == DTS6012M_DIST_INVALID) { |
| 158 | + count_out_of_range++; |
| 159 | + } else if (dist_mm > DTS6012M_DIST_MAX_MM) { |
| 160 | + count_out_of_range++; |
| 161 | + } else { |
| 162 | + sum_mm += dist_mm; |
| 163 | + count++; |
| 164 | + } |
| 165 | + |
| 166 | + // capture primary target intensity for signal quality (data bytes 10-11, LE) |
| 167 | + latest_intensity = int32_t(uint16_t(data[11]) << 8 | data[10]); |
| 168 | + |
| 169 | + // reset for next frame |
| 170 | + linebuf_len = 0; |
| 171 | + } |
| 172 | + |
| 173 | + // update signal quality from latest intensity reading |
| 174 | + if (latest_intensity >= 0) { |
| 175 | + // map intensity to 0-100%, clamping at 10000 as practical maximum |
| 176 | + _signal_quality_pct = int8_t(constrain_int32(latest_intensity * 100 / 10000, 0, 100)); |
| 177 | + } |
| 178 | + |
| 179 | + if (count > 0) { |
| 180 | + reading_m = (sum_mm * 0.001f) / count; |
| 181 | + return true; |
| 182 | + } |
| 183 | + |
| 184 | + if (count_out_of_range > 0) { |
| 185 | + reading_m = max_distance() + 1.0f; |
| 186 | + return true; |
| 187 | + } |
| 188 | + |
| 189 | + return false; |
| 190 | +} |
| 191 | + |
| 192 | +#endif // AP_RANGEFINDER_DTS6012M_ENABLED |
0 commit comments