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_AutoTune/AC_AutoTune.h
Views: 1798
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
support for autotune of multirotors. Based on original autotune code from ArduCopter, written by Leonard Hall
17
Converted to a library by Andrew Tridgell
18
*/
19
#pragma once
20
21
#include "AC_AutoTune_config.h"
22
23
#if AC_AUTOTUNE_ENABLED
24
25
#include <AC_AttitudeControl/AC_AttitudeControl.h>
26
#include <AC_AttitudeControl/AC_PosControl.h>
27
#include <AP_Math/AP_Math.h>
28
#include <RC_Channel/RC_Channel.h>
29
#include "AC_AutoTune_FreqResp.h"
30
31
#define AUTOTUNE_AXIS_BITMASK_ROLL 1
32
#define AUTOTUNE_AXIS_BITMASK_PITCH 2
33
#define AUTOTUNE_AXIS_BITMASK_YAW 4
34
#define AUTOTUNE_AXIS_BITMASK_YAW_D 8
35
36
#define AUTOTUNE_SUCCESS_COUNT 4 // The number of successful iterations we need to freeze at current gains
37
38
// Auto Tune message ids for ground station
39
#define AUTOTUNE_MESSAGE_STARTED 0
40
#define AUTOTUNE_MESSAGE_STOPPED 1
41
#define AUTOTUNE_MESSAGE_SUCCESS 2
42
#define AUTOTUNE_MESSAGE_FAILED 3
43
#define AUTOTUNE_MESSAGE_SAVED_GAINS 4
44
#define AUTOTUNE_MESSAGE_TESTING 5
45
#define AUTOTUNE_MESSAGE_TESTING_END 6
46
47
#define AUTOTUNE_ANNOUNCE_INTERVAL_MS 2000
48
49
class AC_AutoTune
50
{
51
public:
52
// constructor
53
AC_AutoTune();
54
55
// main run loop
56
virtual void run();
57
58
// Possibly save gains, called on disarm
59
void disarmed(const bool in_autotune_mode);
60
61
// stop tune, reverting gains
62
void stop();
63
64
// Autotune aux function trigger
65
void do_aux_function(const RC_Channel::AuxSwitchPos ch_flag);
66
67
protected:
68
69
virtual void save_tuning_gains() = 0;
70
71
72
// reset Autotune so that gains are not saved again and autotune can be run again.
73
void reset() {
74
mode = UNINITIALISED;
75
axes_completed = 0;
76
have_pilot_testing_command = false;
77
}
78
79
// axis that can be tuned
80
enum class AxisType {
81
ROLL = 0, // roll axis is being tuned (either angle or rate)
82
PITCH = 1, // pitch axis is being tuned (either angle or rate)
83
YAW = 2, // yaw axis is being tuned using FLTE (either angle or rate)
84
YAW_D = 3, // yaw axis is being tuned using D (either angle or rate)
85
};
86
87
//
88
// methods that must be supplied by the vehicle specific subclass
89
//
90
virtual bool init(void) = 0;
91
92
// get pilot input for desired climb rate
93
virtual float get_pilot_desired_climb_rate_cms(void) const = 0;
94
95
// get pilot input for designed roll and pitch, and yaw rate
96
virtual void get_pilot_desired_rp_yrate_cd(float &roll_cd, float &pitch_cd, float &yaw_rate_cds) = 0;
97
98
// init pos controller Z velocity and accel limits
99
virtual void init_z_limits() = 0;
100
101
#if HAL_LOGGING_ENABLED
102
// log PIDs at full rate for during twitch
103
virtual void log_pids() = 0;
104
#endif
105
106
//
107
// methods to load and save gains
108
//
109
110
// backup original gains and prepare for start of tuning
111
virtual void backup_gains_and_initialise();
112
113
// switch to use original gains
114
virtual void load_orig_gains() = 0;
115
116
// switch to gains found by last successful autotune
117
virtual void load_tuned_gains() = 0;
118
119
// load gains used between tests. called during testing mode's update-gains step to set gains ahead of return-to-level step
120
virtual void load_intra_test_gains() = 0;
121
122
// load gains for next test. relies on axis variable being set
123
virtual void load_test_gains() = 0;
124
125
// reset the test vaariables for each vehicle
126
virtual void reset_vehicle_test_variables() = 0;
127
128
// reset the update gain variables for each vehicle
129
virtual void reset_update_gain_variables() = 0;
130
131
// test initialization and run methods that should be overridden for each vehicle
132
virtual void test_init() = 0;
133
virtual void test_run(AxisType test_axis, const float dir_sign) = 0;
134
135
// return true if user has enabled autotune for roll, pitch or yaw axis
136
bool roll_enabled() const;
137
bool pitch_enabled() const;
138
bool yaw_enabled() const;
139
bool yaw_d_enabled() const;
140
141
// update gains for the rate p up tune type
142
virtual void updating_rate_p_up_all(AxisType test_axis)=0;
143
144
// update gains for the rate d up tune type
145
virtual void updating_rate_d_up_all(AxisType test_axis)=0;
146
147
// update gains for the rate d down tune type
148
virtual void updating_rate_d_down_all(AxisType test_axis)=0;
149
150
// update gains for the angle p up tune type
151
virtual void updating_angle_p_up_all(AxisType test_axis)=0;
152
153
// update gains for the angle p down tune type
154
virtual void updating_angle_p_down_all(AxisType test_axis)=0;
155
156
// set gains post tune for the tune type
157
virtual void set_gains_post_tune(AxisType test_axis)=0;
158
159
// reverse direction for twitch test
160
virtual bool twitch_reverse_direction() = 0;
161
162
163
#if HAL_LOGGING_ENABLED
164
virtual void Log_AutoTune() = 0;
165
virtual void Log_AutoTuneDetails() = 0;
166
virtual void Log_AutoTuneSweep() = 0;
167
#endif
168
169
// internal init function, should be called from init()
170
bool init_internals(bool use_poshold,
171
AC_AttitudeControl *attitude_control,
172
AC_PosControl *pos_control,
173
AP_AHRS_View *ahrs_view,
174
AP_InertialNav *inertial_nav);
175
176
// send intermittent updates to user on status of tune
177
virtual void do_gcs_announcements() = 0;
178
179
// send post test updates to user
180
virtual void do_post_test_gcs_announcements() = 0;
181
182
// send message with high level status (e.g. Started, Stopped)
183
void update_gcs(uint8_t message_id) const;
184
185
// send lower level step status (e.g. Pilot overrides Active)
186
void send_step_string();
187
188
// convert tune type to string for reporting
189
const char *type_string() const;
190
191
// return current axis string
192
const char *axis_string() const;
193
194
// report final gains for a given axis to GCS
195
virtual void report_final_gains(AxisType test_axis) const = 0;
196
197
// Functions added for heli autotune
198
199
// Add additional updating gain functions specific to heli
200
// generic method used by subclasses to update gains for the rate ff up tune type
201
virtual void updating_rate_ff_up_all(AxisType test_axis)=0;
202
203
// generic method used by subclasses to update gains for the max gain tune type
204
virtual void updating_max_gains_all(AxisType test_axis)=0;
205
206
// steps performed while in the tuning mode
207
enum StepType {
208
WAITING_FOR_LEVEL = 0, // autotune is waiting for vehicle to return to level before beginning the next twitch
209
TESTING = 1, // autotune has begun a test and is watching the resulting vehicle movement
210
UPDATE_GAINS = 2, // autotune has completed a test and is updating the gains based on the results
211
ABORT = 3 // load normal gains and return to WAITING_FOR_LEVEL
212
};
213
214
// mini steps performed while in Tuning mode, Testing step
215
enum TuneType {
216
RD_UP = 0, // rate D is being tuned up
217
RD_DOWN = 1, // rate D is being tuned down
218
RP_UP = 2, // rate P is being tuned up
219
RFF_UP = 3, // rate FF is being tuned up
220
SP_DOWN = 4, // angle P is being tuned down
221
SP_UP = 5, // angle P is being tuned up
222
MAX_GAINS = 6, // max allowable stable gains are determined
223
TUNE_CHECK = 7, // frequency sweep with tuned gains
224
TUNE_COMPLETE = 8 // Reached end of tuning
225
};
226
TuneType tune_seq[6]; // holds sequence of tune_types to be performed
227
uint8_t tune_seq_curr; // current tune sequence step
228
229
// get the next tune type
230
void next_tune_type(TuneType &curr_tune_type, bool reset);
231
232
// Sets customizable tune sequence for the vehicle
233
virtual void set_tune_sequence() = 0;
234
235
// get_axis_bitmask accessor
236
virtual uint8_t get_axis_bitmask() const = 0;
237
238
// get_testing_step_timeout_ms accessor
239
virtual uint32_t get_testing_step_timeout_ms() const = 0;
240
241
// get attitude for slow position hold in autotune mode
242
void get_poshold_attitude(float &roll_cd, float &pitch_cd, float &yaw_cd);
243
244
// type of gains to load
245
enum GainType {
246
GAIN_ORIGINAL = 0,
247
GAIN_TEST = 1,
248
GAIN_INTRA_TEST = 2,
249
GAIN_TUNED = 3,
250
} loaded_gains;
251
void load_gains(enum GainType gain_type);
252
253
// autotune modes (high level states)
254
enum TuneMode {
255
UNINITIALISED = 0, // autotune has never been run
256
TUNING = 1, // autotune is testing gains
257
SUCCESS = 2, // tuning has completed, user is flight testing the new gains
258
FAILED = 3, // tuning has failed, user is flying on original gains
259
};
260
TuneMode mode; // see TuneMode for what modes are allowed
261
262
// copies of object pointers to make code a bit clearer
263
AC_AttitudeControl *attitude_control;
264
AC_PosControl *pos_control;
265
AP_AHRS_View *ahrs_view;
266
AP_InertialNav *inertial_nav;
267
AP_Motors *motors;
268
269
AxisType axis; // current axis being tuned. see AxisType enum
270
bool positive_direction; // false = tuning in negative direction (i.e. left for roll), true = positive direction (i.e. right for roll)
271
StepType step; // see StepType for what steps are performed
272
TuneType tune_type; // see TuneType
273
bool twitch_first_iter; // true on first iteration of a twitch (used to signal we must step the attitude or rate target)
274
uint8_t axes_completed; // bitmask of completed axes
275
uint32_t step_start_time_ms; // start time of current tuning step (used for timeout checks)
276
uint32_t step_time_limit_ms; // time limit of current autotune process
277
uint32_t level_start_time_ms; // start time of waiting for level
278
int8_t counter; // counter for tuning gains
279
float start_angle; // start angle
280
float start_rate; // start rate - parent and multi
281
float test_accel_max; // maximum acceleration variable
282
float desired_yaw_cd; // yaw heading during tune - parent and Tradheli
283
float step_scaler; // scaler to reduce maximum target step - parent and multi
284
285
LowPassFilterFloat rotation_rate_filt; // filtered rotation rate in radians/second
286
287
// backup of currently being tuned parameter values
288
float orig_roll_rp, orig_roll_ri, orig_roll_rd, orig_roll_rff, orig_roll_dff, orig_roll_fltt, orig_roll_smax, orig_roll_sp, orig_roll_accel, orig_roll_rate;
289
float orig_pitch_rp, orig_pitch_ri, orig_pitch_rd, orig_pitch_rff, orig_pitch_dff, orig_pitch_fltt, orig_pitch_smax, orig_pitch_sp, orig_pitch_accel, orig_pitch_rate;
290
float orig_yaw_rp, orig_yaw_ri, orig_yaw_rd, orig_yaw_rff, orig_yaw_dff, orig_yaw_fltt, orig_yaw_smax, orig_yaw_rLPF, orig_yaw_sp, orig_yaw_accel, orig_yaw_rate;
291
bool orig_bf_feedforward;
292
293
// currently being tuned parameter values
294
float tune_roll_rp, tune_roll_rd, tune_roll_sp, tune_roll_accel;
295
float tune_pitch_rp, tune_pitch_rd, tune_pitch_sp, tune_pitch_accel;
296
float tune_yaw_rp, tune_yaw_rLPF, tune_yaw_sp, tune_yaw_accel;
297
float tune_roll_rff, tune_pitch_rff, tune_yaw_rd, tune_yaw_rff;
298
299
uint32_t announce_time;
300
float lean_angle;
301
float rotation_rate;
302
float roll_cd, pitch_cd;
303
304
// heli specific variables
305
float start_freq; //start freq for dwell test
306
float stop_freq; //ending freq for dwell test
307
308
private:
309
// return true if we have a good position estimate
310
virtual bool position_ok();
311
312
// methods subclasses must implement to specify max/min test angles:
313
virtual float target_angle_max_rp_cd() const = 0;
314
315
// methods subclasses must implement to specify max/min test angles:
316
virtual float target_angle_max_y_cd() const = 0;
317
318
// methods subclasses must implement to specify max/min test angles:
319
virtual float target_angle_min_rp_cd() const = 0;
320
321
// methods subclasses must implement to specify max/min test angles:
322
virtual float target_angle_min_y_cd() const = 0;
323
324
// methods subclasses must implement to specify max/min test angles:
325
virtual float angle_lim_max_rp_cd() const = 0;
326
327
// methods subclasses must implement to specify max/min test angles:
328
virtual float angle_lim_neg_rpy_cd() const = 0;
329
330
// initialise position controller
331
bool init_position_controller();
332
333
// main state machine to level vehicle, perform a test and update gains
334
// directly updates attitude controller with targets
335
void control_attitude();
336
337
// returns true if vehicle is close to level
338
bool currently_level();
339
340
bool pilot_override; // true = pilot is overriding controls so we suspend tuning temporarily
341
bool use_poshold; // true = enable position hold
342
bool have_position; // true = start_position is value
343
Vector3f start_position; // target when holding position as an offset from EKF origin in cm in NEU frame
344
345
// variables
346
uint32_t override_time; // the last time the pilot overrode the controls
347
348
// time in ms of last pilot override warning
349
uint32_t last_pilot_override_warning;
350
351
// True if we ever got a pilot testing command of tuned gains.
352
// If true then disarming will save if the tuned gains are currently active.
353
bool have_pilot_testing_command;
354
355
};
356
357
#endif // AC_AUTOTUNE_ENABLED
358
359