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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "AP_Proximity.h"
|
2021-03-25 04:32:09 -03:00
|
|
|
|
|
|
|
#if HAL_PROXIMITY_ENABLED
|
2023-04-13 06:56:18 -03:00
|
|
|
#include "AP_Proximity_Backend.h"
|
2017-07-07 13:04:55 -03:00
|
|
|
#include "AP_Proximity_RPLidarA2.h"
|
2017-02-03 00:36:03 -04:00
|
|
|
#include "AP_Proximity_TeraRangerTower.h"
|
2018-06-15 06:08:01 -03:00
|
|
|
#include "AP_Proximity_TeraRangerTowerEvo.h"
|
2017-02-09 07:30:04 -04:00
|
|
|
#include "AP_Proximity_RangeFinder.h"
|
2016-12-23 02:03:21 -04:00
|
|
|
#include "AP_Proximity_MAV.h"
|
2019-11-13 04:46:29 -04:00
|
|
|
#include "AP_Proximity_LightWareSF40C.h"
|
2020-09-29 10:30:24 -03:00
|
|
|
#include "AP_Proximity_LightWareSF45B.h"
|
2016-11-14 00:48:38 -04:00
|
|
|
#include "AP_Proximity_SITL.h"
|
2019-07-10 19:15:58 -03:00
|
|
|
#include "AP_Proximity_AirSimSITL.h"
|
2021-11-05 09:17:26 -03:00
|
|
|
#include "AP_Proximity_Cygbot_D1.h"
|
2022-09-27 16:44:39 -03:00
|
|
|
#include "AP_Proximity_DroneCAN.h"
|
2016-08-15 03:17:15 -03:00
|
|
|
|
2022-03-03 21:12:18 -04:00
|
|
|
#include <AP_Logger/AP_Logger.h>
|
|
|
|
|
2016-08-15 03:17:15 -03:00
|
|
|
extern const AP_HAL::HAL &hal;
|
|
|
|
|
|
|
|
// table of user settable parameters
|
|
|
|
const AP_Param::GroupInfo AP_Proximity::var_info[] = {
|
|
|
|
// 0 is reserved for possible addition of an ENABLED parameter
|
|
|
|
|
2022-07-05 04:28:24 -03:00
|
|
|
// 1 was _TYPE
|
|
|
|
// 2 was _ORIENT
|
|
|
|
// 3 was _YAW_CORR
|
|
|
|
// 4 to 15 was _IGN_ANG1 to _IGN_WID6
|
2016-11-15 03:27:34 -04:00
|
|
|
|
2021-02-19 14:55:28 -04:00
|
|
|
// @Param{Copter}: _IGN_GND
|
|
|
|
// @DisplayName: Proximity sensor land detection
|
|
|
|
// @Description: Ignore proximity data that is within 1 meter of the ground below the vehicle. This requires a downward facing rangefinder
|
|
|
|
// @Values: 0:Disabled, 1:Enabled
|
2016-08-15 03:17:15 -03:00
|
|
|
// @User: Standard
|
2021-04-04 03:46:29 -03:00
|
|
|
AP_GROUPINFO_FRAME("_IGN_GND", 16, AP_Proximity, _ign_gnd_enable, 0, AP_PARAM_FRAME_COPTER | AP_PARAM_FRAME_HELI | AP_PARAM_FRAME_TRICOPTER),
|
2016-08-15 03:17:15 -03:00
|
|
|
|
2021-02-27 12:49:27 -04:00
|
|
|
// @Param: _LOG_RAW
|
|
|
|
// @DisplayName: Proximity raw distances log
|
|
|
|
// @Description: Set this parameter to one if logging unfiltered(raw) distances from sensor should be enabled
|
|
|
|
// @Values: 0:Off, 1:On
|
|
|
|
// @User: Advanced
|
2021-02-19 14:55:28 -04:00
|
|
|
AP_GROUPINFO("_LOG_RAW", 17, AP_Proximity, _raw_log_enable, 0),
|
2021-02-27 12:49:27 -04:00
|
|
|
|
2021-03-15 15:01:53 -03:00
|
|
|
// @Param: _FILT
|
|
|
|
// @DisplayName: Proximity filter cutoff frequency
|
|
|
|
// @Description: Cutoff frequency for low pass filter applied to each face in the proximity boundary
|
|
|
|
// @Units: Hz
|
|
|
|
// @Range: 0 20
|
|
|
|
// @User: Advanced
|
|
|
|
AP_GROUPINFO("_FILT", 18, AP_Proximity, _filt_freq, 0.25f),
|
|
|
|
|
2022-07-05 04:28:24 -03:00
|
|
|
// 19 was _MIN
|
|
|
|
// 20 was _MAX
|
2021-12-29 07:16:34 -04:00
|
|
|
|
2022-09-12 14:36:34 -03:00
|
|
|
// @Param{Copter}: _ALT_MIN
|
|
|
|
// @DisplayName: Proximity lowest altitude.
|
|
|
|
// @Description: Minimum altitude below which proximity should not work.
|
|
|
|
// @Units: m
|
|
|
|
// @Range: 0 10
|
|
|
|
// @User: Advanced
|
|
|
|
AP_GROUPINFO_FRAME("_ALT_MIN", 25, AP_Proximity, _alt_min, 1.0f, AP_PARAM_FRAME_COPTER | AP_PARAM_FRAME_HELI | AP_PARAM_FRAME_TRICOPTER),
|
|
|
|
|
2022-07-05 04:28:24 -03:00
|
|
|
// @Group: 1
|
|
|
|
// @Path: AP_Proximity_Params.cpp
|
|
|
|
AP_SUBGROUPINFO(params[0], "1", 21, AP_Proximity, AP_Proximity_Params),
|
|
|
|
|
|
|
|
#if PROXIMITY_MAX_INSTANCES > 1
|
|
|
|
// @Group: 2
|
|
|
|
// @Path: AP_Proximity_Params.cpp
|
|
|
|
AP_SUBGROUPINFO(params[1], "2", 22, AP_Proximity, AP_Proximity_Params),
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if PROXIMITY_MAX_INSTANCES > 2
|
|
|
|
// @Group: 3
|
|
|
|
// @Path: AP_Proximity_Params.cpp
|
|
|
|
AP_SUBGROUPINFO(params[2], "3", 23, AP_Proximity, AP_Proximity_Params),
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if PROXIMITY_MAX_INSTANCES > 3
|
|
|
|
// @Group: 4
|
|
|
|
// @Path: AP_Proximity_Params.cpp
|
|
|
|
AP_SUBGROUPINFO(params[3], "4", 24, AP_Proximity, AP_Proximity_Params),
|
|
|
|
#endif
|
2021-12-29 07:16:34 -04:00
|
|
|
|
2016-08-15 03:17:15 -03:00
|
|
|
AP_GROUPEND
|
|
|
|
};
|
|
|
|
|
2019-09-27 05:06:44 -03:00
|
|
|
AP_Proximity::AP_Proximity()
|
2016-08-15 03:17:15 -03:00
|
|
|
{
|
|
|
|
AP_Param::setup_object_defaults(this, var_info);
|
2018-05-09 04:45:43 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
|
|
|
|
if (_singleton != nullptr) {
|
|
|
|
AP_HAL::panic("AP_Proximity must be singleton");
|
|
|
|
}
|
|
|
|
#endif // CONFIG_HAL_BOARD == HAL_BOARD_SITL
|
|
|
|
_singleton = this;
|
2016-08-15 03:17:15 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// initialise the Proximity class. We do detection of attached sensors here
|
|
|
|
// we don't allow for hot-plugging of sensors (i.e. reboot required)
|
2022-07-05 04:58:38 -03:00
|
|
|
void AP_Proximity::init()
|
2016-08-15 03:17:15 -03:00
|
|
|
{
|
|
|
|
if (num_instances != 0) {
|
|
|
|
// init called a 2nd time?
|
|
|
|
return;
|
|
|
|
}
|
2022-07-12 02:29:35 -03:00
|
|
|
|
|
|
|
// instantiate backends
|
|
|
|
uint8_t serial_instance = 0;
|
2023-04-05 09:25:54 -03:00
|
|
|
(void)serial_instance; // in case no serial backends are compiled in
|
2022-07-12 02:29:35 -03:00
|
|
|
for (uint8_t instance=0; instance<PROXIMITY_MAX_INSTANCES; instance++) {
|
|
|
|
switch (get_type(instance)) {
|
|
|
|
case Type::None:
|
|
|
|
break;
|
2023-04-05 09:25:54 -03:00
|
|
|
#if AP_PROXIMITY_RPLIDARA2_ENABLED
|
2022-07-12 02:29:35 -03:00
|
|
|
case Type::RPLidarA2:
|
|
|
|
if (AP_Proximity_RPLidarA2::detect(serial_instance)) {
|
|
|
|
state[instance].instance = instance;
|
|
|
|
drivers[instance] = new AP_Proximity_RPLidarA2(*this, state[instance], params[instance], serial_instance);
|
|
|
|
serial_instance++;
|
|
|
|
}
|
|
|
|
break;
|
2023-04-05 09:25:54 -03:00
|
|
|
#endif
|
|
|
|
#if AP_PROXIMITY_MAV_ENABLED
|
2022-07-12 02:29:35 -03:00
|
|
|
case Type::MAV:
|
|
|
|
state[instance].instance = instance;
|
|
|
|
drivers[instance] = new AP_Proximity_MAV(*this, state[instance], params[instance]);
|
|
|
|
break;
|
2023-04-05 09:25:54 -03:00
|
|
|
#endif
|
|
|
|
#if AP_PROXIMITY_TERARANGERTOWER_ENABLED
|
2022-07-12 02:29:35 -03:00
|
|
|
case Type::TRTOWER:
|
|
|
|
if (AP_Proximity_TeraRangerTower::detect(serial_instance)) {
|
|
|
|
state[instance].instance = instance;
|
|
|
|
drivers[instance] = new AP_Proximity_TeraRangerTower(*this, state[instance], params[instance], serial_instance);
|
|
|
|
serial_instance++;
|
|
|
|
}
|
|
|
|
break;
|
2023-04-05 09:25:54 -03:00
|
|
|
#endif
|
|
|
|
#if AP_PROXIMITY_TERARANGERTOWEREVO_ENABLED
|
2022-07-12 02:29:35 -03:00
|
|
|
case Type::TRTOWEREVO:
|
|
|
|
if (AP_Proximity_TeraRangerTowerEvo::detect(serial_instance)) {
|
|
|
|
state[instance].instance = instance;
|
|
|
|
drivers[instance] = new AP_Proximity_TeraRangerTowerEvo(*this, state[instance], params[instance], serial_instance);
|
|
|
|
serial_instance++;
|
|
|
|
}
|
|
|
|
break;
|
2023-04-05 09:25:54 -03:00
|
|
|
#endif
|
|
|
|
#if AP_PROXIMITY_RANGEFINDER_ENABLED
|
2022-07-12 02:29:35 -03:00
|
|
|
case Type::RangeFinder:
|
|
|
|
state[instance].instance = instance;
|
|
|
|
drivers[instance] = new AP_Proximity_RangeFinder(*this, state[instance], params[instance]);
|
|
|
|
break;
|
2023-04-05 09:25:54 -03:00
|
|
|
#endif
|
|
|
|
#if AP_PROXIMITY_LIGHTWARE_SF40C_ENABLED
|
2022-07-12 02:29:35 -03:00
|
|
|
case Type::SF40C:
|
|
|
|
if (AP_Proximity_LightWareSF40C::detect(serial_instance)) {
|
|
|
|
state[instance].instance = instance;
|
|
|
|
drivers[instance] = new AP_Proximity_LightWareSF40C(*this, state[instance], params[instance], serial_instance);
|
|
|
|
serial_instance++;
|
|
|
|
}
|
|
|
|
break;
|
2023-04-05 09:25:54 -03:00
|
|
|
#endif
|
|
|
|
#if AP_PROXIMITY_LIGHTWARE_SF45B_ENABLED
|
2022-07-12 02:29:35 -03:00
|
|
|
case Type::SF45B:
|
|
|
|
if (AP_Proximity_LightWareSF45B::detect(serial_instance)) {
|
|
|
|
state[instance].instance = instance;
|
|
|
|
drivers[instance] = new AP_Proximity_LightWareSF45B(*this, state[instance], params[instance], serial_instance);
|
|
|
|
serial_instance++;
|
|
|
|
}
|
|
|
|
break;
|
2023-04-05 09:25:54 -03:00
|
|
|
#endif
|
2022-07-12 02:29:35 -03:00
|
|
|
#if AP_PROXIMITY_CYGBOT_ENABLED
|
2023-04-05 19:56:53 -03:00
|
|
|
case Type::CYGBOT_D1:
|
2022-07-12 02:29:35 -03:00
|
|
|
if (AP_Proximity_Cygbot_D1::detect(serial_instance)) {
|
|
|
|
state[instance].instance = instance;
|
|
|
|
drivers[instance] = new AP_Proximity_Cygbot_D1(*this, state[instance], params[instance], serial_instance);
|
|
|
|
serial_instance++;
|
|
|
|
}
|
2022-09-27 16:44:39 -03:00
|
|
|
break;
|
2022-07-12 02:29:35 -03:00
|
|
|
# endif
|
2023-04-05 09:25:54 -03:00
|
|
|
#if AP_PROXIMITY_DRONECAN_ENABLED
|
2022-09-27 16:44:39 -03:00
|
|
|
case Type::DroneCAN:
|
|
|
|
num_instances = instance+1;
|
2022-07-12 02:29:35 -03:00
|
|
|
break;
|
2023-04-05 09:25:54 -03:00
|
|
|
#endif
|
|
|
|
#if AP_PROXIMITY_SITL_ENABLED
|
2022-07-12 02:29:35 -03:00
|
|
|
case Type::SITL:
|
|
|
|
state[instance].instance = instance;
|
|
|
|
drivers[instance] = new AP_Proximity_SITL(*this, state[instance], params[instance]);
|
|
|
|
break;
|
2023-04-05 09:25:54 -03:00
|
|
|
#endif
|
|
|
|
#if AP_PROXIMITY_AIRSIMSITL_ENABLED
|
2022-07-12 02:29:35 -03:00
|
|
|
case Type::AirSimSITL:
|
|
|
|
state[instance].instance = instance;
|
|
|
|
drivers[instance] = new AP_Proximity_AirSimSITL(*this, state[instance], params[instance]);
|
|
|
|
break;
|
|
|
|
#endif
|
2022-08-16 22:56:18 -03:00
|
|
|
}
|
2022-07-12 02:29:35 -03:00
|
|
|
|
|
|
|
if (drivers[instance] != nullptr) {
|
2016-08-15 03:17:15 -03:00
|
|
|
// we loaded a driver for this instance, so it must be
|
|
|
|
// present (although it may not be healthy)
|
2022-07-12 02:29:35 -03:00
|
|
|
num_instances = instance+1;
|
2016-08-15 03:17:15 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// initialise status
|
2022-07-12 02:29:35 -03:00
|
|
|
state[instance].status = Status::NotConnected;
|
2016-08-15 03:17:15 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// update Proximity state for all instances. This should be called at a high rate by the main loop
|
2022-07-05 04:58:38 -03:00
|
|
|
void AP_Proximity::update()
|
2016-08-15 03:17:15 -03:00
|
|
|
{
|
|
|
|
for (uint8_t i=0; i<num_instances; i++) {
|
2019-09-27 05:48:49 -03:00
|
|
|
if (!valid_instance(i)) {
|
|
|
|
continue;
|
2016-08-15 03:17:15 -03:00
|
|
|
}
|
2019-09-27 05:48:49 -03:00
|
|
|
drivers[i]->update();
|
2016-08-15 03:17:15 -03:00
|
|
|
}
|
|
|
|
|
2022-07-07 00:35:43 -03:00
|
|
|
// set boundary cutoff freq for low pass filter
|
|
|
|
boundary.set_filter_freq(get_filter_freq());
|
|
|
|
|
|
|
|
// check if any face has valid distance when it should not
|
|
|
|
boundary.check_face_timeout();
|
2016-08-15 03:17:15 -03:00
|
|
|
}
|
|
|
|
|
2022-07-05 04:58:38 -03:00
|
|
|
AP_Proximity::Type AP_Proximity::get_type(uint8_t instance) const
|
|
|
|
{
|
|
|
|
if (instance < PROXIMITY_MAX_INSTANCES) {
|
|
|
|
return (Type)((uint8_t)params[instance].type);
|
|
|
|
}
|
|
|
|
return Type::None;
|
|
|
|
}
|
|
|
|
|
2016-08-15 03:17:15 -03:00
|
|
|
// return sensor health
|
2022-08-22 14:39:33 -03:00
|
|
|
AP_Proximity::Status AP_Proximity::get_instance_status(uint8_t instance) const
|
2016-08-15 03:17:15 -03:00
|
|
|
{
|
|
|
|
// sanity check instance number
|
2019-09-27 05:48:49 -03:00
|
|
|
if (!valid_instance(instance)) {
|
2019-09-27 05:58:52 -03:00
|
|
|
return Status::NotConnected;
|
2016-08-15 03:17:15 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
return state[instance].status;
|
|
|
|
}
|
|
|
|
|
2019-09-27 05:58:52 -03:00
|
|
|
AP_Proximity::Status AP_Proximity::get_status() const
|
2016-08-15 03:17:15 -03:00
|
|
|
{
|
2022-08-22 14:39:33 -03:00
|
|
|
for (uint8_t i=0; i<num_instances; i++) {
|
|
|
|
const Status sensors_status = get_instance_status(i);
|
|
|
|
if (sensors_status != Status::Good) {
|
|
|
|
// return first bad status
|
|
|
|
return sensors_status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// All valid sensors seem to be working
|
|
|
|
return Status::Good;
|
|
|
|
}
|
|
|
|
|
|
|
|
// prearm checks
|
|
|
|
bool AP_Proximity::prearm_healthy(char *failure_msg, const uint8_t failure_msg_len) const
|
|
|
|
{
|
|
|
|
for (uint8_t i=0; i<num_instances; i++) {
|
|
|
|
switch (get_instance_status(i)) {
|
|
|
|
case Status::NoData:
|
|
|
|
hal.util->snprintf(failure_msg, failure_msg_len, "PRX%d: No Data", i + 1);
|
|
|
|
return false;
|
|
|
|
case Status::NotConnected:
|
|
|
|
hal.util->snprintf(failure_msg, failure_msg_len, "PRX%d: Not Connected", i + 1);
|
|
|
|
return false;
|
|
|
|
case Status::Good:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2016-08-15 03:17:15 -03:00
|
|
|
}
|
|
|
|
|
2022-08-22 14:39:33 -03:00
|
|
|
// get maximum and minimum distances (in meters)
|
2022-07-05 04:58:38 -03:00
|
|
|
float AP_Proximity::distance_max() const
|
2017-01-09 23:09:18 -04:00
|
|
|
{
|
2022-08-16 23:10:53 -03:00
|
|
|
float dist_max = 0;
|
|
|
|
|
|
|
|
// return longest distance from all backends
|
|
|
|
for (uint8_t i=0; i<num_instances; i++) {
|
|
|
|
if (valid_instance(i)) {
|
|
|
|
dist_max = MAX(dist_max, drivers[i]->distance_max());
|
|
|
|
}
|
2017-01-09 23:09:18 -04:00
|
|
|
}
|
2022-08-16 23:10:53 -03:00
|
|
|
return dist_max;
|
2017-01-09 23:09:18 -04:00
|
|
|
}
|
2022-07-05 04:58:38 -03:00
|
|
|
float AP_Proximity::distance_min() const
|
2022-07-05 04:28:24 -03:00
|
|
|
{
|
2022-08-16 23:10:53 -03:00
|
|
|
float dist_min = 0;
|
|
|
|
bool found_dist_min = false;
|
|
|
|
|
|
|
|
// calculate shortest distance from all backends
|
|
|
|
for (uint8_t i=0; i<num_instances; i++) {
|
|
|
|
if (valid_instance(i)) {
|
|
|
|
const float disti_min = drivers[i]->distance_min();
|
|
|
|
if (!found_dist_min || (disti_min <= dist_min)) {
|
|
|
|
dist_min = disti_min;
|
|
|
|
found_dist_min = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found_dist_min) {
|
|
|
|
return dist_min;
|
2022-07-05 04:28:24 -03:00
|
|
|
}
|
2022-08-16 23:10:53 -03:00
|
|
|
return 0;
|
2022-07-05 04:28:24 -03:00
|
|
|
}
|
|
|
|
|
2016-08-15 03:17:15 -03:00
|
|
|
|
2017-04-12 06:00:41 -03:00
|
|
|
// get distances in 8 directions. used for sending distances to ground station
|
|
|
|
bool AP_Proximity::get_horizontal_distances(Proximity_Distance_Array &prx_dist_array) const
|
|
|
|
{
|
2022-07-07 00:35:43 -03:00
|
|
|
Proximity_Distance_Array prx_filt_dist_array; // unused
|
|
|
|
return boundary.get_layer_distances(PROXIMITY_MIDDLE_LAYER, distance_max(), prx_dist_array, prx_filt_dist_array);
|
2021-02-27 12:49:27 -04:00
|
|
|
}
|
|
|
|
|
2020-12-06 08:23:55 -04:00
|
|
|
// get total number of obstacles, used in GPS based Simple Avoidance
|
|
|
|
uint8_t AP_Proximity::get_obstacle_count() const
|
2022-07-07 00:35:43 -03:00
|
|
|
{
|
|
|
|
return boundary.get_obstacle_count();
|
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 AP_Proximity::get_obstacle(uint8_t obstacle_num, Vector3f& vec_to_obstacle) const
|
2016-11-15 03:29:35 -04:00
|
|
|
{
|
2022-07-07 00:35:43 -03:00
|
|
|
return boundary.get_obstacle(obstacle_num, vec_to_obstacle);
|
2016-11-15 03:29:35 -04: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"
|
2022-07-07 00:35:43 -03:00
|
|
|
// returns FLT_MAX if it's an invalid instance.
|
2021-05-26 16:02:53 -03:00
|
|
|
bool AP_Proximity::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
|
|
|
{
|
2022-07-07 00:35:43 -03:00
|
|
|
return boundary.closest_point_from_segment_to_obstacle(obstacle_num , seg_start, seg_end, closest_point);
|
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 AP_Proximity::get_closest_object(float& angle_deg, float &distance) const
|
|
|
|
{
|
2022-07-07 00:35:43 -03:00
|
|
|
return boundary.get_closest_object(angle_deg, distance);
|
2016-12-16 23:42:13 -04:00
|
|
|
}
|
|
|
|
|
2022-07-07 00:35:43 -03:00
|
|
|
// get number of objects, angle and distance - used for non-GPS avoidance
|
2016-12-16 23:42:13 -04:00
|
|
|
uint8_t AP_Proximity::get_object_count() const
|
|
|
|
{
|
2022-07-07 00:35:43 -03:00
|
|
|
return boundary.get_horizontal_object_count();
|
2016-12-16 23:42:13 -04:00
|
|
|
}
|
|
|
|
|
2022-07-07 00:35:43 -03:00
|
|
|
// get number of objects, angle and distance - used for non-GPS avoidance
|
2016-12-16 23:42:13 -04:00
|
|
|
bool AP_Proximity::get_object_angle_and_distance(uint8_t object_number, float& angle_deg, float &distance) const
|
|
|
|
{
|
2022-07-07 00:35:43 -03:00
|
|
|
return boundary.get_horizontal_object_angle_and_distance(object_number, angle_deg, distance);
|
2016-11-15 03:15:46 -04:00
|
|
|
}
|
2016-11-25 01:01:21 -04:00
|
|
|
|
2022-09-27 16:44:39 -03:00
|
|
|
// get obstacle pitch and angle for a particular obstacle num
|
|
|
|
bool AP_Proximity::get_obstacle_info(uint8_t obstacle_num, float &angle_deg, float &pitch, float &distance) const
|
|
|
|
{
|
|
|
|
return boundary.get_obstacle_info(obstacle_num, angle_deg, pitch, distance);
|
|
|
|
}
|
|
|
|
|
2022-08-16 22:56:18 -03:00
|
|
|
// handle mavlink messages
|
2022-07-05 04:58:38 -03:00
|
|
|
void AP_Proximity::handle_msg(const mavlink_message_t &msg)
|
2016-11-25 01:01:21 -04:00
|
|
|
{
|
2022-07-05 04:58:38 -03:00
|
|
|
for (uint8_t i=0; i<num_instances; i++) {
|
|
|
|
if (valid_instance(i)) {
|
|
|
|
drivers[i]->handle_msg(msg);
|
|
|
|
}
|
2016-11-25 01:01:21 -04:00
|
|
|
}
|
|
|
|
}
|
2022-07-05 04:58:38 -03:00
|
|
|
|
|
|
|
// methods for mavlink SYS_STATUS message (send_sys_status)
|
|
|
|
bool AP_Proximity::sensor_present() const
|
2016-11-25 01:01:21 -04:00
|
|
|
{
|
2022-07-05 04:58:38 -03:00
|
|
|
return get_status() != Status::NotConnected;
|
|
|
|
}
|
2022-08-22 14:39:33 -03:00
|
|
|
|
2022-07-05 04:58:38 -03:00
|
|
|
bool AP_Proximity::sensor_enabled() const
|
|
|
|
{
|
2022-08-22 14:39:33 -03:00
|
|
|
// check atleast one sensor is enabled
|
2022-08-23 18:42:42 -03:00
|
|
|
return (num_instances > 0);
|
2022-07-05 04:58:38 -03:00
|
|
|
}
|
2022-08-22 14:39:33 -03:00
|
|
|
|
2022-07-05 04:58:38 -03:00
|
|
|
bool AP_Proximity::sensor_failed() const
|
|
|
|
{
|
|
|
|
return get_status() != Status::Good;
|
2016-11-25 01:01:21 -04:00
|
|
|
}
|
2017-01-16 02:58:14 -04:00
|
|
|
|
|
|
|
// get distance in meters upwards, returns true on success
|
|
|
|
bool AP_Proximity::get_upward_distance(uint8_t instance, float &distance) const
|
|
|
|
{
|
2019-09-27 05:43:39 -03:00
|
|
|
if (!valid_instance(instance)) {
|
2017-01-16 02:58:14 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// get upward distance from backend
|
|
|
|
return drivers[instance]->get_upward_distance(distance);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AP_Proximity::get_upward_distance(float &distance) const
|
|
|
|
{
|
2022-08-22 14:53:43 -03:00
|
|
|
// get upward distance from backend
|
|
|
|
for (uint8_t i=0; i<num_instances; i++) {
|
|
|
|
// return first good upward distance
|
2022-08-23 18:42:42 -03:00
|
|
|
if (get_upward_distance(i, distance)) {
|
2022-08-22 14:53:43 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2017-01-16 02:58:14 -04:00
|
|
|
}
|
2018-05-09 04:45:43 -03:00
|
|
|
|
2022-03-03 21:12:18 -04:00
|
|
|
#if HAL_LOGGING_ENABLED
|
|
|
|
// Write proximity sensor distances
|
|
|
|
void AP_Proximity::log()
|
|
|
|
{
|
2022-07-07 08:18:29 -03:00
|
|
|
// exit immediately if no sensors
|
|
|
|
if (num_sensors() == 0) {
|
2022-03-03 21:12:18 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Proximity_Distance_Array dist_array{}; // raw distances stored here
|
|
|
|
Proximity_Distance_Array filt_dist_array{}; //filtered distances stored here
|
|
|
|
auto &logger { AP::logger() };
|
2022-07-07 00:35:43 -03:00
|
|
|
for (uint8_t i = 0; i < boundary.get_num_layers(); i++) {
|
|
|
|
const bool active = boundary.get_layer_distances(i, distance_max(), dist_array, filt_dist_array);
|
2022-03-03 21:12:18 -04:00
|
|
|
if (!active) {
|
|
|
|
// nothing on this layer
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
float dist_up;
|
|
|
|
if (!get_upward_distance(dist_up)) {
|
|
|
|
dist_up = 0.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
float closest_ang = 0.0f;
|
|
|
|
float closest_dist = 0.0f;
|
|
|
|
get_closest_object(closest_ang, closest_dist);
|
|
|
|
|
|
|
|
const struct log_Proximity pkt_proximity{
|
|
|
|
LOG_PACKET_HEADER_INIT(LOG_PROXIMITY_MSG),
|
|
|
|
time_us : AP_HAL::micros64(),
|
|
|
|
instance : i,
|
|
|
|
health : (uint8_t)get_status(),
|
|
|
|
dist0 : filt_dist_array.distance[0],
|
|
|
|
dist45 : filt_dist_array.distance[1],
|
|
|
|
dist90 : filt_dist_array.distance[2],
|
|
|
|
dist135 : filt_dist_array.distance[3],
|
|
|
|
dist180 : filt_dist_array.distance[4],
|
|
|
|
dist225 : filt_dist_array.distance[5],
|
|
|
|
dist270 : filt_dist_array.distance[6],
|
|
|
|
dist315 : filt_dist_array.distance[7],
|
|
|
|
distup : dist_up,
|
|
|
|
closest_angle : closest_ang,
|
|
|
|
closest_dist : closest_dist
|
|
|
|
};
|
|
|
|
logger.WriteBlock(&pkt_proximity, sizeof(pkt_proximity));
|
|
|
|
|
|
|
|
if (_raw_log_enable) {
|
|
|
|
const struct log_Proximity_raw pkt_proximity_raw{
|
|
|
|
LOG_PACKET_HEADER_INIT(LOG_RAW_PROXIMITY_MSG),
|
|
|
|
time_us : AP_HAL::micros64(),
|
|
|
|
instance : i,
|
|
|
|
raw_dist0 : dist_array.distance[0],
|
|
|
|
raw_dist45 : dist_array.distance[1],
|
|
|
|
raw_dist90 : dist_array.distance[2],
|
|
|
|
raw_dist135 : dist_array.distance[3],
|
|
|
|
raw_dist180 : dist_array.distance[4],
|
|
|
|
raw_dist225 : dist_array.distance[5],
|
|
|
|
raw_dist270 : dist_array.distance[6],
|
|
|
|
raw_dist315 : dist_array.distance[7],
|
|
|
|
};
|
|
|
|
logger.WriteBlock(&pkt_proximity_raw, sizeof(pkt_proximity_raw));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2021-02-19 14:55:28 -04:00
|
|
|
|
2022-07-05 04:58:38 -03:00
|
|
|
// return true if the given instance exists
|
|
|
|
bool AP_Proximity::valid_instance(uint8_t i) const
|
|
|
|
{
|
|
|
|
if (i >= PROXIMITY_MAX_INSTANCES) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (drivers[i] == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return (Type)params[i].type.get() != Type::None;
|
|
|
|
}
|
|
|
|
|
2018-05-09 04:45:43 -03:00
|
|
|
AP_Proximity *AP_Proximity::_singleton;
|
2019-05-22 03:02:11 -03:00
|
|
|
|
|
|
|
namespace AP {
|
|
|
|
|
|
|
|
AP_Proximity *proximity()
|
|
|
|
{
|
|
|
|
return AP_Proximity::get_singleton();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2021-03-25 04:32:09 -03:00
|
|
|
|
|
|
|
#endif // HAL_PROXIMITY_ENABLED
|