Skip to content

Commit bc8bbec

Browse files
SITL: add DTS6012M rangefinder simulation
1 parent 72a3b5f commit bc8bbec

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

libraries/AP_HAL_SITL/SITL_State_common.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <SITL/SIM_RF_Benewake_TF03.h>
2828
#include <SITL/SIM_RF_Benewake_TFmini.h>
2929
#include <SITL/SIM_RF_BLping.h>
30+
#include <SITL/SIM_RF_DTS6012M.h>
3031
#include <SITL/SIM_RF_GYUS42v2.h>
3132
#include <SITL/SIM_RF_JRE.h>
3233
#include <SITL/SIM_RF_Lanbao.h>
@@ -56,6 +57,7 @@ static const struct {
5657
{ "benewake_tf03", SITL::RF_Benewake_TF03::create },
5758
{ "benewake_tfmini", SITL::RF_Benewake_TFmini::create },
5859
{ "blping", SITL::RF_BLping::create },
60+
{ "dts6012m", SITL::RF_DTS6012M::create },
5961
{ "gyus42v2", SITL::RF_GYUS42v2::create },
6062
{ "jre", SITL::RF_JRE::create },
6163
{ "lanbao", SITL::RF_Lanbao::create },

libraries/SITL/SIM_RF_DTS6012M.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
}

libraries/SITL/SIM_RF_DTS6012M.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
./Tools/autotest/sim_vehicle.py --gdb --debug -v ArduCopter -A --serial5=sim:dts6012m --speedup=1
19+
20+
param set SERIAL5_PROTOCOL 9
21+
param set RNGFND1_TYPE 47
22+
graph RANGEFINDER.distance
23+
graph GLOBAL_POSITION_INT.relative_alt/1000-RANGEFINDER.distance
24+
reboot
25+
26+
arm throttle
27+
rc 3 1600
28+
29+
*/
30+
31+
#pragma once
32+
33+
#include "SIM_SerialRangeFinder.h"
34+
35+
namespace SITL {
36+
37+
class RF_DTS6012M : public SerialRangeFinder {
38+
public:
39+
40+
static SerialRangeFinder *create() { return NEW_NOTHROW RF_DTS6012M(); }
41+
42+
uint32_t packet_for_alt(float alt_m, uint8_t *buffer, uint8_t buflen) override;
43+
44+
uint16_t reading_interval_ms() const override { return 100; } // 10Hz
45+
46+
};
47+
48+
}

0 commit comments

Comments
 (0)