Skip to content

StateMachine Callbacks #81

@Elan456

Description

@Elan456

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

  1. Based on the current state, update all the state-determining tools (e.g. LaunchDetector, ApogeeDetector, Vertical Velocity Estimator, etc....)
  2. Check if the state should switch
  3. If we change state, call the registered onStateEntry methods

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

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions