forked from AeroQuad/AeroQuad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlightControl.pde
375 lines (341 loc) · 16.4 KB
/
FlightControl.pde
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
AeroQuad v2.5 Beta 1 - July 2011
www.AeroQuad.com
Copyright (c) 2011 Ted Carancho. All rights reserved.
An Open Source Arduino based multicopter.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// FlightControl.pde is responsible for combining sensor measurements and
// transmitter commands into motor commands for the defined flight configuration (X, +, etc.)
//////////////////////////////////////////////////////////////////////////////
/////////////////////////// calculateFlightError /////////////////////////////
//////////////////////////////////////////////////////////////////////////////
#define ATTITUDE_SCALING (0.75 * PWM2RAD)
void calculateFlightError(void)
{
if (flightMode == ACRO) {
motors.setMotorAxisCommand(ROLL, updatePID(receiver.getSIData(ROLL), gyro.getData(ROLL), &PID[ROLL]));
motors.setMotorAxisCommand(PITCH, updatePID(receiver.getSIData(PITCH), -gyro.getData(PITCH), &PID[PITCH]));
}
else {
float rollAttitudeCmd = updatePID((receiver.getData(ROLL) - receiver.getZero(ROLL)) * ATTITUDE_SCALING, flightAngle->getData(ROLL), &PID[LEVELROLL]);
float pitchAttitudeCmd = updatePID((receiver.getData(PITCH) - receiver.getZero(PITCH)) * ATTITUDE_SCALING, -flightAngle->getData(PITCH), &PID[LEVELPITCH]);
motors.setMotorAxisCommand(ROLL, updatePID(rollAttitudeCmd, gyro.getData(ROLL), &PID[LEVELGYROROLL]));
motors.setMotorAxisCommand(PITCH, updatePID(pitchAttitudeCmd, -gyro.getData(PITCH), &PID[LEVELGYROPITCH]));
}
}
//////////////////////////////////////////////////////////////////////////////
/////////////////////////// processCalibrateESC //////////////////////////////
//////////////////////////////////////////////////////////////////////////////
void processCalibrateESC(void)
{
switch (calibrateESC) { // used for calibrating ESC's
case 1:
for (byte motor = FRONT; motor < LASTMOTOR; motor++)
motors.setMotorCommand(motor, MAXCOMMAND);
break;
case 3:
for (byte motor = FRONT; motor < LASTMOTOR; motor++)
motors.setMotorCommand(motor, constrain(testCommand, 1000, 1200));
break;
case 5:
for (byte motor = FRONT; motor < LASTMOTOR; motor++)
motors.setMotorCommand(motor, constrain(motors.getRemoteCommand(motor), 1000, 1200));
safetyCheck = ON;
break;
default:
for (byte motor = FRONT; motor < LASTMOTOR; motor++)
motors.setMotorCommand(motor, MINCOMMAND);
}
// Send calibration commands to motors
motors.write(); // Defined in Motors.h
}
//////////////////////////////////////////////////////////////////////////////
/////////////////////////// processHeadingHold ///////////////////////////////
//////////////////////////////////////////////////////////////////////////////
void processHeading(void)
{
if (headingHoldConfig == ON) {
#if defined(HeadingMagHold) || defined(AeroQuadMega_CHR6DM) || defined(APM_OP_CHR6DM)
heading = degrees(flightAngle->getHeading(YAW));
#else
heading = degrees(gyro.getHeading());
#endif
// Always center relative heading around absolute heading chosen during yaw command
// This assumes that an incorrect yaw can't be forced on the AeroQuad >180 or <-180 degrees
// This is done so that AeroQuad does not accidentally hit transition between 0 and 360 or -180 and 180
// AKA - THERE IS A BUG HERE - if relative heading is greater than 180 degrees, the PID will swing from negative to positive
// Doubt that will happen as it would have to be uncommanded.
relativeHeading = heading - setHeading;
if (heading <= (setHeading - 180)) relativeHeading += 360;
if (heading >= (setHeading + 180)) relativeHeading -= 360;
// Apply heading hold only when throttle high enough to start flight
if (receiver.getData(THROTTLE) > MINCHECK ) {
if ((receiver.getData(YAW) > (MIDCOMMAND + 25)) || (receiver.getData(YAW) < (MIDCOMMAND - 25))) {
// If commanding yaw, turn off heading hold and store latest heading
setHeading = heading;
headingHold = 0;
PID[HEADING].integratedError = 0;
headingHoldState = OFF;
headingTime = currentTime;
}
else {
if (relativeHeading < .25 && relativeHeading > -.25) {
headingHold = 0;
PID[HEADING].integratedError = 0;
}
else if (headingHoldState == OFF) { // quick fix to soften heading hold on new heading
if ((currentTime - headingTime) > 500000) {
headingHoldState = ON;
headingTime = currentTime;
setHeading = heading;
headingHold = 0;
}
}
else {
// No new yaw input, calculate current heading vs. desired heading heading hold
// Relative heading is always centered around zero
headingHold = updatePID(0, relativeHeading, &PID[HEADING]);
headingTime = currentTime; // quick fix to soften heading hold, wait 100ms before applying heading hold
}
}
}
else {
// minimum throttle not reached, use off settings
setHeading = heading;
headingHold = 0;
PID[HEADING].integratedError = 0;
}
}
// NEW SI Version
commandedYaw = constrain(receiver.getSIData(YAW) + radians(headingHold), -PI, PI);
motors.setMotorAxisCommand(YAW, updatePID(commandedYaw, gyro.getData(YAW), &PID[YAW]));
// uses flightAngle unbias rate
//motors.setMotorAxisCommand(YAW, updatePID(commandedYaw, flightAngle->getGyroUnbias(YAW), &PID[YAW]));
}
//////////////////////////////////////////////////////////////////////////////
/////////////////////////// processAltitudeHold //////////////////////////////
//////////////////////////////////////////////////////////////////////////////
void processAltitudeHold(void)
{
// ****************************** Altitude Adjust *************************
// Thanks to Honk for his work with altitude hold
// http://aeroquad.com/showthread.php?792-Problems-with-BMP085-I2C-barometer
// Thanks to Sherbakov for his work in Z Axis dampening
// http://aeroquad.com/showthread.php?359-Stable-flight-logic...&p=10325&viewfull=1#post10325
#ifdef AltitudeHold
if (altitudeHold == ON) {
throttleAdjust = updatePID(holdAltitude, altitude.getData(), &PID[ALTITUDE]);
//throttleAdjust = constrain((holdAltitude - altitude.getData()) * PID[ALTITUDE].P, minThrottleAdjust, maxThrottleAdjust);
throttleAdjust = constrain(throttleAdjust, minThrottleAdjust, maxThrottleAdjust);
if (abs(holdThrottle - receiver.getData(THROTTLE)) > PANICSTICK_MOVEMENT) {
altitudeHold = ALTPANIC; // too rapid of stick movement so PANIC out of ALTHOLD
} else {
if (receiver.getData(THROTTLE) > (holdThrottle + ALTBUMP)) { // AKA changed to use holdThrottle + ALTBUMP - (was MAXCHECK) above 1900
holdAltitude += 0.01;
}
if (receiver.getData(THROTTLE) < (holdThrottle - ALTBUMP)) { // AKA change to use holdThorrle - ALTBUMP - (was MINCHECK) below 1100
holdAltitude -= 0.01;
}
}
}
else {
// Altitude hold is off, get throttle from receiver
holdThrottle = receiver.getData(THROTTLE);
throttleAdjust = autoDescent; // autoDescent is lowered from BatteryMonitor.h during battery alarm
}
// holdThrottle set in FlightCommand.pde if altitude hold is on
throttle = holdThrottle + throttleAdjust; // holdThrottle is also adjust by BatteryMonitor.h during battery alarm
#else
// If altitude hold not enabled in AeroQuad.pde, get throttle from receiver
throttle = receiver.getData(THROTTLE) + autoDescent; //autoDescent is lowered from BatteryMonitor.h while battery critical, otherwise kept 0
#endif
}
//////////////////////////////////////////////////////////////////////////////
/////////////////////////// processMinMaxMotorCommand ////////////////////////
//////////////////////////////////////////////////////////////////////////////
void processMinMaxMotorCommand(void)
{
// Prevents too little power applied to motors during hard manuevers
// Also provides even motor power on both sides if limit encountered
if ((motors.getMotorCommand(FRONT) <= MINTHROTTLE) || (motors.getMotorCommand(REAR) <= MINTHROTTLE)){
delta = receiver.getData(THROTTLE) - MINTHROTTLE;
motors.setMaxCommand(RIGHT, constrain(receiver.getData(THROTTLE) + delta, MINTHROTTLE, MAXCHECK));
motors.setMaxCommand(LEFT, constrain(receiver.getData(THROTTLE) + delta, MINTHROTTLE, MAXCHECK));
}
else if ((motors.getMotorCommand(FRONT) >= MAXCOMMAND) || (motors.getMotorCommand(REAR) >= MAXCOMMAND)) {
delta = MAXCOMMAND - receiver.getData(THROTTLE);
motors.setMinCommand(RIGHT, constrain(receiver.getData(THROTTLE) - delta, MINTHROTTLE, MAXCOMMAND));
motors.setMinCommand(LEFT, constrain(receiver.getData(THROTTLE) - delta, MINTHROTTLE, MAXCOMMAND));
}
else {
motors.setMaxCommand(RIGHT, MAXCOMMAND);
motors.setMaxCommand(LEFT, MAXCOMMAND);
motors.setMinCommand(RIGHT, MINTHROTTLE);
motors.setMinCommand(LEFT, MINTHROTTLE);
}
if ((motors.getMotorCommand(LEFT) <= MINTHROTTLE) || (motors.getMotorCommand(RIGHT) <= MINTHROTTLE)){
delta = receiver.getData(THROTTLE) - MINTHROTTLE;
motors.setMaxCommand(FRONT, constrain(receiver.getData(THROTTLE) + delta, MINTHROTTLE, MAXCHECK));
motors.setMaxCommand(REAR, constrain(receiver.getData(THROTTLE) + delta, MINTHROTTLE, MAXCHECK));
}
else if ((motors.getMotorCommand(LEFT) >= MAXCOMMAND) || (motors.getMotorCommand(RIGHT) >= MAXCOMMAND)) {
delta = MAXCOMMAND - receiver.getData(THROTTLE);
motors.setMinCommand(FRONT, constrain(receiver.getData(THROTTLE) - delta, MINTHROTTLE, MAXCOMMAND));
motors.setMinCommand(REAR, constrain(receiver.getData(THROTTLE) - delta, MINTHROTTLE, MAXCOMMAND));
}
else {
motors.setMaxCommand(FRONT, MAXCOMMAND);
motors.setMaxCommand(REAR, MAXCOMMAND);
motors.setMinCommand(FRONT, MINTHROTTLE);
motors.setMinCommand(REAR, MINTHROTTLE);
}
}
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////// processHardManuevers ////////////////////////
//////////////////////////////////////////////////////////////////////////////
void processHardManuevers()
{
#ifdef XConfig // Fix for + mode hardmanuevers
if (receiver.getRaw(ROLL) < MINCHECK) {
motors.setMaxCommand(FRONT, minAcro);
motors.setMaxCommand(REAR, MAXCOMMAND);
motors.setMaxCommand(LEFT, minAcro);
motors.setMaxCommand(RIGHT, MAXCOMMAND);
}
else if (receiver.getRaw(ROLL) > MAXCHECK) {
motors.setMaxCommand(FRONT, MAXCOMMAND);
motors.setMaxCommand(REAR, minAcro);
motors.setMaxCommand(LEFT, MAXCOMMAND);
motors.setMaxCommand(RIGHT, minAcro);
}
else if (receiver.getRaw(PITCH) < MINCHECK) {
motors.setMaxCommand(FRONT, MAXCOMMAND);
motors.setMaxCommand(REAR, minAcro);
motors.setMaxCommand(LEFT, minAcro);
motors.setMaxCommand(RIGHT, MAXCOMMAND);
}
else if (receiver.getRaw(PITCH) > MAXCHECK) {
motors.setMaxCommand(FRONT, minAcro);
motors.setMaxCommand(REAR, MAXCOMMAND);
motors.setMaxCommand(LEFT, MAXCOMMAND);
motors.setMaxCommand(RIGHT, minAcro);
}
#endif
#ifdef plusConfig
if (receiver.getRaw(ROLL) < MINCHECK) {
motors.setMinCommand(LEFT, minAcro);
motors.setMaxCommand(RIGHT, MAXCOMMAND);
}
else if (receiver.getRaw(ROLL) > MAXCHECK) {
motors.setMaxCommand(LEFT, MAXCOMMAND);
motors.setMinCommand(RIGHT, minAcro);
}
else if (receiver.getRaw(PITCH) < MINCHECK) {
motors.setMaxCommand(FRONT, MAXCOMMAND);
motors.setMinCommand(REAR, minAcro);
}
else if (receiver.getRaw(PITCH) > MAXCHECK) {
motors.setMinCommand(FRONT, minAcro);
motors.setMaxCommand(REAR, MAXCOMMAND);
}
#endif
}
#ifdef XConfig
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////// X MODE //////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
void processFlightControlXMode(void) {
// ********************** Calculate Flight Error ***************************
calculateFlightError();
// ********************** Update Yaw ***************************************
processHeading();
// ********************** Altitude Adjust **********************************
//processAltitudeHold();
// ********************** Calculate Motor Commands *************************
if (armed && safetyCheck) {
// Front = Front/Right, Back = Left/Rear, Left = Front/Left, Right = Right/Rear
motors.setMotorCommand(FRONT, throttle - motors.getMotorAxisCommand(PITCH) + motors.getMotorAxisCommand(ROLL) - motors.getMotorAxisCommand(YAW));
motors.setMotorCommand(RIGHT, throttle - motors.getMotorAxisCommand(PITCH) - motors.getMotorAxisCommand(ROLL) + motors.getMotorAxisCommand(YAW));
motors.setMotorCommand(LEFT, throttle + motors.getMotorAxisCommand(PITCH) + motors.getMotorAxisCommand(ROLL) + motors.getMotorAxisCommand(YAW));
motors.setMotorCommand(REAR, throttle + motors.getMotorAxisCommand(PITCH) - motors.getMotorAxisCommand(ROLL) - motors.getMotorAxisCommand(YAW));
}
// *********************** process min max motor command *******************
processMinMaxMotorCommand();
// Allows quad to do acrobatics by lowering power to opposite motors during hard manuevers
if (flightMode == ACRO) {
processHardManuevers();
}
// Apply limits to motor commands
for (byte motor = FRONT; motor < LASTMOTOR; motor++) {
motors.setMotorCommand(motor, constrain(motors.getMotorCommand(motor), motors.getMinCommand(motor), motors.getMaxCommand(motor)));
}
// If throttle in minimum position, don't apply yaw
if (receiver.getData(THROTTLE) < MINCHECK) {
for (byte motor = FRONT; motor < LASTMOTOR; motor++) {
motors.setMotorCommand(motor, MINTHROTTLE);
}
}
// ESC Calibration
if (armed == OFF) {
processCalibrateESC();
}
// *********************** Command Motors **********************
if (armed == ON && safetyCheck == ON) {
motors.write(); // Defined in Motors.h
}
}
#endif
#ifdef plusConfig
//////////////////////////////////////////////////////////////////////////////
///////////////////////////////// PLUS MODE //////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
void processFlightControlPlusMode(void) {
// ********************** Calculate Flight Error ***************************
calculateFlightError();
// ********************** Update Yaw ***************************************
processHeading();
// ********************** Altitude Adjust **********************************
//processAltitudeHold();
// ********************** Calculate Motor Commands *************************
if (armed && safetyCheck) {
motors.setMotorCommand(FRONT, throttle - motors.getMotorAxisCommand(PITCH) - motors.getMotorAxisCommand(YAW));
motors.setMotorCommand(REAR, throttle + motors.getMotorAxisCommand(PITCH) - motors.getMotorAxisCommand(YAW));
motors.setMotorCommand(RIGHT, throttle - motors.getMotorAxisCommand(ROLL) + motors.getMotorAxisCommand(YAW));
motors.setMotorCommand(LEFT, throttle + motors.getMotorAxisCommand(ROLL) + motors.getMotorAxisCommand(YAW));
}
// *********************** process min max motor command *******************
processMinMaxMotorCommand();
// Allows quad to do acrobatics by lowering power to opposite motors during hard manuevers
if (flightMode == ACRO) {
processHardManuevers();
}
// Apply limits to motor commands
for (byte motor = FRONT; motor < LASTMOTOR; motor++) {
motors.setMotorCommand(motor, constrain(motors.getMotorCommand(motor), motors.getMinCommand(motor), motors.getMaxCommand(motor)));
}
// If throttle in minimum position, don't apply yaw
if (receiver.getData(THROTTLE) < MINCHECK) {
for (byte motor = FRONT; motor < LASTMOTOR; motor++) {
motors.setMotorCommand(motor, MINTHROTTLE);
}
}
// ESC Calibration
if (armed == OFF) {
processCalibrateESC();
}
// *********************** Command Motors **********************
if (armed == ON && safetyCheck == ON) {
motors.write(); // Defined in Motors.h
}
}
#endif