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/APM_Control/AP_AutoTune.h
Views: 1798
1
#pragma once
2
3
#include <AP_Logger/LogStructure.h>
4
#include <AP_Param/AP_Param.h>
5
#include <AP_Vehicle/AP_FixedWing.h>
6
#include <Filter/SlewLimiter.h>
7
8
#include <Filter/ModeFilter.h>
9
10
class AP_AutoTune
11
{
12
public:
13
struct ATGains {
14
AP_Float tau;
15
AP_Int16 rmax_pos;
16
AP_Int16 rmax_neg;
17
float FF, P, I, D, IMAX;
18
float flt_T, flt_E, flt_D;
19
};
20
21
enum ATType {
22
AUTOTUNE_ROLL = 0,
23
AUTOTUNE_PITCH = 1,
24
AUTOTUNE_YAW = 2,
25
};
26
27
enum Options {
28
DISABLE_FLTD_UPDATE = 0,
29
DISABLE_FLTT_UPDATE = 1
30
};
31
32
struct PACKED log_ATRP {
33
LOG_PACKET_HEADER;
34
uint64_t time_us;
35
uint8_t type;
36
uint8_t state;
37
float actuator;
38
float P_slew;
39
float D_slew;
40
float FF_single;
41
float FF;
42
float P;
43
float I;
44
float D;
45
uint8_t action;
46
float rmax;
47
float tau;
48
};
49
50
// constructor
51
AP_AutoTune(ATGains &_gains, ATType type, const AP_FixedWing &parms, class AC_PID &rpid);
52
53
// called when autotune mode is entered
54
void start(void);
55
56
// called to stop autotune and restore gains when user leaves
57
// autotune
58
void stop(void);
59
60
// update called whenever autotune mode is active. This is
61
// called at the main loop rate
62
void update(struct AP_PIDInfo &pid_info, float scaler, float angle_err_deg);
63
64
// are we running?
65
bool running;
66
67
private:
68
// the current gains
69
ATGains &current;
70
class AC_PID &rpid;
71
72
// what type of autotune is this
73
ATType type;
74
75
const AP_FixedWing &aparm;
76
77
// values to restore if we leave autotune mode
78
ATGains restore;
79
ATGains last_save;
80
81
// last logging time
82
uint32_t last_log_ms;
83
84
// the demanded/achieved state
85
enum class ATState {IDLE,
86
DEMAND_POS,
87
DEMAND_NEG
88
};
89
ATState state;
90
91
// the demanded/achieved state
92
enum class Action {NONE,
93
LOW_RATE,
94
SHORT,
95
RAISE_PD,
96
LOWER_PD,
97
IDLE_LOWER_PD,
98
RAISE_D,
99
RAISE_P,
100
LOWER_D,
101
LOWER_P
102
};
103
Action action;
104
105
// when we entered the current state
106
uint32_t state_enter_ms;
107
108
void check_state_exit(uint32_t state_time_ms);
109
void save_gains(void);
110
111
void save_float_if_changed(AP_Float &v, float value);
112
void save_int16_if_changed(AP_Int16 &v, int16_t value);
113
void state_change(ATState newstate);
114
const char *axis_string(void) const;
115
116
// get gains with PID components
117
ATGains get_gains(void);
118
void restore_gains(void);
119
120
// update rmax and tau towards target
121
void update_rmax();
122
123
bool has_option(Options option) {
124
return (aparm.autotune_options.get() & uint32_t(1<<uint32_t(option))) != 0;
125
}
126
127
// 5 point mode filter for FF estimate
128
ModeFilterFloat_Size5 ff_filter;
129
130
LowPassFilterConstDtFloat actuator_filter;
131
LowPassFilterConstDtFloat rate_filter;
132
LowPassFilterConstDtFloat target_filter;
133
134
// separate slew limiters for P and D
135
float slew_limit_max, slew_limit_tau;
136
SlewLimiter slew_limiter_P{slew_limit_max, slew_limit_tau};
137
SlewLimiter slew_limiter_D{slew_limit_max, slew_limit_tau};
138
139
float max_actuator;
140
float min_actuator;
141
float max_rate;
142
float min_rate;
143
float max_target;
144
float min_target;
145
float max_P;
146
float max_D;
147
float min_Dmod;
148
float max_Dmod;
149
float max_SRate_P;
150
float max_SRate_D;
151
float FF_single;
152
uint16_t ff_count;
153
float dt;
154
float D_limit;
155
float P_limit;
156
uint32_t D_set_ms;
157
uint32_t P_set_ms;
158
uint8_t done_count;
159
};
160
161