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
# include <AP_Common/AP_Common.h>
# include <AP_HAL/AP_HAL.h>
# include "AP_Proximity.h"
2019-06-29 02:57:09 -03:00
# include <AP_Common/Location.h>
2016-08-15 03:17:15 -03:00
2019-12-03 22:46:03 -04:00
# define PROXIMITY_NUM_SECTORS 8 // number of sectors
# define PROXIMITY_SECTOR_WIDTH_DEG 45.0f // width of sectors in degrees
2016-11-25 22:22:39 -04:00
# define PROXIMITY_BOUNDARY_DIST_MIN 0.6f // minimum distance for a boundary point. This ensures the object avoidance code doesn't think we are outside the boundary.
2017-01-10 01:02:43 -04:00
# define PROXIMITY_BOUNDARY_DIST_DEFAULT 100 // if we have no data for a sector, boundary is placed 100m out
2016-11-15 03:11:14 -04:00
2016-08-15 03:17:15 -03:00
class AP_Proximity_Backend
{
public :
// constructor. This incorporates initialisation as well.
AP_Proximity_Backend ( AP_Proximity & _frontend , AP_Proximity : : Proximity_State & _state ) ;
// we declare a virtual destructor so that Proximity drivers can
// override with a custom destructor if need be
virtual ~ AP_Proximity_Backend ( void ) { }
// update the state structure
virtual void update ( ) = 0 ;
2016-11-25 01:01:21 -04:00
// get maximum and minimum distances (in meters) of sensor
virtual float distance_max ( ) const = 0 ;
virtual float distance_min ( ) const = 0 ;
2017-01-16 02:58:14 -04:00
// get distance upwards in meters. returns true on success
virtual bool get_upward_distance ( float & distance ) const { return false ; }
2017-01-09 23:09:18 -04:00
// handle mavlink DISTANCE_SENSOR messages
2019-04-30 07:22:49 -03:00
virtual void handle_msg ( const mavlink_message_t & msg ) { }
2017-01-09 23:09:18 -04:00
2016-11-15 03:29:35 -04:00
// get boundary points around vehicle for use by avoidance
// returns nullptr and sets num_points to zero if no boundary can be returned
const Vector2f * get_boundary_points ( uint16_t & num_points ) const ;
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 ;
2016-11-25 01:01:21 -04:00
2016-12-16 23:42:13 -04:00
// get number of objects, angle and distance - used for non-GPS avoidance
uint8_t get_object_count ( ) const ;
bool get_object_angle_and_distance ( uint8_t object_number , float & angle_deg , float & distance ) const ;
2016-11-25 01:01:21 -04:00
// get distances in 8 directions. used for sending distances to ground station
2017-04-12 06:00:41 -03:00
bool get_horizontal_distances ( AP_Proximity : : Proximity_Distance_Array & prx_dist_array ) const ;
2016-08-15 03:17:15 -03:00
protected :
// set status and update valid_count
2019-09-27 05:58:52 -03:00
void set_status ( AP_Proximity : : Status status ) ;
2016-08-15 03:17:15 -03:00
2016-11-15 03:11:14 -04:00
// find which sector a given angle falls into
2019-12-03 22:46:03 -04:00
uint8_t convert_angle_to_sector ( float angle_degrees ) const ;
2016-11-15 03:11:14 -04:00
2017-01-10 01:02:43 -04:00
// initialise the boundary and sector_edge_vector array used for object avoidance
// should be called if the sector_middle_deg or _setor_width_deg arrays are changed
void init_boundary ( ) ;
2016-11-15 03:29:35 -04:00
// update boundary points used for object avoidance based on a single sector's 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
2019-06-29 02:57:09 -03:00
void update_boundary_for_sector ( const uint8_t sector , const bool push_to_OA_DB ) ;
2016-11-15 03:29:35 -04:00
2019-12-03 22:46:03 -04:00
// check if a reading should be ignored because it falls into an ignore area
// angles should be in degrees and in the range of 0 to 360
bool ignore_reading ( uint16_t angle_deg ) const ;
2016-11-15 03:27:34 -04:00
2019-11-26 21:32:56 -04:00
// database helpers. all angles are in degrees
2019-12-04 03:17:23 -04:00
bool database_prepare_for_push ( Vector2f & current_pos , float & current_heading ) ;
2019-11-26 21:32:56 -04:00
void database_push ( float angle , float distance ) ;
2019-12-04 03:17:23 -04:00
void database_push ( float angle , float distance , uint32_t timestamp_ms , const Vector2f & current_pos , float current_heading ) ;
2019-05-29 03:20:48 -03:00
2016-08-15 03:17:15 -03:00
AP_Proximity & frontend ;
AP_Proximity : : Proximity_State & state ; // reference to this instances state
2016-11-15 03:11:14 -04:00
// sectors
2019-12-03 22:46:03 -04:00
const uint16_t _sector_middle_deg [ PROXIMITY_NUM_SECTORS ] = { 0 , 45 , 90 , 135 , 180 , 225 , 270 , 315 } ; // middle angle of each sector
2016-11-15 03:11:14 -04:00
// sensor data
2019-12-03 22:46:03 -04:00
float _angle [ PROXIMITY_NUM_SECTORS ] ; // angle to closest object within each sector
float _distance [ PROXIMITY_NUM_SECTORS ] ; // distance to closest object within each sector
bool _distance_valid [ PROXIMITY_NUM_SECTORS ] ; // true if a valid distance received for each sector
2016-11-15 03:29:35 -04:00
// fence boundary
2019-12-03 22:46:03 -04:00
Vector2f _sector_edge_vector [ PROXIMITY_NUM_SECTORS ] ; // vector for right-edge of each sector, used to speed up calculation of boundary
Vector2f _boundary_point [ PROXIMITY_NUM_SECTORS ] ; // bounding polygon around the vehicle calculated conservatively for object avoidance
2016-08-15 03:17:15 -03:00
} ;