2016-08-15 03:17:15 -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/>.
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2022-07-11 21:48:35 -03:00
|
|
|
#include "AP_Proximity_config.h"
|
2021-03-25 04:32:09 -03:00
|
|
|
|
|
|
|
#if HAL_PROXIMITY_ENABLED
|
|
|
|
|
|
|
|
#include <AP_Common/AP_Common.h>
|
2016-08-15 03:17:15 -03:00
|
|
|
#include <AP_Param/AP_Param.h>
|
|
|
|
#include <AP_Math/AP_Math.h>
|
2019-09-27 06:04:30 -03:00
|
|
|
#include <GCS_MAVLink/GCS_MAVLink.h>
|
2022-07-05 04:28:24 -03:00
|
|
|
#include "AP_Proximity_Params.h"
|
2022-07-07 00:35:43 -03:00
|
|
|
#include "AP_Proximity_Boundary_3D.h"
|
2022-09-27 16:44:39 -03:00
|
|
|
#include <AP_Vehicle/AP_Vehicle_Type.h>
|
|
|
|
|
|
|
|
#include <AP_HAL/Semaphores.h>
|
2016-08-15 03:17:15 -03:00
|
|
|
|
2022-08-21 14:25:44 -03:00
|
|
|
#define PROXIMITY_MAX_INSTANCES 3 // Maximum number of proximity sensor instances available on this platform
|
2017-05-30 06:55:20 -03:00
|
|
|
#define PROXIMITY_SENSOR_ID_START 10
|
2016-08-15 03:17:15 -03:00
|
|
|
|
|
|
|
class AP_Proximity_Backend;
|
|
|
|
|
|
|
|
class AP_Proximity
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
friend class AP_Proximity_Backend;
|
2022-09-27 16:44:39 -03:00
|
|
|
friend class AP_Proximity_DroneCAN;
|
2016-08-15 03:17:15 -03:00
|
|
|
|
2019-09-27 05:06:44 -03:00
|
|
|
AP_Proximity();
|
2016-08-15 03:17:15 -03:00
|
|
|
|
2022-09-30 06:50:43 -03:00
|
|
|
/* Do not allow copies */
|
|
|
|
CLASS_NO_COPY(AP_Proximity);
|
2018-05-09 04:45:43 -03:00
|
|
|
|
2016-08-15 03:17:15 -03:00
|
|
|
// Proximity driver types
|
2019-09-27 05:43:39 -03:00
|
|
|
enum class Type {
|
|
|
|
None = 0,
|
2022-01-06 03:15:10 -04:00
|
|
|
// 1 was SF40C_v09
|
2023-04-05 09:25:54 -03:00
|
|
|
#if AP_PROXIMITY_MAV_ENABLED
|
2019-09-27 05:43:39 -03:00
|
|
|
MAV = 2,
|
2023-04-05 09:25:54 -03:00
|
|
|
#endif
|
|
|
|
#if AP_PROXIMITY_TERARANGERTOWER_ENABLED
|
2019-09-27 05:43:39 -03:00
|
|
|
TRTOWER = 3,
|
2023-04-05 09:25:54 -03:00
|
|
|
#endif
|
|
|
|
#if AP_PROXIMITY_RANGEFINDER_ENABLED
|
2019-09-27 05:43:39 -03:00
|
|
|
RangeFinder = 4,
|
2023-04-05 09:25:54 -03:00
|
|
|
#endif
|
|
|
|
#if AP_PROXIMITY_RPLIDARA2_ENABLED
|
2019-09-27 05:43:39 -03:00
|
|
|
RPLidarA2 = 5,
|
2023-04-05 09:25:54 -03:00
|
|
|
#endif
|
|
|
|
#if AP_PROXIMITY_TERARANGERTOWEREVO_ENABLED
|
2019-09-27 05:43:39 -03:00
|
|
|
TRTOWEREVO = 6,
|
2023-04-05 09:25:54 -03:00
|
|
|
#endif
|
|
|
|
#if AP_PROXIMITY_LIGHTWARE_SF40C_ENABLED
|
2019-11-13 04:46:29 -04:00
|
|
|
SF40C = 7,
|
2023-04-05 09:25:54 -03:00
|
|
|
#endif
|
|
|
|
#if AP_PROXIMITY_LIGHTWARE_SF45B_ENABLED
|
2020-09-29 10:30:24 -03:00
|
|
|
SF45B = 8,
|
2023-04-05 09:25:54 -03:00
|
|
|
#endif
|
|
|
|
#if AP_PROXIMITY_SITL_ENABLED
|
2019-09-27 05:43:39 -03:00
|
|
|
SITL = 10,
|
2023-04-05 09:25:54 -03:00
|
|
|
#endif
|
|
|
|
#if AP_PROXIMITY_AIRSIMSITL_ENABLED
|
2019-09-27 05:43:39 -03:00
|
|
|
AirSimSITL = 12,
|
|
|
|
#endif
|
2023-04-05 19:56:53 -03:00
|
|
|
#if AP_PROXIMITY_CYGBOT_ENABLED
|
2021-11-05 09:17:26 -03:00
|
|
|
CYGBOT_D1 = 13,
|
2023-04-05 19:56:53 -03:00
|
|
|
#endif
|
2023-04-05 09:25:54 -03:00
|
|
|
#if AP_PROXIMITY_DRONECAN_ENABLED
|
2022-09-27 16:44:39 -03:00
|
|
|
DroneCAN = 14,
|
2023-07-04 16:40:06 -03:00
|
|
|
#endif
|
|
|
|
#if AP_PROXIMITY_SCRIPTING_ENABLED
|
|
|
|
Scripting = 15,
|
2022-06-17 00:28:26 -03:00
|
|
|
#endif
|
|
|
|
#if AP_PROXIMITY_LD06_ENABLED
|
|
|
|
LD06 = 16,
|
2023-04-05 09:25:54 -03:00
|
|
|
#endif
|
2016-08-15 03:17:15 -03:00
|
|
|
};
|
|
|
|
|
2019-12-04 23:05:45 -04:00
|
|
|
enum class Status {
|
2019-09-27 05:58:52 -03:00
|
|
|
NotConnected = 0,
|
|
|
|
NoData,
|
|
|
|
Good
|
2016-08-15 03:17:15 -03:00
|
|
|
};
|
|
|
|
|
2016-12-23 02:00:55 -04:00
|
|
|
// detect and initialise any available proximity sensors
|
2022-07-05 04:58:38 -03:00
|
|
|
void init();
|
2016-08-15 03:17:15 -03:00
|
|
|
|
2016-12-23 02:00:55 -04:00
|
|
|
// update state of all proximity sensors. Should be called at high rate from main loop
|
2022-07-05 04:58:38 -03:00
|
|
|
void update();
|
|
|
|
|
|
|
|
// return the number of proximity sensor backends
|
|
|
|
uint8_t num_sensors() const { return num_instances; }
|
|
|
|
|
|
|
|
// return sensor type of a given instance
|
|
|
|
Type get_type(uint8_t instance) const;
|
2016-08-15 03:17:15 -03:00
|
|
|
|
2022-07-18 05:51:43 -03:00
|
|
|
// return distance filter frequency
|
2021-03-15 15:01:53 -03:00
|
|
|
float get_filter_freq() const { return _filt_freq; }
|
2016-08-15 03:17:15 -03:00
|
|
|
|
|
|
|
// return sensor health
|
2022-08-22 14:39:33 -03:00
|
|
|
Status get_instance_status(uint8_t instance) const;
|
2019-09-27 05:58:52 -03:00
|
|
|
Status get_status() const;
|
2016-08-15 03:17:15 -03:00
|
|
|
|
2022-08-22 14:39:33 -03:00
|
|
|
// prearm checks
|
|
|
|
bool prearm_healthy(char *failure_msg, const uint8_t failure_msg_len) const;
|
|
|
|
|
|
|
|
// get maximum and minimum distances (in meters)
|
2022-07-05 04:58:38 -03:00
|
|
|
float distance_max() const;
|
|
|
|
float distance_min() const;
|
|
|
|
|
|
|
|
//
|
|
|
|
// 3D boundary related methods
|
|
|
|
//
|
|
|
|
|
2017-05-30 06:55:20 -03:00
|
|
|
// get distances in PROXIMITY_MAX_DIRECTION directions. used for sending distances to ground station
|
2017-04-12 06:00:41 -03:00
|
|
|
bool get_horizontal_distances(Proximity_Distance_Array &prx_dist_array) const;
|
|
|
|
|
2020-12-06 08:23:55 -04:00
|
|
|
// get total number of obstacles, used in GPS based Simple Avoidance
|
|
|
|
uint8_t get_obstacle_count() const;
|
2022-07-05 04:43:46 -03:00
|
|
|
|
2020-12-06 08:23:55 -04:00
|
|
|
// get vector to obstacle based on obstacle_num passed, used in GPS based Simple Avoidance
|
2020-12-27 12:21:58 -04:00
|
|
|
bool get_obstacle(uint8_t obstacle_num, Vector3f& vec_to_obstacle) const;
|
2022-07-05 04:43:46 -03:00
|
|
|
|
2020-12-06 08:23:55 -04:00
|
|
|
// returns shortest distance to "obstacle_num" obstacle, from a line segment formed between "seg_start" and "seg_end"
|
2021-01-10 14:14:24 -04:00
|
|
|
// returns FLT_MAX if it's an invalid instance.
|
2021-05-26 16:02:53 -03:00
|
|
|
bool closest_point_from_segment_to_obstacle(uint8_t obstacle_num, const Vector3f& seg_start, const Vector3f& seg_end, Vector3f& closest_point) const;
|
2016-11-15 03:29:35 -04:00
|
|
|
|
2016-11-15 03:15:46 -04:00
|
|
|
// get distance and angle to closest object (used for pre-arm check)
|
|
|
|
// returns true on success, false if no valid readings
|
|
|
|
bool get_closest_object(float& angle_deg, float &distance) const;
|
|
|
|
|
2022-08-23 18:44:34 -03:00
|
|
|
// get number of objects
|
2016-12-16 23:42:13 -04:00
|
|
|
uint8_t get_object_count() const;
|
|
|
|
bool get_object_angle_and_distance(uint8_t object_number, float& angle_deg, float &distance) const;
|
|
|
|
|
2022-09-27 16:44:39 -03:00
|
|
|
// get obstacle pitch and angle for a particular obstacle num
|
|
|
|
bool get_obstacle_info(uint8_t obstacle_num, float &angle_deg, float &pitch, float &distance) const;
|
|
|
|
|
2022-07-05 04:58:38 -03:00
|
|
|
//
|
|
|
|
// mavlink related methods
|
|
|
|
//
|
2016-11-25 01:01:21 -04:00
|
|
|
|
2022-08-16 22:56:18 -03:00
|
|
|
// handle mavlink messages
|
2019-04-30 07:22:49 -03:00
|
|
|
void handle_msg(const mavlink_message_t &msg);
|
2017-01-09 23:09:18 -04:00
|
|
|
|
2022-07-05 04:58:38 -03:00
|
|
|
// methods for mavlink SYS_STATUS message (send_sys_status)
|
|
|
|
bool sensor_present() const;
|
|
|
|
bool sensor_enabled() const;
|
|
|
|
bool sensor_failed() const;
|
2016-08-15 03:17:15 -03:00
|
|
|
|
2017-01-16 02:58:14 -04:00
|
|
|
//
|
2022-07-05 04:58:38 -03:00
|
|
|
// support for upwards and downwards facing sensors
|
2017-01-16 02:58:14 -04:00
|
|
|
//
|
|
|
|
|
|
|
|
// get distance upwards in meters. returns true on success
|
|
|
|
bool get_upward_distance(uint8_t instance, float &distance) const;
|
|
|
|
bool get_upward_distance(float &distance) const;
|
|
|
|
|
2022-07-05 04:58:38 -03:00
|
|
|
// set alt as read from downward facing rangefinder. Tilt is already adjusted for
|
|
|
|
void set_rangefinder_alt(bool use, bool healthy, float alt_cm);
|
|
|
|
|
|
|
|
// method called by vehicle to have AP_Proximity write onboard log messages
|
|
|
|
void log();
|
|
|
|
|
|
|
|
// The Proximity_State structure is filled in by the backend driver
|
|
|
|
struct Proximity_State {
|
|
|
|
uint8_t instance; // the instance number of this proximity sensor
|
|
|
|
Status status; // sensor status
|
|
|
|
};
|
2018-05-09 04:45:43 -03:00
|
|
|
|
2016-08-15 03:17:15 -03:00
|
|
|
// parameter list
|
|
|
|
static const struct AP_Param::GroupInfo var_info[];
|
|
|
|
|
2018-05-09 04:45:43 -03:00
|
|
|
static AP_Proximity *get_singleton(void) { return _singleton; };
|
|
|
|
|
2023-07-04 16:40:06 -03:00
|
|
|
// return backend object for Lua scripting
|
|
|
|
AP_Proximity_Backend *get_backend(uint8_t id) const;
|
|
|
|
|
2022-07-07 00:35:43 -03:00
|
|
|
// 3D boundary
|
|
|
|
AP_Proximity_Boundary_3D boundary;
|
|
|
|
|
2022-08-22 14:42:10 -03:00
|
|
|
// Check if Obstacle defined by body-frame yaw and pitch is near ground
|
|
|
|
bool check_obstacle_near_ground(float pitch, float yaw, float distance) const;
|
|
|
|
|
2022-09-27 16:44:39 -03:00
|
|
|
// get proximity address (for AP_Periph CAN)
|
|
|
|
uint8_t get_address(uint8_t id) const {
|
|
|
|
return id >= PROXIMITY_MAX_INSTANCES? 0 : uint8_t(params[id].address.get());
|
|
|
|
}
|
|
|
|
|
2022-07-05 04:28:24 -03:00
|
|
|
protected:
|
|
|
|
|
|
|
|
// parameters for backends
|
|
|
|
AP_Proximity_Params params[PROXIMITY_MAX_INSTANCES];
|
|
|
|
|
2016-08-15 03:17:15 -03:00
|
|
|
private:
|
2018-05-09 04:45:43 -03:00
|
|
|
static AP_Proximity *_singleton;
|
2016-08-15 03:17:15 -03:00
|
|
|
Proximity_State state[PROXIMITY_MAX_INSTANCES];
|
|
|
|
AP_Proximity_Backend *drivers[PROXIMITY_MAX_INSTANCES];
|
2019-02-03 21:30:50 -04:00
|
|
|
uint8_t num_instances;
|
2016-08-15 03:17:15 -03:00
|
|
|
|
2022-07-05 04:28:24 -03:00
|
|
|
// return true if the given instance exists
|
|
|
|
bool valid_instance(uint8_t i) const;
|
|
|
|
|
2016-08-15 03:17:15 -03:00
|
|
|
// parameters for all instances
|
2022-09-12 14:36:34 -03:00
|
|
|
AP_Int8 _raw_log_enable; // enable logging raw distances
|
2021-02-19 14:55:28 -04:00
|
|
|
AP_Int8 _ign_gnd_enable; // true if land detection should be enabled
|
2021-03-15 15:01:53 -03:00
|
|
|
AP_Float _filt_freq; // cutoff frequency for low pass filter
|
2022-09-12 14:36:34 -03:00
|
|
|
AP_Float _alt_min; // Minimum altitude -in meters- below which proximity should not work.
|
2022-08-22 14:42:10 -03:00
|
|
|
|
|
|
|
// get alt from rangefinder in meters. This reading is corrected for vehicle tilt
|
|
|
|
bool get_rangefinder_alt(float &alt_m) const;
|
|
|
|
|
|
|
|
struct RangeFinderState {
|
|
|
|
bool use; // true if enabled
|
|
|
|
bool healthy; // true if we can trust the altitude from the rangefinder
|
|
|
|
int16_t alt_cm; // tilt compensated altitude (in cm) from rangefinder
|
|
|
|
uint32_t last_downward_update_ms; // last update ms
|
|
|
|
} _rangefinder_state;
|
|
|
|
|
2022-09-27 16:44:39 -03:00
|
|
|
HAL_Semaphore detect_sem;
|
2016-08-15 03:17:15 -03:00
|
|
|
};
|
2019-05-22 03:02:11 -03:00
|
|
|
|
|
|
|
namespace AP {
|
|
|
|
AP_Proximity *proximity();
|
|
|
|
};
|
2021-03-25 04:32:09 -03:00
|
|
|
|
|
|
|
#endif // HAL_PROXIMITY_ENABLED
|