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 : dataSaver(dataSaver),
13 launchDetector(launchDetector),
14 apogeeDetector(apogeeDetector),
15 verticalVelocityEstimator(verticalVelocityEstimator),
16 state(STATE_ARMED)
17{
18}
19
20int StateMachine::update(const AccelerationTriplet& accel, const DataPoint& alt) {
21 // Update the state
22 int lpStatus = LP_DEFAULT_FAIL;
23
24 switch (state) {
25 case STATE_ARMED:
26 // Serial.println("lp update");
27 lpStatus = launchDetector->update(accel);
28 // Serial.println(lpStatus);
29 if (launchDetector->isLaunched()) {
30 // Change state to ascent
31 state = STATE_ASCENT;
32
33 // Log the state change
34 dataSaver->saveDataPoint(
37 );
38
39 // Put the data saver into post-launch mode
40 dataSaver->launchDetected(launchDetector->getLaunchedTime());
41
42 // Start the apogee detection system
43 apogeeDetector->init({alt.data, alt.timestamp_ms});
44
45 // Update the vertical velocity estimator
46 verticalVelocityEstimator->update(accel, alt);
47 }
48 break;
49 case STATE_ASCENT:
50 // Serial.println("apogee update");
51 // Update the vertical velocity estimator
52 verticalVelocityEstimator->update(accel, alt);
53 apogeeDetector->update(verticalVelocityEstimator);
54 if (apogeeDetector->isApogeeDetected()) {
55 state = STATE_DESCENT;
56
57 // Log the state change
58 dataSaver->saveDataPoint(
61 );
62 }
63 break;
64
65 case STATE_DESCENT:
66 // Do nothing
67 break;
68 }
69
70 return 0;
71}
72
73uint8_t StateMachine::getState() const {
74 return state;
75}
#define STATE_CHANGE
Definition DataNames.h:26
@ LP_DEFAULT_FAIL
@ STATE_ARMED
Definition States.h:14
@ STATE_ASCENT
Definition States.h:15
@ STATE_DESCENT
Definition States.h:18
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)
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.