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)
14 dataSaver_(dataSaver),
15 launchDetector_(launchDetector),
16 apogeeDetector_(apogeeDetector),
17 verticalVelocityEstimator_(verticalVelocityEstimator),
18 fastLaunchDetector_(fastLaunchDetector)
19{
20}
21
22int StateMachine::update(const AccelerationTriplet& accel, const DataPoint& alt) {
23 // Update the state.
24 switch (getFlightState()) {
25 case STATE_ARMED:
26
27 // Update launch detector and check for launch detection
28 // As soon as this is true, jump straight to ascent, regardless of the FLD
29 launchDetector_->update(accel);
30 if (launchDetector_->isLaunched()) {
31 // Change state to ascent.
33
34 // Log the state change.
35 dataSaver_->saveDataPoint(
38 );
39
40 // Put the data saver into post-launch mode
41 dataSaver_->launchDetected(launchDetector_->getLaunchedTime());
42
43 // Start the apogee detection system
44 apogeeDetector_->init({alt.data, alt.timestamp_ms});
45
46 // Update the vertical velocity estimator
47 verticalVelocityEstimator_->update(accel, alt);
48 return 0;
49 }
50
51
52 // If the launch detector didn't trigger, check the fast launch detector
53 // The fast launch detector should trigger before the launch detector, but it is more susceptible to noise.
54 // This allows us to jump to a soft ascent while waiting for the launch detector to confirm.
55 fastLaunchDetector_->update(accel);
56 if (fastLaunchDetector_->hasLaunched()) {
57 // Change state to soft ascent.
59
60 // Save the FLD launch time
61 fldLaunchTime_ms_ = fastLaunchDetector_->getLaunchedTime();
62
63 // Log the state change.
64 dataSaver_->saveDataPoint(
67 );
68
69 // Put the data saver into post-launch mode
70 dataSaver_->launchDetected(fastLaunchDetector_->getLaunchedTime());
71 return 0;
72 }
73 break;
74
76 /*
77 * In soft ascent, we are waiting for confirmation of launch from the LaunchDetector.
78 * If LaunchDetector confirms launch within the confirmation window, we transition to ASCENT.
79 * If the confirmation window passes without confirmation, we revert to ARMED
80 * and clear post-launch mode.
81 */
82 // Serial.println("lp update");
83 launchDetector_->update(accel);
84 if (launchDetector_->isLaunched()) {
85 // Change state to ascent.
87
88 // Log the state change.
89 dataSaver_->saveDataPoint(
92 );
93
94 // Start the apogee detection system
95 apogeeDetector_->init({alt.data, alt.timestamp_ms});
96
97 // Update the vertical velocity estimator
98 verticalVelocityEstimator_->update(accel, alt);
99 return 0;
100 }
101 if (accel.x.timestamp_ms - fldLaunchTime_ms_ > fastLaunchDetector_->getConfirmationWindow()) {
102 // If the confirmation window has passed without launch detected by LaunchDetector,
103 // Revert to ARMED state.
105 fldLaunchTime_ms_ = 0;
106 fastLaunchDetector_->reset();
107
108 // Log the state change.
109 dataSaver_->saveDataPoint(
112 );
113
114 // Clear post-launch mode
115 dataSaver_->clearPostLaunchMode();
116 return 0;
117 }
118 break;
119
120 case STATE_ASCENT:
121 // Serial.println("apogee update");
122 // Update the vertical velocity estimator
123 verticalVelocityEstimator_->update(accel, alt);
124 apogeeDetector_->update(verticalVelocityEstimator_);
125 if (apogeeDetector_->isApogeeDetected()) {
127
128 // Log the state change.
129 dataSaver_->saveDataPoint(
132 );
133 }
134 return 0;
135
136 case STATE_DESCENT:
137 return 0; // Do nothing state
138
139 default:
140 // Unexpected state, error return
141 return 1;
142 }
143
144 return 0;
145}
#define STATE_CHANGE
Definition DataNames.h:26
@ STATE_ARMED
Definition States.h:19
@ STATE_ASCENT
Definition States.h:21
@ STATE_SOFT_ASCENT
Definition States.h:20
@ STATE_DESCENT
Definition States.h:24
Detects the apogee (peak altitude) of a rocket flight using estimated altitude and vertical velocity.
bool changeState(FlightState newState)
Transition to a new state and trigger registered on-entry callbacks.
FlightState getFlightState() const
Current state as FlightState enum.
BaseStateMachine(FlightState initialState=STATE_UNARMED)
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.
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.