Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Ardupilot
GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/APM_Control/AP_RollController.cpp
9325 views
1
/*
2
This program is free software: you can redistribute it and/or modify
3
it under the terms of the GNU General Public License as published by
4
the Free Software Foundation, either version 3 of the License, or
5
(at your option) any later version.
6
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
11
12
You should have received a copy of the GNU General Public License
13
along with this program. If not, see <http://www.gnu.org/licenses/>.
14
*/
15
16
// Code by Jon Challinger
17
// Modified by Paul Riseborough
18
//
19
20
#include <AP_HAL/AP_HAL.h>
21
#include "AP_RollController.h"
22
#include <AP_AHRS/AP_AHRS.h>
23
#include <AP_Scheduler/AP_Scheduler.h>
24
25
extern const AP_HAL::HAL& hal;
26
27
const AP_Param::GroupInfo AP_RollController::var_info[] = {
28
// @Param: 2SRV_TCONST
29
// @DisplayName: Roll Time Constant
30
// @Description: Time constant in seconds from demanded to achieved roll angle. Most models respond well to 0.5. May be reduced for faster responses, but setting lower than a model can achieve will not help.
31
// @Range: 0.4 1.0
32
// @Units: s
33
// @Increment: 0.1
34
// @User: Advanced
35
AP_GROUPINFO("2SRV_TCONST", 0, AP_RollController, gains.tau, 0.5f),
36
37
// index 1 to 3 reserved for old PID values
38
39
// @Param: 2SRV_RMAX
40
// @DisplayName: Maximum Roll Rate
41
// @Description: This sets the maximum roll rate that the attitude controller will demand (degrees/sec) in angle stabilized modes. Setting it to zero disables this limit.
42
// @Range: 0 180
43
// @Units: deg/s
44
// @Increment: 1
45
// @User: Advanced
46
AP_GROUPINFO("2SRV_RMAX", 4, AP_RollController, gains.rmax_pos, 0),
47
48
// index 5, 6 reserved for old IMAX, FF
49
50
// @Param: _RATE_P
51
// @DisplayName: Roll axis rate controller P gain
52
// @Description: Roll axis rate controller P gain. Corrects in proportion to the difference between the desired roll rate vs actual roll rate
53
// @Range: 0.08 0.35
54
// @Increment: 0.005
55
// @User: Standard
56
57
// @Param: _RATE_I
58
// @DisplayName: Roll axis rate controller I gain
59
// @Description: Roll axis rate controller I gain. Corrects long-term difference in desired roll rate vs actual roll rate
60
// @Range: 0.01 0.6
61
// @Increment: 0.01
62
// @User: Standard
63
64
// @Param: _RATE_IMAX
65
// @DisplayName: Roll axis rate controller I gain maximum
66
// @Description: Roll axis rate controller I gain maximum. Constrains the maximum that the I term will output
67
// @Range: 0 1
68
// @Increment: 0.01
69
// @User: Standard
70
71
// @Param: _RATE_D
72
// @DisplayName: Roll axis rate controller D gain
73
// @Description: Roll axis rate controller D gain. Compensates for short-term change in desired roll rate vs actual roll rate
74
// @Range: 0.001 0.03
75
// @Increment: 0.001
76
// @User: Standard
77
78
// @Param: _RATE_FF
79
// @DisplayName: Roll axis rate controller feed forward
80
// @Description: Roll axis rate controller feed forward
81
// @Range: 0 3.0
82
// @Increment: 0.001
83
// @User: Standard
84
85
// @Param: _RATE_FLTT
86
// @DisplayName: Roll axis rate controller target frequency in Hz
87
// @Description: Roll axis rate controller target frequency in Hz
88
// @Range: 2 50
89
// @Increment: 1
90
// @Units: Hz
91
// @User: Standard
92
93
// @Param: _RATE_FLTE
94
// @DisplayName: Roll axis rate controller error frequency in Hz
95
// @Description: Roll axis rate controller error frequency in Hz
96
// @Range: 2 50
97
// @Increment: 1
98
// @Units: Hz
99
// @User: Standard
100
101
// @Param: _RATE_FLTD
102
// @DisplayName: Roll axis rate controller derivative frequency in Hz
103
// @Description: Roll axis rate controller derivative frequency in Hz
104
// @Range: 0 50
105
// @Increment: 1
106
// @Units: Hz
107
// @User: Standard
108
109
// @Param: _RATE_SMAX
110
// @DisplayName: Roll slew rate limit
111
// @Description: Sets an upper limit on the slew rate produced by the combined P and D gains. If the amplitude of the control action produced by the rate feedback exceeds this value, then the D+P gain is reduced to respect the limit. This limits the amplitude of high frequency oscillations caused by an excessive gain. The limit should be set to no more than 25% of the actuators maximum slew rate to allow for load effects. Note: The gain will not be reduced to less than 10% of the nominal value. A value of zero will disable this feature.
112
// @Range: 0 200
113
// @Increment: 0.5
114
// @User: Advanced
115
116
// @Param: _RATE_PDMX
117
// @DisplayName: Roll axis rate controller PD sum maximum
118
// @Description: Roll axis rate controller PD sum maximum. The maximum/minimum value that the sum of the P and D term can output
119
// @Range: 0 1
120
// @Increment: 0.01
121
122
// @Param: _RATE_D_FF
123
// @DisplayName: Roll Derivative FeedForward Gain
124
// @Description: FF D Gain which produces an output that is proportional to the rate of change of the target
125
// @Range: 0 0.03
126
// @Increment: 0.001
127
// @User: Advanced
128
129
// @Param: _RATE_NTF
130
// @DisplayName: Roll Target notch filter index
131
// @Description: Roll Target notch filter index
132
// @Range: 1 8
133
// @User: Advanced
134
135
// @Param: _RATE_NEF
136
// @DisplayName: Roll Error notch filter index
137
// @Description: Roll Error notch filter index
138
// @Range: 1 8
139
// @User: Advanced
140
141
AP_SUBGROUPINFO(rate_pid, "_RATE_", 9, AP_RollController, AC_PID),
142
143
AP_GROUPEND
144
};
145
146
// constructor
147
AP_RollController::AP_RollController(const AP_FixedWing &parms)
148
: AP_FW_Controller(parms,
149
AC_PID::Defaults{
150
.p = 0.08,
151
.i = 0.15,
152
.d = 0.0,
153
.ff = 0.345,
154
.imax = 0.666,
155
.filt_T_hz = 3.0,
156
.filt_E_hz = 0.0,
157
.filt_D_hz = 12.0,
158
.srmax = 150.0,
159
.srtau = 1.0
160
},
161
AP_AutoTune::ATType::AUTOTUNE_ROLL)
162
{
163
AP_Param::setup_object_defaults(this, var_info);
164
}
165
166
float AP_RollController::get_measured_rate() const
167
{
168
return AP::ahrs().get_gyro().x;
169
}
170
171
float AP_RollController::get_airspeed() const
172
{
173
float aspeed;
174
if (!AP::ahrs().airspeed_EAS(aspeed)) {
175
// If no airspeed available use 0
176
aspeed = 0.0;
177
}
178
return aspeed;
179
}
180
181
bool AP_RollController::is_underspeed(const float aspeed) const
182
{
183
return aspeed <= float(aparm.airspeed_min);
184
}
185
186
/*
187
Function returns an equivalent aileron deflection in centi-degrees in the range from -4500 to 4500
188
A positive demand is up
189
Inputs are:
190
1) demanded bank angle in centi-degrees
191
2) control gain scaler = scaling_speed / aspeed
192
3) boolean which is true when stabilise mode is active
193
4) minimum FBW airspeed (metres/sec)
194
*/
195
float AP_RollController::get_servo_out(int32_t angle_err, float scaler, bool disable_integrator, bool ground_mode)
196
{
197
if (gains.tau < 0.05f) {
198
gains.tau.set(0.05f);
199
}
200
201
// Calculate the desired roll rate (deg/sec) from the angle error
202
angle_err_deg = angle_err * 0.01;
203
float desired_rate = angle_err_deg/ gains.tau;
204
205
/*
206
prevent indecision in the roll controller when target roll is
207
close to 180 degrees from the current roll
208
*/
209
const float indecision_threshold_deg = 160;
210
const float last_desired_rate = _pid_info.target;
211
const float abs_angle_err_deg = fabsf(angle_err_deg);
212
if (abs_angle_err_deg > indecision_threshold_deg &&
213
angle_err_deg <= 180) {
214
if (desired_rate * last_desired_rate < 0) {
215
desired_rate = -desired_rate;
216
// increase the desired rate in proportion to the extra
217
// angle we are requesting
218
const float new_angle_err_deg = abs_angle_err_deg + (180 - abs_angle_err_deg)*2;
219
desired_rate *= new_angle_err_deg / abs_angle_err_deg;
220
}
221
}
222
223
if (!in_recovery) {
224
// Limit the demanded roll rate. When we are in a VTOL
225
// recovery we don't apply the limit
226
if (gains.rmax_pos && desired_rate < -gains.rmax_pos) {
227
desired_rate = - gains.rmax_pos;
228
} else if (gains.rmax_pos && desired_rate > gains.rmax_pos) {
229
desired_rate = gains.rmax_pos;
230
}
231
}
232
233
// the in_recovery flag is single loop only
234
in_recovery = false;
235
236
return _get_rate_out(desired_rate, scaler, disable_integrator, get_airspeed(), ground_mode);
237
}
238
239
/*
240
convert from old to new PIDs
241
this is a temporary conversion function during development
242
*/
243
void AP_RollController::convert_pid()
244
{
245
AP_Float &ff = rate_pid.ff();
246
if (ff.configured()) {
247
return;
248
}
249
float old_ff=0, old_p=1.0, old_i=0.3, old_d=0.08;
250
int16_t old_imax=3000;
251
bool have_old = AP_Param::get_param_by_index(this, 1, AP_PARAM_FLOAT, &old_p);
252
have_old |= AP_Param::get_param_by_index(this, 3, AP_PARAM_FLOAT, &old_i);
253
have_old |= AP_Param::get_param_by_index(this, 2, AP_PARAM_FLOAT, &old_d);
254
have_old |= AP_Param::get_param_by_index(this, 6, AP_PARAM_FLOAT, &old_ff);
255
have_old |= AP_Param::get_param_by_index(this, 5, AP_PARAM_INT16, &old_imax);
256
if (!have_old) {
257
// none of the old gains were set
258
return;
259
}
260
261
const float kp_ff = MAX((old_p - old_i * gains.tau) * gains.tau - old_d, 0);
262
rate_pid.ff().set_and_save(old_ff + kp_ff);
263
rate_pid.kI().set_and_save_ifchanged(old_i * gains.tau);
264
rate_pid.kP().set_and_save_ifchanged(old_d);
265
rate_pid.kD().set_and_save_ifchanged(0);
266
rate_pid.kIMAX().set_and_save_ifchanged(old_imax/4500.0);
267
}
268
269