2016-11-14 00:48:38 -04: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/>.
|
|
|
|
*/
|
|
|
|
|
2021-03-25 04:32:09 -03:00
|
|
|
#include "AP_Proximity_SITL.h"
|
2016-11-14 00:48:38 -04:00
|
|
|
|
2021-03-25 04:32:09 -03:00
|
|
|
#if HAL_PROXIMITY_ENABLED
|
2016-11-14 00:48:38 -04:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2021-03-25 04:32:09 -03:00
|
|
|
|
2016-11-14 00:48:38 -04:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
|
|
|
|
#include <AP_Param/AP_Param.h>
|
2021-03-25 04:32:09 -03:00
|
|
|
|
2019-05-22 02:03:47 -03:00
|
|
|
#include <AC_Fence/AC_Fence.h>
|
2016-11-14 00:48:38 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2016-11-25 01:01:21 -04:00
|
|
|
#define PROXIMITY_MAX_RANGE 200.0f
|
|
|
|
#define PROXIMITY_ACCURACY 0.1f
|
2016-11-14 00:48:38 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
The constructor also initialises the proximity sensor.
|
|
|
|
*/
|
|
|
|
AP_Proximity_SITL::AP_Proximity_SITL(AP_Proximity &_frontend,
|
|
|
|
AP_Proximity::Proximity_State &_state):
|
2018-06-15 16:26:02 -03:00
|
|
|
AP_Proximity_Backend(_frontend, _state),
|
|
|
|
sitl(AP::sitl())
|
2016-11-14 00:48:38 -04:00
|
|
|
{
|
|
|
|
ap_var_type ptype;
|
2017-01-16 02:58:14 -04:00
|
|
|
fence_alt_max = (AP_Float *)AP_Param::find("FENCE_ALT_MAX", &ptype);
|
|
|
|
if (fence_alt_max == nullptr || ptype != AP_PARAM_FLOAT) {
|
|
|
|
AP_HAL::panic("Proximity_SITL: Failed to find FENCE_ALT_MAX");
|
|
|
|
}
|
2016-11-14 00:48:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// update the state of the sensor
|
|
|
|
void AP_Proximity_SITL::update(void)
|
|
|
|
{
|
|
|
|
current_loc.lat = sitl->state.latitude * 1.0e7;
|
|
|
|
current_loc.lng = sitl->state.longitude * 1.0e7;
|
|
|
|
current_loc.alt = sitl->state.altitude * 1.0e2;
|
2019-05-22 02:03:47 -03:00
|
|
|
|
|
|
|
if (!AP::fence()->polyfence().breached()) {
|
|
|
|
// only called to prompt polyfence to reload fence if required
|
|
|
|
}
|
2019-09-16 02:24:46 -03:00
|
|
|
if (AP::fence()->polyfence().inclusion_boundary_available()) {
|
2021-02-19 14:55:28 -04:00
|
|
|
set_status(AP_Proximity::Status::Good);
|
2020-12-14 03:59:53 -04:00
|
|
|
// update distance in each sector
|
|
|
|
for (uint8_t sector=0; sector < PROXIMITY_NUM_SECTORS; sector++) {
|
|
|
|
const float yaw_angle_deg = sector * 45.0f;
|
|
|
|
AP_Proximity_Boundary_3D::Face face = boundary.get_face(yaw_angle_deg);
|
|
|
|
float fence_distance;
|
|
|
|
if (get_distance_to_fence(yaw_angle_deg, fence_distance)) {
|
|
|
|
boundary.set_face_attributes(face, yaw_angle_deg, fence_distance);
|
|
|
|
// update OA database
|
|
|
|
database_push(yaw_angle_deg, fence_distance);
|
|
|
|
} else {
|
|
|
|
boundary.reset_face(face);
|
|
|
|
}
|
2016-11-15 03:12:45 -04:00
|
|
|
}
|
2016-11-14 00:48:38 -04:00
|
|
|
} else {
|
2019-09-27 05:58:52 -03:00
|
|
|
set_status(AP_Proximity::Status::NoData);
|
2016-11-14 00:48:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-15 03:12:45 -04:00
|
|
|
// get distance in meters to fence in a particular direction in degrees (0 is forward, angles increase in the clockwise direction)
|
|
|
|
bool AP_Proximity_SITL::get_distance_to_fence(float angle_deg, float &distance) const
|
|
|
|
{
|
2019-09-16 02:24:46 -03:00
|
|
|
if (!AP::fence()->polyfence().inclusion_boundary_available()) {
|
2016-11-15 03:12:45 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert to earth frame
|
|
|
|
angle_deg = wrap_360(sitl->state.yawDeg + angle_deg);
|
|
|
|
|
|
|
|
/*
|
|
|
|
simple bisection search to find distance. Not really efficient,
|
|
|
|
but we can afford the CPU in SITL
|
|
|
|
*/
|
|
|
|
float min_dist = 0, max_dist = PROXIMITY_MAX_RANGE;
|
|
|
|
while (max_dist - min_dist > PROXIMITY_ACCURACY) {
|
|
|
|
float test_dist = (max_dist+min_dist)*0.5f;
|
2019-05-22 02:03:47 -03:00
|
|
|
|
2016-11-15 03:12:45 -04:00
|
|
|
Location loc = current_loc;
|
2019-04-05 03:11:26 -03:00
|
|
|
loc.offset_bearing(angle_deg, test_dist);
|
2019-05-22 02:03:47 -03:00
|
|
|
if (AP::fence()->polyfence().breached(loc)) {
|
2016-11-15 03:12:45 -04:00
|
|
|
max_dist = test_dist;
|
|
|
|
} else {
|
|
|
|
min_dist = test_dist;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
distance = min_dist;
|
2021-12-29 07:16:34 -04:00
|
|
|
if (ignore_reading(angle_deg, distance, false)) {
|
2021-02-19 14:55:28 -04:00
|
|
|
// obstacle near land, lets ignore it
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-15 03:12:45 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-25 01:01:21 -04:00
|
|
|
// get maximum and minimum distances (in meters) of primary sensor
|
|
|
|
float AP_Proximity_SITL::distance_max() const
|
|
|
|
{
|
|
|
|
return PROXIMITY_MAX_RANGE;
|
|
|
|
}
|
|
|
|
float AP_Proximity_SITL::distance_min() const
|
|
|
|
{
|
|
|
|
return 0.0f;
|
|
|
|
}
|
|
|
|
|
2017-01-16 02:58:14 -04:00
|
|
|
// get distance upwards in meters. returns true on success
|
|
|
|
bool AP_Proximity_SITL::get_upward_distance(float &distance) const
|
|
|
|
{
|
|
|
|
// return distance to fence altitude
|
|
|
|
distance = MAX(0.0f, fence_alt_max->get() - sitl->height_agl);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-14 00:48:38 -04:00
|
|
|
#endif // CONFIG_HAL_BOARD
|
2021-03-25 04:32:09 -03:00
|
|
|
|
|
|
|
#endif // HAL_PROXIMITY_ENABLED
|