|
| 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 | + Simulator for the DTS6012M serial rangefinder |
| 17 | +*/ |
| 18 | + |
| 19 | +#include "SIM_RF_DTS6012M.h" |
| 20 | + |
| 21 | +#include <AP_Math/crc.h> |
| 22 | + |
| 23 | +using namespace SITL; |
| 24 | + |
| 25 | +uint32_t RF_DTS6012M::packet_for_alt(float alt_m, uint8_t *buffer, uint8_t buflen) |
| 26 | +{ |
| 27 | + const uint8_t PACKET_LEN = 23; |
| 28 | + |
| 29 | + if (buflen < PACKET_LEN) { |
| 30 | + return 0; |
| 31 | + } |
| 32 | + |
| 33 | + // clamp to sensor's 20m max range |
| 34 | + const uint16_t dist_mm = (uint16_t)MIN((uint32_t)(alt_m * 1000), (uint32_t)20000U); |
| 35 | + |
| 36 | + // 7-byte header |
| 37 | + buffer[0] = 0xA5; // frame header |
| 38 | + buffer[1] = 0x03; // device ID |
| 39 | + buffer[2] = 0x20; // device type |
| 40 | + buffer[3] = 0x01; // command echo (start stream) |
| 41 | + buffer[4] = 0x00; // reserved |
| 42 | + buffer[5] = 0x00; // data length high byte (14 = 0x000E, big-endian) |
| 43 | + buffer[6] = 0x0E; // data length low byte |
| 44 | + |
| 45 | + // 14-byte data payload |
| 46 | + buffer[7] = 0xFF; // secondary target distance low byte (0xFFFF = invalid) |
| 47 | + buffer[8] = 0xFF; // secondary target distance high byte |
| 48 | + buffer[9] = 0x00; // secondary target correction low |
| 49 | + buffer[10] = 0x00; // secondary target correction high |
| 50 | + buffer[11] = 0x00; // secondary target intensity low |
| 51 | + buffer[12] = 0x00; // secondary target intensity high |
| 52 | + buffer[13] = dist_mm & 0xFF; // primary target distance low byte (little-endian, mm) |
| 53 | + buffer[14] = dist_mm >> 8; // primary target distance high byte |
| 54 | + buffer[15] = 0x00; // primary target correction low |
| 55 | + buffer[16] = 0x00; // primary target correction high |
| 56 | + buffer[17] = 0x10; // primary target intensity low byte (10000 = 0x2710 → 100% quality) |
| 57 | + buffer[18] = 0x27; // primary target intensity high byte |
| 58 | + buffer[19] = 0x00; // sunlight base low |
| 59 | + buffer[20] = 0x00; // sunlight base high |
| 60 | + |
| 61 | + // CRC-16/MODBUS over bytes 0–20, stored big-endian (high byte first) |
| 62 | + const uint16_t crc = calc_crc_modbus(buffer, PACKET_LEN - 2); |
| 63 | + buffer[21] = crc >> 8; // CRC high byte |
| 64 | + buffer[22] = crc & 0xFF; // CRC low byte |
| 65 | + |
| 66 | + return PACKET_LEN; |
| 67 | +} |
0 commit comments