Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Ardupilot
GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AC_PID/AC_PID_Basic.cpp
9417 views
1
/// @file AC_PID_Basic.cpp
2
/// @brief Lightweight PID controller with error and derivative filtering, integrator limit, and EEPROM gain storage.
3
4
#include <AP_Math/AP_Math.h>
5
#include <AP_InternalError/AP_InternalError.h>
6
#include "AC_PID_Basic.h"
7
8
#define AC_PID_Basic_FILT_E_HZ_MIN 0.01f // minimum input filter frequency
9
#define AC_PID_Basic_FILT_D_HZ_MIN 0.005f // minimum input filter frequency
10
11
const AP_Param::GroupInfo AC_PID_Basic::var_info[] = {
12
// @Param: P
13
// @DisplayName: PID Proportional Gain
14
// @Description: P Gain which produces an output value that is proportional to the current error value
15
AP_GROUPINFO_FLAGS_DEFAULT_POINTER("P", 0, AC_PID_Basic, _kp, default_kp),
16
17
// @Param: I
18
// @DisplayName: PID Integral Gain
19
// @Description: I Gain which produces an output that is proportional to both the magnitude and the duration of the error
20
AP_GROUPINFO_FLAGS_DEFAULT_POINTER("I", 1, AC_PID_Basic, _ki, default_ki),
21
22
// @Param: IMAX
23
// @DisplayName: PID Integral Maximum
24
// @Description: The maximum/minimum value that the I term can output
25
AP_GROUPINFO_FLAGS_DEFAULT_POINTER("IMAX", 2, AC_PID_Basic, _kimax, default_kimax),
26
27
// @Param: FLTE
28
// @DisplayName: PID Error filter frequency in Hz
29
// @Description: Low-pass filter frequency applied to the error (Hz)
30
// @Units: Hz
31
AP_GROUPINFO_FLAGS_DEFAULT_POINTER("FLTE", 3, AC_PID_Basic, _filt_E_hz, default_filt_E_hz),
32
33
// @Param: D
34
// @DisplayName: PID Derivative Gain
35
// @Description: D Gain which produces an output that is proportional to the rate of change of the error
36
AP_GROUPINFO_FLAGS_DEFAULT_POINTER("D", 4, AC_PID_Basic, _kd, default_kd),
37
38
// @Param: FLTD
39
// @DisplayName: D term filter frequency in Hz
40
// @Description: Low-pass filter frequency applied to the derivative (Hz)
41
// @Units: Hz
42
AP_GROUPINFO_FLAGS_DEFAULT_POINTER("FLTD", 5, AC_PID_Basic, _filt_D_hz, default_filt_D_hz),
43
44
// @Param: FF
45
// @DisplayName: PID Feed Forward Gain
46
// @Description: FF Gain which produces an output that is proportional to the magnitude of the target
47
AP_GROUPINFO_FLAGS_DEFAULT_POINTER("FF", 6, AC_PID_Basic, _kff, default_kff),
48
49
AP_GROUPEND
50
};
51
52
// Constructor
53
AC_PID_Basic::AC_PID_Basic(float initial_p, float initial_i, float initial_d, float initial_ff, float initial_imax, float initial_filt_E_hz, float initial_filt_D_hz) :
54
default_kp(initial_p),
55
default_ki(initial_i),
56
default_kd(initial_d),
57
default_kff(initial_ff),
58
default_kimax(initial_imax),
59
default_filt_E_hz(initial_filt_E_hz),
60
default_filt_D_hz(initial_filt_D_hz)
61
{
62
// load parameter values from eeprom
63
AP_Param::setup_object_defaults(this, var_info);
64
65
// reset input filter to first value received
66
_reset_filter = true;
67
}
68
69
float AC_PID_Basic::update_all(float target, float measurement, float dt, bool limit)
70
{
71
return update_all(target, measurement, dt, (limit && is_negative(_integrator)), (limit && is_positive(_integrator)));
72
}
73
74
// Computes the PID output using a target and measurement input.
75
// Applies filters to the error and derivative, then updates the integrator.
76
// If `limit` is true, the integrator is allowed to shrink but not grow.
77
float AC_PID_Basic::update_all(float target, float measurement, float dt, bool limit_neg, bool limit_pos)
78
{
79
// Return zero if inputs are invalid (NaN or infinite)
80
if (!isfinite(target) || isnan(target) ||
81
!isfinite(measurement) || isnan(measurement)) {
82
INTERNAL_ERROR(AP_InternalError::error_t::invalid_arg_or_result);
83
return 0.0f;
84
}
85
86
_target = target;
87
88
// Reset filter state to match current inputs (on first run or after reset)
89
if (_reset_filter) {
90
// Reset filters to match the current inputs
91
_reset_filter = false;
92
_error = _target - measurement;
93
_derivative = 0.0f;
94
} else {
95
float error_last = _error;
96
_error += get_filt_E_alpha(dt) * ((_target - measurement) - _error);
97
98
// Compute and low-pass filter the error derivative (D term)
99
if (is_positive(dt)) {
100
float derivative = (_error - error_last) / dt;
101
_derivative += get_filt_D_alpha(dt) * (derivative - _derivative);
102
}
103
}
104
105
// update I term
106
update_i(dt, limit_neg, limit_pos);
107
108
const float P_out = _error * _kp;
109
const float D_out = _derivative * _kd;
110
111
_pid_info.target = _target;
112
_pid_info.actual = measurement;
113
_pid_info.error = _error;
114
_pid_info.P = _error * _kp;
115
_pid_info.I = _integrator;
116
_pid_info.D = _derivative * _kd;
117
_pid_info.FF = _target * _kff;
118
119
return P_out + _integrator + D_out + _target * _kff;
120
}
121
122
// Updates the integrator using current error and dt.
123
// If `limit_neg` is true, integrator may only increase.
124
// If `limit_pos` is true, integrator may only decrease.
125
void AC_PID_Basic::update_i(float dt, bool limit_neg, bool limit_pos)
126
{
127
if (!is_zero(_ki)) {
128
// Ensure that integrator can only be reduced if the output is saturated
129
if (!((limit_neg && is_negative(_error)) || (limit_pos && is_positive(_error)))) {
130
_integrator += ((float)_error * _ki) * dt;
131
_integrator = constrain_float(_integrator, -_kimax, _kimax);
132
}
133
} else {
134
_integrator = 0.0f;
135
}
136
}
137
138
void AC_PID_Basic::reset_I()
139
{
140
_integrator = 0.0;
141
}
142
143
// Saves controller configuration from EEPROM, including gains and filter frequencies. (not used)
144
void AC_PID_Basic::save_gains()
145
{
146
_kp.save();
147
_ki.save();
148
_kd.save();
149
_kff.save();
150
_kimax.save();
151
_filt_E_hz.save();
152
_filt_D_hz.save();
153
}
154
155
// Returns alpha value for the error low-pass filter (based on filter frequency and dt)
156
float AC_PID_Basic::get_filt_E_alpha(float dt) const
157
{
158
return calc_lowpass_alpha_dt(dt, _filt_E_hz);
159
}
160
161
// Returns alpha value for the derivative low-pass filter (based on filter frequency and dt)
162
float AC_PID_Basic::get_filt_D_alpha(float dt) const
163
{
164
return calc_lowpass_alpha_dt(dt, _filt_D_hz);
165
}
166
167
// Sets integrator based on target, measurement, and desired total PID output.
168
void AC_PID_Basic::set_integrator(float target, float measurement, float i)
169
{
170
set_integrator(target - measurement, i);
171
}
172
173
// Sets integrator using error and desired total output.
174
void AC_PID_Basic::set_integrator(float error, float i)
175
{
176
set_integrator(i - error * _kp);
177
}
178
179
// Sets the integrator directly.
180
void AC_PID_Basic::set_integrator(float i)
181
{
182
_integrator = constrain_float(i, -_kimax, _kimax);
183
}
184
185