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_Basic.cpp
Views: 1798
1
/// @file AC_PID_Basic.cpp
2
/// @brief Generic PID algorithm
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: Error filter frequency in 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: D term filter frequency in 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
// update_all - set target and measured inputs to PID controller and calculate outputs
75
// target and error are filtered
76
// the derivative is then calculated and filtered
77
// the integral is then updated based on the setting of the limit flag
78
float AC_PID_Basic::update_all(float target, float measurement, float dt, bool limit_neg, bool limit_pos)
79
{
80
// don't process inf or NaN
81
if (!isfinite(target) || isnan(target) ||
82
!isfinite(measurement) || isnan(measurement)) {
83
INTERNAL_ERROR(AP_InternalError::error_t::invalid_arg_or_result);
84
return 0.0f;
85
}
86
87
_target = target;
88
89
// reset input filter to value received
90
if (_reset_filter) {
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
// calculate and filter derivative
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
// update_i - update the integral
123
// if limit_neg is true, the integral can only increase
124
// if limit_pos is true, the integral can 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
// save_gains - save gains to eeprom
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
// get_filt_T_alpha - get the target filter alpha
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
// get_filt_D_alpha - get the derivative filter alpha
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
void AC_PID_Basic::set_integrator(float target, float measurement, float i)
168
{
169
set_integrator(target - measurement, i);
170
}
171
172
void AC_PID_Basic::set_integrator(float error, float i)
173
{
174
set_integrator(i - error * _kp);
175
}
176
177
void AC_PID_Basic::set_integrator(float i)
178
{
179
_integrator = constrain_float(i, -_kimax, _kimax);
180
}
181
182