2016-02-17 21:25:11 -04:00
|
|
|
#pragma once
|
2014-01-27 01:08:11 -04:00
|
|
|
|
2015-08-11 03:28:41 -03:00
|
|
|
#include <AP_Common/AP_Common.h>
|
|
|
|
#include <AP_Param/AP_Param.h>
|
|
|
|
#include <AP_Math/AP_Math.h>
|
|
|
|
#include <AP_InertialNav/AP_InertialNav.h> // Inertial Navigation library
|
|
|
|
#include <AC_AttitudeControl/AC_PosControl.h> // Position control library
|
2014-01-27 01:08:11 -04:00
|
|
|
|
|
|
|
// loiter maximum velocities and accelerations
|
|
|
|
#define AC_CIRCLE_RADIUS_DEFAULT 1000.0f // radius of the circle in cm that the vehicle will fly
|
|
|
|
#define AC_CIRCLE_RATE_DEFAULT 20.0f // turn rate in deg/sec. Positive to turn clockwise, negative for counter clockwise
|
2015-07-22 09:40:20 -03:00
|
|
|
#define AC_CIRCLE_ANGULAR_ACCEL_MIN 2.0f // angular acceleration should never be less than 2deg/sec
|
2020-01-06 18:35:17 -04:00
|
|
|
#define AC_CIRCLE_RADIUS_MAX 200000.0f // maximum allowed circle radius of 2km
|
2014-01-27 01:08:11 -04:00
|
|
|
|
|
|
|
class AC_Circle
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
/// Constructor
|
2017-02-11 18:33:43 -04:00
|
|
|
AC_Circle(const AP_InertialNav& inav, const AP_AHRS_View& ahrs, AC_PosControl& pos_control);
|
2014-01-27 01:08:11 -04:00
|
|
|
|
2014-04-16 04:19:41 -03:00
|
|
|
/// init - initialise circle controller setting center specifically
|
2020-04-08 03:20:56 -03:00
|
|
|
/// set terrain_alt to true if center.z should be interpreted as an alt-above-terrain
|
2014-05-07 03:05:03 -03:00
|
|
|
/// caller should set the position controller's x,y and z speeds and accelerations before calling this
|
2021-06-17 22:19:53 -03:00
|
|
|
void init(const Vector3p& center, bool terrain_alt);
|
2014-04-16 04:19:41 -03:00
|
|
|
|
|
|
|
/// init - initialise circle controller setting center using stopping point and projecting out based on the copter's heading
|
2014-05-07 03:05:03 -03:00
|
|
|
/// caller should set the position controller's x,y and z speeds and accelerations before calling this
|
2014-04-16 04:19:41 -03:00
|
|
|
void init();
|
2014-01-27 01:08:11 -04:00
|
|
|
|
2020-04-08 03:20:56 -03:00
|
|
|
/// set circle center to a Location
|
|
|
|
void set_center(const Location& center);
|
|
|
|
|
|
|
|
/// set_circle_center as a vector from ekf origin
|
|
|
|
/// terrain_alt should be true if center.z is alt is above terrain
|
2021-06-17 22:19:53 -03:00
|
|
|
void set_center(const Vector3f& center, bool terrain_alt) { _center = center.topostype(); _terrain_alt = terrain_alt; }
|
2014-01-27 01:08:11 -04:00
|
|
|
|
|
|
|
/// get_circle_center in cm from home
|
2021-06-17 22:19:53 -03:00
|
|
|
const Vector3p& get_center() const { return _center; }
|
2014-01-27 01:08:11 -04:00
|
|
|
|
2020-04-08 03:20:56 -03:00
|
|
|
/// returns true if using terrain altitudes
|
|
|
|
bool center_is_terrain_alt() const { return _terrain_alt; }
|
|
|
|
|
2014-04-16 04:19:41 -03:00
|
|
|
/// get_radius - returns radius of circle in cm
|
2021-04-09 03:33:28 -03:00
|
|
|
float get_radius() const { return is_positive(_radius)?_radius:_radius_parm; }
|
2020-01-06 18:35:17 -04:00
|
|
|
|
2022-02-06 12:37:10 -04:00
|
|
|
/// set_radius_cm - sets circle radius in cm
|
|
|
|
void set_radius_cm(float radius_cm);
|
2020-01-06 18:35:17 -04:00
|
|
|
|
|
|
|
/// get_rate - returns target rate in deg/sec held in RATE parameter
|
|
|
|
float get_rate() const { return _rate; }
|
2014-01-27 01:08:11 -04:00
|
|
|
|
2020-01-06 18:35:17 -04:00
|
|
|
/// get_rate_current - returns actual calculated rate target in deg/sec, which may be less than _rate
|
|
|
|
float get_rate_current() const { return ToDeg(_angular_vel); }
|
|
|
|
|
|
|
|
/// set_rate - set circle rate in degrees per second
|
2015-07-22 09:40:20 -03:00
|
|
|
void set_rate(float deg_per_sec);
|
2014-01-27 01:08:11 -04:00
|
|
|
|
|
|
|
/// get_angle_total - return total angle in radians that vehicle has circled
|
|
|
|
float get_angle_total() const { return _angle_total; }
|
|
|
|
|
|
|
|
/// update - update circle controller
|
2020-04-08 03:20:56 -03:00
|
|
|
/// returns false on failure which indicates a terrain failsafe
|
2021-05-03 22:40:32 -03:00
|
|
|
bool update(float climb_rate_cms = 0.0f) WARN_IF_UNUSED;
|
2014-01-27 01:08:11 -04:00
|
|
|
|
|
|
|
/// get desired roll, pitch which should be fed into stabilize controllers
|
2021-05-12 01:24:48 -03:00
|
|
|
float get_roll() const { return _pos_control.get_roll_cd(); }
|
|
|
|
float get_pitch() const { return _pos_control.get_pitch_cd(); }
|
2021-04-13 01:22:56 -03:00
|
|
|
Vector3f get_thrust_vector() const { return _pos_control.get_thrust_vector(); }
|
2019-04-04 17:57:45 -03:00
|
|
|
float get_yaw() const { return _yaw; }
|
2014-04-16 04:19:41 -03:00
|
|
|
|
2020-04-21 08:42:32 -03:00
|
|
|
/// returns true if update has been run recently
|
|
|
|
/// used by vehicle code to determine if get_yaw() is valid
|
|
|
|
bool is_active() const;
|
|
|
|
|
2014-04-16 04:19:41 -03:00
|
|
|
// get_closest_point_on_circle - returns closest point on the circle
|
|
|
|
// circle's center should already have been set
|
|
|
|
// closest point on the circle will be placed in result
|
|
|
|
// result's altitude (i.e. z) will be set to the circle_center's altitude
|
|
|
|
// if vehicle is at the center of the circle, the edge directly behind vehicle will be returned
|
2020-01-06 18:35:17 -04:00
|
|
|
void get_closest_point_on_circle(Vector3f &result) const;
|
2014-01-27 01:08:11 -04:00
|
|
|
|
2018-03-27 23:09:39 -03:00
|
|
|
/// get horizontal distance to loiter target in cm
|
2021-05-03 22:40:32 -03:00
|
|
|
float get_distance_to_target() const { return _pos_control.get_pos_error_xy_cm(); }
|
2018-03-27 23:09:39 -03:00
|
|
|
|
|
|
|
/// get bearing to target in centi-degrees
|
2021-05-03 22:40:32 -03:00
|
|
|
int32_t get_bearing_to_target() const { return _pos_control.get_bearing_to_target_cd(); }
|
2018-03-27 23:09:39 -03:00
|
|
|
|
2020-01-06 18:35:17 -04:00
|
|
|
/// true if pilot control of radius and turn rate is enabled
|
2020-09-15 11:50:33 -03:00
|
|
|
bool pilot_control_enabled() const { return (_options.get() & CircleOptions::MANUAL_CONTROL) != 0; }
|
2020-01-06 18:35:17 -04:00
|
|
|
|
2020-04-08 03:20:56 -03:00
|
|
|
/// provide rangefinder altitude
|
|
|
|
void set_rangefinder_alt(bool use, bool healthy, float alt_cm) { _rangefinder_available = use; _rangefinder_healthy = healthy; _rangefinder_alt_cm = alt_cm; }
|
|
|
|
|
2021-05-01 21:41:30 -03:00
|
|
|
/// check for a change in the radius params
|
|
|
|
void check_param_change();
|
|
|
|
|
2014-01-27 01:08:11 -04:00
|
|
|
static const struct AP_Param::GroupInfo var_info[];
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
// calc_velocities - calculate angular velocity max and acceleration based on radius and rate
|
|
|
|
// this should be called whenever the radius or rate are changed
|
|
|
|
// initialises the yaw and current position around the circle
|
2015-07-22 09:40:20 -03:00
|
|
|
// init_velocity should be set true if vehicle is just starting circle
|
|
|
|
void calc_velocities(bool init_velocity);
|
2014-01-27 01:08:11 -04:00
|
|
|
|
2014-04-16 04:19:41 -03:00
|
|
|
// init_start_angle - sets the starting angle around the circle and initialises the angle_total
|
|
|
|
// if use_heading is true the vehicle's heading will be used to init the angle causing minimum yaw movement
|
|
|
|
// if use_heading is false the vehicle's position from the center will be used to initialise the angle
|
|
|
|
void init_start_angle(bool use_heading);
|
|
|
|
|
2020-04-08 03:20:56 -03:00
|
|
|
// get expected source of terrain data
|
|
|
|
enum class TerrainSource {
|
|
|
|
TERRAIN_UNAVAILABLE,
|
|
|
|
TERRAIN_FROM_RANGEFINDER,
|
|
|
|
TERRAIN_FROM_TERRAINDATABASE,
|
|
|
|
};
|
|
|
|
AC_Circle::TerrainSource get_terrain_source() const;
|
|
|
|
|
|
|
|
// get terrain's altitude (in cm above the ekf origin) at the current position (+ve means terrain below vehicle is above ekf origin's altitude)
|
|
|
|
bool get_terrain_offset(float& offset_cm);
|
|
|
|
|
2014-01-27 01:08:11 -04:00
|
|
|
// flags structure
|
|
|
|
struct circle_flags {
|
|
|
|
uint8_t panorama : 1; // true if we are doing a panorama
|
|
|
|
} _flags;
|
|
|
|
|
|
|
|
// references to inertial nav and ahrs libraries
|
|
|
|
const AP_InertialNav& _inav;
|
2017-02-11 18:33:43 -04:00
|
|
|
const AP_AHRS_View& _ahrs;
|
2014-01-27 01:08:11 -04:00
|
|
|
AC_PosControl& _pos_control;
|
|
|
|
|
2020-09-15 11:50:33 -03:00
|
|
|
enum CircleOptions {
|
|
|
|
MANUAL_CONTROL = 1U << 0,
|
|
|
|
FACE_DIRECTION_OF_TRAVEL = 1U << 1,
|
|
|
|
INIT_AT_CENTER = 1U << 2, // true then the circle center will be the current location, false and the center will be 1 radius ahead
|
|
|
|
};
|
|
|
|
|
2014-01-27 01:08:11 -04:00
|
|
|
// parameters
|
2021-04-09 03:33:28 -03:00
|
|
|
AP_Float _radius_parm; // radius of circle in cm loaded from params
|
2014-01-27 01:08:11 -04:00
|
|
|
AP_Float _rate; // rotation speed in deg/sec
|
2020-09-15 11:50:33 -03:00
|
|
|
AP_Int16 _options; // stick control enable/disable
|
2014-01-27 01:08:11 -04:00
|
|
|
|
|
|
|
// internal variables
|
2021-06-17 22:19:53 -03:00
|
|
|
Vector3p _center; // center of circle in cm from home
|
2021-04-09 03:33:28 -03:00
|
|
|
float _radius; // radius of circle in cm
|
2014-01-27 01:08:11 -04:00
|
|
|
float _yaw; // yaw heading (normally towards circle center)
|
|
|
|
float _angle; // current angular position around circle in radians (0=directly north of the center of the circle)
|
2017-01-05 14:07:14 -04:00
|
|
|
float _angle_total; // total angle traveled in radians
|
2014-01-27 01:08:11 -04:00
|
|
|
float _angular_vel; // angular velocity in radians/sec
|
|
|
|
float _angular_vel_max; // maximum velocity in radians/sec
|
|
|
|
float _angular_accel; // angular acceleration in radians/sec/sec
|
2020-04-21 08:42:32 -03:00
|
|
|
uint32_t _last_update_ms; // system time of last update
|
2021-05-01 21:41:30 -03:00
|
|
|
float _last_radius_param; // last value of radius param, used to update radius on param change
|
2020-04-08 03:20:56 -03:00
|
|
|
|
|
|
|
// terrain following variables
|
|
|
|
bool _terrain_alt; // true if _center.z is alt-above-terrain, false if alt-above-ekf-origin
|
|
|
|
bool _rangefinder_available; // true if range finder could be used
|
|
|
|
bool _rangefinder_healthy; // true if range finder is healthy
|
|
|
|
float _rangefinder_alt_cm; // latest rangefinder altitude
|
2014-01-27 01:08:11 -04:00
|
|
|
};
|