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