-
Notifications
You must be signed in to change notification settings - Fork 1
Description
There have been multiple instances where we want something to trigger when the rocket switches into a new state.
For example, we need to tell the GroundLevelEstimator and DataSaver when launch happens.
The idea is to have a way to register functions with the state machine to be called when certain events happen.
Example
Example prototype
bool StateMachine::registerOnStateEntry(FlightState state, void (*fn)());Once registered, the state machine will call any associated functions it enters into the set state.
Example utilization (imagine a main.cpp for MARTHA or JEM)
// Creating a function with no parameters to be registered for calling on launch
static void OnLaunch() {
groundLevelEstimator.launchDetected();
dataSaver->launchDetected(launchDetector->getLaunchedTime());
led_toggle_delay = 50; // Make the LED blink faster
}
// Registering our OnLaunch function with the state machine when entering the ascent state
void setup(){
stateMachine.registerOnStateEntry(STATE_ASCENT, &OnLaunch)
}
Motiviation
The state machine tools provided by Avionics are meant to be generic to any kind of flight computer. Instead of creating a bunch of state machines that trigger different things depending on the needs of each flight computer, we can have a few state machines that can have callbacks registered to them.
All the code in the state machine should be towards doings the following two things.
- Changing the state
- Handling the callbacks
We don't want the state machine to get cluttered with data saver, ground level estimator, telemetry, and other calls. Instead, it just does the generic callbacks which are flight-computer defined.
State machine logic is just
- Based on the current state, update all the state-determining tools (e.g. LaunchDetector, ApogeeDetector, Vertical Velocity Estimator, etc....)
- Check if the state should switch
- If we change state, call the registered
onStateEntrymethods
When to make a new state machine
A new state machine should be made when you want to use different states or different tools to transition between states than other state machines, not when you want to call external tools at different times.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status