|
| 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 | + |
| 17 | +#include "AP_RangeFinder_config.h" |
| 18 | + |
| 19 | +#if AP_RANGEFINDER_DTS6012M_ENABLED |
| 20 | + |
| 21 | +#include "AP_RangeFinder_DTS6012M.h" |
| 22 | +#include <AP_HAL/AP_HAL.h> |
| 23 | +#include <AP_Math/crc.h> |
| 24 | + |
| 25 | +// DTS6012M protocol constants |
| 26 | +#define DTS6012M_FRAME_HEADER 0xA5 |
| 27 | +#define DTS6012M_DEVICE_ID 0x03 |
| 28 | +#define DTS6012M_DEVICE_TYPE 0x20 |
| 29 | +#define DTS6012M_CMD_START_STREAM 0x01 |
| 30 | +#define DTS6012M_HEADER_LEN 7 // header(1) + devid(1) + devtype(1) + cmd(1) + reserved(1) + length(2) |
| 31 | +#define DTS6012M_DATA_LEN 14 // measurement data length |
| 32 | +#define DTS6012M_CRC_LEN 2 |
| 33 | +#define DTS6012M_FRAME_LEN (DTS6012M_HEADER_LEN + DTS6012M_DATA_LEN + DTS6012M_CRC_LEN) // 23 bytes |
| 34 | +#define DTS6012M_DIST_MAX_MM 20000 // 20m max range |
| 35 | +#define DTS6012M_DIST_INVALID 0xFFFF |
| 36 | + |
| 37 | +extern const AP_HAL::HAL& hal; |
| 38 | + |
| 39 | +/* |
| 40 | + send the start stream command (0x01) to begin periodic measurement output |
| 41 | + Frame: A5 03 20 01 00 00 00 CRC16_H CRC16_L |
| 42 | +*/ |
| 43 | +void AP_RangeFinder_DTS6012M::send_start_command() |
| 44 | +{ |
| 45 | + static const uint8_t cmd[] = { |
| 46 | + DTS6012M_FRAME_HEADER, // 0xA5 |
| 47 | + DTS6012M_DEVICE_ID, // 0x03 |
| 48 | + DTS6012M_DEVICE_TYPE, // 0x20 |
| 49 | + DTS6012M_CMD_START_STREAM, // 0x01 |
| 50 | + 0x00, // reserved |
| 51 | + 0x00, 0x00 // length = 0 (no data) |
| 52 | + }; |
| 53 | + |
| 54 | + // calculate CRC over the command bytes |
| 55 | + const uint16_t crc = calc_crc_modbus(cmd, sizeof(cmd)); |
| 56 | + |
| 57 | + uart->write(cmd, sizeof(cmd)); |
| 58 | + // CRC is sent high byte first per protocol spec |
| 59 | + uart->write(uint8_t(crc >> 8)); |
| 60 | + uart->write(uint8_t(crc & 0xFF)); |
| 61 | +} |
| 62 | + |
| 63 | +/* |
| 64 | + search for frame header byte in buffer starting at offset, |
| 65 | + shifting remaining data to re-sync after line noise |
| 66 | +*/ |
| 67 | +void AP_RangeFinder_DTS6012M::find_signature_in_buffer(uint8_t start) |
| 68 | +{ |
| 69 | + uint8_t *buf = (uint8_t *)&frame; |
| 70 | + for (uint8_t i = start; i < linebuf_len; i++) { |
| 71 | + if (buf[i] == DTS6012M_FRAME_HEADER) { |
| 72 | + memmove(&buf[0], &buf[i], linebuf_len - i); |
| 73 | + linebuf_len -= i; |
| 74 | + return; |
| 75 | + } |
| 76 | + } |
| 77 | + linebuf_len = 0; |
| 78 | +} |
| 79 | + |
| 80 | +/* |
| 81 | + read from the sensor and return distance in meters |
| 82 | + see frame struct in header for protocol layout |
| 83 | +*/ |
| 84 | +bool AP_RangeFinder_DTS6012M::get_reading(float &reading_m) |
| 85 | +{ |
| 86 | + if (uart == nullptr) { |
| 87 | + return false; |
| 88 | + } |
| 89 | + |
| 90 | + // keep sending start command until we receive a valid reading, |
| 91 | + // allowing for slow-startup or late connection of the device |
| 92 | + if (!got_reading) { |
| 93 | + send_start_command(); |
| 94 | + } |
| 95 | + |
| 96 | + // bulk read available bytes into buffer |
| 97 | + uint8_t *buf = (uint8_t *)&frame; |
| 98 | + const uint8_t num_read = uart->read(&buf[linebuf_len], sizeof(frame) - linebuf_len); |
| 99 | + linebuf_len += num_read; |
| 100 | + |
| 101 | + if (linebuf_len == 0) { |
| 102 | + return false; |
| 103 | + } |
| 104 | + |
| 105 | + // ensure buffer starts with frame header |
| 106 | + if (frame.header != DTS6012M_FRAME_HEADER) { |
| 107 | + find_signature_in_buffer(1); |
| 108 | + return false; |
| 109 | + } |
| 110 | + |
| 111 | + // wait for a complete frame |
| 112 | + if (linebuf_len < DTS6012M_FRAME_LEN) { |
| 113 | + return false; |
| 114 | + } |
| 115 | + |
| 116 | + // validate header fields |
| 117 | + if (frame.device_id != DTS6012M_DEVICE_ID || |
| 118 | + frame.device_type != DTS6012M_DEVICE_TYPE || |
| 119 | + frame.command != DTS6012M_CMD_START_STREAM) { |
| 120 | + find_signature_in_buffer(1); |
| 121 | + return false; |
| 122 | + } |
| 123 | + |
| 124 | + // verify length field |
| 125 | + if (be16toh(frame.data_len) != DTS6012M_DATA_LEN) { |
| 126 | + find_signature_in_buffer(1); |
| 127 | + return false; |
| 128 | + } |
| 129 | + |
| 130 | + // verify CRC-16 over header + data (all bytes except last 2) |
| 131 | + const uint16_t crc_calc = calc_crc_modbus(buf, DTS6012M_FRAME_LEN - DTS6012M_CRC_LEN); |
| 132 | + if (crc_calc != be16toh(frame.crc)) { |
| 133 | + find_signature_in_buffer(1); |
| 134 | + return false; |
| 135 | + } |
| 136 | + |
| 137 | + // extract primary target distance (little-endian, in mm) |
| 138 | + const uint16_t dist_mm = le16toh(frame.primary_distance_mm); |
| 139 | + |
| 140 | + // capture primary target intensity for signal quality |
| 141 | + const int32_t intensity = int32_t(le16toh(frame.primary_intensity)); |
| 142 | + // map intensity to 0-100%, clamping at 10000 as practical maximum |
| 143 | + _signal_quality_pct = int8_t(constrain_int32(intensity * 100 / 10000, 0, 100)); |
| 144 | + |
| 145 | + // frame consumed, reset buffer and discard any stale data so the |
| 146 | + // next call reads the freshest frame from this high-rate sensor |
| 147 | + linebuf_len = 0; |
| 148 | + uart->discard_input(); |
| 149 | + got_reading = true; |
| 150 | + |
| 151 | + if (dist_mm == DTS6012M_DIST_INVALID || dist_mm > DTS6012M_DIST_MAX_MM) { |
| 152 | + reading_m = max_distance() + 1.0f; |
| 153 | + return true; |
| 154 | + } |
| 155 | + |
| 156 | + reading_m = dist_mm * 0.001f; |
| 157 | + return true; |
| 158 | +} |
| 159 | + |
| 160 | +#endif // AP_RANGEFINDER_DTS6012M_ENABLED |
0 commit comments