-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.c
203 lines (168 loc) · 4.47 KB
/
main.c
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
/*
ChibiCopter - https://github.com/grantmd/ChibiCopter
A quadcopter platform running under ChibiOS/RT.
Based on demo code from ChibiOS/RT
*/
#include <stdlib.h>
#include "ch.h"
#include "hal.h"
#include "Comms.h"
#include "I2CSensor.h"
#include "Accel.h"
#include "Gyro.h"
#include "Spektrum.h"
#include "Motors.h"
#include "GPS.h"
#include <mavlink.h>
#define ST2MS(st) ((st / CH_FREQUENCY) * 1000L)
/*
* This is a periodic thread that blinks some leds
*/
static WORKING_AREA(BlinkWA, 128);
static msg_t Blink(void *arg){
(void)arg;
chRegSetThreadName("blinker");
while (TRUE){
//palSetPad(GPIOD, GPIOD_LED5); // red
chThdSleepMilliseconds(100);
//palClearPad(GPIOD, GPIOD_LED5); // red
palSetPad(GPIOD, GPIOD_LED6); // blue
chThdSleepMilliseconds(100);
palClearPad(GPIOD, GPIOD_LED6); // blue
//palSetPad(GPIOD, GPIOD_LED4); // green
chThdSleepMilliseconds(100);
//palClearPad(GPIOD, GPIOD_LED4); // green
palSetPad(GPIOD, GPIOD_LED3); // orange
chThdSleepMilliseconds(100);
palClearPad(GPIOD, GPIOD_LED3); // orange
}
return 0;
}
/*
* Application entry point.
*/
int main(void){
/*
* System initializations.
* - HAL initialization, this also initializes the configured device drivers
* and performs the board-specific initializations.
* - Kernel initialization, the main() function becomes a thread and the
* RTOS is active.
*/
halInit();
chSysInit();
systime_t startTime = chTimeNow();
/*
* Startup comms
*/
CommsInit();
/*
* Sensor I/O
*/
mavlink_system.state = MAV_STATE_CALIBRATING;
I2CSensorInit();
AccelInit();
GyroInit();
/*
* Receiver I/O
*/
SpektrumInit();
/*
* Motors I/O
*/
MotorsInit();
/*
* GPS
*/
GPSInit();
/*
* Creates the threads
*/
chThdCreateStatic(BlinkWA, sizeof(BlinkWA), NORMALPRIO, Blink, NULL);
/*
* Normal main() thread activity
*/
mavlink_system.state = MAV_STATE_STANDBY;
mavlink_system.mode = MAV_MODE_STABILIZE_DISARMED;
/*******************************************************************
// tasks (microseconds of interval)
ReadGyro ( 5000); // 200hz
ReadAccel ( 5000); // 200hz
RunDCM ( 10000); // 100hz
FlightControls ( 10000); // 100hz
ReadReceiver ( 20000); // 50hz
ReadBaro ( 40000); // 25hz
ReadCompass ( 100000); // 10Hz
ProcessTelem ( 100000); // 10Hz
ReadBattery ( 100000); // 10Hz
ProcessGPS ( 200000); // 5Hz
Heartbeats (1000000); // 1Hz
*******************************************************************/
systime_t previousTime = chTimeNow();
systime_t currentTime = 0;
systime_t deltaTime;
uint8_t frameCounter = 0;
float accelRoll = 0.0;
float accelPitch = 0.0;
float accelYaw = 0.0;
uint8_t gpsFixType = 0;
uint32_t fixAge = 0;
int32_t latitude = 0;
int32_t longitude = 0;
int32_t altitude = 0;
uint16_t velocity = 0;
uint16_t cog = 0;
while (TRUE){
currentTime = chTimeNow();
deltaTime = currentTime - previousTime;
// Main scheduler loop set for 100hz
// TODO: Use sleep until to have the thread sleep until 100hz
if (deltaTime >= US2ST(10000)){
frameCounter++;
/*
* 100hz task loop
*/
if (frameCounter % 1 == 0){
GyroRead();
AccelRead();
}
/*
* 50hz task loop
*/
if (frameCounter % 2 == 0){
MotorsSetSpeed(0, getSpektrumData(THROTTLE_CHANNEL));
}
/*
* 25hz task loop
*/
if (frameCounter % 4 == 0){
}
/*
* 10hz task loop
*/
if (frameCounter % 10 == 0){
accelRoll = AccelGetRollAngle();
accelPitch = AccelGetPitchAngle();
accelYaw = AccelGetYawAngle();
CommsSendAttitude(ST2MS(currentTime - startTime), accelRoll, accelPitch, accelYaw, 0, 0, 0);
}
/*
* 5hz task loop
*/
if (frameCounter % 20 == 0){
// GPS data is read in a separate thread, and here we assume it is ready and we can use it
// My GPS advertises a 5hz rate, so that's how often we send it back over mavlink
CommsSendGPSRaw(ST2MS(currentTime - startTime), gpsFixType, latitude, longitude, altitude, 65535, 65535, velocity, cog, 255);
}
/*
* 1hz task loop
*/
if (frameCounter % 100 == 0){
CommsSendSysStatus();
CommsSendHeartbeat();
}
previousTime = currentTime;
if (frameCounter >= 100) frameCounter = 0;
}
}
}