CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AC_PID/AC_PID_2D.h
Views: 1798
1
#pragma once
2
3
/// @file AC_PID_2D.h
4
/// @brief Generic PID algorithm, with EEPROM-backed storage of constants.
5
6
#include <AP_Common/AP_Common.h>
7
#include <AP_Param/AP_Param.h>
8
#include <stdlib.h>
9
#include <cmath>
10
#include <AC_PID/AP_PIDInfo.h>
11
#include <Filter/SlewCalculator2D.h>
12
13
/// @class AC_PID_2D
14
/// @brief Copter PID control class
15
class AC_PID_2D {
16
public:
17
18
// Constructor for PID
19
AC_PID_2D(float initial_kP, float initial_kI, float initial_kD, float initial_kFF, float initial_imax, float initial_filt_hz, float initial_filt_d_hz);
20
21
CLASS_NO_COPY(AC_PID_2D);
22
23
// update_all - set target and measured inputs to PID controller and calculate outputs
24
// target and error are filtered
25
// the derivative is then calculated and filtered
26
// the integral is then updated if it does not increase in the direction of the limit vector
27
Vector2f update_all(const Vector2f &target, const Vector2f &measurement, float dt, const Vector2f &limit);
28
Vector2f update_all(const Vector3f &target, const Vector3f &measurement, float dt, const Vector3f &limit);
29
30
// update the integral
31
// if the limit flag is set the integral is only allowed to shrink
32
void update_i(float dt, const Vector2f &limit);
33
34
// get results from pid controller
35
Vector2f get_p() const;
36
const Vector2f& get_i() const;
37
Vector2f get_d() const;
38
Vector2f get_ff();
39
const Vector2f& get_error() const { return _error; }
40
41
// reset the integrator
42
void reset_I();
43
44
// reset_filter - input and D term filter will be reset to the next value provided to set_input()
45
void reset_filter() { _reset_filter = true; }
46
47
// save gain to eeprom
48
void save_gains();
49
50
// get accessors
51
AP_Float &kP() { return _kp; }
52
AP_Float &kI() { return _ki; }
53
AP_Float &kD() { return _kd; }
54
AP_Float &ff() { return _kff;}
55
AP_Float &filt_E_hz() { return _filt_E_hz; }
56
AP_Float &filt_D_hz() { return _filt_D_hz; }
57
float imax() const { return _kimax.get(); }
58
float get_filt_E_alpha(float dt) const;
59
float get_filt_D_alpha(float dt) const;
60
61
// set accessors
62
void set_kP(float v) { _kp.set(v); }
63
void set_kI(float v) { _ki.set(v); }
64
void set_kD(float v) { _kd.set(v); }
65
void set_ff(float v) { _kff.set(v); }
66
void set_imax(float v) { _kimax.set(fabsf(v)); }
67
void set_filt_E_hz(float hz) { _filt_E_hz.set(fabsf(hz)); }
68
void set_filt_D_hz(float hz) { _filt_D_hz.set(fabsf(hz)); }
69
70
// integrator setting functions
71
void set_integrator(const Vector2f& target, const Vector2f& measurement, const Vector2f& i);
72
void set_integrator(const Vector2f& error, const Vector2f& i);
73
void set_integrator(const Vector3f& i) { set_integrator(Vector2f{i.x, i.y}); }
74
void set_integrator(const Vector2f& i);
75
76
// return current slew rate of slew limiter. Will return 0 if SMAX is zero
77
float get_slew_rate(void) const { return _slew_calc.get_slew_rate(); }
78
79
const AP_PIDInfo& get_pid_info_x(void) const { return _pid_info_x; }
80
const AP_PIDInfo& get_pid_info_y(void) const { return _pid_info_y; }
81
82
// parameter var table
83
static const struct AP_Param::GroupInfo var_info[];
84
85
protected:
86
87
// parameters
88
AP_Float _kp;
89
AP_Float _ki;
90
AP_Float _kd;
91
AP_Float _kff;
92
AP_Float _kimax;
93
AP_Float _filt_E_hz; // PID error filter frequency in Hz
94
AP_Float _filt_D_hz; // PID derivative filter frequency in Hz
95
96
// internal variables
97
Vector2f _target; // target value to enable filtering
98
Vector2f _error; // error value to enable filtering
99
Vector2f _derivative; // last derivative from low-pass filter
100
Vector2f _integrator; // integrator value
101
bool _reset_filter; // true when input filter should be reset during next call to update_all
102
103
AP_PIDInfo _pid_info_x;
104
AP_PIDInfo _pid_info_y;
105
106
SlewCalculator2D _slew_calc; // 2D slew rate calculator
107
108
private:
109
const float default_kp;
110
const float default_ki;
111
const float default_kd;
112
const float default_kff;
113
const float default_kimax;
114
const float default_filt_E_hz;
115
const float default_filt_D_hz;
116
};
117
118