-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyDriver.hpp
287 lines (257 loc) · 8.25 KB
/
MyDriver.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include "Driver.hpp"
#include "src/LibPID.hpp"
#include "src/LibMisc.hpp"
////////////////////////////////////////////////////////////////////////////////
// Define My Drivers
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Thru driver
////////////////////////////////////////////////////////////////////////////////
class thruDriver: public Driver {
public:
void setup() { };
void loop() {
for (int i=0; i<4; i++) OUT[i] = IN[i];
};
} thruDriver;
////////////////////////////////////////////////////////////////////////////////
// G-Vectoring mode
////////////////////////////////////////////////////////////////////////////////
ParameterList gvectConfig = {
.body = {0},
.conf = {
PCONF_RANGE("gain",50,"%%",0,100,1),
PCONF_RANGE("hpf",1,"span",1,100,1),
PCONF_RANGE("lpf",1,"span",1,100,1),
PCONF_END,
},
};
#define GVECT_GAIN (gvectConfig.getIval(0)/0.5)
#define GVECT_HPF (gvectConfig.getIval(1))
#define GVECT_LPF (gvectConfig.getIval(2))
class gvectDriver: public Driver {
int MIN = 1000;
int MAX = 2000;
FilterHP HPF;
FilterLP LPF;
public:
void setup() {
MIN = min(CH1_MIN,CH1_MAX);
MAX = max(CH1_MIN,CH1_MAX);
HPF.setup(GVECT_HPF,0.0);
LPF.setup(GVECT_LPF,0.0);
};
void loop() {
//
float Gx = ACCL[0];
float dotGx = HPF.update(Gx);
float Gyc = -GVECT_GAIN*LPF.update(dotGx);
if (Gx*dotGx < 0.0) Gyc = -Gyc;
Gyc = constrain(Gyc, -1.0,1.0);
// ch1
OUT[0] = constrain(IN[0],MIN,MAX);
// ch2
if (IN[1] > CH2_MID + CH2_DEAD) {
OUT[1] = CH2_MID + (IN[1] - CH2_MID)*(1.0 + Gyc);
OUT[1] = constrain(OUT[1], CH2_MIN,CH2_MAX);
} else
OUT[1] = IN[1];
};
} gvectDriver;
////////////////////////////////////////////////////////////////////////////////
// Drift mode
////////////////////////////////////////////////////////////////////////////////
ParameterList driftConfig = {
.body = {0},
.conf = {
PCONF_RANGE("rate",50,"deg/sec",10,720,10),
PCONF_NUMBER("P",50,"%%",0,100),
PCONF_NUMBER("I",0,"%%",0,100),
PCONF_NUMBER("D",0,"%%",0,100),
PCONF_RANGE("rev",0,"bool",0,1,1),
PCONF_END,
},
};
#define DRIFT_G (500./driftConfig.getIval(0))
#define DRIFT_P (driftConfig.getIval(1)/50.)
#define DRIFT_I (driftConfig.getIval(2)/250.)
#define DRIFT_D (driftConfig.getIval(3)/5000.)
#define DRIFT_REV (driftConfig.getIval(4))
class driftDriver: public Driver {
int MIN = 1000;
int MAX = 2000;
LibPID CH1_PID;
public:
void setup() {
MIN = min(CH1_MIN,CH1_MAX);
MAX = max(CH1_MIN,CH1_MAX);
CH1_PID.setup(DRIFT_P,DRIFT_I,DRIFT_D,MIN,CH1_MID,MAX,400);
};
void loop() {
float YAW_RATE = GYRO[2];
OUT[0] = CH1_PID.loop(IN[0], DRIFT_G*(DRIFT_REV? -YAW_RATE: YAW_RATE));
OUT[1] = IN[1];
//
OUT[0] = constrain(OUT[0],MIN,MAX);
};
} driftDriver;
////////////////////////////////////////////////////////////////////////////////
// Stunt mode
////////////////////////////////////////////////////////////////////////////////
ParameterList stuntConfig = {
.body = {0},
.conf = {
PCONF_FETCH("roll",0,"deg","ajax",5,30,60),
PCONF_NUMBER("G",50,"%%",0,100),
PCONF_NUMBER("P",50,"%%",0,100),
PCONF_NUMBER("I",0,"%%",0,100),
PCONF_NUMBER("D",0,"%%",0,100),
PCONF_RANGE("rev",0,"bool",0,1,1),
PCONF_NUMBER("ch1",0,"%%",0,100),
PCONF_END,
},
};
#define STUNT_ROLL (stuntConfig.getIval(0))
#define STUNT_G (stuntConfig.getIval(1)/50.*(500./20.))
#define STUNT_P (stuntConfig.getIval(2)/50.)
#define STUNT_I (stuntConfig.getIval(3)/500.)
#define STUNT_D (stuntConfig.getIval(4)/500.)
#define STUNT_REV (stuntConfig.getIval(5))
#define STUNT_CH1 (stuntConfig.getIval(6)/50.)
class stuntDriver: public Driver {
int MIN = 1000;
int MAX = 2000;
LibPID CH1_PID;
public:
void setup() {
MIN = min(CH1_MIN,CH1_MAX);
MAX = max(CH1_MIN,CH1_MAX);
CH1_PID.setup(STUNT_P,STUNT_I,STUNT_D,MIN,CH1_MID,MAX,400);
};
void loop() {
float IMU_ROLL = AHRS[1];
float ABS_ROLL = abs(STUNT_ROLL);
if (abs(IMU_ROLL) > 30) {
float DEL_ROLL = (IMU_ROLL>0? IMU_ROLL-ABS_ROLL: IMU_ROLL+ABS_ROLL);
float CH1 = CH1_MID + STUNT_CH1*(IN[0] - CH1_MID);
OUT[0] = CH1_PID.loop(CH1, STUNT_G*(STUNT_REV? -DEL_ROLL: DEL_ROLL));
OUT[1] = IN[1];
} else {
OUT[0] = IN[0];
OUT[1] = IN[1];
}
OUT[0] = constrain(OUT[0],MIN,MAX);
};
} stuntDriver;
////////////////////////////////////////////////////////////////////////////////
// Tank mode
////////////////////////////////////////////////////////////////////////////////
ParameterList tankConfig = {
.body = {0},
.conf = {
PCONF_RANGE("rate",60,"deg/sec",10,720,10),
PCONF_NUMBER("P",50,"%%",0,100),
PCONF_NUMBER("I",0,"%%",0,100),
PCONF_NUMBER("D",0,"%%",0,100),
PCONF_RANGE("rev",0,"bool",0,1,1),
//PCONF_NUMBER("mix",50,"%%",0,100),
PCONF_END,
},
};
#define TANK_RATE (500./tankConfig.getIval(0))
#define TANK_P (tankConfig.getIval(1)/50.)
#define TANK_I (tankConfig.getIval(2)/250.)
#define TANK_D (tankConfig.getIval(3)/500.)
#define TANK_REV (tankConfig.getIval(4))
#define TANK_MIX (tankConfig.getIval(5)/50.)
class tankDriver: public Driver {
int MIN = 1000;
int MAX = 2000;
LibPID RATE_PID;
public:
void setup() {
MIN = min(CH1_MIN,CH1_MAX);
MAX = max(CH1_MIN,CH1_MAX);
RATE_PID.setup(TANK_P,TANK_I,TANK_D,MIN,CH1_MID,MAX,400);
};
void loop() {
float YAW_RATE = IN[1] > CH2_MID? GYRO[2]: -GYRO[2];
float OUT_RATE = RATE_PID.loop(IN[0], TANK_RATE*(TANK_REV? -YAW_RATE: YAW_RATE)) - CH1_MID;
//
if (abs(IN[1] - CH2_MID) < CH2_DEAD) {
// Neutral
OUT[0] = OUT[1] = CH2_MID;
}
else if (IN[1] >= CH2_MID) {
// Forward
OUT[0] = constrain(IN[1] + OUT_RATE,CH2_MIN,CH2_MAX);
OUT[1] = constrain(IN[1] - OUT_RATE,CH2_MIN,CH2_MAX);
}
else if (IN[1] < CH2_MID) {
// Reverse
OUT[0] = constrain(IN[1] - OUT_RATE,CH2_MIN,CH2_MAX);
OUT[1] = constrain(IN[1] + OUT_RATE,CH2_MIN,CH2_MAX);
}
};
} tankDriver;
////////////////////////////////////////////////////////////////////////////////
// Drone mode
////////////////////////////////////////////////////////////////////////////////
ParameterList droneConfig = {
.body = {0},
.conf = {
PCONF_RANGE("angle",30,"deg",10,60,5),
PCONF_NUMBER("P1",50,"%%",0,100),
PCONF_NUMBER("I1",0,"%%",0,100),
PCONF_NUMBER("D1",0,"%%",0,100),
PCONF_RANGE("rate",50,"deg/sec",10,360,5),
PCONF_NUMBER("P2",50,"%%",0,100),
PCONF_NUMBER("I2",0,"%%",0,100),
PCONF_NUMBER("D2",0,"%%",0,100),
PCONF_END,
},
};
#define DRONE_ANGL (500./droneConfig.getIval(0))
#define DRONE_P1 (droneConfig.getIval(1)/50.)
#define DRONE_I1 (droneConfig.getIval(2)/250.)
#define DRONE_D1 (droneConfig.getIval(3)/500.)
#define DRONE_RATE (500./droneConfig.getIval(4))
#define DRONE_P2 (droneConfig.getIval(5)/50.)
#define DRONE_I2 (droneConfig.getIval(6)/250.)
#define DRONE_D2 (droneConfig.getIval(7)/500.)
class droneDriver: public Driver {
const int MIN = 1000;
const int MID = 1500;
const int MAX = 2000;
const int FREQ = 400;
LibPID PITCH_PID;
LibPID ROLL_PID;
LibPID RATE_PID;
public:
void setup() {
PITCH_PID.setup(DRONE_P1,DRONE_I1,DRONE_D1,MIN,MID,MAX,FREQ);
ROLL_PID.setup(DRONE_P1,DRONE_I1,DRONE_D1,MIN,MID,MAX,FREQ);
RATE_PID.setup(DRONE_P2,DRONE_I2,DRONE_D2,MIN,MID,MAX,FREQ);
};
void loop() {
float PITCH = AHRS[0];
float ROLL = AHRS[1];
float RATE = GYRO[2];
float OUT_PITCH = PITCH_PID.loop(IN[1], DRONE_ANGL*PITCH) - MID;
float OUT_ROLL = ROLL_PID.loop(IN[0], DRONE_ANGL*ROLL) - MID;
float OUT_RATE = RATE_PID.loop(IN[3], DRONE_RATE*RATE) - MID;
//
OUT[0] = IN[1] - OUT_PITCH - OUT_ROLL + OUT_RATE;
OUT[1] = IN[1] - OUT_PITCH + OUT_ROLL - OUT_RATE;
OUT[2] = IN[1] + OUT_PITCH + OUT_ROLL + OUT_RATE;
OUT[3] = IN[1] + OUT_PITCH - OUT_ROLL - OUT_RATE;
//
OUT[0] = constrain(OUT[0],MIN,MAX);
OUT[1] = constrain(OUT[1],MIN,MAX);
OUT[2] = constrain(OUT[2],MIN,MAX);
OUT[3] = constrain(OUT[3],MIN,MAX);
};
} droneDriver;
////////////////////////////////////////////////////////////////////////////////
// EOF
////////////////////////////////////////////////////////////////////////////////