Skip to content

Commit 1d1730e

Browse files
committed
AP_Rangefinder: add generic Chinese Sonar sensor
1 parent f2a47bf commit 1d1730e

File tree

6 files changed

+119
-1
lines changed

6 files changed

+119
-1
lines changed

libraries/AP_RangeFinder/AP_RangeFinder.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
#include "AP_RangeFinder.h"
1717

18+
#include "AP_RangeFinder_GenericCSonar_Serial.h"
19+
1820
#if AP_RANGEFINDER_ENABLED
1921

2022
#include "AP_RangeFinder_analog.h"
@@ -45,6 +47,7 @@
4547
#include "AP_RangeFinder_Benewake_TFMini.h"
4648
#include "AP_RangeFinder_Benewake_TFMiniPlus.h"
4749
#include "AP_RangeFinder_PWM.h"
50+
#include "AP_RangeFinder_GenericCSonar_Serial.h"
4851
#include "AP_RangeFinder_GYUS42v2.h"
4952
#include "AP_RangeFinder_HC_SR04.h"
5053
#include "AP_RangeFinder_Bebop.h"
@@ -513,6 +516,11 @@ void RangeFinder::detect_instance(uint8_t instance, uint8_t& serial_instance)
513516
break;
514517
#endif
515518

519+
#if AP_RANGEFINDER_GENERICCSONAR_SERIAL_ENABLED
520+
case Type::GENERICCSONAR_SERIAL:
521+
serial_create_fn = AP_RangeFinder_GenericCSonar_Serial::create;
522+
break;
523+
#endif
516524
#if AP_RANGEFINDER_GYUS42V2_ENABLED
517525
case Type::GYUS42v2:
518526
serial_create_fn = AP_RangeFinder_GYUS42v2::create;

libraries/AP_RangeFinder/AP_RangeFinder.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ class RangeFinder
182182
#if AP_RANGEFINDER_RDS02UF_ENABLED
183183
RDS02UF = 43,
184184
#endif
185+
#if AP_RANGEFINDER_GENERICCSONAR_SERIAL_ENABLED
186+
GENERICCSONAR_SERIAL = 44,
187+
#endif
185188
#if AP_RANGEFINDER_SIM_ENABLED
186189
SIM = 100,
187190
#endif
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
driver for GenericCSonar_Serial : ME007YS; A02YYUW;
17+
*/
18+
#include "AP_RangeFinder_GenericCSonar_Serial.h"
19+
20+
#if AP_RANGEFINDER_GENERICCSONAR_SERIAL_ENABLED
21+
22+
#include <AP_HAL/AP_HAL.h>
23+
#include <AP_Math/crc.h>
24+
#include <stdio.h>
25+
26+
extern const AP_HAL::HAL& hal;
27+
28+
static constexpr float GENERICCSONAR_SERIAL_MAX_RANGE_M = 4.5;
29+
static constexpr uint8_t GENERICCSONAR_HEADER = 0xFF;
30+
31+
// read - return last value measured by sensor
32+
bool AP_RangeFinder_GenericCSonar_Serial::get_reading(float &reading_m)
33+
{
34+
if (uart == nullptr) {
35+
return false;
36+
}
37+
38+
// format is: [ 0xFF | DATA_H | DATA_L | SUM ]
39+
40+
// read any available lines from the lidar
41+
for (auto i=0; i<8192; i++) {
42+
uint8_t b;
43+
if (!uart->read(b)) {
44+
break;
45+
}
46+
if (buf_len == 0 && b != GENERICCSONAR_HEADER) {
47+
// discard
48+
continue;
49+
}
50+
buf[buf_len++] = b;
51+
if (buf_len == sizeof(buf)) {
52+
buf_len = 0;
53+
const uint16_t crc = (buf[0] + buf[1] + buf[2]) & 0x00FF;
54+
if (crc != buf[3]) {
55+
// bad CRC, discard
56+
continue;
57+
}
58+
reading_m = ((buf[1] << 8) + buf[2]) * 0.001;
59+
return reading_m <= GENERICCSONAR_SERIAL_MAX_RANGE_M;
60+
}
61+
}
62+
return false;
63+
}
64+
65+
#endif // AP_RANGEFINDER_GENERICCSONAR_SERIAL_ENABLED
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#pragma once
2+
3+
#include "AP_RangeFinder_config.h"
4+
5+
#if AP_RANGEFINDER_GENERICCSONAR_SERIAL_ENABLED
6+
7+
#include "AP_RangeFinder.h"
8+
#include "AP_RangeFinder_Backend_Serial.h"
9+
10+
class AP_RangeFinder_GenericCSonar_Serial : public AP_RangeFinder_Backend_Serial
11+
{
12+
13+
public:
14+
15+
static AP_RangeFinder_Backend_Serial *create(
16+
RangeFinder::RangeFinder_State &_state,
17+
AP_RangeFinder_Params &_params) {
18+
return NEW_NOTHROW AP_RangeFinder_GenericCSonar_Serial(_state, _params);
19+
}
20+
21+
protected:
22+
23+
MAV_DISTANCE_SENSOR _get_mav_distance_sensor_type() const override {
24+
return MAV_DISTANCE_SENSOR_ULTRASOUND;
25+
}
26+
27+
private:
28+
29+
using AP_RangeFinder_Backend_Serial::AP_RangeFinder_Backend_Serial;
30+
31+
// get a reading
32+
bool get_reading(float &reading_m) override;
33+
34+
uint8_t buf[4];
35+
uint8_t buf_len = 0;
36+
};
37+
38+
#endif // AP_RANGEFINDER_GENERICCSONAR_SERIAL_ENABLED

