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.h
Views: 1798
1
#pragma once
2
3
/// @file AC_PID.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 <Filter/SlewLimiter.h>
11
#include <Filter/NotchFilter.h>
12
#include <Filter/AP_Filter.h>
13
14
#define AC_PID_TFILT_HZ_DEFAULT 0.0f // default input filter frequency
15
#define AC_PID_EFILT_HZ_DEFAULT 0.0f // default input filter frequency
16
#define AC_PID_DFILT_HZ_DEFAULT 20.0f // default input filter frequency
17
#define AC_PID_RESET_TC 0.16f // Time constant for integrator reset decay to zero
18
19
#include "AP_PIDInfo.h"
20
21
/// @class AC_PID
22
/// @brief Copter PID control class
23
class AC_PID {
24
public:
25
26
struct Defaults {
27
float p;
28
float i;
29
float d;
30
float ff;
31
float imax;
32
float filt_T_hz;
33
float filt_E_hz;
34
float filt_D_hz;
35
float srmax;
36
float srtau;
37
float dff;
38
};
39
40
// Constructor for PID
41
AC_PID(float initial_p, float initial_i, float initial_d, float initial_ff, float initial_imax, float initial_filt_T_hz, float initial_filt_E_hz, float initial_filt_D_hz,
42
float initial_srmax=0, float initial_srtau=1.0, float initial_dff=0);
43
AC_PID(const AC_PID::Defaults &defaults) :
44
AC_PID(
45
defaults.p,
46
defaults.i,
47
defaults.d,
48
defaults.ff,
49
defaults.imax,
50
defaults.filt_T_hz,
51
defaults.filt_E_hz,
52
defaults.filt_D_hz,
53
defaults.srmax,
54
defaults.srtau,
55
defaults.dff
56
)
57
{ }
58
59
CLASS_NO_COPY(AC_PID);
60
61
// update_all - set target and measured inputs to PID controller and calculate outputs
62
// target and error are filtered
63
// the derivative is then calculated and filtered
64
// the integral is then updated based on the setting of the limit flag
65
float update_all(float target, float measurement, float dt, bool limit = false, float boost = 1.0f);
66
67
// update_error - set error input to PID controller and calculate outputs
68
// target is set to zero and error is set and filtered
69
// the derivative then is calculated and filtered
70
// the integral is then updated based on the setting of the limit flag
71
// Target and Measured must be set manually for logging purposes.
72
// todo: remove function when it is no longer used.
73
float update_error(float error, float dt, bool limit = false);
74
75
// get_pid - get results from pid controller
76
float get_p() const;
77
float get_i() const;
78
float get_d() const;
79
float get_ff() const;
80
81
// reset_I - reset the integrator
82
void reset_I();
83
84
// reset_filter - input filter will be reset to the next value provided to set_input()
85
void reset_filter() {
86
_flags._reset_filter = true;
87
}
88
89
// load gain from eeprom
90
void load_gains();
91
92
// save gain to eeprom
93
void save_gains();
94
95
// get accessors
96
const AP_Float &kP() const { return _kp; }
97
AP_Float &kP() { return _kp; }
98
AP_Float &kI() { return _ki; }
99
AP_Float &kD() { return _kd; }
100
AP_Float &kIMAX() { return _kimax; }
101
AP_Float &kPDMAX() { return _kpdmax; }
102
AP_Float &ff() { return _kff;}
103
AP_Float &filt_T_hz() { return _filt_T_hz; }
104
AP_Float &filt_E_hz() { return _filt_E_hz; }
105
AP_Float &filt_D_hz() { return _filt_D_hz; }
106
AP_Float &slew_limit() { return _slew_rate_max; }
107
AP_Float &kDff() { return _kdff; }
108
109
float imax() const { return _kimax.get(); }
110
float pdmax() const { return _kpdmax.get(); }
111
112
float get_filt_T_alpha(float dt) const;
113
float get_filt_E_alpha(float dt) const;
114
float get_filt_D_alpha(float dt) const;
115
116
// set accessors
117
void set_kP(const float v) { _kp.set(v); }
118
void set_kI(const float v) { _ki.set(v); }
119
void set_kD(const float v) { _kd.set(v); }
120
void set_ff(const float v) { _kff.set(v); }
121
void set_imax(const float v) { _kimax.set(fabsf(v)); }
122
void set_pdmax(const float v) { _kpdmax.set(fabsf(v)); }
123
void set_filt_T_hz(const float v);
124
void set_filt_E_hz(const float v);
125
void set_filt_D_hz(const float v);
126
void set_slew_limit(const float v);
127
void set_kDff(const float v) { _kdff.set(v); }
128
129
// set the desired and actual rates (for logging purposes)
130
void set_target_rate(float target) { _pid_info.target = target; }
131
void set_actual_rate(float actual) { _pid_info.actual = actual; }
132
133
// integrator setting functions
134
void set_integrator(float i);
135
void relax_integrator(float integrator, float dt, float time_constant);
136
137
// set slew limiter scale factor
138
void set_slew_limit_scale(int8_t scale) { _slew_limit_scale = scale; }
139
140
// return current slew rate of slew limiter. Will return 0 if SMAX is zero
141
float get_slew_rate(void) const { return _slew_limiter.get_slew_rate(); }
142
143
const AP_PIDInfo& get_pid_info(void) const { return _pid_info; }
144
145
void set_notch_sample_rate(float);
146
147
// parameter var table
148
static const struct AP_Param::GroupInfo var_info[];
149
150
protected:
151
152
// update_i - update the integral
153
// if the limit flag is set the integral is only allowed to shrink
154
void update_i(float dt, bool limit);
155
156
// parameters
157
AP_Float _kp;
158
AP_Float _ki;
159
AP_Float _kd;
160
AP_Float _kff;
161
AP_Float _kimax;
162
AP_Float _kpdmax;
163
AP_Float _filt_T_hz; // PID target filter frequency in Hz
164
AP_Float _filt_E_hz; // PID error filter frequency in Hz
165
AP_Float _filt_D_hz; // PID derivative filter frequency in Hz
166
AP_Float _slew_rate_max;
167
AP_Float _kdff;
168
#if AP_FILTER_ENABLED
169
AP_Int8 _notch_T_filter;
170
AP_Int8 _notch_E_filter;
171
#endif
172
173
// the time constant tau is not currently configurable, but is set
174
// as an AP_Float to make it easy to make it configurable for a
175
// single user of AC_PID by adding the parameter in the param
176
// table of the parent class. It is made public for this reason
177
AP_Float _slew_rate_tau;
178
179
SlewLimiter _slew_limiter{_slew_rate_max, _slew_rate_tau};
180
181
// flags
182
struct ac_pid_flags {
183
bool _reset_filter :1; // true when input filter should be reset during next call to set_input
184
bool _I_set :1; // true if if the I terms has been set externally including zeroing
185
} _flags;
186
187
// internal variables
188
float _integrator; // integrator value
189
float _target; // target value to enable filtering
190
float _error; // error value to enable filtering
191
float _derivative; // derivative value to enable filtering
192
int8_t _slew_limit_scale;
193
float _target_derivative; // target derivative value to enable dff
194
#if AP_FILTER_ENABLED
195
NotchFilterFloat* _target_notch;
196
NotchFilterFloat* _error_notch;
197
#endif
198
199
AP_PIDInfo _pid_info;
200
201
private:
202
const float default_kp;
203
const float default_ki;
204
const float default_kd;
205
const float default_kff;
206
const float default_kdff;
207
const float default_kimax;
208
const float default_filt_T_hz;
209
const float default_filt_E_hz;
210
const float default_filt_D_hz;
211
const float default_slew_rate_max;
212
};
213
214