Avionics
Core avionics package for CURE flight computers
Loading...
Searching...
No Matches
StateMachine.cpp
Go to the documentation of this file.
1#include "ArduinoHAL.h"
2
6
7
9 LaunchDetector* launchDetector,
10 ApogeeDetector* apogeeDetector,
11 VerticalVelocityEstimator* verticalVelocityEstimator,
12 FastLaunchDetector* fastLaunchDetector)
13 : dataSaver(dataSaver),
14 launchDetector(launchDetector),
15 apogeeDetector(apogeeDetector),
16 verticalVelocityEstimator(verticalVelocityEstimator),
17 fastLaunchDetector(fastLaunchDetector),
18 state(STATE_ARMED)
19{
20}
21
22int StateMachine::update(const AccelerationTriplet& accel, const DataPoint& alt) {
23 // Update the state
24 int lpStatus = LP_DEFAULT_FAIL;
25 int fldStatus = FLD_DEFAULT_FAIL;
26
27 switch (state) {
28 case STATE_ARMED:
29 // Serial.println("lp update");
30 lpStatus = launchDetector->update(accel);
31 fldStatus = fastLaunchDetector->update(accel);
32 // Serial.println(lpStatus);
33 if (fastLaunchDetector->hasLaunched()) {
34 // Change state to soft ascent
35 state = STATE_SOFT_ASCENT;
36
37 // Save the FLD launch time
38 fldLaunchTime_ms = fastLaunchDetector->getLaunchedTime();
39
40 // Log the state change
41 dataSaver->saveDataPoint(
44 );
45
46 // Put the data saver into post-launch mode
47 dataSaver->launchDetected(fastLaunchDetector->getLaunchedTime());
48 }
49
50 // The FLD should always trigger before the LP, but we check for LP launch just in case
51 if (launchDetector->isLaunched()) {
52 // Change state to ascent
53 state = STATE_ASCENT;
54
55 // Log the state change
56 dataSaver->saveDataPoint(
59 );
60
61 // Put the data saver into post-launch mode
62 dataSaver->launchDetected(launchDetector->getLaunchedTime());
63
64 // Start the apogee detection system
65 apogeeDetector->init({alt.data, alt.timestamp_ms});
66
67 // Update the vertical velocity estimator
68 verticalVelocityEstimator->update(accel, alt);
69 }
70 break;
71
73 /*
74 * In soft ascent, we are waiting for confirmation of launch from the LaunchDetector.
75 * If LaunchDetector confirms launch within the confirmation window, we transition to ASCENT.
76 * If the confirmation window passes without confirmation, we revert to ARMED
77 * and clear post-launch mode.
78 */
79 // Serial.println("lp update");
80 lpStatus = launchDetector->update(accel);
81 // Serial.println(lpStatus);
82 if (launchDetector->isLaunched()) {
83 // Change state to ascent
84 state = STATE_ASCENT;
85
86 // Log the state change
87 dataSaver->saveDataPoint(
90 );
91
92 // Start the apogee detection system
93 apogeeDetector->init({alt.data, alt.timestamp_ms});
94
95 // Update the vertical velocity estimator
96 verticalVelocityEstimator->update(accel, alt);
97 }
98 else if (accel.x.timestamp_ms - fldLaunchTime_ms > fastLaunchDetector->getConfirmationWindow()) {
99 // If the confirmation window has passed without launch detected by LaunchDetector,
100 // revert to ARMED state
101 state = STATE_ARMED;
102 fldLaunchTime_ms = 0;
103 fastLaunchDetector->reset();
104
105 // Log the state change
106 dataSaver->saveDataPoint(
109 );
110
111 // Clear post-launch mode
112 dataSaver->clearPostLaunchMode();
113 }
114 break;
115
116 case STATE_ASCENT:
117 // Serial.println("apogee update");
118 // Update the vertical velocity estimator
119 verticalVelocityEstimator->update(accel, alt);
120 apogeeDetector->update(verticalVelocityEstimator);
121 if (apogeeDetector->isApogeeDetected()) {
122 state = STATE_DESCENT;
123
124 // Log the state change
125 dataSaver->saveDataPoint(
128 );
129 }
130 break;
131
132 case STATE_DESCENT:
133 // Do nothing
134 break;
135 }
136
137 return 0;
138}
139
140uint8_t StateMachine::getState() const {
141 return state;
142}
#define STATE_CHANGE
Definition DataNames.h:26
@ FLD_DEFAULT_FAIL
@ LP_DEFAULT_FAIL
@ STATE_ARMED
Definition States.h:14
@ STATE_ASCENT
Definition States.h:16
@ STATE_SOFT_ASCENT
Definition States.h:15
@ STATE_DESCENT
Definition States.h:19
Detects the apogee (peak altitude) of a rocket flight using estimated altitude and vertical velocity.
Timestamped float measurement container.
Definition DataPoint.h:11
float data
Definition DataPoint.h:14
uint32_t timestamp_ms
Definition DataPoint.h:13
Abstract interface for persisting timestamped data points.
Definition DataSaver.h:13
Sliding-window launch detector based on acceleration magnitude.
StateMachine(IDataSaver *dataSaver, LaunchDetector *launchDetector, ApogeeDetector *apogeeDetector, VerticalVelocityEstimator *verticalVelocityEstimator, FastLaunchDetector *fastLaunchDetector)
Wire dependencies for the state machine.
uint8_t getState() const override
Retrieve the current state value.
int update(const AccelerationTriplet &accel, const DataPoint &alt) override
Process new sensor data and transition states if thresholds are met.
1D Kalman filter fusing altimeter and accelerometer data.