libraries/AP_RangeFinder/AP_RangeFinder_Params.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const AP_Param::GroupInfo AP_RangeFinder_Params::var_info[] = {
1515
// @DisplayName: Rangefinder type
1616
// @Description: Type of connected rangefinder
1717
// @SortValues: AlphabeticalZeroAtTop
18-
// @Values: 0:None,1:Analog,2:MaxbotixI2C,3:LidarLite-I2C,5:PWM,6:BBB-PRU,7:LightWareI2C,8:LightWareSerial,9:Bebop,10:MAVLink,11:USD1_Serial,12:LeddarOne,13:MaxbotixSerial,14:TeraRangerI2C,15:LidarLiteV3-I2C,16:VL53L0X or VL53L1X,17:NMEA,18:WASP-LRF,19:BenewakeTF02,20:Benewake-Serial,21:LidarLightV3HP,22:PWM,23:BlueRoboticsPing,24:DroneCAN,25:BenewakeTFminiPlus-I2C,26:LanbaoPSK-CM8JL65-CC5,27:BenewakeTF03,28:VL53L1X-ShortRange,29:LeddarVu8-Serial,30:HC-SR04,31:GYUS42v2,32:MSP,33:USD1_CAN,34:Benewake_CAN,35:TeraRangerSerial,36:Lua_Scripting,37:NoopLoop_TOFSense,38:NoopLoop_TOFSense_CAN,39:NRA24_CAN,40:NoopLoop_TOFSenseF_I2C,41:JRE_Serial,42:Ainstein_LR_D1,43:RDS02UF,100:SITL
18+
// @Values: 0:None,1:Analog,2:MaxbotixI2C,3:LidarLite-I2C,5:PWM,6:BBB-PRU,7:LightWareI2C,8:LightWareSerial,9:Bebop,10:MAVLink,11:USD1_Serial,12:LeddarOne,13:MaxbotixSerial,14:TeraRangerI2C,15:LidarLiteV3-I2C,16:VL53L0X or VL53L1X,17:NMEA,18:WASP-LRF,19:BenewakeTF02,20:Benewake-Serial,21:LidarLightV3HP,22:PWM,23:BlueRoboticsPing,24:DroneCAN,25:BenewakeTFminiPlus-I2C,26:LanbaoPSK-CM8JL65-CC5,27:BenewakeTF03,28:VL53L1X-ShortRange,29:LeddarVu8-Serial,30:HC-SR04,31:GYUS42v2,32:MSP,33:USD1_CAN,34:Benewake_CAN,35:TeraRangerSerial,36:Lua_Scripting,37:NoopLoop_TOFSense,38:NoopLoop_TOFSense_CAN,39:NRA24_CAN,40:NoopLoop_TOFSenseF_I2C,41:JRE_Serial,42:Ainstein_LR_D1,43:RDS02UF,44:GenericCSonar_Serial,100:SITL
1919
// @User: Standard
2020
AP_GROUPINFO_FLAGS("TYPE", 1, AP_RangeFinder_Params, type, 0, AP_PARAM_FLAG_ENABLE),
2121

libraries/AP_RangeFinder/AP_RangeFinder_config.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@
6767
#define AP_RANGEFINDER_DRONECAN_ENABLED (HAL_ENABLE_DRONECAN_DRIVERS && AP_RANGEFINDER_BACKEND_DEFAULT_ENABLED)
6868
#endif
6969

70+
#ifndef AP_RANGEFINDER_GENERICCSONAR_SERIAL_ENABLED
71+
#define AP_RANGEFINDER_GENERICCSONAR_SERIAL_ENABLED AP_RANGEFINDER_BACKEND_DEFAULT_ENABLED
72+
#endif
73+
7074
#ifndef AP_RANGEFINDER_GYUS42V2_ENABLED
7175
#define AP_RANGEFINDER_GYUS42V2_ENABLED AP_RANGEFINDER_BACKEND_DEFAULT_ENABLED
7276
#endif

0 commit comments

Comments
 (0)