Skip to content

Commit ca6d6c3

Browse files
SITL: add DTS6012M rangefinder simulation
1 parent c234be2 commit ca6d6c3

File tree

4 files changed

+133
-0
lines changed

4 files changed

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

libraries/SITL/SIM_RF_DTS6012M.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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_config.h"
34+
35+
#if AP_SIM_RF_DTS6012M_ENABLED
36+
37+
#include "SIM_SerialRangeFinder.h"
38+
39+
40+
namespace SITL {
41+
42+
class RF_DTS6012M : public SerialRangeFinder {
43+
public:
44+
45+
static SerialRangeFinder *create() { return NEW_NOTHROW RF_DTS6012M(); }
46+
47+
uint32_t packet_for_alt(float alt_m, uint8_t *buffer, uint8_t buflen) override;
48+
49+
uint16_t reading_interval_ms() const override { return 100; } // 10Hz
50+
51+
};
52+
53+
}
54+
55+
#endif // AP_SIM_RF_DTS6012M_ENABLED

libraries/SITL/SIM_config.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,10 @@
245245
#define AP_SIM_RF_BENEWAKE_TFMINIPLUS_ENABLED (CONFIG_HAL_BOARD == HAL_BOARD_SITL)
246246
#endif // AP_SIM_RF_BENEWAKE_TFMINIPLUS_ENABLED
247247

248+
#ifndef AP_SIM_RF_DTS6012M_ENABLED
249+
#define AP_SIM_RF_DTS6012M_ENABLED (CONFIG_HAL_BOARD == HAL_BOARD_SITL)
250+
#endif // AP_SIM_RF_DTS6012M_ENABLED
251+
248252
#ifndef AP_SIM_RAMTRON_ENABLED
249253
#define AP_SIM_RAMTRON_ENABLED (CONFIG_HAL_BOARD == HAL_BOARD_SITL)
250254
#endif // AP_SIM_RAMTRON_ENABLED

0 commit comments

Comments
 (0)