-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathQuadCopter.pde
147 lines (116 loc) · 3.02 KB
/
QuadCopter.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
/*
Quadcopter!!!!
Created by Myles Grant <[email protected]>
See also: https://github.com/grantmd/QuadCopter
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/>.
*/
// Include libs
#include <Wire.h>
#include "Definitions.h"
#include <EEPROM.h>
#include "EEPROM_lib.h"
#include "Gyro.h"
#include "Accel.h"
#include "Baro.h"
#include "Mag.h"
#include "IMU.h"
#include "INS.h"
#include "PID.h"
Gyro gyro;
Accel accel;
Baro baro;
Mag mag;
IMU imu;
INS ins;
#include "Engines.h"
Engines engines;
#include "Receiver.h"
Receiver receiver;
#include "Battery.h"
Battery battery;
// Status
byte systemMode = 0; // 0: Manual, 1: Auto, 2: PID Test
byte activityLight = 0; // 0: Off, 1: On
unsigned long previousTime = 0;
unsigned long currentTime = 0;
unsigned long deltaTime = 0;
unsigned long serialTime = 0;
unsigned long activityTime = 0;
void setup(){
Serial.begin(115200);
Wire.begin(); // For the gyro and accel
//
// Activity LEDs
//
pinMode(RED_LED, OUTPUT);
pinMode(YELLOW_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
//
// Run inits
//
battery.init();
engines.init();
gyro.init();
accel.init();
baro.init();
mag.init();
//
// It's go time
//
previousTime = micros();
}
void loop(){
//
// Measure loop rate
//
currentTime = micros();
deltaTime = currentTime - previousTime;
previousTime = currentTime;
//
// Get the latest data
//
gyro.updateAll();
accel.updateAll();
baro.measure();
mag.updateAll(accel.getYAngle(), accel.getXAngle());
imu.update(deltaTime/1000, gyro.getRoll(), gyro.getPitch(), gyro.getYaw(), accel.getYAngle(), accel.getXAngle(), accel.getZAngle(), mag.getHeadingDegrees());
ins.update(deltaTime/1000, accel.getRoll(), accel.getPitch(), accel.getYaw(), imu.getHeading());
receiver.updateAll();
battery.measure();
//
// Decide what to do and do it (flight control)
//
processFlightControl();
//
// Take commands from.... where? (flight command)
//
processFlightCommand();
//
// Read serial commands and set them/reply
//
if (currentTime > serialTime){
serialTime = currentTime + SERIAL_RATE;
readSerialCommand();
sendSerialTelemetry();
}
if (currentTime > activityTime){
activityTime = currentTime + ACTIVITY_RATE;
if (activityLight == 1){
digitalWrite(GREEN_LED, LOW);
activityLight = 0;
}
else{
digitalWrite(GREEN_LED, HIGH);
activityLight = 1;
}
}
}