2021-03-25 04:32:09 -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/>.
|
|
|
|
*/
|
|
|
|
|
2020-12-06 08:20:02 -04:00
|
|
|
#include "AP_Proximity_Boundary_3D.h"
|
|
|
|
|
2022-07-07 00:35:43 -03:00
|
|
|
#define PROXIMITY_BOUNDARY_3D_TIMEOUT_MS 750 // we should check the 3D boundary faces after this many ms
|
2021-03-25 04:32:09 -03:00
|
|
|
|
2020-12-06 08:20:02 -04:00
|
|
|
/*
|
|
|
|
Constructor.
|
|
|
|
This incorporates initialisation as well.
|
|
|
|
*/
|
|
|
|
AP_Proximity_Boundary_3D::AP_Proximity_Boundary_3D()
|
|
|
|
{
|
|
|
|
// initialise sector edge vector used for building the boundary fence
|
2020-12-14 02:51:18 -04:00
|
|
|
init();
|
2020-12-06 08:20:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// initialise the boundary and sector_edge_vector array used for object avoidance
|
|
|
|
// should be called if the sector_middle_deg or _sector_width_deg arrays are changed
|
2020-12-14 02:51:18 -04:00
|
|
|
void AP_Proximity_Boundary_3D::init()
|
2020-12-06 08:20:02 -04:00
|
|
|
{
|
2020-12-14 02:51:18 -04:00
|
|
|
for (uint8_t layer=0; layer < PROXIMITY_NUM_LAYERS; layer++) {
|
2021-01-10 14:14:24 -04:00
|
|
|
const float pitch = ((float)_pitch_middle_deg[layer]);
|
2020-12-06 08:20:02 -04:00
|
|
|
for (uint8_t sector=0; sector < PROXIMITY_NUM_SECTORS; sector++) {
|
2021-01-10 14:14:24 -04:00
|
|
|
const float angle_rad = ((float)_sector_middle_deg[sector]+(PROXIMITY_SECTOR_WIDTH_DEG/2.0f));
|
2020-12-14 02:51:18 -04:00
|
|
|
_sector_edge_vector[layer][sector].offset_bearing(angle_rad, pitch, 100.0f);
|
|
|
|
_boundary_points[layer][sector] = _sector_edge_vector[layer][sector] * PROXIMITY_BOUNDARY_DIST_DEFAULT;
|
2020-12-06 08:20:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-14 02:51:18 -04:00
|
|
|
// returns face corresponding to the provided yaw and (optionally) pitch
|
2021-01-10 14:14:24 -04:00
|
|
|
// pitch is the vertical body-frame angle (in degrees) to the obstacle (0=directly ahead, 90 is above the vehicle)
|
2020-12-14 02:51:18 -04:00
|
|
|
// yaw is the horizontal body-frame angle (in degrees) to the obstacle (0=directly ahead of the vehicle, 90 is to the right of the vehicle)
|
|
|
|
AP_Proximity_Boundary_3D::Face AP_Proximity_Boundary_3D::get_face(float pitch, float yaw) const
|
2022-07-07 05:25:16 -03:00
|
|
|
{
|
2020-12-06 08:20:02 -04:00
|
|
|
const uint8_t sector = wrap_360(yaw + (PROXIMITY_SECTOR_WIDTH_DEG * 0.5f)) / 45.0f;
|
2020-12-14 02:51:18 -04:00
|
|
|
const float pitch_limited = constrain_float(pitch, -75.0f, 74.9f);
|
|
|
|
const uint8_t layer = (pitch_limited + 75.0f)/PROXIMITY_PITCH_WIDTH_DEG;
|
2021-01-10 14:14:24 -04:00
|
|
|
return Face{layer, sector};
|
2020-12-06 08:20:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the actual body-frame angle(yaw), pitch, and distance of the detected object.
|
2022-07-07 04:55:05 -03:00
|
|
|
// This method will also mark the sector and layer to be "valid",
|
|
|
|
// This distance can then be used for Obstacle Avoidance
|
|
|
|
// Assume detected obstacle is horizontal (zero pitch), if no pitch is passed
|
|
|
|
// prx_instance should be set to the proximity sensor backend instance number
|
|
|
|
void AP_Proximity_Boundary_3D::set_face_attributes(const Face &face, float pitch, float angle, float distance, uint8_t prx_instance)
|
2020-12-14 02:51:18 -04:00
|
|
|
{
|
|
|
|
if (!face.valid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-07 04:55:05 -03:00
|
|
|
// ignore update if another instance has provided a shorter distance within the last 0.2 seconds
|
|
|
|
if ((prx_instance != _prx_instance[face.layer][face.sector]) && _distance_valid[face.layer][face.sector] && (_filtered_distance[face.layer][face.sector].get() < distance)) {
|
|
|
|
// check if recent
|
|
|
|
const uint32_t now_ms = AP_HAL::millis();
|
|
|
|
if (now_ms - _last_update_ms[face.layer][face.sector] < PROXIMITY_FACE_RESET_MS) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-14 02:51:18 -04:00
|
|
|
_angle[face.layer][face.sector] = angle;
|
|
|
|
_pitch[face.layer][face.sector] = pitch;
|
|
|
|
_distance[face.layer][face.sector] = distance;
|
|
|
|
_distance_valid[face.layer][face.sector] = true;
|
2022-07-07 04:55:05 -03:00
|
|
|
_prx_instance[face.layer][face.sector] = prx_instance;
|
2020-12-14 02:51:18 -04:00
|
|
|
|
2021-02-27 12:49:27 -04:00
|
|
|
// apply filter
|
|
|
|
set_filtered_distance(face, distance);
|
|
|
|
|
2020-12-14 02:51:18 -04:00
|
|
|
// update boundary used for simple avoidance
|
|
|
|
update_boundary(face);
|
|
|
|
}
|
|
|
|
|
2021-03-15 15:01:53 -03:00
|
|
|
// apply a new cutoff_freq to low-pass filter
|
|
|
|
void AP_Proximity_Boundary_3D::apply_filter_freq(float cutoff_freq)
|
|
|
|
{
|
|
|
|
for (uint8_t layer=0; layer < PROXIMITY_NUM_LAYERS; layer++) {
|
|
|
|
for (uint8_t sector=0; sector < PROXIMITY_NUM_SECTORS; sector++) {
|
|
|
|
_filtered_distance[layer][sector].set_cutoff_frequency(cutoff_freq);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-27 12:49:27 -04:00
|
|
|
// Apply low pass filter on the raw distance
|
|
|
|
void AP_Proximity_Boundary_3D::set_filtered_distance(const Face &face, float distance)
|
|
|
|
{
|
|
|
|
if (!face.valid()) {
|
|
|
|
return;
|
2020-12-14 02:51:18 -04:00
|
|
|
}
|
2021-03-15 15:01:53 -03:00
|
|
|
if (!is_equal(_filtered_distance[face.layer][face.sector].get_cutoff_freq(), _filter_freq)) {
|
|
|
|
// cutoff freq has changed
|
|
|
|
apply_filter_freq(_filter_freq);
|
|
|
|
}
|
2021-02-27 12:49:27 -04:00
|
|
|
|
|
|
|
const uint32_t now_ms = AP_HAL::millis();
|
|
|
|
const uint32_t dt = now_ms - _last_update_ms[face.layer][face.sector];
|
|
|
|
if (dt < PROXIMITY_FILT_RESET_TIME) {
|
|
|
|
_filtered_distance[face.layer][face.sector].apply(distance, dt* 0.001f);
|
|
|
|
} else {
|
|
|
|
// reset filter since last distance was passed a long time back
|
|
|
|
_filtered_distance[face.layer][face.sector].reset(distance);
|
|
|
|
}
|
|
|
|
_last_update_ms[face.layer][face.sector] = now_ms;
|
2020-12-06 08:20:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// update boundary points used for object avoidance based on a single sector and pitch distance changing
|
|
|
|
// the boundary points lie on the line between sectors meaning two boundary points may be updated based on a single sector's distance changing
|
|
|
|
// the boundary point is set to the shortest distance found in the two adjacent sectors, this is a conservative boundary around the vehicle
|
2021-01-10 14:14:24 -04:00
|
|
|
void AP_Proximity_Boundary_3D::update_boundary(const Face &face)
|
2020-12-06 08:20:02 -04:00
|
|
|
{
|
|
|
|
// sanity check
|
2020-12-14 02:51:18 -04:00
|
|
|
if (!face.valid()) {
|
2020-12-06 08:20:02 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-14 02:51:18 -04:00
|
|
|
const uint8_t layer = face.layer;
|
|
|
|
const uint8_t sector = face.sector;
|
|
|
|
|
2020-12-06 08:20:02 -04:00
|
|
|
// find adjacent sector (clockwise)
|
2020-12-27 12:21:58 -04:00
|
|
|
const uint8_t next_sector = get_next_sector(sector);
|
2020-12-06 08:20:02 -04:00
|
|
|
|
|
|
|
// boundary point lies on the line between the two sectors at the shorter distance found in the two sectors
|
|
|
|
float shortest_distance = PROXIMITY_BOUNDARY_DIST_DEFAULT;
|
2020-12-14 02:51:18 -04:00
|
|
|
if (_distance_valid[layer][sector] && _distance_valid[layer][next_sector]) {
|
2021-02-27 12:49:27 -04:00
|
|
|
shortest_distance = MIN(_filtered_distance[layer][sector].get(), _filtered_distance[layer][next_sector].get());
|
2020-12-14 02:51:18 -04:00
|
|
|
} else if (_distance_valid[layer][sector]) {
|
2021-02-27 12:49:27 -04:00
|
|
|
shortest_distance = _filtered_distance[layer][sector].get();
|
2020-12-14 02:51:18 -04:00
|
|
|
} else if (_distance_valid[layer][next_sector]) {
|
2021-02-27 12:49:27 -04:00
|
|
|
shortest_distance = _filtered_distance[layer][next_sector].get();
|
2020-12-06 08:20:02 -04:00
|
|
|
}
|
|
|
|
if (shortest_distance < PROXIMITY_BOUNDARY_DIST_MIN) {
|
|
|
|
shortest_distance = PROXIMITY_BOUNDARY_DIST_MIN;
|
|
|
|
}
|
2020-12-14 02:51:18 -04:00
|
|
|
_boundary_points[layer][sector] = _sector_edge_vector[layer][sector] * shortest_distance;
|
2020-12-06 08:20:02 -04:00
|
|
|
|
|
|
|
// if the next sector (clockwise) has an invalid distance, set boundary to create a cup like boundary
|
2020-12-14 02:51:18 -04:00
|
|
|
if (!_distance_valid[layer][next_sector]) {
|
|
|
|
_boundary_points[layer][next_sector] = _sector_edge_vector[layer][next_sector] * shortest_distance;
|
2020-12-06 08:20:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// repeat for edge between sector and previous sector
|
2020-12-27 12:21:58 -04:00
|
|
|
const uint8_t prev_sector = get_prev_sector(sector);
|
2020-12-06 08:20:02 -04:00
|
|
|
shortest_distance = PROXIMITY_BOUNDARY_DIST_DEFAULT;
|
2020-12-14 02:51:18 -04:00
|
|
|
if (_distance_valid[layer][prev_sector] && _distance_valid[layer][sector]) {
|
2021-02-27 12:49:27 -04:00
|
|
|
shortest_distance = MIN(_filtered_distance[layer][prev_sector].get(), _filtered_distance[layer][sector].get());
|
2020-12-14 02:51:18 -04:00
|
|
|
} else if (_distance_valid[layer][prev_sector]) {
|
2021-02-27 12:49:27 -04:00
|
|
|
shortest_distance = _filtered_distance[layer][prev_sector].get();
|
2020-12-14 02:51:18 -04:00
|
|
|
} else if (_distance_valid[layer][sector]) {
|
2021-02-27 12:49:27 -04:00
|
|
|
shortest_distance = _filtered_distance[layer][sector].get();
|
2020-12-06 08:20:02 -04:00
|
|
|
}
|
2020-12-14 02:51:18 -04:00
|
|
|
_boundary_points[layer][prev_sector] = _sector_edge_vector[layer][prev_sector] * shortest_distance;
|
2020-12-06 08:20:02 -04:00
|
|
|
|
2021-01-10 14:14:24 -04:00
|
|
|
// if the sector counter-clockwise from the previous sector has an invalid distance, set boundary to create a cup-like boundary
|
2020-12-27 12:21:58 -04:00
|
|
|
const uint8_t prev_sector_ccw = get_prev_sector(prev_sector);
|
2020-12-14 02:51:18 -04:00
|
|
|
if (!_distance_valid[layer][prev_sector_ccw]) {
|
|
|
|
_boundary_points[layer][prev_sector_ccw] = _sector_edge_vector[layer][prev_sector_ccw] * shortest_distance;
|
2020-12-06 08:20:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-14 02:51:18 -04:00
|
|
|
// reset boundary. marks all distances as invalid
|
|
|
|
void AP_Proximity_Boundary_3D::reset()
|
2020-12-06 08:20:02 -04:00
|
|
|
{
|
2020-12-14 02:51:18 -04:00
|
|
|
for (uint8_t layer=0; layer < PROXIMITY_NUM_LAYERS; layer++) {
|
|
|
|
for (uint8_t sector=0; sector < PROXIMITY_NUM_SECTORS; sector++) {
|
|
|
|
_distance_valid[layer][sector] = false;
|
|
|
|
}
|
2020-12-06 08:20:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-14 02:51:18 -04:00
|
|
|
// Reset this location, specified by Face object, back to default
|
|
|
|
// i.e Distance is marked as not-valid, and set to a large number.
|
2022-07-07 04:55:05 -03:00
|
|
|
// prx_instance should be set to the proximity sensor's backend instance number
|
|
|
|
void AP_Proximity_Boundary_3D::reset_face(const Face &face, uint8_t prx_instance)
|
2020-12-06 08:20:02 -04:00
|
|
|
{
|
2020-12-14 02:51:18 -04:00
|
|
|
if (!face.valid()) {
|
|
|
|
return;
|
2020-12-06 08:20:02 -04:00
|
|
|
}
|
2022-07-07 04:55:05 -03:00
|
|
|
|
|
|
|
// return immediately if face already has no valid distance
|
|
|
|
if (!_distance_valid[face.layer][face.sector]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ignore reset if another instance provided this face's distance within the last 0.2 seconds
|
|
|
|
if (prx_instance != _prx_instance[face.layer][face.sector]) {
|
|
|
|
const uint32_t now_ms = AP_HAL::millis();
|
|
|
|
if (now_ms - _last_update_ms[face.layer][face.sector] < 200) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-14 02:51:18 -04:00
|
|
|
_distance_valid[face.layer][face.sector] = false;
|
|
|
|
|
|
|
|
// update simple avoidance boundary
|
|
|
|
update_boundary(face);
|
|
|
|
}
|
|
|
|
|
2021-03-15 15:01:53 -03:00
|
|
|
// check if a face has valid distance even if it was updated a long time back
|
|
|
|
void AP_Proximity_Boundary_3D::check_face_timeout()
|
|
|
|
{
|
2022-07-07 00:35:43 -03:00
|
|
|
// exit immediately if already checked recently
|
|
|
|
const uint32_t now_ms = AP_HAL::millis();
|
|
|
|
if ((now_ms - _last_check_face_timeout_ms) < PROXIMITY_BOUNDARY_3D_TIMEOUT_MS) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_last_check_face_timeout_ms = now_ms;
|
|
|
|
|
2021-03-15 15:01:53 -03:00
|
|
|
for (uint8_t layer=0; layer < PROXIMITY_NUM_LAYERS; layer++) {
|
|
|
|
for (uint8_t sector=0; sector < PROXIMITY_NUM_SECTORS; sector++) {
|
|
|
|
if (_distance_valid[layer][sector]) {
|
2022-07-07 04:55:05 -03:00
|
|
|
if ((now_ms - _last_update_ms[layer][sector]) > PROXIMITY_FACE_RESET_MS) {
|
2021-03-15 15:01:53 -03:00
|
|
|
// this face has a valid distance but wasn't updated for a long time, reset it
|
2022-07-07 04:55:05 -03:00
|
|
|
_distance_valid[layer][sector] = false;
|
|
|
|
update_boundary(AP_Proximity_Boundary_3D::Face{layer, sector});
|
2021-03-15 15:01:53 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-14 02:51:18 -04:00
|
|
|
// get distance for a face. returns true on success and fills in distance argument with distance in meters
|
2021-01-10 14:14:24 -04:00
|
|
|
bool AP_Proximity_Boundary_3D::get_distance(const Face &face, float &distance) const
|
2020-12-14 02:51:18 -04:00
|
|
|
{
|
|
|
|
if (!face.valid()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (_distance_valid[face.layer][face.sector]) {
|
|
|
|
distance = _distance[face.layer][face.sector];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2020-12-06 08:20:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// get the total number of obstacles
|
2020-12-27 12:21:58 -04:00
|
|
|
uint8_t AP_Proximity_Boundary_3D::get_obstacle_count() const
|
2020-12-06 08:20:02 -04:00
|
|
|
{
|
2020-12-27 12:21:58 -04:00
|
|
|
return PROXIMITY_NUM_LAYERS * PROXIMITY_NUM_SECTORS;
|
2020-12-06 08:20:02 -04:00
|
|
|
}
|
|
|
|
|
2020-12-27 12:21:58 -04:00
|
|
|
// Converts obstacle_num passed from avoidance library into appropriate face of the boundary
|
|
|
|
// Returns false if the face is invalid
|
|
|
|
// "update_boundary" method manipulates two sectors ccw and one sector cw from any valid face.
|
|
|
|
// Any boundary that does not fall into these manipulated faces are useless, and will be marked as false
|
|
|
|
// The resultant is packed into a Boundary Location object and returned by reference as "face"
|
|
|
|
bool AP_Proximity_Boundary_3D::convert_obstacle_num_to_face(uint8_t obstacle_num, Face& face) const
|
2020-12-06 08:20:02 -04:00
|
|
|
{
|
2020-12-27 12:21:58 -04:00
|
|
|
// obstacle num is just "flattened layers, and sectors"
|
|
|
|
const uint8_t layer = obstacle_num / PROXIMITY_NUM_SECTORS;
|
2020-12-06 08:20:02 -04:00
|
|
|
const uint8_t sector = obstacle_num % PROXIMITY_NUM_SECTORS;
|
2020-12-27 12:21:58 -04:00
|
|
|
face.sector = sector;
|
|
|
|
face.layer = layer;
|
|
|
|
|
2021-02-11 03:37:51 -04:00
|
|
|
uint8_t valid_sector = sector;
|
2021-01-10 14:14:24 -04:00
|
|
|
// check for 3 adjacent sectors
|
2021-02-11 03:37:51 -04:00
|
|
|
for (uint8_t i=0; i < 3; i++) {
|
2021-01-10 14:14:24 -04:00
|
|
|
if (_distance_valid[layer][valid_sector]) {
|
2021-02-11 03:37:51 -04:00
|
|
|
// update boundary has manipulated this face
|
2021-01-10 14:14:24 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
valid_sector = get_next_sector(valid_sector);
|
2020-12-27 12:21:58 -04:00
|
|
|
}
|
2020-12-06 08:20:02 -04:00
|
|
|
|
2020-12-27 12:21:58 -04:00
|
|
|
// this face was not manipulated by "update_boundary" and is stale. Don't use it
|
|
|
|
return false;
|
2020-12-06 08:20:02 -04:00
|
|
|
}
|
|
|
|
|
2020-12-14 02:51:18 -04:00
|
|
|
// Appropriate layer and sector are found from the passed obstacle_num
|
|
|
|
// This function then draws a line between this sector, and sector + 1 at the given layer
|
2020-12-06 08:20:02 -04:00
|
|
|
// Then returns the closest point on this line from vehicle, in body-frame.
|
|
|
|
// Used by GPS based Simple Avoidance
|
2021-01-10 14:14:24 -04:00
|
|
|
// False is returned if the obstacle_num provided does not produce a valid obstacle
|
2020-12-27 12:21:58 -04:00
|
|
|
bool AP_Proximity_Boundary_3D::get_obstacle(uint8_t obstacle_num, Vector3f& vec_to_obstacle) const
|
2020-12-14 02:51:18 -04:00
|
|
|
{
|
2020-12-27 12:21:58 -04:00
|
|
|
Face face;
|
|
|
|
if (!convert_obstacle_num_to_face(obstacle_num, face)) {
|
|
|
|
// not a valid face
|
|
|
|
return false;
|
2020-12-06 08:20:02 -04:00
|
|
|
}
|
2020-12-27 12:21:58 -04:00
|
|
|
const uint8_t sector_end = face.sector;
|
|
|
|
const uint8_t sector_start = get_next_sector(face.sector);
|
|
|
|
|
2020-12-14 02:51:18 -04:00
|
|
|
const Vector3f start = _boundary_points[face.layer][sector_start];
|
|
|
|
const Vector3f end = _boundary_points[face.layer][sector_end];
|
2021-01-10 14:14:24 -04:00
|
|
|
vec_to_obstacle = Vector3f::point_on_line_closest_to_other_point(start, end, Vector3f{});
|
2020-12-27 12:21:58 -04:00
|
|
|
return true;
|
2020-12-06 08:20:02 -04:00
|
|
|
}
|
|
|
|
|
2020-12-14 02:51:18 -04:00
|
|
|
// Appropriate layer and sector are found from the passed obstacle_num
|
|
|
|
// This function then draws a line between this sector, and sector + 1 at the given layer
|
2020-12-06 08:20:02 -04:00
|
|
|
// Then returns the closest point on this line from the segment that was passed, in body-frame.
|
2021-05-26 16:02:53 -03:00
|
|
|
// Addionally a 3-D plane is constructed using the closest point found above as normal, and a point on the line segment in the boundary.
|
|
|
|
// True is returned when the passed line segment intersects this plane.
|
|
|
|
// This helps us know if the passed line segment was in the direction of the boundary, or going in a different direction.
|
|
|
|
// Used by GPS based Simple Avoidance - for "brake mode"
|
|
|
|
// False is returned if the obstacle_num provided does not produce a valid obstacle
|
|
|
|
bool AP_Proximity_Boundary_3D::closest_point_from_segment_to_obstacle(uint8_t obstacle_num, const Vector3f& seg_start, const Vector3f& seg_end, Vector3f& closest_point) const
|
|
|
|
{
|
|
|
|
Face face;
|
2020-12-27 12:21:58 -04:00
|
|
|
if (!convert_obstacle_num_to_face(obstacle_num, face)) {
|
|
|
|
// not a valid a face
|
2021-05-26 16:02:53 -03:00
|
|
|
return false;
|
2020-12-06 08:20:02 -04:00
|
|
|
}
|
2020-12-27 12:21:58 -04:00
|
|
|
|
|
|
|
const uint8_t sector_end = face.sector;
|
|
|
|
const uint8_t sector_start = get_next_sector(face.sector);
|
2020-12-14 02:51:18 -04:00
|
|
|
const Vector3f start = _boundary_points[face.layer][sector_start];
|
|
|
|
const Vector3f end = _boundary_points[face.layer][sector_end];
|
2021-05-26 16:02:53 -03:00
|
|
|
|
|
|
|
// closest point between passed line segment and boundary
|
|
|
|
Vector3f::segment_to_segment_closest_point(seg_start, seg_end, start, end, closest_point);
|
|
|
|
if (closest_point == start) {
|
|
|
|
// draw a plane using the closest point as normal vector, and a point on the boundary
|
|
|
|
// return false if the passed segment does not intersect the plane
|
|
|
|
return Vector3f::segment_plane_intersect(seg_start, seg_end, closest_point, end);
|
|
|
|
}
|
|
|
|
return Vector3f::segment_plane_intersect(seg_start, seg_end, closest_point, start);
|
2020-12-06 08:20:02 -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_Boundary_3D::get_closest_object(float& angle_deg, float &distance) const
|
|
|
|
{
|
2020-12-14 02:51:18 -04:00
|
|
|
bool closest_found = false;
|
|
|
|
uint8_t closest_sector = 0;
|
|
|
|
uint8_t closest_layer = 0;
|
2020-12-06 08:20:02 -04:00
|
|
|
|
|
|
|
// check boundary for shortest distance
|
|
|
|
// only check for middle layers and higher
|
|
|
|
// lower layers might contain ground, which will give false pre-arm failure
|
2020-12-14 02:51:18 -04:00
|
|
|
for (uint8_t layer=PROXIMITY_MIDDLE_LAYER; layer<PROXIMITY_NUM_LAYERS; layer++) {
|
|
|
|
for (uint8_t sector=0; sector<PROXIMITY_NUM_SECTORS; sector++) {
|
|
|
|
if (_distance_valid[layer][sector]) {
|
|
|
|
if (!closest_found || (_distance[layer][sector] < _distance[closest_layer][closest_sector])) {
|
|
|
|
closest_layer = layer;
|
|
|
|
closest_sector = sector;
|
|
|
|
closest_found = true;
|
2020-12-06 08:20:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-14 02:51:18 -04:00
|
|
|
if (closest_found) {
|
|
|
|
angle_deg = _angle[closest_layer][closest_sector];
|
|
|
|
distance = _distance[closest_layer][closest_sector];
|
2020-12-06 08:20:02 -04:00
|
|
|
}
|
2020-12-14 02:51:18 -04:00
|
|
|
return closest_found;
|
2020-12-06 08:20:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// get number of objects, used for non-GPS avoidance
|
|
|
|
uint8_t AP_Proximity_Boundary_3D::get_horizontal_object_count() const
|
|
|
|
{
|
|
|
|
return PROXIMITY_NUM_SECTORS;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get an object's angle and distance, used for non-GPS avoidance
|
|
|
|
// returns false if no angle or distance could be returned for some reason
|
2021-01-10 14:14:24 -04:00
|
|
|
bool AP_Proximity_Boundary_3D::get_horizontal_object_angle_and_distance(uint8_t object_number, float &angle_deg, float &distance) const
|
2020-12-06 08:20:02 -04:00
|
|
|
{
|
2020-12-14 02:51:18 -04:00
|
|
|
if ((object_number < PROXIMITY_NUM_SECTORS) && _distance_valid[PROXIMITY_MIDDLE_LAYER][object_number]) {
|
|
|
|
angle_deg = _angle[PROXIMITY_MIDDLE_LAYER][object_number];
|
2021-02-27 12:49:27 -04:00
|
|
|
distance = _filtered_distance[PROXIMITY_MIDDLE_LAYER][object_number].get();
|
2020-12-06 08:20:02 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2021-02-27 12:49:27 -04:00
|
|
|
|
2022-09-27 16:44:39 -03:00
|
|
|
// get an obstacle info for AP_Periph
|
|
|
|
// returns false if no angle or distance could be returned for some reason
|
|
|
|
bool AP_Proximity_Boundary_3D::get_obstacle_info(uint8_t obstacle_num, float &angle_deg, float &pitch_deg, float &distance) const
|
|
|
|
{
|
|
|
|
// obstacle num is just "flattened layers, and sectors"
|
|
|
|
const uint8_t layer = obstacle_num / PROXIMITY_NUM_SECTORS;
|
|
|
|
const uint8_t sector = obstacle_num % PROXIMITY_NUM_SECTORS;
|
|
|
|
if (_distance_valid[layer][sector]) {
|
|
|
|
angle_deg = _angle[layer][sector];
|
|
|
|
pitch_deg = _pitch[layer][sector];
|
|
|
|
distance = _filtered_distance[layer][sector].get();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-02-27 12:49:27 -04:00
|
|
|
// Return filtered distance for the passed in face
|
|
|
|
bool AP_Proximity_Boundary_3D::get_filtered_distance(const Face &face, float &distance) const
|
|
|
|
{
|
|
|
|
if (!face.valid()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_distance_valid[face.layer][face.sector]) {
|
|
|
|
// invalid distace
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
distance = _filtered_distance[face.layer][face.sector].get();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get raw and filtered distances in 8 directions per layer
|
2022-07-07 00:35:43 -03:00
|
|
|
bool AP_Proximity_Boundary_3D::get_layer_distances(uint8_t layer_number, float dist_max, Proximity_Distance_Array &prx_dist_array, Proximity_Distance_Array &prx_filt_dist_array) const
|
2021-02-27 12:49:27 -04:00
|
|
|
{
|
|
|
|
// cycle through all sectors filling in distances and orientations
|
|
|
|
// see MAV_SENSOR_ORIENTATION for orientations (0 = forward, 1 = 45 degree clockwise from north, etc)
|
|
|
|
bool valid_distances = false;
|
|
|
|
prx_dist_array.offset_valid = 0;
|
|
|
|
prx_filt_dist_array.offset_valid = 0;
|
|
|
|
for (uint8_t i=0; i<PROXIMITY_MAX_DIRECTION; i++) {
|
|
|
|
prx_dist_array.orientation[i] = i;
|
|
|
|
const AP_Proximity_Boundary_3D::Face face(layer_number, i);
|
|
|
|
if (!face.valid()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (get_distance(face, prx_dist_array.distance[i]) && get_filtered_distance(face, prx_filt_dist_array.distance[i])) {
|
|
|
|
valid_distances = true;
|
|
|
|
prx_dist_array.offset_valid |= (1U << i);
|
|
|
|
prx_filt_dist_array.offset_valid |= (1U << i);
|
|
|
|
} else {
|
|
|
|
prx_dist_array.distance[i] = dist_max;
|
|
|
|
prx_filt_dist_array.distance[i] = dist_max;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return valid_distances;
|
|
|
|
}
|
2021-03-25 04:32:09 -03:00
|
|
|
|
2021-03-08 13:21:45 -04:00
|
|
|
// reset the temporary boundary. This fills in distances with FLT_MAX
|
|
|
|
void AP_Proximity_Temp_Boundary::reset()
|
|
|
|
{
|
|
|
|
for (uint8_t layer=0; layer < PROXIMITY_NUM_LAYERS; layer++) {
|
|
|
|
for (uint8_t sector=0; sector < PROXIMITY_NUM_SECTORS; sector++) {
|
|
|
|
_distances[layer][sector] = FLT_MAX;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// add a distance to the temp boundary if it is shorter than any other provided distance since the last time the boundary was reset
|
|
|
|
// pitch and yaw are in degrees, distance is in meters
|
|
|
|
void AP_Proximity_Temp_Boundary::add_distance(const AP_Proximity_Boundary_3D::Face &face, float pitch, float yaw, float distance)
|
|
|
|
{
|
|
|
|
if (face.valid() && distance < _distances[face.layer][face.sector]) {
|
|
|
|
_distances[face.layer][face.sector] = distance;
|
|
|
|
_angle[face.layer][face.sector] = yaw;
|
|
|
|
_pitch[face.layer][face.sector] = pitch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// fill the original 3D boundary with the contents of this temporary boundary
|
2022-07-07 04:55:05 -03:00
|
|
|
// prx_instance should be set to the proximity sensor's backend instance number
|
|
|
|
void AP_Proximity_Temp_Boundary::update_3D_boundary(uint8_t prx_instance, AP_Proximity_Boundary_3D &boundary)
|
2021-03-08 13:21:45 -04:00
|
|
|
{
|
|
|
|
for (uint8_t layer=0; layer < PROXIMITY_NUM_LAYERS; layer++) {
|
|
|
|
for (uint8_t sector=0; sector < PROXIMITY_NUM_SECTORS; sector++) {
|
|
|
|
if (_distances[layer][sector] < FLT_MAX) {
|
|
|
|
AP_Proximity_Boundary_3D::Face face{layer, sector};
|
2022-07-07 04:55:05 -03:00
|
|
|
boundary.set_face_attributes(face, _pitch[layer][sector], _angle[layer][sector], _distances[layer][sector], prx_instance);
|
2021-03-08 13:21:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|