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.cpp
Views: 1798
1
/// @file AC_PID_2D.cpp
2
/// @brief Generic PID algorithm
3
4
#include <AP_Math/AP_Math.h>
5
#include "AC_PID_2D.h"
6
7
#define AC_PID_2D_FILT_D_HZ_MIN 0.005f // minimum input filter frequency
8
9
const AP_Param::GroupInfo AC_PID_2D::var_info[] = {
10
// @Param: P
11
// @DisplayName: PID Proportional Gain
12
// @Description: P Gain which produces an output value that is proportional to the current error value
13
AP_GROUPINFO_FLAGS_DEFAULT_POINTER("P", 0, AC_PID_2D, _kp, default_kp),
14
15
// @Param: I
16
// @DisplayName: PID Integral Gain
17
// @Description: I Gain which produces an output that is proportional to both the magnitude and the duration of the error
18
AP_GROUPINFO_FLAGS_DEFAULT_POINTER("I", 1, AC_PID_2D, _ki, default_ki),
19
20
// @Param: IMAX
21
// @DisplayName: PID Integral Maximum
22
// @Description: The maximum/minimum value that the I term can output
23
AP_GROUPINFO_FLAGS_DEFAULT_POINTER("IMAX", 2, AC_PID_2D, _kimax, default_kimax),
24
25
// @Param: FLTE
26
// @DisplayName: PID Input filter frequency in Hz
27
// @Description: Input filter frequency in Hz
28
// @Units: Hz
29
AP_GROUPINFO_FLAGS_DEFAULT_POINTER("FLTE", 3, AC_PID_2D, _filt_E_hz, default_filt_E_hz),
30
31
// @Param: D
32
// @DisplayName: PID Derivative Gain
33
// @Description: D Gain which produces an output that is proportional to the rate of change of the error
34
AP_GROUPINFO_FLAGS_DEFAULT_POINTER("D", 4, AC_PID_2D, _kd, default_kd),
35
36
// @Param: FLTD
37
// @DisplayName: D term filter frequency in Hz
38
// @Description: D term filter frequency in Hz
39
// @Units: Hz
40
AP_GROUPINFO_FLAGS_DEFAULT_POINTER("FLTD", 5, AC_PID_2D, _filt_D_hz, default_filt_D_hz),
41
42
// @Param: FF
43
// @DisplayName: PID Feed Forward Gain
44
// @Description: FF Gain which produces an output that is proportional to the magnitude of the target
45
AP_GROUPINFO_FLAGS_DEFAULT_POINTER("FF", 6, AC_PID_2D, _kff, default_kff),
46
47
AP_GROUPEND
48
};
49
50
// Constructor
51
AC_PID_2D::AC_PID_2D(float initial_kP, float initial_kI, float initial_kD, float initial_kFF, float initial_imax, float initial_filt_E_hz, float initial_filt_D_hz) :
52
default_kp(initial_kP),
53
default_ki(initial_kI),
54
default_kd(initial_kD),
55
default_kff(initial_kFF),
56
default_kimax(initial_imax),
57
default_filt_E_hz(initial_filt_E_hz),
58
default_filt_D_hz(initial_filt_D_hz)
59
{
60
// load parameter values from eeprom
61
AP_Param::setup_object_defaults(this, var_info);
62
63
// reset input filter to first value received
64
_reset_filter = true;
65
}
66
67
// update_all - set target and measured inputs to PID controller and calculate outputs
68
// target and error are filtered
69
// the derivative is then calculated and filtered
70
// the integral is then updated if it does not increase in the direction of the limit vector
71
Vector2f AC_PID_2D::update_all(const Vector2f &target, const Vector2f &measurement, float dt, const Vector2f &limit)
72
{
73
// don't process inf or NaN
74
if (target.is_nan() || target.is_inf() ||
75
measurement.is_nan() || measurement.is_inf()) {
76
return Vector2f{};
77
}
78
79
_target = target;
80
81
// reset input filter to value received
82
if (_reset_filter) {
83
_reset_filter = false;
84
_error = _target - measurement;
85
_derivative.zero();
86
} else {
87
Vector2f error_last{_error};
88
_error += ((_target - measurement) - _error) * get_filt_E_alpha(dt);
89
90
// calculate and filter derivative
91
if (is_positive(dt)) {
92
const Vector2f derivative{(_error - error_last) / dt};
93
_derivative += (derivative - _derivative) * get_filt_D_alpha(dt);
94
}
95
}
96
97
// update I term
98
update_i(dt, limit);
99
100
// calculate slew limit
101
_slew_calc.update(Vector2f{_pid_info_x.P + _pid_info_x.D, _pid_info_y.P + _pid_info_y.D}, dt);
102
_pid_info_x.slew_rate = _pid_info_y.slew_rate = _slew_calc.get_slew_rate();
103
104
_pid_info_x.target = _target.x;
105
_pid_info_x.actual = measurement.x;
106
_pid_info_x.error = _error.x;
107
_pid_info_x.P = _error.x * _kp;
108
_pid_info_x.I = _integrator.x;
109
_pid_info_x.D = _derivative.x * _kd;
110
_pid_info_x.FF = _target.x * _kff;
111
112
_pid_info_y.target = _target.y;
113
_pid_info_y.actual = measurement.y;
114
_pid_info_y.error = _error.y;
115
_pid_info_y.P = _error.y * _kp;
116
_pid_info_y.I = _integrator.y;
117
_pid_info_y.D = _derivative.y * _kd;
118
_pid_info_y.FF = _target.y * _kff;
119
120
return _error * _kp + _integrator + _derivative * _kd + _target * _kff;
121
}
122
123
Vector2f AC_PID_2D::update_all(const Vector3f &target, const Vector3f &measurement, float dt, const Vector3f &limit)
124
{
125
return update_all(Vector2f{target.x, target.y}, Vector2f{measurement.x, measurement.y}, dt, Vector2f{limit.x, limit.y});
126
}
127
128
// update_i - update the integral
129
// If the limit is set the integral is only allowed to reduce in the direction of the limit
130
void AC_PID_2D::update_i(float dt, const Vector2f &limit)
131
{
132
_pid_info_x.limit = false;
133
_pid_info_y.limit = false;
134
135
Vector2f delta_integrator = (_error * _ki) * dt;
136
float integrator_length = _integrator.length();
137
_integrator += delta_integrator;
138
// do not let integrator increase in length if delta_integrator is in the direction of limit
139
if (is_positive(delta_integrator * limit) && _integrator.limit_length(integrator_length)) {
140
_pid_info_x.limit = true;
141
_pid_info_y.limit = true;
142
}
143
144
_integrator.limit_length(_kimax);
145
}
146
147
Vector2f AC_PID_2D::get_p() const
148
{
149
return _error * _kp;
150
}
151
152
const Vector2f& AC_PID_2D::get_i() const
153
{
154
return _integrator;
155
}
156
157
Vector2f AC_PID_2D::get_d() const
158
{
159
return _derivative * _kd;
160
}
161
162
Vector2f AC_PID_2D::get_ff()
163
{
164
_pid_info_x.FF = _target.x * _kff;
165
_pid_info_y.FF = _target.y * _kff;
166
return _target * _kff;
167
}
168
169
void AC_PID_2D::reset_I()
170
{
171
_integrator.zero();
172
}
173
174
// save_gains - save gains to eeprom
175
void AC_PID_2D::save_gains()
176
{
177
_kp.save();
178
_ki.save();
179
_kd.save();
180
_kff.save();
181
_kimax.save();
182
_filt_E_hz.save();
183
_filt_D_hz.save();
184
}
185
186
// get the target filter alpha
187
float AC_PID_2D::get_filt_E_alpha(float dt) const
188
{
189
return calc_lowpass_alpha_dt(dt, _filt_E_hz);
190
}
191
192
// get the derivative filter alpha
193
float AC_PID_2D::get_filt_D_alpha(float dt) const
194
{
195
return calc_lowpass_alpha_dt(dt, _filt_D_hz);
196
}
197
198
void AC_PID_2D::set_integrator(const Vector2f& target, const Vector2f& measurement, const Vector2f& i)
199
{
200
set_integrator(target - measurement, i);
201
}
202
203
void AC_PID_2D::set_integrator(const Vector2f& error, const Vector2f& i)
204
{
205
set_integrator(i - error * _kp);
206
}
207
208
void AC_PID_2D::set_integrator(const Vector2f& i)
209
{
210
_integrator = i;
211
_integrator.limit_length(_kimax);
212
}
213
214
215