Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/libraries/AC_WPNav/AC_WPNav.h
Views: 1798
#pragma once12#include <AP_Common/AP_Common.h>3#include <AP_Param/AP_Param.h>4#include <AP_Math/AP_Math.h>5#include <AP_Math/SCurve.h>6#include <AP_Math/SplineCurve.h>7#include <AP_Common/Location.h>8#include <AP_InertialNav/AP_InertialNav.h> // Inertial Navigation library9#include <AC_AttitudeControl/AC_PosControl.h> // Position control library10#include <AC_AttitudeControl/AC_AttitudeControl.h> // Attitude control library11#include <AP_Terrain/AP_Terrain.h>12#include <AC_Avoidance/AC_Avoid.h> // Stop at fence library1314// maximum velocities and accelerations15#define WPNAV_ACCELERATION 250.0f // maximum horizontal acceleration in cm/s/s that wp navigation will request1617class AC_WPNav18{19public:2021/// Constructor22AC_WPNav(const AP_InertialNav& inav, const AP_AHRS_View& ahrs, AC_PosControl& pos_control, const AC_AttitudeControl& attitude_control);2324/// provide rangefinder based terrain offset25/// terrain offset is the terrain's height above the EKF origin26void set_rangefinder_terrain_offset(bool use, bool healthy, float terrain_offset_cm) { _rangefinder_available = use; _rangefinder_healthy = healthy; _rangefinder_terrain_offset_cm = terrain_offset_cm;}2728// return true if range finder may be used for terrain following29bool rangefinder_used() const { return _rangefinder_use; }30bool rangefinder_used_and_healthy() const { return _rangefinder_use && _rangefinder_healthy; }3132// get expected source of terrain data if alt-above-terrain command is executed (used by Copter's ModeRTL)33enum class TerrainSource {34TERRAIN_UNAVAILABLE,35TERRAIN_FROM_RANGEFINDER,36TERRAIN_FROM_TERRAINDATABASE,37};38AC_WPNav::TerrainSource get_terrain_source() const;3940// 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)41bool get_terrain_offset(float& offset_cm);4243// return terrain following altitude margin. vehicle will stop if distance from target altitude is larger than this margin44float get_terrain_margin() const { return MAX(_terrain_margin, 0.1); }4546// convert location to vector from ekf origin. terrain_alt is set to true if resulting vector's z-axis should be treated as alt-above-terrain47// returns false if conversion failed (likely because terrain data was not available)48bool get_vector_NEU(const Location &loc, Vector3f &vec, bool &terrain_alt);4950///51/// waypoint controller52///5354/// wp_and_spline_init - initialise straight line and spline waypoint controllers55/// speed_cms is the desired max speed to travel between waypoints. should be a positive value or omitted to use the default speed56/// updates target roll, pitch targets and I terms based on vehicle lean angles57/// should be called once before the waypoint controller is used but does not need to be called before subsequent updates to destination58void wp_and_spline_init(float speed_cms = 0.0f, Vector3f stopping_point = Vector3f{});5960/// set current target horizontal speed during wp navigation61void set_speed_xy(float speed_cms);6263/// set pause or resume during wp navigation64void set_pause() { _paused = true; }65void set_resume() { _paused = false; }6667/// get paused status68bool paused() { return _paused; }6970/// set current target climb or descent rate during wp navigation71void set_speed_up(float speed_up_cms);72void set_speed_down(float speed_down_cms);7374/// get default target horizontal velocity during wp navigation75float get_default_speed_xy() const { return _wp_speed_cms; }7677/// get default target climb speed in cm/s during missions78float get_default_speed_up() const { return _wp_speed_up_cms; }7980/// get default target descent rate in cm/s during missions. Note: always positive81float get_default_speed_down() const { return fabsf(_wp_speed_down_cms); }8283/// get_speed_z - returns target descent speed in cm/s during missions. Note: always positive84float get_accel_z() const { return _wp_accel_z_cmss; }8586/// get_wp_acceleration - returns acceleration in cm/s/s during missions87float get_wp_acceleration() const { return (is_positive(_wp_accel_cmss)) ? _wp_accel_cmss : WPNAV_ACCELERATION; }8889/// get_corner_acceleration - returns maximum acceleration in cm/s/s used during cornering in missions90float get_corner_acceleration() const { return (is_positive(_wp_accel_c_cmss)) ? _wp_accel_c_cmss : 2.0 * get_wp_acceleration(); }9192/// get_wp_destination waypoint using position vector93/// x,y are distance from ekf origin in cm94/// z may be cm above ekf origin or terrain (see origin_and_destination_are_terrain_alt method)95const Vector3f &get_wp_destination() const { return _destination; }9697/// get origin using position vector (distance from ekf origin in cm)98const Vector3f &get_wp_origin() const { return _origin; }99100/// true if origin.z and destination.z are alt-above-terrain, false if alt-above-ekf-origin101bool origin_and_destination_are_terrain_alt() const { return _terrain_alt; }102103/// set_wp_destination waypoint using location class104/// provide the next_destination if known105/// returns false if conversion from location to vector from ekf origin cannot be calculated106bool set_wp_destination_loc(const Location& destination);107bool set_wp_destination_next_loc(const Location& destination);108109// get destination as a location. Altitude frame will be absolute (AMSL) or above terrain110// returns false if unable to return a destination (for example if origin has not yet been set)111bool get_wp_destination_loc(Location& destination) const;112113// returns object avoidance adjusted destination which is always the same as get_wp_destination114// having this function unifies the AC_WPNav_OA and AC_WPNav interfaces making vehicle code simpler115virtual bool get_oa_wp_destination(Location& destination) const { return get_wp_destination_loc(destination); }116117/// set_wp_destination waypoint using position vector (distance from ekf origin in cm)118/// terrain_alt should be true if destination.z is a desired altitude above terrain119virtual bool set_wp_destination(const Vector3f& destination, bool terrain_alt = false);120bool set_wp_destination_next(const Vector3f& destination, bool terrain_alt = false);121122/// set waypoint destination using NED position vector from ekf origin in meters123/// provide next_destination_NED if known124bool set_wp_destination_NED(const Vector3f& destination_NED);125bool set_wp_destination_next_NED(const Vector3f& destination_NED);126127/// shifts the origin and destination horizontally to the current position128/// used to reset the track when taking off without horizontal position control129/// relies on set_wp_destination or set_wp_origin_and_destination having been called first130void shift_wp_origin_and_destination_to_current_pos_xy();131132/// shifts the origin and destination horizontally to the achievable stopping point133/// used to reset the track when horizontal navigation is enabled after having been disabled (see Copter's wp_navalt_min)134/// relies on set_wp_destination or set_wp_origin_and_destination having been called first135void shift_wp_origin_and_destination_to_stopping_point_xy();136137/// get_wp_stopping_point_xy - calculates stopping point based on current position, velocity, waypoint acceleration138/// results placed in stopping_position vector139void get_wp_stopping_point_xy(Vector2f& stopping_point) const;140void get_wp_stopping_point(Vector3f& stopping_point) const;141142/// get_wp_distance_to_destination - get horizontal distance to destination in cm143virtual float get_wp_distance_to_destination() const;144145/// get_bearing_to_destination - get bearing to next waypoint in centi-degrees146virtual int32_t get_wp_bearing_to_destination() const;147148/// reached_destination - true when we have come within RADIUS cm of the waypoint149virtual bool reached_wp_destination() const { return _flags.reached_destination; }150151// reached_wp_destination_xy - true if within RADIUS_CM of waypoint in x/y152bool reached_wp_destination_xy() const {153return get_wp_distance_to_destination() < _wp_radius_cm;154}155156// get wp_radius parameter value in cm157float get_wp_radius_cm() const { return _wp_radius_cm; }158159/// update_wpnav - run the wp controller - should be called at 100hz or higher160virtual bool update_wpnav();161162// returns true if update_wpnav has been run very recently163bool is_active() const;164165// force stopping at next waypoint. Used by Dijkstra's object avoidance when path from destination to next destination is not clear166// only affects regular (e.g. non-spline) waypoints167// returns true if this had any affect on the path168bool force_stop_at_next_wp();169170///171/// spline methods172///173174/// set_spline_destination waypoint using location class175/// returns false if conversion from location to vector from ekf origin cannot be calculated176/// next_destination should be the next segment's destination177/// next_is_spline should be true if next_destination is a spline segment178bool set_spline_destination_loc(const Location& destination, const Location& next_destination, bool next_is_spline);179180/// set next destination (e.g. the one after the current destination) as a spline segment specified as a location181/// returns false if conversion from location to vector from ekf origin cannot be calculated182/// next_next_destination should be the next segment's destination183/// next_next_is_spline should be true if next_next_destination is a spline segment184bool set_spline_destination_next_loc(const Location& next_destination, const Location& next_next_destination, bool next_next_is_spline);185186/// set_spline_destination waypoint using position vector (distance from ekf origin in cm)187/// terrain_alt should be true if destination.z is a desired altitude above terrain (false if its desired altitudes above ekf origin)188/// next_destination is the next segment's destination189/// next_terrain_alt should be true if next_destination.z is a desired altitude above terrain (false if its desired altitudes above ekf origin)190/// next_destination.z must be in the same "frame" as destination.z (i.e. if destination is a alt-above-terrain, next_destination must be too)191/// next_is_spline should be true if next_destination is a spline segment192bool set_spline_destination(const Vector3f& destination, bool terrain_alt, const Vector3f& next_destination, bool next_terrain_alt, bool next_is_spline);193194/// set next destination (e.g. the one after the current destination) as an offset (in cm, NEU frame) from the EKF origin195/// next_terrain_alt should be true if next_destination.z is a desired altitude above terrain (false if its desired altitudes above ekf origin)196/// next_next_destination is the next segment's destination197/// next_next_terrain_alt should be true if next_next_destination.z is a desired altitude above terrain (false if it is desired altitude above ekf origin)198/// next_next_destination.z must be in the same "frame" as destination.z (i.e. if next_destination is a alt-above-terrain, next_next_destination must be too)199/// next_next_is_spline should be true if next_next_destination is a spline segment200bool set_spline_destination_next(const Vector3f& next_destination, bool next_terrain_alt, const Vector3f& next_next_destination, bool next_next_terrain_alt, bool next_next_is_spline);201202///203/// shared methods204///205206/// get desired roll, pitch which should be fed into stabilize controllers207float get_roll() const { return _pos_control.get_roll_cd(); }208float get_pitch() const { return _pos_control.get_pitch_cd(); }209Vector3f get_thrust_vector() const { return _pos_control.get_thrust_vector(); }210211// get target yaw in centi-degrees212float get_yaw() const { return _pos_control.get_yaw_cd(); }213/// advance_wp_target_along_track - move target location along track from origin to destination214bool advance_wp_target_along_track(float dt);215216/// recalculate path with update speed and/or acceleration limits217void update_track_with_speed_accel_limits();218219/// return the crosstrack_error - horizontal error of the actual position vs the desired position220float crosstrack_error() const { return _pos_control.crosstrack_error();}221222static const struct AP_Param::GroupInfo var_info[];223224protected:225226// flags structure227struct wpnav_flags {228uint8_t reached_destination : 1; // true if we have reached the destination229uint8_t fast_waypoint : 1; // true if we should ignore the waypoint radius and consider the waypoint complete once the intermediate target has reached the waypoint230uint8_t wp_yaw_set : 1; // true if yaw target has been set231} _flags;232233// helper function to calculate scurve jerk and jerk_time values234// updates _scurve_jerk and _scurve_snap235void calc_scurve_jerk_and_snap();236237// references and pointers to external libraries238const AP_InertialNav& _inav;239const AP_AHRS_View& _ahrs;240AC_PosControl& _pos_control;241const AC_AttitudeControl& _attitude_control;242243// parameters244AP_Float _wp_speed_cms; // default maximum horizontal speed in cm/s during missions245AP_Float _wp_speed_up_cms; // default maximum climb rate in cm/s246AP_Float _wp_speed_down_cms; // default maximum descent rate in cm/s247AP_Float _wp_radius_cm; // distance from a waypoint in cm that, when crossed, indicates the wp has been reached248AP_Float _wp_accel_cmss; // horizontal acceleration in cm/s/s during missions249AP_Float _wp_accel_c_cmss; // cornering acceleration in cm/s/s during missions250AP_Float _wp_accel_z_cmss; // vertical acceleration in cm/s/s during missions251AP_Float _wp_jerk; // maximum jerk used to generate scurve trajectories in m/s/s/s252AP_Float _terrain_margin; // terrain following altitude margin. vehicle will stop if distance from target altitude is larger than this margin253254// WPNAV_SPEED param change checker255bool _check_wp_speed_change; // if true WPNAV_SPEED param should be checked for changes in-flight256float _last_wp_speed_cms; // last recorded WPNAV_SPEED, used for changing speed in-flight257float _last_wp_speed_up_cms; // last recorded WPNAV_SPEED_UP, used for changing speed in-flight258float _last_wp_speed_down_cms; // last recorded WPNAV_SPEED_DN, used for changing speed in-flight259260// scurve261SCurve _scurve_prev_leg; // previous scurve trajectory used to blend with current scurve trajectory262SCurve _scurve_this_leg; // current scurve trajectory263SCurve _scurve_next_leg; // next scurve trajectory used to blend with current scurve trajectory264float _scurve_jerk; // scurve jerk max in m/s/s/s265float _scurve_snap; // scurve snap in m/s/s/s/s266267// spline curves268SplineCurve _spline_this_leg; // spline curve for current segment269SplineCurve _spline_next_leg; // spline curve for next segment270271// the type of this leg272bool _this_leg_is_spline; // true if this leg is a spline273bool _next_leg_is_spline; // true if the next leg is a spline274275// waypoint controller internal variables276uint32_t _wp_last_update; // time of last update_wpnav call277float _wp_desired_speed_xy_cms; // desired wp speed in cm/sec278Vector3f _origin; // starting point of trip to next waypoint in cm from ekf origin279Vector3f _destination; // target destination in cm from ekf origin280Vector3f _next_destination; // next target destination in cm from ekf origin281float _track_scalar_dt; // time compression multiplier to slow the progress along the track282float _offset_vel; // horizontal velocity reference used to slow the aircraft for pause and to ensure the aircraft can maintain height above terrain283float _offset_accel; // horizontal acceleration reference used to slow the aircraft for pause and to ensure the aircraft can maintain height above terrain284bool _paused; // flag for pausing waypoint controller285286// terrain following variables287bool _terrain_alt; // true if origin and destination.z are alt-above-terrain, false if alt-above-ekf-origin288bool _rangefinder_available; // true if rangefinder is enabled (user switch can turn this true/false)289AP_Int8 _rangefinder_use; // parameter that specifies if the range finder should be used for terrain following commands290bool _rangefinder_healthy; // true if rangefinder distance is healthy (i.e. between min and maximum)291float _rangefinder_terrain_offset_cm; // latest rangefinder based terrain offset (e.g. terrain's height above EKF origin)292};293294295