forked from AeroQuad/AeroQuad
-
Notifications
You must be signed in to change notification settings - Fork 1
/
AeroQuadUVic.pde
491 lines (446 loc) · 17 KB
/
AeroQuadUVic.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*
AeroQuad v2.3 - March 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/>.
*/
/****************************************************************************
Before flight, select the different user options for your AeroQuad below
If you need additional assitance go to http://AeroQuad.com/forum
*****************************************************************************/
/****************************************************************************
************************* Hardware Configuration ***************************
****************************************************************************/
// Select which hardware you wish to use with the AeroQuad Flight Software
//#define AeroQuad_v1 // Arduino 2009 with AeroQuad Shield v1.7 and below
//#define AeroQuad_v1_IDG // Arduino 2009 with AeroQuad Shield v1.7 and below using IDG yaw gyro
//#define AeroQuad_v18 // Arduino 2009 with AeroQuad Shield v1.8
//#define AeroQuad_Wii // Arduino 2009 with Wii Sensors and AeroQuad Shield v1.x
//#define AeroQuadMega_v1 // Arduino Mega with AeroQuad Shield v1.7 and below
#define AeroQuadMega_v2 // Arduino Mega with AeroQuad Shield v2.x
//#define AeroQuadMega_Wii // Arduino Mega with Wii Sensors and AeroQuad Shield v2.x
//#define ArduCopter // ArduPilot Mega (APM) with APM Sensor Board
//#define AeroQuadMega_CHR6DM // Clean Arduino Mega with CHR6DM as IMU/heading ref.
//#define APM_OP_CHR6DM // ArduPilot Mega with CHR6DM as IMU/heading ref., Oilpan for barometer (just uncomment AltitudeHold for baro), and voltage divider
/****************************************************************************
*********************** Define Flight Configuration ************************
****************************************************************************/
// Use only one of the following definitions
#define XConfig
//#define plusConfig
//#define HEXACOAXIAL
//#define HEXARADIAL
// *******************************************************************************************************************************
// Optional Sensors
// Warning: If you enable HeadingHold or AltitudeHold and do not have the correct sensors connected, the flight software may hang
// *******************************************************************************************************************************
// You must define one of the next 3 attitude stabilization modes or the software will not build
// *******************************************************************************************************************************
//#define UseArduPirateSuperStable // Enable the imported stable mode imported from ArduPirate (experimental, use at your own risk)
#define UseAQStable // Enable the older (pre 2.3) AeroQuad Stable mode
//#define UseAttitudeHold // Enable the new for 2.3 Attitude hold mode
#define HeadingMagHold // Enables HMC5843 Magnetometer, gets automatically selected if CHR6DM is defined
#define AltitudeHold // Enables BMP085 Barometer (experimental, use at your own risk)
#define BattMonitor //define your personal specs in BatteryMonitor.h! Full documentation with schematic there
//#define WirelessTelemetry // Enables Wireless telemetry on Serial3 // Wireless telemetry enable
#define BinaryWrite // Enables fast binary transfer of flight data to Configurator
// *******************************************************************************************************************************
// Camera Stabilization
// Servo output goes to D11(pitch), D12(roll), D13(yaw) on AeroQuad v1.8 shield
// If using v2.0 Shield place jumper between:
// D12 to D33 for roll, connect servo to SERVO1
// D11 to D34 for pitch, connect servo to SERVO2
// D13 to D35 for yaw, connect servo to SERVO3
// Please note that you will need to have battery connected to power on servos with v2.0 shield
// *******************************************************************************************************************************
//#define CameraControl
/****************************************************************************
********************* End of User Definition Section ***********************
****************************************************************************/
#include <EEPROM.h>
#include <Wire.h>
#include "AeroQuad.h"
#include "I2C.h"
#include "PID.h"
#include "AQMath.h"
#include "Receiver.h"
#include "DataAcquisition.h"
#include "Accel.h"
#include "Gyro.h"
#include "Motors.h"
// Create objects defined from Configuration Section above
#ifdef AeroQuad_v1
Accel_AeroQuad_v1 accel;
Gyro_AeroQuad_v1 gyro;
Receiver_AeroQuad receiver;
Motors_PWM motors;
#include "FlightAngle.h"
FlightAngle_DCM tempFlightAngle;
FlightAngle *flightAngle = &tempFlightAngle;
#ifdef CameraControl
#include "Camera.h"
Camera_AeroQuad camera;
#endif
#endif
#ifdef AeroQuad_v1_IDG
Accel_AeroQuad_v1 accel;
Gyro_AeroQuad_v1 gyro;
Receiver_AeroQuad receiver;
Motors_PWM motors;
#include "FlightAngle.h"
FlightAngle_DCM tempFlightAngle;
FlightAngle *flightAngle = &tempFlightAngle;
#ifdef CameraControl
#include "Camera.h"
Camera_AeroQuad camera;
#endif
#endif
#ifdef AeroQuad_v18
Accel_AeroQuadMega_v2 accel;
Gyro_AeroQuadMega_v2 gyro;
Receiver_AeroQuad receiver;
Motors_PWMtimer motors;
//Motors_AeroQuadI2C motors; // Use for I2C based ESC's
#include "FlightAngle.h"
FlightAngle_DCM tempFlightAngle;
FlightAngle *flightAngle = &tempFlightAngle;
#ifdef HeadingMagHold
#include "Compass.h"
Magnetometer_HMC5843 compass;
#endif
#ifdef AltitudeHold
#include "Altitude.h"
Altitude_AeroQuad_v2 altitude;
#endif
#ifdef BattMonitor
#include "BatteryMonitor.h"
BatteryMonitor_AeroQuad batteryMonitor;
#endif
#ifdef CameraControl
#include "Camera.h"
Camera_AeroQuad camera;
#endif
#endif
#ifdef AeroQuadMega_v1
// Special thanks to Wilafau for fixes for this setup
// http://aeroquad.com/showthread.php?991-AeroQuad-Flight-Software-v2.0&p=11466&viewfull=1#post11466
Receiver_AeroQuadMega receiver;
Accel_AeroQuad_v1 accel;
Gyro_AeroQuad_v1 gyro;
Motors_PWM motors;
#include "FlightAngle.h"
FlightAngle_DCM tempFlightAngle;
FlightAngle *flightAngle = &tempFlightAngle;
#ifdef CameraControl
#include "Camera.h"
Camera_AeroQuad camera;
#endif
#endif
#ifdef AeroQuadMega_v2
Receiver_AeroQuadMega receiver;
Motors_PWMtimer motors;
//Motors_AeroQuadI2C motors; // Use for I2C based ESC's
Accel_AeroQuadMega_v2 accel;
Gyro_AeroQuadMega_v2 gyro;
#include "FlightAngle.h"
FlightAngle_DCM tempFlightAngle;
FlightAngle *flightAngle = &tempFlightAngle;
#ifdef HeadingMagHold
#include "Compass.h"
Magnetometer_HMC5843 compass;
#endif
#ifdef AltitudeHold
#include "Altitude.h"
Altitude_AeroQuad_v2 altitude;
#endif
#ifdef BattMonitor
#include "BatteryMonitor.h"
BatteryMonitor_AeroQuad batteryMonitor;
#endif
#ifdef CameraControl
#include "Camera.h"
Camera_AeroQuad camera;
#endif
#endif
#ifdef ArduCopter
Gyro_ArduCopter gyro;
Accel_ArduCopter accel;
Receiver_ArduCopter receiver;
Motors_ArduCopter motors;
#include "FlightAngle.h"
FlightAngle_DCM tempFlightAngle;
FlightAngle *flightAngle = &tempFlightAngle;
#ifdef HeadingMagHold
#include "Compass.h"
Magnetometer_HMC5843 compass;
#endif
#ifdef AltitudeHold
#include "Altitude.h"
Altitude_AeroQuad_v2 altitude;
#endif
#ifdef BattMonitor
#include "BatteryMonitor.h"
BatteryMonitor_APM batteryMonitor;
#endif
#endif
#ifdef AeroQuad_Wii
Accel_Wii accel;
Gyro_Wii gyro;
Receiver_AeroQuad receiver;
Motors_PWM motors;
#include "FlightAngle.h"
// FlightAngle_CompFilter tempFlightAngle;
FlightAngle_DCM tempFlightAngle;
FlightAngle *flightAngle = &tempFlightAngle;
#ifdef CameraControl
#include "Camera.h"
Camera_AeroQuad camera;
#endif
#endif
#ifdef AeroQuadMega_Wii
Accel_Wii accel;
Gyro_Wii gyro;
Receiver_AeroQuadMega receiver;
Motors_PWM motors;
#include "FlightAngle.h"
FlightAngle_DCM tempFlightAngle;
FlightAngle *flightAngle = &tempFlightAngle;
#ifdef CameraControl
#include "Camera.h"
Camera_AeroQuad camera;
#endif
#endif
#ifdef AeroQuadMega_CHR6DM
Accel_CHR6DM accel;
Gyro_CHR6DM gyro;
Receiver_AeroQuadMega receiver;
Motors_PWM motors;
#include "FlightAngle.h"
FlightAngle_CHR6DM tempFlightAngle;
FlightAngle *flightAngle = &tempFlightAngle;
#include "Compass.h"
Compass_CHR6DM compass;
#ifdef AltitudeHold
#include "Altitude.h"
Altitude_AeroQuad_v2 altitude;
#endif
#ifdef BattMonitor
#include "BatteryMonitor.h"
BatteryMonitor_APM batteryMonitor;
#endif
#ifdef CameraControl
#include "Camera.h"
Camera_AeroQuad camera;
#endif
#endif
#ifdef APM_OP_CHR6DM
Accel_CHR6DM accel;
Gyro_CHR6DM gyro;
Receiver_ArduCopter receiver;
Motors_ArduCopter motors;
#include "FlightAngle.h"
FlightAngle_CHR6DM tempFlightAngle;
FlightAngle *flightAngle = &tempFlightAngle;
#include "Compass.h"
Compass_CHR6DM compass;
#ifdef AltitudeHold
#include "Altitude.h"
Altitude_AeroQuad_v2 altitude;
#endif
#ifdef BattMonitor
#include "BatteryMonitor.h"
BatteryMonitor_APM batteryMonitor;
#endif
#ifdef CameraControl
#include "Camera.h"
Camera_AeroQuad camera;
#endif
#endif
#ifdef XConfig
void (*processFlightControl)() = &processFlightControlXMode;
#endif
#ifdef plusConfig
void (*processFlightControl)() = &processFlightControlPlusMode;
#endif
#if defined(UseArduPirateSuperStable)
void (*processStableMode)() = &processArdupirateSuperStableMode;
#endif
#if defined(UseAttitudeHold)
void (*processStableMode)() = &processAttitudeMode;
#endif
#if defined(UseAQStable)
void (*processStableMode)() = &processAeroQuadStableMode;
#endif
// Include this last as it contains objects from above declarations
#include "DataStorage.h"
// ************************************************************
// ********************** Setup AeroQuad **********************
// ************************************************************
void setup() {
Serial.begin(BAUD);
pinMode(LEDPIN, OUTPUT);
digitalWrite(LEDPIN, LOW);
#if defined(AeroQuadMega_CHR6DM) || defined(APM_OP_CHR6DM)
Serial1.begin(BAUD);
PORTD = B00000100;
#endif
#if defined(AeroQuad_v18) || defined(AeroQuadMega_v2)
pinMode(LED2PIN, OUTPUT);
digitalWrite(LED2PIN, LOW);
pinMode(LED3PIN, OUTPUT);
digitalWrite(LED3PIN, LOW);
#endif
#ifdef AeroQuadMega_v2
// pins set to INPUT for camera stabilization so won't interfere with new camera class
pinMode(33, INPUT); // disable SERVO 1, jumper D12 for roll
pinMode(34, INPUT); // disable SERVO 2, jumper D11 for pitch
pinMode(35, INPUT); // disable SERVO 3, jumper D13 for yaw
pinMode(43, OUTPUT); // LED 1
pinMode(44, OUTPUT); // LED 2
pinMode(45, OUTPUT); // LED 3
pinMode(46, OUTPUT); // LED 4
digitalWrite(43, HIGH); // LED 1 on
digitalWrite(44, HIGH); // LED 2 on
digitalWrite(45, HIGH); // LED 3 on
digitalWrite(46, HIGH); // LED 4 on
#endif
#if defined(APM_OP_CHR6DM) || defined(ArduCopter)
pinMode(LED_Red, OUTPUT);
pinMode(LED_Yellow, OUTPUT);
pinMode(LED_Green, OUTPUT);
#endif
#if defined(AeroQuad_v18) || defined(AeroQuadMega_v2) || defined(AeroQuad_Wii) || defined(AeroQuadMega_Wii) || defined(AeroQuadMega_CHR6DM) || defined(APM_OP_CHR6DM) || defined(ArduCopter)
Wire.begin();
#endif
#if defined(AeroQuad_v18) || defined(AeroQuadMega_v2)
// Recommendation from Mgros to increase I2C speed to 400kHz
// http://aeroquad.com/showthread.php?991-AeroQuad-Flight-Software-v2.0&p=11262&viewfull=1#post11262
TWBR = 12;
#endif
// Read user values from EEPROM
readEEPROM(); // defined in DataStorage.h
// Configure motors
motors.initialize(); // defined in Motors.h
// Setup receiver pins for pin change interrupts
if (receiverLoop == ON) receiver.initialize(); // defined in Received.h
// Initialize sensors
// If sensors have a common initialization routine
// insert it into the gyro class because it executes first
gyro.initialize(); // defined in Gyro.h
accel.initialize(); // defined in Accel.h
#ifdef AeroQuad_v1_IDG
gyro.invert(YAW);
#endif
// Calibrate sensors
gyro.autoZero(); // defined in Gyro.h
zeroIntegralError();
levelAdjust[ROLL] = 0;
levelAdjust[PITCH] = 0;
// Flight angle estimation
#ifdef HeadingMagHold
compass.initialize();
//setHeading = compass.getHeading();
flightAngle->initialize(compass.getHdgXY(XAXIS), compass.getHdgXY(YAXIS));
#else
flightAngle->initialize(1.0, 0.0); // with no compass, DCM matrix initalizes to a heading of 0 degrees
#endif
// Optional Sensors
#ifdef AltitudeHold
altitude.initialize();
#endif
// Battery Monitor
#ifdef BattMonitor
batteryMonitor.initialize();
#endif
// Camera stabilization setup
#ifdef CameraControl
camera.initialize();
camera.setmCameraRoll(11.11); // Need to figure out nice way to reverse servos
camera.setCenterRoll(1500); // Need to figure out nice way to set center position
camera.setmCameraPitch(11.11);
camera.setCenterPitch(1300);
#endif
previousTime = micros();
digitalWrite(LEDPIN, HIGH);
safetyCheck = 0;
}
// ************************************************************
// ******************** Main AeroQuad Loop ********************
// ************************************************************
void loop () {
// Measure loop rate
currentTime = micros();
deltaTime = currentTime - previousTime;
G_Dt = deltaTime / 1000000.0;
previousTime = currentTime;
#ifdef DEBUG
if (testSignal == LOW) testSignal = HIGH;
else testSignal = LOW;
digitalWrite(LEDPIN, testSignal);
#endif
// Measures sensor data and calculates attitude
if (sensorLoop == ON) {
readSensors(); // defined in Sensors.pde
}
// Combines external pilot commands and measured sensor data to generate motor commands
if (controlLoop == ON) {
processFlightControl();
}
// Reads external pilot commands and performs functions based on stick configuration
if ((receiverLoop == ON) && (currentTime > receiverTime)) {// 50Hz
readPilotCommands(); // defined in FlightCommand.pde
receiverTime = currentTime + RECEIVERLOOPTIME;
}
// Listen for configuration commands and reports telemetry
if ((telemetryLoop == ON) && (currentTime > telemetryTime)) { // 20Hz
readSerialCommand(); // defined in SerialCom.pde
sendSerialTelemetry(); // defined in SerialCom.pde
telemetryTime = currentTime + TELEMETRYLOOPTIME;
}
#ifdef BinaryWrite
// **************************************************************
// ***************** Fast Transfer Of Sensor Data ***************
// **************************************************************
// AeroQuad.h defines the output rate to be 10ms
// Since writing to UART is done by hardware, unable to measure data rate directly
// Through analysis: 115200 baud = 115200 bits/second = 14400 bytes/second
// If float = 4 bytes, then 3600 floats/second
// Experimentally found 15ms output rate produces no flight jitter.
// If 15 ms output rate, then 54 floats/15ms
// Number of floats written using sendBinaryFloat is 15
if ((fastTransfer == ON) && (currentTime > (fastTelemetryTime + FASTTELEMETRYTIME))) {
printInt(21845); // Start word of 0x5555
for (byte axis = ROLL; axis < LASTAXIS; axis++) sendBinaryFloat(gyro.getData(axis));
for (byte axis = XAXIS; axis < LASTAXIS; axis++) sendBinaryFloat(accel.getData(axis));
for (byte axis = ROLL; axis < LASTAXIS; axis++)
#ifdef HeadingMagHold
sendBinaryFloat(compass.getRawData(axis));
#else
sendBinaryFloat(0);
#endif
for (byte axis = ROLL; axis < LASTAXIS; axis++) sendBinaryFloat(flightAngle->getGyroUnbias(axis));
for (byte axis = ROLL; axis < LASTAXIS; axis++) sendBinaryFloat(flightAngle->getData(axis));
printInt(32767); // Stop word of 0x7FFF
fastTelemetryTime = currentTime;
}
#endif
#ifdef CameraControl // Experimental, not fully implemented yet
if ((cameraLoop == ON) && (currentTime > cameraTime)) { // 50Hz
camera.setPitch(degrees(flightAngle->getData(PITCH)));
camera.setRoll(degrees(flightAngle->getData(ROLL)));
camera.setYaw(degrees(flightAngle->getData(YAW)));
camera.move();
cameraTime = currentTime + CAMERALOOPTIME;
}
#endif
}