This repository was archived by the owner on Jan 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflight-computer.ino
More file actions
71 lines (59 loc) · 1.45 KB
/
flight-computer.ino
File metadata and controls
71 lines (59 loc) · 1.45 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
#include "bmp3xx_api.h"
#include "lsm9ds1_api.h"
#include "fsm.h"
#include "mosfet_igniter.h"
#include "adafruit_gps_api.h"
#include "definitions.h"
LSM9DS1_API imu_sensor = LSM9DS1_API::getInstance();
BMP3XX_API altimeter = BMP3XX_API::getInstance();
Telemetry telemetry = Telemetry::getInstance();
MosfetIgniter igniter = MosfetIgniter::getInstance();
Adafruit_GPS_API gps = Adafruit_GPS_API::getInstance();
FSM *fsm;
// TODO: improve buzzer code
const int buzzer = 17;
bool sound = false;
uint8_t loop_frequency = 10; // Hz
void setup() {
#if SERIAL_DEBUG
Serial.begin(115200);
#endif
#if BUZZER
pinMode(buzzer, OUTPUT);
tone(buzzer, 500);
#endif
fsm = new FSM(&telemetry, &imu_sensor, &altimeter, &gps, &igniter, &loop_frequency);
#if BUZZER
noTone(buzzer);
#endif
}
void loop() {
int timerStart = millis();
if (telemetry.messageAvailable()) {
String message = telemetry.receiveMessage();
if (message.substring(0, 5).equals("SPCMD")) {
int event;
if (sscanf(message.c_str(), "SPCMD:%i--", &event) == 1) {
fsm->process_event((EVENT)event);
}
}
}
#if SERIAL_DEBUG
if (Serial.available()) {
int a = Serial.parseInt();
fsm->process_event((EVENT)a);
}
#endif
#if BUZZER
if (sound) {
sound = false;
noTone(buzzer);
} else {
sound = true;
tone(buzzer, 500);
}
#endif
fsm->runCurrentState();
int delayTime = (1000/loop_frequency) - (millis() - timerStart);
delay(max(0, delayTime));
}