-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.c
More file actions
179 lines (148 loc) · 3.86 KB
/
main.c
File metadata and controls
179 lines (148 loc) · 3.86 KB
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
/*
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 "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;
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 readGyro ( 5000); // 200hz
ReadAccel readAccel ( 5000); // 200hz
RunDCM runDCM ( 10000); // 100hz
FlightControls flightControls( 10000); // 100hz
ReadReceiver readReceiver ( 20000); // 50hz
ReadBaro readBaro ( 40000); // 25hz
ReadCompass readCompass ( 100000); // 10Hz
ProcessTelem processTelem ( 100000); // 10Hz
ReadBattery readBattery ( 100000); // 10Hz
*******************************************************************/
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;
while (TRUE){
currentTime = chTimeNow();
deltaTime = currentTime - previousTime;
// Main scheduler loop set for 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);
}
/*
* 1hz task loop
*/
if (frameCounter % 100 == 0){
CommsSendSysStatus();
CommsSendHeartbeat();
}
previousTime = currentTime;
if (frameCounter >= 100) frameCounter = 0;
}
}
}