State Machine

The state machine library provides a generic interface for initializing, updating, and querying the flight state. The simple state machine implementation defines a 9-state flight sequence driven by sensor thresholds.

typedef int (*sm_error_cb_t)(void *args)

Callback invoked when the state machine encounters an error.

The implementation can define specific recovery logic.

Param args:

Pointer to an implementation-specific config structure.

Return:

0 if the error was mitigated, negative errno otherwise.

void sm_init(const struct sm_thresholds *cfg, struct sm_error_handling_args *err_hdl)

Initialize the rocket state machine.

This function prepares the state machine, loads the threshold configuration, initializes internal timers, and sets the initial state to SM_IDLE.

Parameters:
  • cfg – Pointer to a threshold configuration structure.

  • err_hdl – Pointer to an error handling configuration (callback + args), or NULL.

void sm_deinit(void)

Deinitialize the rocket state machine.

This function resets the state machine, unloads the threshold configuration, stops internal timers, and sets the initial state.

void sm_update(const struct sm_inputs *inputs)

Update the state machine using current sensor readings.

Function evaluates sensor data and executes state transitions according to the flight logic diagram. Must be called regularly (e.g. at sensor update rate).

Parameters:

inputs – Pointer to populated sensor readings.

enum sm_state sm_get_state(void)

Retrieve the current state of the state machine.

Returns:

Current state (usually an enum implementation in state implementation).

struct sm_thresholds
#include <simple.h>

Threshold configuration for the rocket state machine.

Defines thresholds for state transitions based on orientation, altitude, acceleration, and timing.

struct sm_inputs
#include <simple.h>

Sensor input structure for the state machine.

These values must be filled each update cycle to evaluate state transitions.

struct sm_error_handling_args
#include <state.h>

Error handling configuration for the state machine.