APM:Libraries
AC_PosControl.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <AP_Common/AP_Common.h>
4 #include <AP_Param/AP_Param.h>
5 #include <AP_Math/AP_Math.h>
6 #include <AC_PID/AC_P.h> // P library
7 #include <AC_PID/AC_PID.h> // PID library
8 #include <AC_PID/AC_PI_2D.h> // PI library (2-axis)
9 #include <AC_PID/AC_PID_2D.h> // PID library (2-axis)
10 #include <AP_InertialNav/AP_InertialNav.h> // Inertial Navigation library
11 #include "AC_AttitudeControl.h" // Attitude control library
12 #include <AP_Motors/AP_Motors.h> // motors library
13 #include <AP_Vehicle/AP_Vehicle.h> // common vehicle parameters
14 
15 
16 // position controller default definitions
17 #define POSCONTROL_ACCELERATION_MIN 50.0f // minimum horizontal acceleration in cm/s/s - used for sanity checking acceleration in leash length calculation
18 #define POSCONTROL_ACCEL_XY 100.0f // default horizontal acceleration in cm/s/s. This is overwritten by waypoint and loiter controllers
19 #define POSCONTROL_ACCEL_XY_MAX 980.0f // max horizontal acceleration in cm/s/s that the position velocity controller will ask from the lower accel controller
20 #define POSCONTROL_STOPPING_DIST_UP_MAX 300.0f // max stopping distance (in cm) vertically while climbing
21 #define POSCONTROL_STOPPING_DIST_DOWN_MAX 200.0f // max stopping distance (in cm) vertically while descending
22 
23 #define POSCONTROL_SPEED 500.0f // default horizontal speed in cm/s
24 #define POSCONTROL_SPEED_DOWN -150.0f // default descent rate in cm/s
25 #define POSCONTROL_SPEED_UP 250.0f // default climb rate in cm/s
26 
27 #define POSCONTROL_ACCEL_Z 250.0f // default vertical acceleration in cm/s/s.
28 
29 #define POSCONTROL_LEASH_LENGTH_MIN 100.0f // minimum leash lengths in cm
30 
31 #define POSCONTROL_DT_50HZ 0.02f // time difference in seconds for 50hz update rate
32 #define POSCONTROL_DT_400HZ 0.0025f // time difference in seconds for 400hz update rate
33 
34 #define POSCONTROL_ACTIVE_TIMEOUT_MS 200 // position controller is considered active if it has been called within the past 0.2 seconds
35 
36 #define POSCONTROL_VEL_ERROR_CUTOFF_FREQ 4.0f // low-pass filter on velocity error (unit: hz)
37 #define POSCONTROL_THROTTLE_CUTOFF_FREQ 2.0f // low-pass filter on accel error (unit: hz)
38 #define POSCONTROL_ACCEL_FILTER_HZ 2.0f // low-pass filter on acceleration (unit: hz)
39 #define POSCONTROL_JERK_RATIO 1.0f // Defines the time it takes to reach the requested acceleration
40 
41 #define POSCONTROL_OVERSPEED_GAIN_Z 2.0f // gain controlling rate at which z-axis speed is brought back within SPEED_UP and SPEED_DOWN range
42 
44 {
45 public:
46 
48  AC_PosControl(const AP_AHRS_View& ahrs, const AP_InertialNav& inav,
49  const AP_Motors& motors, AC_AttitudeControl& attitude_control);
50 
54 
57  void set_dt(float delta_sec);
58  float get_dt() const { return _dt; }
59 
63 
67  void set_speed_z(float speed_down, float speed_up);
68 
70  float get_speed_up() const { return _speed_up_cms; }
71 
73  float get_speed_down() const { return _speed_down_cms; }
74 
76  float get_vel_target_z() const { return _vel_target.z; }
77 
80  void set_accel_z(float accel_cmss);
81 
83  float get_accel_z() const { return _accel_z_cms; }
84 
87  void calc_leash_length_z();
88 
90  void set_alt_target(float alt_cm) { _pos_target.z = alt_cm; }
91 
96  void set_alt_target_with_slew(float alt_cm, float dt);
97 
103  virtual void set_alt_target_from_climb_rate(float climb_rate_cms, float dt, bool force_descend);
104 
110  virtual void set_alt_target_from_climb_rate_ff(float climb_rate_cms, float dt, bool force_descend);
111 
115  void add_takeoff_climb_rate(float climb_rate_cms, float dt);
116 
119 
121  void shift_alt_target(float z_cm);
122 
124  void relax_alt_hold_controllers(float throttle_setting);
125 
128  float get_alt_target() const { return _pos_target.z; }
129 
131  float get_alt_error() const;
132 
133  // returns horizontal error in cm
134  float get_horizontal_error() const;
135 
138 
140  void get_stopping_point_z(Vector3f& stopping_point) const;
141 
143  void init_takeoff();
144 
145  // is_active - returns true if the z-axis position controller has been run very recently
146  bool is_active_z() const;
147 
149  void update_z_controller();
150 
151  // get_leash_down_z, get_leash_up_z - returns vertical leash lengths in cm
152  float get_leash_down_z() const { return _leash_down_z; }
153  float get_leash_up_z() const { return _leash_up_z; }
154 
158 
160  float get_lean_angle_max_cd() const;
161 
166  void init_xy_controller(bool reset_I = true);
167 
170  void set_accel_xy(float accel_cmss);
171  float get_accel_xy() const { return _accel_cms; }
172 
175  void set_speed_xy(float speed_cms);
176  float get_speed_xy() const { return _speed_cms; }
177 
180  void set_limit_accel_xy(void) { _limit.accel_xy = true; }
181 
184  void calc_leash_length_xy();
185 
187  void set_leash_length_xy(float leash) { _leash = leash; _flags.recalc_leash_xy = false; }
188 
190  const Vector3f& get_pos_target() const { return _pos_target; }
191 
193  void set_pos_target(const Vector3f& position);
194 
196  void set_xy_target(float x, float y);
197 
199  void shift_pos_xy_target(float x_cm, float y_cm);
200 
203 
205  void set_desired_velocity_z(float vel_z_cms) {_vel_desired.z = vel_z_cms;}
206 
207  // clear desired velocity feed-forward in z axis
209 
210  // set desired acceleration in cm/s in xy axis
211  void set_desired_accel_xy(float accel_lat_cms, float accel_lon_cms) { _accel_desired.x = accel_lat_cms; _accel_desired.y = accel_lon_cms; }
212 
216  void set_desired_velocity_xy(float vel_lat_cms, float vel_lon_cms) {_vel_desired.x = vel_lat_cms; _vel_desired.y = vel_lon_cms; }
217 
220  void set_desired_velocity(const Vector3f &des_vel) { _vel_desired = des_vel; }
221 
222  // overrides the velocity process variable for one timestep
224 
226  void freeze_ff_z() { _flags.freeze_ff_z = true; }
227 
228  // is_active_xy - returns true if the xy position controller has been run very recently
229  bool is_active_xy() const;
230 
233  void update_xy_controller(float ekfNavVelGainScaler);
234 
237 
243  void get_stopping_point_xy(Vector3f &stopping_point) const;
244 
246  float get_distance_to_target() const;
247 
249  int32_t get_bearing_to_target() const;
250 
252 
255 
260  void update_vel_controller_xy(float ekfNavVelGainScaler);
261 
266  void update_vel_controller_xyz(float ekfNavVelGainScaler);
267 
269  float get_roll() const { return _roll_target; }
270  float get_pitch() const { return _pitch_target; }
271 
272  // get_leash_xy - returns horizontal leash length in cm
273  float get_leash_xy() const { return _leash; }
274 
276  AC_P& get_pos_z_p() { return _p_pos_z; }
277  AC_P& get_vel_z_p() { return _p_vel_z; }
279  AC_P& get_pos_xy_p() { return _p_pos_xy; }
281 
283  const Vector3f& get_vel_target() const { return _vel_target; }
284  const Vector3f& get_accel_target() const { return _accel_target; }
285 
286  // lean_angles_to_accel - convert roll, pitch lean angles to lat/lon frame accelerations in cm/s/s
287  void accel_to_lean_angles(float accel_x_cmss, float accel_y_cmss, float& roll_target, float& pitch_target) const;
288 
289  // lean_angles_to_accel - convert roll, pitch lean angles to lat/lon frame accelerations in cm/s/s
290  void lean_angles_to_accel(float& accel_x_cmss, float& accel_y_cmss) const;
291 
292  // time_since_last_xy_update - returns time in seconds since the horizontal position controller was last run
293  float time_since_last_xy_update() const;
294 
295  // write log to dataflash
296  void write_log();
297 
298  static const struct AP_Param::GroupInfo var_info[];
299 
300 protected:
301 
302  // general purpose flags
304  uint16_t recalc_leash_z : 1; // 1 if we should recalculate the z axis leash length
305  uint16_t recalc_leash_xy : 1; // 1 if we should recalculate the xy axis leash length
306  uint16_t reset_desired_vel_to_pos : 1; // 1 if we should reset the rate_to_accel_xy step
307  uint16_t reset_accel_to_lean_xy : 1; // 1 if we should reset the accel to lean angle step
308  uint16_t reset_rate_to_accel_z : 1; // 1 if we should reset the rate_to_accel_z step
309  uint16_t reset_accel_to_throttle : 1; // 1 if we should reset the accel_to_throttle step of the z-axis controller
310  uint16_t freeze_ff_z : 1; // 1 used to freeze velocity to accel feed forward for one iteration
311  uint16_t use_desvel_ff_z : 1; // 1 to use z-axis desired velocity as feed forward into velocity step
312  uint16_t vehicle_horiz_vel_override : 1; // 1 if we should use _vehicle_horiz_vel as our velocity process variable for one timestep
313  } _flags;
314 
315  // limit flags structure
317  uint8_t pos_up : 1; // 1 if we have hit the vertical position leash limit while going up
318  uint8_t pos_down : 1; // 1 if we have hit the vertical position leash limit while going down
319  uint8_t vel_up : 1; // 1 if we have hit the vertical velocity limit going up
320  uint8_t vel_down : 1; // 1 if we have hit the vertical velocity limit going down
321  uint8_t accel_xy : 1; // 1 if we have hit the horizontal accel limit
322  } _limit;
323 
327 
328  // run position control for Z axis
329  // target altitude should be set with one of these functions
330  // set_alt_target
331  // set_target_to_stopping_point_z
332  // init_takeoff
333  void run_z_controller();
334 
338 
340  void desired_accel_to_vel(float nav_dt);
341 
343  void desired_vel_to_pos(float nav_dt);
344 
350  void run_xy_controller(float dt, float ekfNavVelGainScaler);
351 
353  float calc_leash_length(float speed_cms, float accel_cms, float kP) const;
354 
356  static bool limit_vector_length(float& vector_x, float& vector_y, float max_length);
357 
359  static Vector3f sqrt_controller(const Vector3f& error, float p, float second_ord_lim);
360 
362  void init_ekf_xy_reset();
363  void check_for_ekf_xy_reset();
364  void init_ekf_z_reset();
365  void check_for_ekf_z_reset();
366 
367  // references to inertial nav and ahrs libraries
372 
373  // parameters
374  AP_Float _accel_xy_filt_hz; // XY acceleration filter cutoff frequency
375  AP_Float _lean_angle_max; // Maximum autopilot commanded angle (in degrees). Set to zero for Angle Max
381 
382  // internal variables
383  float _dt; // time difference (in seconds) between calls from the main program
384  uint32_t _last_update_xy_ms; // system time of last update_xy_controller call
385  uint32_t _last_update_z_ms; // system time of last update_z_controller call
386  float _speed_down_cms; // max descent rate in cm/s
387  float _speed_up_cms; // max climb rate in cm/s
388  float _speed_cms; // max horizontal speed in cm/s
389  float _accel_z_cms; // max vertical acceleration in cm/s/s
390  float _accel_last_z_cms; // max vertical acceleration in cm/s/s
391  float _accel_cms; // max horizontal acceleration in cm/s/s
392  float _leash; // horizontal leash length in cm. target will never be further than this distance from the vehicle
393  float _leash_down_z; // vertical leash down in cm. target will never be further than this distance below the vehicle
394  float _leash_up_z; // vertical leash up in cm. target will never be further than this distance above the vehicle
395 
396  // output from controller
397  float _roll_target; // desired roll angle in centi-degrees calculated by position controller
398  float _pitch_target; // desired roll pitch in centi-degrees calculated by position controller
399 
400  // position controller internal variables
401  Vector3f _pos_target; // target location in cm from home
402  Vector3f _pos_error; // error between desired and actual position in cm
403  Vector3f _vel_desired; // desired velocity in cm/s
404  Vector3f _vel_target; // velocity target in cm/s calculated by pos_to_rate step
405  Vector3f _vel_error; // error between desired and actual acceleration in cm/s
406  Vector3f _vel_last; // previous iterations velocity in cm/s
407  Vector3f _accel_desired; // desired acceleration in cm/s/s (feed forward)
408  Vector3f _accel_target; // acceleration target in cm/s/s
409  Vector3f _accel_error; // acceleration error in cm/s/s
410  Vector2f _vehicle_horiz_vel; // velocity to use if _flags.vehicle_horiz_vel_override is set
411  float _distance_to_target; // distance to position target - for reporting only
412  LowPassFilterFloat _vel_error_filter; // low-pass-filter on z-axis velocity error
413 
414  LowPassFilterVector2f _accel_target_filter; // acceleration target filter
415 
416  // ekf reset handling
417  uint32_t _ekf_xy_reset_ms; // system time of last recorded ekf xy position reset
418  uint32_t _ekf_z_reset_ms; // system time of last recorded ekf altitude reset
419 };
Generic PID algorithm, with EEPROM-backed storage of constants.
float get_speed_xy() const
void get_stopping_point_xy(Vector3f &stopping_point) const
AC_P & get_vel_z_p()
uint32_t _last_update_z_ms
void init_takeoff()
init_takeoff - initialises target altitude if we are taking off
float get_alt_target() const
void run_z_controller()
const AP_Motors & _motors
void set_desired_velocity_xy(float vel_lat_cms, float vel_lon_cms)
float get_lean_angle_max_cd() const
get_lean_angle_max_cd - returns the maximum lean angle the autopilot may request
void init_ekf_xy_reset()
initialise and check for ekf position resets
void shift_pos_xy_target(float x_cm, float y_cm)
shift position target target in x, y axis
Generic PID algorithm, with EEPROM-backed storage of constants.
void set_alt_target_with_slew(float alt_cm, float dt)
const Vector3f & get_accel_target() const
float _distance_to_target
AP_AHRS_NavEKF & ahrs
Definition: AHRS_Test.cpp:39
const Vector3f & get_vel_target() const
accessors for reporting
static const struct AP_Param::GroupInfo var_info[]
float get_distance_to_target() const
get_distance_to_target - get horizontal distance to position target in cm (used for reporting) ...
virtual void set_alt_target_from_climb_rate(float climb_rate_cms, float dt, bool force_descend)
AC_P & get_pos_xy_p()
float get_alt_error() const
get_alt_error - returns altitude error in cm
float get_pitch() const
void get_stopping_point_z(Vector3f &stopping_point) const
get_stopping_point_z - calculates stopping point based on current position, velocity, vehicle acceleration
const AP_AHRS_View & _ahrs
void clear_desired_velocity_ff_z()
void update_z_controller()
update_z_controller - fly to altitude in cm above home
void set_desired_accel_xy(float accel_lat_cms, float accel_lon_cms)
void init_vel_controller_xyz()
xyz velocity controller
void run_xy_controller(float dt, float ekfNavVelGainScaler)
AC_PosControl(const AP_AHRS_View &ahrs, const AP_InertialNav &inav, const AP_Motors &motors, AC_AttitudeControl &attitude_control)
Constructor.
float get_leash_up_z() const
void update_xy_controller(float ekfNavVelGainScaler)
update_xy_controller - run the horizontal position controller - should be called at 100hz or higher ...
void set_limit_accel_xy(void)
float get_horizontal_error() const
Vector3f _pos_error
static Vector3f sqrt_controller(const Vector3f &error, float p, float second_ord_lim)
Proportional controller with piecewise sqrt sections to constrain second derivative.
LowPassFilterVector2f _accel_target_filter
void override_vehicle_velocity_xy(const Vector2f &vel_xy)
void set_leash_length_xy(float leash)
set the horizontal leash length
Vector3f _vel_desired
float _speed_down_cms
void update_vel_controller_xy(float ekfNavVelGainScaler)
Generic PID algorithm, with EEPROM-backed storage of constants.
uint32_t _ekf_z_reset_ms
void set_xy_target(float x, float y)
set_xy_target in cm from home
AP_MotorsMatrix motors(400)
Vector3f _pos_target
float get_roll() const
get desired roll, pitch which should be fed into stabilize controllers
void add_takeoff_climb_rate(float climb_rate_cms, float dt)
void update_vel_controller_xyz(float ekfNavVelGainScaler)
Object managing one P controller.
Definition: AC_P.h:13
bool is_active_xy() const
A system for managing and storing variables that are of general interest to the system.
void desired_vel_to_pos(float nav_dt)
desired_vel_to_pos - move position target using desired velocities
float get_accel_xy() const
#define x(i)
bool is_active_z() const
float get_speed_up() const
get_speed_up - accessor for current up speed in cm/s
Definition: AC_PosControl.h:70
void check_for_ekf_xy_reset()
check for ekf position reset and adjust loiter or brake target position
void relax_alt_hold_controllers(float throttle_setting)
relax_alt_hold_controllers - set all desired and targets to measured
ArduCopter attitude control library.
void lean_angles_to_accel(float &accel_x_cmss, float &accel_y_cmss) const
const Vector3f & get_pos_target() const
get_pos_target - get target as position vector (from home in cm)
struct AC_PosControl::poscontrol_limit_flags _limit
void accel_to_lean_angles(float accel_x_cmss, float accel_y_cmss, float &roll_target, float &pitch_target) const
AC_AttitudeControl & _attitude_control
AP_Float _accel_xy_filt_hz
void init_xy_controller(bool reset_I=true)
void set_desired_velocity_z(float vel_z_cms)
set_desired_velocity_z - sets desired velocity in cm/s in z axis
T y
Definition: vector3.h:67
float get_leash_down_z() const
void set_speed_z(float speed_down, float speed_up)
struct AC_PosControl::poscontrol_flags _flags
void calc_leash_length_z()
T z
Definition: vector3.h:67
void set_desired_velocity(const Vector3f &des_vel)
void set_dt(float delta_sec)
set_dt - sets time delta in seconds for all controllers (i.e. 100hz = 0.01, 400hz = 0...
AC_PID_2D & get_vel_xy_pid()
AP_Float _lean_angle_max
void calc_leash_length_xy()
float get_accel_z() const
get_accel_z - returns current vertical acceleration in cm/s/s
Definition: AC_PosControl.h:83
void set_accel_z(float accel_cmss)
set_accel_z - set vertical acceleration in cm/s/s
void init_ekf_z_reset()
initialise ekf z axis reset check
AC_P & get_pos_z_p()
get pid controllers
float get_speed_down() const
get_speed_down - accessors for current down speed in cm/s. Will be a negative number ...
Definition: AC_PosControl.h:73
Vector3f _vel_error
virtual void set_alt_target_from_climb_rate_ff(float climb_rate_cms, float dt, bool force_descend)
void set_pos_target(const Vector3f &position)
set_pos_target in cm from home
const AP_InertialNav & _inav
Vector3f _accel_error
Vector2f _vehicle_horiz_vel
Common definitions and utility routines for the ArduPilot libraries.
float get_dt() const
Definition: AC_PosControl.h:58
int32_t get_bearing_to_target() const
get_bearing_to_target - get bearing to target position in centi-degrees
void freeze_ff_z()
freeze_ff_z - used to stop the feed forward being calculated during a known discontinuity ...
const Vector3f & get_desired_velocity()
get_desired_velocity - returns xy desired velocity (i.e. feed forward) in cm/s in lat and lon directi...
static bool limit_vector_length(float &vector_x, float &vector_y, float max_length)
limit vector to a given length, returns true if vector was limited
void shift_alt_target(float z_cm)
shift altitude target (positive means move altitude up)
Copter PID control class.
Definition: AC_PID_2D.h:13
Vector3f _accel_target
void set_target_to_stopping_point_xy()
set_target_to_stopping_point_xy - sets horizontal target to reasonable stopping position in cm from h...
void set_alt_target(float alt_cm)
set_alt_target - set altitude target in cm above home
Definition: AC_PosControl.h:90
void check_for_ekf_z_reset()
check for ekf position reset and adjust loiter or brake target position
void set_accel_xy(float accel_cmss)
set_accel_xy - set horizontal acceleration in cm/s/s
uint32_t _last_update_xy_ms
#define error(fmt, args ...)
AC_PID_2D _pid_vel_xy
void set_target_to_stopping_point_z()
set_target_to_stopping_point_z - sets altitude target to reasonable stopping altitude in cm above hom...
float time_since_last_xy_update() const
Vector3f _vel_last
void desired_accel_to_vel(float nav_dt)
move velocity target using desired acceleration
Copter PID control class.
Definition: AC_PID.h:17
AC_PID & get_accel_z_pid()
Vector3f _accel_desired
float get_leash_xy() const
float get_vel_target_z() const
get_vel_target_z - returns current vertical speed in cm/s
Definition: AC_PosControl.h:76
virtual float get_altitude() const =0
float _accel_last_z_cms
void set_speed_xy(float speed_cms)
set_speed_xy - set horizontal speed maximum in cm/s
LowPassFilterFloat _vel_error_filter
AC_PID _pid_accel_z
T x
Definition: vector3.h:67
uint32_t _ekf_xy_reset_ms
Vector3f _vel_target
float calc_leash_length(float speed_cms, float accel_cms, float kP) const
calc_leash_length - calculates the horizontal leash length given a maximum speed, acceleration and po...
void set_alt_target_to_current_alt()
set_alt_target_to_current_alt - set altitude target to current altitude