Avionics
Core avionics package for CURE flight computers
Loading...
Searching...
No Matches
Serial_Sim.h
Go to the documentation of this file.
1#ifndef SERIAL_SIM_H
2#define SERIAL_SIM_H
3
4#include "Adafruit_Sensor.h"
5#include "ArduinoHAL.h"
8
14class SerialSim {
15public:
16 static SerialSim& getInstance() {
17 static SerialSim instance; // Singleton
18 return instance;
19 }
20
21 void begin(Stream *inStream, BaseStateMachine *stateMachine) {
22 serial_ = inStream;
23 this->stateMachine_ = stateMachine;
24
25 // Handshake: send "START\n" until we get ACK (0x06)
26 uint8_t ack = 0;
27 while (ack != 0x06) {
28 serial_->write("START\n");
29 delay(100);
30 if (serial_->available() > 0) {
31 ack = serial_->read();
32 }
33 }
34 }
35
36 // Non‐blocking update:
37 // 1) Read as many characters as available
38 // 2) If we detect a newline, parse that line
39 void update() {
40 while (serial_->available() > 0) {
41 char c = static_cast<char>(serial_->read());
42
43 // If we get a newline, parse the line
44 if (c == '\n') {
45 // We have a full line in partialLine_
46 handleIncomingLine(partialLine_);
47 partialLine_ = ""; // Clear for the next line
48 }
49 else {
50 partialLine_ += c;
51 }
52 }
53 }
54
55 bool serialAvailable(void){
56 return serial_->available() > 0;
57 }
58
59 // These next update functions provide the sensor data to calling code
60 void updateTimeStamp(float &timestamp) {
61 timestamp = timestamp_;
62 }
63
64 void updateAcl(sensors_event_t *accel) {
65 accel->acceleration.x = accelX_;
66 accel->acceleration.y = accelY_;
67 accel->acceleration.z = accelZ_;
68 }
69
70 void updateGyro(sensors_event_t *gyro) {
71 gyro->gyro.x = gyroX_;
72 gyro->gyro.y = gyroY_;
73 gyro->gyro.z = gyroZ_;
74 }
75
76 void updateMag(sensors_event_t *mag) {
77 mag->magnetic.x = magneticX_;
78 mag->magnetic.y = magneticY_;
79 mag->magnetic.z = magneticZ_;
80 }
81
82 void updateAlt(float &alt){
83 alt = alt_;
84 }
85
86 void updatePres(float &pres){
87 pres = pres_;
88 }
89
90 void updateTemp(sensors_event_t &temp){
91 temp.temperature = temp_;
92 }
93
94private:
95 SerialSim() {} // Private constructor for singleton
96 SerialSim(const SerialSim&) = delete;
97 SerialSim& operator=(const SerialSim&) = delete;
98
99 // Called once a full line of data is received
100 void handleIncomingLine(String &line) {
101 Serial.println("Handling line: " + line);
102 if (line.length() < 5) {
103 Serial.println("Line too short");
104 return; // not enough data to parse anything meaningful
105 }
106
107 // We'll parse each comma‐separated float in the line
108 timestamp_ = parseNextFloat(line);
109 accelX_ = parseNextFloat(line);
110 accelY_ = parseNextFloat(line);
111 accelZ_ = parseNextFloat(line);
112 alt_ = parseNextFloat(line);
113 // gyroX_ = parseNextFloat(line);
114 // gyroY_ = parseNextFloat(line);
115 // gyroZ_ = parseNextFloat(line);
116 // magneticX_ = parseNextFloat(line);
117 // magneticY_ = parseNextFloat(line);
118 // magneticZ_ = parseNextFloat(line);
119
120 // pres_ = parseNextFloat(line);
121 // temp_ = line.toFloat(); // The remaining chunk is the last float
122
123 Serial.println("Parsed Data!");
124
125 // After parsing the line, send an ACK
126 ack();
127 }
128
129 float parseNextFloat(String &data) {
130 int commaIndex = data.indexOf(',');
131 if (commaIndex == -1) {
132 // If there's no comma, parse as much as we can
133 float val = data.toFloat();
134 data = "";
135 return val;
136 }
137
138 // Parse substring up to the comma
139 float value = data.substring(0, commaIndex).toFloat();
140 // Remove parsed portion (including the comma)
141 data = data.substring(commaIndex + 1);
142 return value;
143 }
144
145 // Send ACK alongside the current state to the serial_ port
146 void ack(){
147 serial_->write(stateMachine_->getState());
148 // Ack will be a series of 0xaa, 0xbb, 0xcc
149 serial_->write(0xaa);
150 serial_->write(0xbb);
151 serial_->write(0xcc);
152 }
153
154private:
155 Stream* serial_ = nullptr;
156 BaseStateMachine* stateMachine_ = nullptr;
157 String partialLine_; // used to accumulate characters until newline
158
159 // Parsed sensor data
160 float timestamp_ = 0;
161 float accelX_ = 0, accelY_ = 0, accelZ_ = 0;
162 float gyroX_ = 0, gyroY_ = 0, gyroZ_ = 0;
163 float magneticX_ = 0, magneticY_ = 0, magneticZ_ = 0;
164 float alt_ = 0;
165 float pres_ = 0;
166 float temp_ = 0;
167};
168
169#endif // SERIAL_SIM_H
Base class for flight state machines driven by IMU/altimeter data.
Serial-based sensor/flight simulation singleton for hardware-in-the-loop.
Definition Serial_Sim.h:14
void updateTemp(sensors_event_t &temp)
Definition Serial_Sim.h:90
void updateMag(sensors_event_t *mag)
Definition Serial_Sim.h:76
void updateAlt(float &alt)
Definition Serial_Sim.h:82
void update()
Definition Serial_Sim.h:39
void updatePres(float &pres)
Definition Serial_Sim.h:86
void updateTimeStamp(float &timestamp)
Definition Serial_Sim.h:60
void begin(Stream *inStream, BaseStateMachine *stateMachine)
Definition Serial_Sim.h:21
bool serialAvailable(void)
Definition Serial_Sim.h:55
void updateGyro(sensors_event_t *gyro)
Definition Serial_Sim.h:70
void updateAcl(sensors_event_t *accel)
Definition Serial_Sim.h:64
static SerialSim & getInstance()
Definition Serial_Sim.h:16