2014-06-09 14:14:50 -03:00
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2016-02-17 21:25:44 -04:00
|
|
|
#pragma once
|
2012-01-10 19:44:04 -04:00
|
|
|
|
2015-08-11 03:28:45 -03:00
|
|
|
#include <AP_Common/AP_Common.h>
|
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
#include <AP_Param/AP_Param.h>
|
2019-07-09 03:14:52 -03:00
|
|
|
#include <GCS_MAVLink/GCS.h>
|
2020-08-04 17:42:19 -03:00
|
|
|
#include <AP_MSP/msp.h>
|
2018-07-04 11:22:17 -03:00
|
|
|
#include "AP_RangeFinder_Params.h"
|
2012-01-10 19:44:04 -04:00
|
|
|
|
2014-06-09 14:14:50 -03:00
|
|
|
// Maximum number of range finder instances available on this platform
|
2019-10-19 01:13:36 -03:00
|
|
|
#ifndef RANGEFINDER_MAX_INSTANCES
|
2018-07-04 11:22:17 -03:00
|
|
|
#define RANGEFINDER_MAX_INSTANCES 10
|
2019-10-19 01:13:36 -03:00
|
|
|
#endif
|
|
|
|
|
2015-04-12 23:38:25 -03:00
|
|
|
#define RANGEFINDER_GROUND_CLEARANCE_CM_DEFAULT 10
|
2015-04-13 00:18:46 -03:00
|
|
|
#define RANGEFINDER_PREARM_ALT_MAX_CM 200
|
2018-10-02 20:43:50 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
|
|
|
|
#define RANGEFINDER_PREARM_REQUIRED_CHANGE_CM 0
|
|
|
|
#else
|
2015-04-13 00:18:46 -03:00
|
|
|
#define RANGEFINDER_PREARM_REQUIRED_CHANGE_CM 50
|
2018-10-02 20:43:50 -03:00
|
|
|
#endif
|
2014-06-26 23:56:50 -03:00
|
|
|
|
2020-08-31 06:16:32 -03:00
|
|
|
#ifndef HAL_MSP_RANGEFINDER_ENABLED
|
|
|
|
#define HAL_MSP_RANGEFINDER_ENABLED HAL_MSP_ENABLED && !HAL_MINIMIZE_FEATURES
|
|
|
|
#endif
|
|
|
|
|
2017-08-25 14:20:18 -03:00
|
|
|
class AP_RangeFinder_Backend;
|
|
|
|
|
2012-01-10 19:44:04 -04:00
|
|
|
class RangeFinder
|
|
|
|
{
|
2015-02-21 06:55:21 -04:00
|
|
|
friend class AP_RangeFinder_Backend;
|
2019-04-08 06:42:46 -03:00
|
|
|
//UAVCAN drivers are initialised in the Backend, hence list of drivers is needed there.
|
|
|
|
friend class AP_RangeFinder_UAVCAN;
|
2017-08-25 14:20:18 -03:00
|
|
|
public:
|
2019-07-09 03:14:52 -03:00
|
|
|
RangeFinder();
|
2017-08-25 14:20:18 -03:00
|
|
|
|
|
|
|
/* Do not allow copies */
|
|
|
|
RangeFinder(const RangeFinder &other) = delete;
|
|
|
|
RangeFinder &operator=(const RangeFinder&) = delete;
|
2014-06-09 14:14:50 -03:00
|
|
|
|
|
|
|
// RangeFinder driver types
|
2019-11-01 00:03:14 -03:00
|
|
|
enum class Type {
|
|
|
|
NONE = 0,
|
|
|
|
ANALOG = 1,
|
|
|
|
MBI2C = 2,
|
|
|
|
PLI2C = 3,
|
2020-02-11 22:22:20 -04:00
|
|
|
// PX4 = 4, // no longer used, but may be in some user's parameters
|
2019-11-01 00:03:14 -03:00
|
|
|
PX4_PWM= 5,
|
|
|
|
BBB_PRU= 6,
|
|
|
|
LWI2C = 7,
|
|
|
|
LWSER = 8,
|
|
|
|
BEBOP = 9,
|
|
|
|
MAVLink = 10,
|
2021-10-18 00:47:20 -03:00
|
|
|
USD1_Serial = 11,
|
2019-11-01 00:03:14 -03:00
|
|
|
LEDDARONE = 12,
|
|
|
|
MBSER = 13,
|
|
|
|
TRI2C = 14,
|
|
|
|
PLI2CV3= 15,
|
|
|
|
VL53L0X = 16,
|
|
|
|
NMEA = 17,
|
|
|
|
WASP = 18,
|
|
|
|
BenewakeTF02 = 19,
|
|
|
|
BenewakeTFmini = 20,
|
|
|
|
PLI2CV3HP = 21,
|
|
|
|
PWM = 22,
|
|
|
|
BLPing = 23,
|
|
|
|
UAVCAN = 24,
|
|
|
|
BenewakeTFminiPlus = 25,
|
|
|
|
Lanbao = 26,
|
|
|
|
BenewakeTF03 = 27,
|
2019-12-10 23:10:02 -04:00
|
|
|
VL53L1X_Short = 28,
|
2020-01-13 01:48:57 -04:00
|
|
|
LeddarVu8_Serial = 29,
|
2020-05-17 03:41:50 -03:00
|
|
|
HC_SR04 = 30,
|
2020-06-14 03:11:25 -03:00
|
|
|
GYUS42v2 = 31,
|
2020-08-04 17:42:19 -03:00
|
|
|
MSP = 32,
|
2020-12-31 20:10:39 -04:00
|
|
|
USD1_CAN = 33,
|
2021-11-12 06:11:48 -04:00
|
|
|
Benewake_CAN = 34,
|
2021-07-30 07:41:47 -03:00
|
|
|
SIM = 100,
|
2014-06-09 14:14:50 -03:00
|
|
|
};
|
|
|
|
|
2019-11-01 00:06:21 -03:00
|
|
|
enum class Function {
|
|
|
|
LINEAR = 0,
|
|
|
|
INVERTED = 1,
|
|
|
|
HYPERBOLA = 2
|
2014-06-09 14:14:50 -03:00
|
|
|
};
|
2014-06-26 23:56:50 -03:00
|
|
|
|
2019-11-01 02:10:52 -03:00
|
|
|
enum class Status {
|
|
|
|
NotConnected = 0,
|
|
|
|
NoData,
|
|
|
|
OutOfRangeLow,
|
|
|
|
OutOfRangeHigh,
|
|
|
|
Good
|
2015-04-13 03:03:19 -03:00
|
|
|
};
|
2014-06-26 23:56:50 -03:00
|
|
|
|
2014-06-09 14:14:50 -03:00
|
|
|
// The RangeFinder_State structure is filled in by the backend driver
|
|
|
|
struct RangeFinder_State {
|
2021-10-18 02:45:33 -03:00
|
|
|
float distance_m; // distance in meters
|
2018-08-27 03:04:32 -03:00
|
|
|
uint16_t voltage_mv; // voltage in millivolts, if applicable, otherwise 0
|
2019-11-01 02:10:52 -03:00
|
|
|
enum RangeFinder::Status status; // sensor status
|
2018-08-27 03:04:32 -03:00
|
|
|
uint8_t range_valid_count; // number of consecutive valid readings (maxes out at 10)
|
AP_RangeFinder: support last_reading_ms
Benewake, LeddarOne, LightWareSerial, MAVLink, MaxsonarI2CXL, MaxsonarSerialLV, NMEA, PX4_PWM, uLanding and Wasp already stored the last read time so for these drivers, this change just moves that storage to the state structure
analog, BBB_PRU, Bebop, LightWareI2C, PulsedLightLRF, TeraRangerI2C, VL53L0X did not store the last read time so this was added
2018-08-27 04:02:51 -03:00
|
|
|
uint32_t last_reading_ms; // system time of last successful update from sensor
|
2017-08-07 00:04:56 -03:00
|
|
|
|
2018-05-16 20:05:50 -03:00
|
|
|
const struct AP_Param::GroupInfo *var_info;
|
2014-06-09 14:14:50 -03:00
|
|
|
};
|
2012-01-10 19:44:04 -04:00
|
|
|
|
2018-05-16 20:05:50 -03:00
|
|
|
static const struct AP_Param::GroupInfo *backend_var_info[RANGEFINDER_MAX_INSTANCES];
|
2014-06-26 23:56:50 -03:00
|
|
|
|
2017-08-07 00:04:56 -03:00
|
|
|
// parameters for each instance
|
2014-06-09 14:14:50 -03:00
|
|
|
static const struct AP_Param::GroupInfo var_info[];
|
2019-04-05 06:20:22 -03:00
|
|
|
|
|
|
|
void set_log_rfnd_bit(uint32_t log_rfnd_bit) { _log_rfnd_bit = log_rfnd_bit; }
|
|
|
|
|
2020-11-26 17:51:38 -04:00
|
|
|
/*
|
|
|
|
Return the number of range finder instances. Note that if users
|
|
|
|
sets up rangefinders with a gap in the types then this is the
|
|
|
|
index of the maximum sensor ID plus one, so this gives the value
|
|
|
|
that should be used when iterating over all sensors
|
|
|
|
*/
|
2014-06-09 14:14:50 -03:00
|
|
|
uint8_t num_sensors(void) const {
|
|
|
|
return num_instances;
|
2012-11-27 21:39:15 -04:00
|
|
|
}
|
2012-01-10 19:44:04 -04:00
|
|
|
|
2019-04-10 04:32:15 -03:00
|
|
|
// prearm checks
|
|
|
|
bool prearm_healthy(char *failure_msg, const uint8_t failure_msg_len) const;
|
|
|
|
|
2014-06-26 23:56:50 -03:00
|
|
|
// detect and initialise any available rangefinders
|
2019-04-05 06:13:42 -03:00
|
|
|
void init(enum Rotation orientation_default);
|
2014-06-26 23:56:50 -03:00
|
|
|
|
|
|
|
// update state of all rangefinders. Should be called at around
|
|
|
|
// 10Hz from main loop
|
2014-06-09 14:14:50 -03:00
|
|
|
void update(void);
|
2016-05-04 00:02:44 -03:00
|
|
|
|
2016-05-03 23:55:05 -03:00
|
|
|
// Handle an incoming DISTANCE_SENSOR message (from a MAVLink enabled range finder)
|
2019-04-30 07:22:49 -03:00
|
|
|
void handle_msg(const mavlink_message_t &msg);
|
2016-05-04 00:02:44 -03:00
|
|
|
|
2020-08-31 06:16:32 -03:00
|
|
|
#if HAL_MSP_RANGEFINDER_ENABLED
|
2020-08-04 17:42:19 -03:00
|
|
|
// Handle an incoming DISTANCE_SENSOR message (from a MSP enabled range finder)
|
2020-09-07 19:29:23 -03:00
|
|
|
void handle_msp(const MSP::msp_rangefinder_data_message_t &pkt);
|
2020-09-01 04:54:29 -03:00
|
|
|
#endif
|
2017-02-09 06:26:57 -04:00
|
|
|
// return true if we have a range finder with the specified orientation
|
|
|
|
bool has_orientation(enum Rotation orientation) const;
|
|
|
|
|
|
|
|
// find first range finder instance with the specified orientation
|
2017-08-08 02:54:09 -03:00
|
|
|
AP_RangeFinder_Backend *find_instance(enum Rotation orientation) const;
|
2017-02-09 06:26:57 -04:00
|
|
|
|
2017-08-08 02:54:09 -03:00
|
|
|
AP_RangeFinder_Backend *get_backend(uint8_t id) const;
|
2017-02-09 06:26:57 -04:00
|
|
|
|
2020-01-24 19:58:15 -04:00
|
|
|
// get rangefinder type for an ID
|
|
|
|
Type get_type(uint8_t id) const {
|
|
|
|
return id >= RANGEFINDER_MAX_INSTANCES? Type::NONE : Type(params[id].type.get());
|
|
|
|
}
|
|
|
|
|
2020-05-11 01:27:55 -03:00
|
|
|
// get rangefinder address (for AP_Periph CAN)
|
|
|
|
uint8_t get_address(uint8_t id) const {
|
|
|
|
return id >= RANGEFINDER_MAX_INSTANCES? 0 : uint8_t(params[id].address.get());
|
|
|
|
}
|
|
|
|
|
2017-08-08 02:54:09 -03:00
|
|
|
// methods to return a distance on a particular orientation from
|
|
|
|
// any sensor which can current supply it
|
2021-10-18 02:45:33 -03:00
|
|
|
float distance_orient(enum Rotation orientation) const;
|
2017-02-09 06:26:57 -04:00
|
|
|
uint16_t distance_cm_orient(enum Rotation orientation) const;
|
|
|
|
int16_t max_distance_cm_orient(enum Rotation orientation) const;
|
|
|
|
int16_t min_distance_cm_orient(enum Rotation orientation) const;
|
|
|
|
int16_t ground_clearance_cm_orient(enum Rotation orientation) const;
|
2017-08-08 04:32:53 -03:00
|
|
|
MAV_DISTANCE_SENSOR get_mav_distance_sensor_type_orient(enum Rotation orientation) const;
|
2019-11-01 02:10:52 -03:00
|
|
|
RangeFinder::Status status_orient(enum Rotation orientation) const;
|
2017-02-09 06:26:57 -04:00
|
|
|
bool has_data_orient(enum Rotation orientation) const;
|
|
|
|
uint8_t range_valid_count_orient(enum Rotation orientation) const;
|
2017-08-08 02:54:09 -03:00
|
|
|
const Vector3f &get_pos_offset_orient(enum Rotation orientation) const;
|
AP_RangeFinder: support last_reading_ms
Benewake, LeddarOne, LightWareSerial, MAVLink, MaxsonarI2CXL, MaxsonarSerialLV, NMEA, PX4_PWM, uLanding and Wasp already stored the last read time so for these drivers, this change just moves that storage to the state structure
analog, BBB_PRU, Bebop, LightWareI2C, PulsedLightLRF, TeraRangerI2C, VL53L0X did not store the last read time so this was added
2018-08-27 04:02:51 -03:00
|
|
|
uint32_t last_reading_ms(enum Rotation orientation) const;
|
2015-02-21 06:55:21 -04:00
|
|
|
|
2021-04-13 05:07:06 -03:00
|
|
|
// get temperature reading in C. returns true on success and populates temp argument
|
2021-06-04 13:32:44 -03:00
|
|
|
bool get_temp(enum Rotation orientation, float &temp) const;
|
2021-04-13 05:07:06 -03:00
|
|
|
|
2015-02-21 06:55:21 -04:00
|
|
|
/*
|
|
|
|
set an externally estimated terrain height. Used to enable power
|
|
|
|
saving (where available) at high altitudes.
|
|
|
|
*/
|
|
|
|
void set_estimated_terrain_height(float height) {
|
|
|
|
estimated_terrain_height = height;
|
|
|
|
}
|
2015-04-13 00:18:46 -03:00
|
|
|
|
2018-05-09 04:45:26 -03:00
|
|
|
static RangeFinder *get_singleton(void) { return _singleton; }
|
|
|
|
|
2018-07-04 11:22:17 -03:00
|
|
|
protected:
|
|
|
|
AP_RangeFinder_Params params[RANGEFINDER_MAX_INSTANCES];
|
2016-10-07 19:29:42 -03:00
|
|
|
|
2014-06-09 14:14:50 -03:00
|
|
|
private:
|
2018-05-09 04:45:26 -03:00
|
|
|
static RangeFinder *_singleton;
|
|
|
|
|
2014-06-09 14:14:50 -03:00
|
|
|
RangeFinder_State state[RANGEFINDER_MAX_INSTANCES];
|
|
|
|
AP_RangeFinder_Backend *drivers[RANGEFINDER_MAX_INSTANCES];
|
2019-02-03 21:30:05 -04:00
|
|
|
uint8_t num_instances;
|
2020-04-30 19:09:27 -03:00
|
|
|
bool init_done;
|
|
|
|
HAL_Semaphore detect_sem;
|
2015-02-21 06:55:21 -04:00
|
|
|
float estimated_terrain_height;
|
2017-02-09 06:26:57 -04:00
|
|
|
Vector3f pos_offset_zero; // allows returning position offsets of zero for invalid requests
|
2014-06-09 14:14:50 -03:00
|
|
|
|
2018-07-04 11:22:17 -03:00
|
|
|
void convert_params(void);
|
|
|
|
|
2018-02-23 08:11:47 -04:00
|
|
|
void detect_instance(uint8_t instance, uint8_t& serial_instance);
|
2015-04-13 00:18:46 -03:00
|
|
|
|
2021-06-04 18:36:24 -03:00
|
|
|
bool _add_backend(AP_RangeFinder_Backend *driver, uint8_t instance, uint8_t serial_instance=0);
|
2019-04-05 06:20:22 -03:00
|
|
|
|
|
|
|
uint32_t _log_rfnd_bit = -1;
|
2021-02-01 12:26:33 -04:00
|
|
|
void Log_RFND() const;
|
2019-04-05 06:20:22 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
namespace AP {
|
|
|
|
RangeFinder *rangefinder();
|
2012-01-10 19:44:04 -04:00
|
|
|
};
|