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_AttitudeControl/AC_PosControl.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 <AC_PID/AC_P.h> // P library6#include <AC_PID/AC_PID.h> // PID library7#include <AC_PID/AC_P_1D.h> // P library (1-axis)8#include <AC_PID/AC_P_2D.h> // P library (2-axis)9#include <AC_PID/AC_PI_2D.h> // PI library (2-axis)10#include <AC_PID/AC_PID_Basic.h> // PID library (1-axis)11#include <AC_PID/AC_PID_2D.h> // PID library (2-axis)12#include <AP_InertialNav/AP_InertialNav.h> // Inertial Navigation library13#include <AP_Scripting/AP_Scripting_config.h>14#include "AC_AttitudeControl.h" // Attitude control library1516#include <AP_Logger/LogStructure.h>1718// position controller default definitions19#define POSCONTROL_ACCEL_XY 100.0f // default horizontal acceleration in cm/s/s. This is overwritten by waypoint and loiter controllers20#define POSCONTROL_JERK_XY 5.0f // default horizontal jerk m/s/s/s2122#define POSCONTROL_STOPPING_DIST_UP_MAX 300.0f // max stopping distance (in cm) vertically while climbing23#define POSCONTROL_STOPPING_DIST_DOWN_MAX 200.0f // max stopping distance (in cm) vertically while descending2425#define POSCONTROL_SPEED 500.0f // default horizontal speed in cm/s26#define POSCONTROL_SPEED_DOWN -150.0f // default descent rate in cm/s27#define POSCONTROL_SPEED_UP 250.0f // default climb rate in cm/s2829#define POSCONTROL_ACCEL_Z 250.0f // default vertical acceleration in cm/s/s.30#define POSCONTROL_JERK_Z 5.0f // default vertical jerk m/s/s/s3132#define POSCONTROL_THROTTLE_CUTOFF_FREQ_HZ 2.0f // low-pass filter on acceleration error (unit: Hz)3334#define POSCONTROL_OVERSPEED_GAIN_Z 2.0f // gain controlling rate at which z-axis speed is brought back within SPEED_UP and SPEED_DOWN range3536#define POSCONTROL_RELAX_TC 0.16f // This is used to decay the I term to 5% in half a second.3738class AC_PosControl39{40public:4142/// Constructor43AC_PosControl(AP_AHRS_View& ahrs, const AP_InertialNav& inav,44const class AP_Motors& motors, AC_AttitudeControl& attitude_control);4546// do not allow copying47CLASS_NO_COPY(AC_PosControl);4849/// set_dt / get_dt - dt is the time since the last time the position controllers were updated50/// _dt should be set based on the time of the last IMU read used by these controllers51/// the position controller should run updates for active controllers on each loop to ensure normal operation52void set_dt(float dt) { _dt = dt; }53float get_dt() const { return _dt; }5455/// get_shaping_jerk_xy_cmsss - gets the jerk limit of the xy kinematic path generation in cm/s/s/s56float get_shaping_jerk_xy_cmsss() const { return _shaping_jerk_xy * 100.0; }575859///60/// 3D position shaper61///6263/// input_pos_xyz - calculate a jerk limited path from the current position, velocity and acceleration to an input position.64/// The function takes the current position, velocity, and acceleration and calculates the required jerk limited adjustment to the acceleration for the next time dt.65/// The kinematic path is constrained by the maximum acceleration and jerk set using the function set_max_speed_accel_xy.66void input_pos_xyz(const Vector3p& pos, float pos_terrain_target, float terrain_buffer);6768/// pos_offset_z_scaler - calculates a multiplier used to reduce the horizontal velocity to allow the z position controller to stay within the provided buffer range69float pos_offset_z_scaler(float pos_offset_z, float pos_offset_z_buffer) const;7071///72/// Lateral position controller73///7475/// set_max_speed_accel_xy - set the maximum horizontal speed in cm/s and acceleration in cm/s/s76/// This function only needs to be called if using the kinematic shaping.77/// This can be done at any time as changes in these parameters are handled smoothly78/// by the kinematic shaping.79void set_max_speed_accel_xy(float speed_cms, float accel_cmss);8081/// set_max_speed_accel_xy - set the position controller correction velocity and acceleration limit82/// This should be done only during initialisation to avoid discontinuities83void set_correction_speed_accel_xy(float speed_cms, float accel_cmss);8485/// get_max_speed_xy_cms - get the maximum horizontal speed in cm/s86float get_max_speed_xy_cms() const { return _vel_max_xy_cms; }8788/// get_max_accel_xy_cmss - get the maximum horizontal acceleration in cm/s/s89float get_max_accel_xy_cmss() const { return _accel_max_xy_cmss; }9091// set the maximum horizontal position error that will be allowed in the horizontal plane92void set_pos_error_max_xy_cm(float error_max) { _p_pos_xy.set_error_max(error_max); }93float get_pos_error_max_xy_cm() { return _p_pos_xy.get_error_max(); }9495/// init_xy_controller_stopping_point - initialise the position controller to the stopping point with zero velocity and acceleration.96/// This function should be used when the expected kinematic path assumes a stationary initial condition but does not specify a specific starting position.97/// The starting position can be retrieved by getting the position target using get_pos_target_cm() after calling this function.98void init_xy_controller_stopping_point();99100// relax_velocity_controller_xy - initialise the position controller to the current position and velocity with decaying acceleration.101/// This function decays the output acceleration by 95% every half second to achieve a smooth transition to zero requested acceleration.102void relax_velocity_controller_xy();103104/// reduce response for landing105void soften_for_landing_xy();106107// init_xy_controller - initialise the position controller to the current position, velocity, acceleration and attitude.108/// This function is the default initialisation for any position control that provides position, velocity and acceleration.109/// This function is private and contains all the shared xy axis initialisation functions110void init_xy_controller();111112/// input_accel_xy - calculate a jerk limited path from the current position, velocity and acceleration to an input acceleration.113/// The function takes the current position, velocity, and acceleration and calculates the required jerk limited adjustment to the acceleration for the next time dt.114/// The kinematic path is constrained by the maximum acceleration and jerk set using the function set_max_speed_accel_xy.115/// The jerk limit defines the acceleration error decay in the kinematic path as the system approaches constant acceleration.116/// The jerk limit also defines the time taken to achieve the maximum acceleration.117void input_accel_xy(const Vector3f& accel);118119/// input_vel_accel_xy - calculate a jerk limited path from the current position, velocity and acceleration to an input velocity and acceleration.120/// The function takes the current position, velocity, and acceleration and calculates the required jerk limited adjustment to the acceleration for the next time dt.121/// The kinematic path is constrained by the maximum acceleration and jerk set using the function set_max_speed_accel_xy.122/// The function alters the vel to be the kinematic path based on accel123/// The parameter limit_output specifies if the velocity and acceleration limits are applied to the sum of commanded and correction values or just correction.124void input_vel_accel_xy(Vector2f& vel, const Vector2f& accel, bool limit_output = true);125126/// input_pos_vel_accel_xy - calculate a jerk limited path from the current position, velocity and acceleration to an input position velocity and acceleration.127/// The function takes the current position, velocity, and acceleration and calculates the required jerk limited adjustment to the acceleration for the next time dt.128/// The kinematic path is constrained by the maximum acceleration and jerk set using the function set_max_speed_accel_xy.129/// The function alters the pos and vel to be the kinematic path based on accel130/// The parameter limit_output specifies if the velocity and acceleration limits are applied to the sum of commanded and correction values or just correction.131void input_pos_vel_accel_xy(Vector2p& pos, Vector2f& vel, const Vector2f& accel, bool limit_output = true);132133// is_active_xy - returns true if the xy position controller has been run in the previous 5 loop times134bool is_active_xy() const;135136/// stop_pos_xy_stabilisation - sets the target to the current position to remove any position corrections from the system137void stop_pos_xy_stabilisation();138139/// stop_vel_xy_stabilisation - sets the target to the current position and velocity to the current velocity to remove any position and velocity corrections from the system140void stop_vel_xy_stabilisation();141142/// update_xy_controller - runs the horizontal position controller correcting position, velocity and acceleration errors.143/// Position and velocity errors are converted to velocity and acceleration targets using PID objects144/// Desired velocity and accelerations are added to these corrections as they are calculated145/// Kinematically consistent target position and desired velocity and accelerations should be provided before calling this function146void update_xy_controller();147148///149/// Vertical position controller150///151152/// set_max_speed_accel_z - set the maximum vertical speed in cm/s and acceleration in cm/s/s153/// speed_down can be positive or negative but will always be interpreted as a descent speed154/// This can be done at any time as changes in these parameters are handled smoothly155/// by the kinematic shaping.156void set_max_speed_accel_z(float speed_down, float speed_up, float accel_cmss);157158/// set_correction_speed_accel_z - set the position controller correction velocity and acceleration limit159/// speed_down can be positive or negative but will always be interpreted as a descent speed160/// This should be done only during initialisation to avoid discontinuities161void set_correction_speed_accel_z(float speed_down, float speed_up, float accel_cmss);162163/// get_max_accel_z_cmss - get the maximum vertical acceleration in cm/s/s164float get_max_accel_z_cmss() const { return _accel_max_z_cmss; }165166// get_pos_error_z_up_cm - get the maximum vertical position error up that will be allowed167float get_pos_error_z_up_cm() { return _p_pos_z.get_error_max(); }168169// get_pos_error_z_down_cm - get the maximum vertical position error down that will be allowed170float get_pos_error_z_down_cm() { return _p_pos_z.get_error_min(); }171172/// get_max_speed_up_cms - accessors for current maximum up speed in cm/s173float get_max_speed_up_cms() const { return _vel_max_up_cms; }174175/// get_max_speed_down_cms - accessors for current maximum down speed in cm/s. Will be a negative number176float get_max_speed_down_cms() const { return _vel_max_down_cms; }177178/// init_z_controller_no_descent - initialise the position controller to the current position, velocity, acceleration and attitude.179/// This function is the default initialisation for any position control that provides position, velocity and acceleration.180/// This function does not allow any negative velocity or acceleration181void init_z_controller_no_descent();182183/// init_z_controller_stopping_point - initialise the position controller to the stopping point with zero velocity and acceleration.184/// This function should be used when the expected kinematic path assumes a stationary initial condition but does not specify a specific starting position.185/// The starting position can be retrieved by getting the position target using get_pos_target_cm() after calling this function.186void init_z_controller_stopping_point();187188// relax_z_controller - initialise the position controller to the current position and velocity with decaying acceleration.189/// This function decays the output acceleration by 95% every half second to achieve a smooth transition to zero requested acceleration.190void relax_z_controller(float throttle_setting);191192// init_z_controller - initialise the position controller to the current position, velocity, acceleration and attitude.193/// This function is the default initialisation for any position control that provides position, velocity and acceleration.194/// This function is private and contains all the shared z axis initialisation functions195void init_z_controller();196197/// input_accel_z - calculate a jerk limited path from the current position, velocity and acceleration to an input acceleration.198/// The function takes the current position, velocity, and acceleration and calculates the required jerk limited adjustment to the acceleration for the next time dt.199/// The kinematic path is constrained by the maximum acceleration and jerk set using the function set_max_speed_accel_z.200virtual void input_accel_z(float accel);201202/// input_vel_accel_z - calculate a jerk limited path from the current position, velocity and acceleration to an input velocity and acceleration.203/// The function takes the current position, velocity, and acceleration and calculates the required jerk limited adjustment to the acceleration for the next time dt.204/// The kinematic path is constrained by the maximum acceleration and jerk set using the function set_max_speed_accel_z.205/// The function alters the vel to be the kinematic path based on accel206/// The parameter limit_output specifies if the velocity and acceleration limits are applied to the sum of commanded and correction values or just correction.207virtual void input_vel_accel_z(float &vel, float accel, bool limit_output = true);208209/// set_pos_target_z_from_climb_rate_cm - adjusts target up or down using a commanded climb rate in cm/s210/// using the default position control kinematic path.211/// The zero target altitude is varied to follow pos_offset_z212void set_pos_target_z_from_climb_rate_cm(float vel);213214/// land_at_climb_rate_cm - adjusts target up or down using a commanded climb rate in cm/s215/// using the default position control kinematic path.216/// ignore_descent_limit turns off output saturation handling to aid in landing detection. ignore_descent_limit should be true unless landing.217void land_at_climb_rate_cm(float vel, bool ignore_descent_limit);218219/// input_pos_vel_accel_z - calculate a jerk limited path from the current position, velocity and acceleration to an input position velocity and acceleration.220/// The function takes the current position, velocity, and acceleration and calculates the required jerk limited adjustment to the acceleration for the next time dt.221/// The function alters the pos and vel to be the kinematic path based on accel222/// The parameter limit_output specifies if the velocity and acceleration limits are applied to the sum of commanded and correction values or just correction.223void input_pos_vel_accel_z(float &pos, float &vel, float accel, bool limit_output = true);224225/// set_alt_target_with_slew - adjusts target up or down using a commanded altitude in cm226/// using the default position control kinematic path.227void set_alt_target_with_slew(float pos);228229// is_active_z - returns true if the z position controller has been run in the previous 5 loop times230bool is_active_z() const;231232/// update_z_controller - runs the vertical position controller correcting position, velocity and acceleration errors.233/// Position and velocity errors are converted to velocity and acceleration targets using PID objects234/// Desired velocity and accelerations are added to these corrections as they are calculated235/// Kinematically consistent target position and desired velocity and accelerations should be provided before calling this function236void update_z_controller();237238239240///241/// Accessors242///243244/// set commanded position (cm), velocity (cm/s) and acceleration (cm/s/s) inputs when the path is created externally.245void set_pos_vel_accel(const Vector3p& pos, const Vector3f& vel, const Vector3f& accel);246void set_pos_vel_accel_xy(const Vector2p& pos, const Vector2f& vel, const Vector2f& accel);247248249/// Position250251/// get_pos_target_cm - returns the position target, frame NEU in cm relative to the EKF origin252const Vector3p& get_pos_target_cm() const { return _pos_target; }253254/// set_pos_desired_xy_cm - sets the position target, frame NEU in cm relative to the EKF origin255void set_pos_desired_xy_cm(const Vector2f& pos) { _pos_desired.xy() = pos.topostype(); }256257/// get_pos_desired_cm - returns the position desired, frame NEU in cm relative to the EKF origin258const Vector3p& get_pos_desired_cm() const { return _pos_desired; }259260/// get_pos_target_z_cm - get target altitude (in cm above the EKF origin)261float get_pos_target_z_cm() const { return _pos_target.z; }262263/// set_pos_desired_z_cm - set altitude target in cm above the EKF origin264void set_pos_desired_z_cm(float pos_z) { _pos_desired.z = pos_z; }265266/// get_pos_desired_z_cm - get target altitude (in cm above the EKF origin)267float get_pos_desired_z_cm() const { return _pos_desired.z; }268269270/// Stopping Point271272/// get_stopping_point_xy_cm - calculates stopping point in NEU cm based on current position, velocity, vehicle acceleration273void get_stopping_point_xy_cm(Vector2p &stopping_point) const;274275/// get_stopping_point_z_cm - calculates stopping point in NEU cm based on current position, velocity, vehicle acceleration276void get_stopping_point_z_cm(postype_t &stopping_point) const;277278279/// Position Error280281/// get_pos_error_cm - get position error vector between the current and target position282const Vector3f get_pos_error_cm() const { return Vector3f(_p_pos_xy.get_error().x, _p_pos_xy.get_error().y, _p_pos_z.get_error()); }283284/// get_pos_error_xy_cm - get the length of the position error vector in the xy plane285float get_pos_error_xy_cm() const { return _p_pos_xy.get_error().length(); }286287/// get_pos_error_z_cm - returns altitude error in cm288float get_pos_error_z_cm() const { return _p_pos_z.get_error(); }289290291/// Velocity292293/// set_vel_desired_cms - sets desired velocity in NEU cm/s294void set_vel_desired_cms(const Vector3f &des_vel) { _vel_desired = des_vel; }295296/// set_vel_desired_xy_cms - sets horizontal desired velocity in NEU cm/s297void set_vel_desired_xy_cms(const Vector2f &vel) {_vel_desired.xy() = vel; }298299/// get_vel_desired_cms - returns desired velocity in cm/s in NEU300const Vector3f& get_vel_desired_cms() { return _vel_desired; }301302// get_vel_target_cms - returns the target velocity in NEU cm/s303const Vector3f& get_vel_target_cms() const { return _vel_target; }304305/// set_vel_desired_z_cms - sets desired velocity in cm/s in z axis306void set_vel_desired_z_cms(float vel_z_cms) {_vel_desired.z = vel_z_cms;}307308/// get_vel_target_z_cms - returns target vertical speed in cm/s309float get_vel_target_z_cms() const { return _vel_target.z; }310311312/// Acceleration313314// set_accel_desired_xy_cmss - set desired acceleration in cm/s in xy axis315void set_accel_desired_xy_cmss(const Vector2f &accel_cms) { _accel_desired.xy() = accel_cms; }316317// get_accel_target_cmss - returns the target acceleration in NEU cm/s/s318const Vector3f& get_accel_target_cmss() const { return _accel_target; }319320321/// Terrain322323// set_pos_terrain_target_cm - set target terrain altitude in cm324void set_pos_terrain_target_cm(float pos_terrain_target) {_pos_terrain_target = pos_terrain_target;}325326// init_pos_terrain_cm - initialises the current terrain altitude and target altitude to pos_offset_terrain_cm327void init_pos_terrain_cm(float pos_offset_terrain_cm);328329// get_pos_terrain_cm - returns the current terrain altitude in cm330float get_pos_terrain_cm() { return _pos_terrain; }331332333/// Offset334335#if AP_SCRIPTING_ENABLED336// position, velocity and acceleration offset target (only used by scripting)337// gets or sets an additional offset to the vehicle's target position, velocity and acceleration338// units are m, m/s and m/s/s in NED frame339bool set_posvelaccel_offset(const Vector3f &pos_offset_NED, const Vector3f &vel_offset_NED, const Vector3f &accel_offset_NED);340bool get_posvelaccel_offset(Vector3f &pos_offset_NED, Vector3f &vel_offset_NED, Vector3f &accel_offset_NED);341342// get target velocity in m/s in NED frame343bool get_vel_target(Vector3f &vel_target_NED);344345// get target acceleration in m/s/s in NED frame346bool get_accel_target(Vector3f &accel_target_NED);347#endif348349/// set the horizontal position, velocity and acceleration offset targets in cm, cms and cm/s/s from EKF origin in NE frame350/// these must be set every 3 seconds (or less) or they will timeout and return to zero351void set_posvelaccel_offset_target_xy_cm(const Vector2p& pos_offset_target_xy_cm, const Vector2f& vel_offset_target_xy_cms, const Vector2f& accel_offset_target_xy_cmss);352void set_posvelaccel_offset_target_z_cm(float pos_offset_target_z_cm, float vel_offset_target_z_cms, float accel_offset_target_z_cmss);353354/// get the position, velocity or acceleration offets in cm from EKF origin in NEU frame355const Vector3p& get_pos_offset_cm() const { return _pos_offset; }356const Vector3f& get_vel_offset_cms() const { return _vel_offset; }357const Vector3f& get_accel_offset_cmss() const { return _accel_offset; }358359/// set_pos_offset_z_cm - set altitude offset in cm above the EKF origin360void set_pos_offset_z_cm(float pos_offset_z) { _pos_offset.z = pos_offset_z; }361362/// get_pos_offset_z_cm - returns altitude offset in cm above the EKF origin363float get_pos_offset_z_cm() const { return _pos_offset.z; }364365/// get_vel_offset_z_cm - returns current vertical offset speed in cm/s366float get_vel_offset_z_cms() const { return _vel_offset.z; }367368/// get_accel_offset_z_cm - returns current vertical offset acceleration in cm/s/s369float get_accel_offset_z_cmss() const { return _accel_offset.z; }370371/// Outputs372373/// get desired roll and pitch to be passed to the attitude controller374float get_roll_cd() const { return _roll_target; }375float get_pitch_cd() const { return _pitch_target; }376377/// get desired yaw to be passed to the attitude controller378float get_yaw_cd() const { return _yaw_target; }379380/// get desired yaw rate to be passed to the attitude controller381float get_yaw_rate_cds() const { return _yaw_rate_target; }382383/// get desired roll and pitch to be passed to the attitude controller384Vector3f get_thrust_vector() const;385386/// get_bearing_to_target_cd - get bearing to target position in centi-degrees387int32_t get_bearing_to_target_cd() const;388389/// get_lean_angle_max_cd - returns the maximum lean angle the autopilot may request390float get_lean_angle_max_cd() const;391392/*393set_lean_angle_max_cd - set the maximum lean angle. A value of zero means to use the ANGLE_MAX parameter.394This is reset to zero on init_xy_controller()395*/396void set_lean_angle_max_cd(float angle_max_cd) { _angle_max_override_cd = angle_max_cd; }397398399/// Other400401/// get pid controllers402AC_P_2D& get_pos_xy_p() { return _p_pos_xy; }403AC_P_1D& get_pos_z_p() { return _p_pos_z; }404AC_PID_2D& get_vel_xy_pid() { return _pid_vel_xy; }405AC_PID_Basic& get_vel_z_pid() { return _pid_vel_z; }406AC_PID& get_accel_z_pid() { return _pid_accel_z; }407408/// set_limit_accel_xy - mark that accel has been limited409/// this prevents integrator buildup410void set_externally_limited_xy() { _limit_vector.x = _accel_target.x; _limit_vector.y = _accel_target.y; }411412// lean_angles_to_accel - convert roll, pitch lean angles to lat/lon frame accelerations in cm/s/s413Vector3f lean_angles_to_accel(const Vector3f& att_target_euler) const;414415// write PSC and/or PSCZ logs416void write_log();417418// provide feedback on whether arming would be a good idea right now:419bool pre_arm_checks(const char *param_prefix,420char *failure_msg,421const uint8_t failure_msg_len);422423// enable or disable high vibration compensation424void set_vibe_comp(bool on_off) { _vibe_comp_enabled = on_off; }425426/// get_vel_z_error_ratio - returns the proportion of error relative to the maximum request427float get_vel_z_control_ratio() const { return constrain_float(_vel_z_control_ratio, 0.0f, 1.0f); }428429/// crosstrack_error - returns horizontal error to the closest point to the current track430float crosstrack_error() const;431432/// standby_xyz_reset - resets I terms and removes position error433/// This function will let Loiter and Alt Hold continue to operate434/// in the event that the flight controller is in control of the435/// aircraft when in standby.436void standby_xyz_reset();437438// get earth-frame Z-axis acceleration with gravity removed in cm/s/s with +ve being up439float get_z_accel_cmss() const { return -(_ahrs.get_accel_ef().z + GRAVITY_MSS) * 100.0f; }440441/// returns true when the forward pitch demand is limited by the maximum allowed tilt442bool get_fwd_pitch_is_limited() const { return _fwd_pitch_is_limited; }443444// set disturbance north445void set_disturb_pos_cm(Vector2f disturb_pos) {_disturb_pos = disturb_pos;}446447// set disturbance north448void set_disturb_vel_cms(Vector2f disturb_vel) {_disturb_vel = disturb_vel;}449450static const struct AP_Param::GroupInfo var_info[];451452static void Write_PSCN(float pos_desired, float pos_target, float pos, float vel_desired, float vel_target, float vel, float accel_desired, float accel_target, float accel);453static void Write_PSCE(float pos_desired, float pos_target, float pos, float vel_desired, float vel_target, float vel, float accel_desired, float accel_target, float accel);454static void Write_PSCD(float pos_desired, float pos_target, float pos, float vel_desired, float vel_target, float vel, float accel_desired, float accel_target, float accel);455static void Write_PSON(float pos_target_offset_cm, float pos_offset_cm, float vel_target_offset_cms, float vel_offset_cms, float accel_target_offset_cmss, float accel_offset_cmss);456static void Write_PSOE(float pos_target_offset_cm, float pos_offset_cm, float vel_target_offset_cms, float vel_offset_cms, float accel_target_offset_cmss, float accel_offset_cmss);457static void Write_PSOD(float pos_target_offset_cm, float pos_offset_cm, float vel_target_offset_cms, float vel_offset_cms, float accel_target_offset_cmss, float accel_offset_cmss);458static void Write_PSOT(float pos_target_offset_cm, float pos_offset_cm, float vel_target_offset_cms, float vel_offset_cms, float accel_target_offset_cmss, float accel_offset_cmss);459460// singleton461static AC_PosControl *get_singleton(void) { return _singleton; }462463protected:464465// get throttle using vibration-resistant calculation (uses feed forward with manually calculated gain)466float get_throttle_with_vibration_override();467468// lean_angles_to_accel - convert roll, pitch lean angles to lat/lon frame accelerations in cm/s/s469void accel_to_lean_angles(float accel_x_cmss, float accel_y_cmss, float& roll_target, float& pitch_target) const;470471// lean_angles_to_accel - convert roll, pitch lean angles to lat/lon frame accelerations in cm/s/s472void lean_angles_to_accel_xy(float& accel_x_cmss, float& accel_y_cmss) const;473474// calculate_yaw_and_rate_yaw - calculate the vehicle yaw and rate of yaw.475void calculate_yaw_and_rate_yaw();476477// calculate_overspeed_gain - calculated increased maximum acceleration and jerk if over speed condition is detected478float calculate_overspeed_gain();479480481/// Terrain Following482483/// set the position, velocity and acceleration offsets in cm, cms and cm/s/s from EKF origin in NE frame484/// this is used to initiate the offsets when initialise the position controller or do an offset reset485/// note that this sets the actual offsets, not the offset targets486void init_terrain();487488/// update_terrain - updates the terrain position, velocity and acceleration estimation489/// this moves the estimated terrain position _pos_terrain towards the target _pos_terrain_target490void update_terrain();491492493/// Offsets494495/// init_offsets - set the position, velocity and acceleration offsets in cm, cms and cm/s/s from EKF origin in NE frame496/// this is used to initiate the offsets when initialise the position controller or do an offset reset497/// note that this sets the actual offsets, not the offset targets498void init_offsets_xy();499void init_offsets_z();500501/// update_offsets - update the position and velocity offsets502/// this moves the offsets (e.g _pos_offset, _vel_offset, _accel_offset) towards the targets (e.g. _pos_offset_target or _vel_offset_target)503void update_offsets_xy();504void update_offsets_z();505506/// initialise and check for ekf position resets507void init_ekf_xy_reset();508void handle_ekf_xy_reset();509void init_ekf_z_reset();510void handle_ekf_z_reset();511512// references to inertial nav and ahrs libraries513AP_AHRS_View& _ahrs;514const AP_InertialNav& _inav;515const class AP_Motors& _motors;516AC_AttitudeControl& _attitude_control;517518// parameters519AP_Float _lean_angle_max; // Maximum autopilot commanded angle (in degrees). Set to zero for Angle Max520AP_Float _shaping_jerk_xy; // Jerk limit of the xy kinematic path generation in m/s^3 used to determine how quickly the aircraft varies the acceleration target521AP_Float _shaping_jerk_z; // Jerk limit of the z kinematic path generation in m/s^3 used to determine how quickly the aircraft varies the acceleration target522AC_P_2D _p_pos_xy; // XY axis position controller to convert distance error to desired velocity523AC_P_1D _p_pos_z; // Z axis position controller to convert altitude error to desired climb rate524AC_PID_2D _pid_vel_xy; // XY axis velocity controller to convert velocity error to desired acceleration525AC_PID_Basic _pid_vel_z; // Z axis velocity controller to convert climb rate error to desired acceleration526AC_PID _pid_accel_z; // Z axis acceleration controller to convert desired acceleration to throttle output527528// internal variables529float _dt; // time difference (in seconds) since the last loop time530uint32_t _last_update_xy_ticks; // ticks of last last update_xy_controller call531uint32_t _last_update_z_ticks; // ticks of last update_z_controller call532float _vel_max_xy_cms; // max horizontal speed in cm/s used for kinematic shaping533float _vel_max_up_cms; // max climb rate in cm/s used for kinematic shaping534float _vel_max_down_cms; // max descent rate in cm/s used for kinematic shaping535float _accel_max_xy_cmss; // max horizontal acceleration in cm/s/s used for kinematic shaping536float _accel_max_z_cmss; // max vertical acceleration in cm/s/s used for kinematic shaping537float _jerk_max_xy_cmsss; // Jerk limit of the xy kinematic path generation in cm/s^3 used to determine how quickly the aircraft varies the acceleration target538float _jerk_max_z_cmsss; // Jerk limit of the z kinematic path generation in cm/s^3 used to determine how quickly the aircraft varies the acceleration target539float _vel_z_control_ratio = 2.0f; // confidence that we have control in the vertical axis540Vector2f _disturb_pos; // position disturbance generated by system ID mode541Vector2f _disturb_vel; // velocity disturbance generated by system ID mode542543// output from controller544float _roll_target; // desired roll angle in centi-degrees calculated by position controller545float _pitch_target; // desired roll pitch in centi-degrees calculated by position controller546float _yaw_target; // desired yaw in centi-degrees calculated by position controller547float _yaw_rate_target; // desired yaw rate in centi-degrees per second calculated by position controller548549// position controller internal variables550Vector3p _pos_desired; // desired location, frame NEU in cm relative to the EKF origin. This is equal to the _pos_target minus offsets551Vector3p _pos_target; // target location, frame NEU in cm relative to the EKF origin. This is equal to the _pos_desired plus offsets552Vector3f _vel_desired; // desired velocity in NEU cm/s553Vector3f _vel_target; // velocity target in NEU cm/s calculated by pos_to_rate step554Vector3f _accel_desired; // desired acceleration in NEU cm/s/s (feed forward)555Vector3f _accel_target; // acceleration target in NEU cm/s/s556Vector3f _limit_vector; // the direction that the position controller is limited, zero when not limited557558bool _fwd_pitch_is_limited; // true when the forward pitch demand is being limited to meet acceleration limits559560// terrain handling variables561float _pos_terrain_target; // position terrain target in cm relative to the EKF origin in NEU frame562float _pos_terrain; // position terrain in cm from the EKF origin in NEU frame. this terrain moves towards _pos_terrain_target563float _vel_terrain; // velocity terrain in NEU cm/s calculated by pos_to_rate step. this terrain moves towards _vel_terrain_target564float _accel_terrain; // acceleration terrain in NEU cm/s/s565566// offset handling variables567Vector3p _pos_offset_target; // position offset target in cm relative to the EKF origin in NEU frame568Vector3p _pos_offset; // position offset in cm from the EKF origin in NEU frame. this offset moves towards _pos_offset_target569Vector3f _vel_offset_target; // velocity offset target in cm/s in NEU frame570Vector3f _vel_offset; // velocity offset in NEU cm/s calculated by pos_to_rate step. this offset moves towards _vel_offset_target571Vector3f _accel_offset_target; // acceleration offset target in cm/s/s in NEU frame572Vector3f _accel_offset; // acceleration offset in NEU cm/s/s573uint32_t _posvelaccel_offset_target_xy_ms; // system time that pos, vel, accel targets were set (used to implement timeouts)574uint32_t _posvelaccel_offset_target_z_ms; // system time that pos, vel, accel targets were set (used to implement timeouts)575576// ekf reset handling577uint32_t _ekf_xy_reset_ms; // system time of last recorded ekf xy position reset578uint32_t _ekf_z_reset_ms; // system time of last recorded ekf altitude reset579580// high vibration handling581bool _vibe_comp_enabled; // true when high vibration compensation is on582583// angle max override, if zero then use ANGLE_MAX parameter584float _angle_max_override_cd;585586// return true if on a real vehicle or SITL with lock-step scheduling587bool has_good_timing(void) const;588589private:590// convenience method for writing out the identical PSCE, PSCN, PSCD - and591// to save bytes592static void Write_PSCx(LogMessages ID, float pos_desired, float pos_target, float pos, float vel_desired, float vel_target, float vel, float accel_desired, float accel_target, float accel);593594// a convenience function for writing out the position controller offsets595static void Write_PSOx(LogMessages id, float pos_target_offset_cm, float pos_offset_cm,596float vel_target_offset_cms, float vel_offset_cms,597float accel_target_offset_cmss, float accel_offset_cmss);598599// singleton600static AC_PosControl *_singleton;601};602603604