State Machine
Deterministic state machine for the engine start / run / shutdown cycle.
Header: core/ecu_sm.h
-
enum ecu_state_t
Engine operating states.
Values:
-
enumerator ECU_STATE_OFF
Controller inactive.
-
enumerator ECU_STATE_PRESTART
Sensor warmup / self-check.
-
enumerator ECU_STATE_SPINUP
Starter driving shaft to minimum RPM.
-
enumerator ECU_STATE_IGNITION
Igniter + first fuel injection.
-
enumerator ECU_STATE_RAMP
Accelerating to target RPM.
-
enumerator ECU_STATE_RUN
Stable operation under PID control.
-
enumerator ECU_STATE_COOLDOWN
Lowering EGT before shutdown.
-
enumerator ECU_STATE_SHUTDOWN
Engine stopped, outputs safe.
-
enumerator ECU_STATE_FAULT
Safety violation — awaiting reset.
-
enumerator ECU_STATE_OFF
-
enum sm_result_t
Result of a state transition attempt.
Values:
-
enumerator SM_NO_CHANGE
State unchanged.
-
enumerator SM_TRANSITION
Transition occurred.
-
enumerator SM_NO_CHANGE
-
typedef void (*ecu_sm_callback_t)(ecu_state_t state, void *ctx)
Callback invoked on state entry or exit.
-
void ecu_sm_init(ecu_sm_t *sm)
Initialise the state machine (state = OFF).
- Parameters:
sm – [out] State machine context.
-
sm_result_t ecu_sm_transition(ecu_sm_t *sm, ecu_state_t next, ecu_time_t now)
Attempt a state transition.
- Parameters:
sm – [inout] State machine context.
next – [in] Target state.
now – [in] Current timestamp (ms).
- Returns:
SM_TRANSITIONon success,SM_NO_CHANGEotherwise.
-
ecu_state_t ecu_sm_state(const ecu_sm_t *sm)
Get the current state.
- Parameters:
sm – [in] State machine context.
- Returns:
Current state.
-
const char *ecu_sm_state_name(ecu_state_t state)
Get a human-readable name for the given state.
- Parameters:
state – [in] State value.
- Returns:
Null-terminated state name string.
-
void ecu_sm_set_callbacks(ecu_sm_t *sm, ecu_sm_callback_t on_enter, ecu_sm_callback_t on_exit, void *ctx)
Register entry/exit callbacks.
- Parameters:
sm – [inout] State machine context.
on_enter – [in] Called after entering a new state (may be NULL).
on_exit – [in] Called before leaving the current state (may be NULL).
ctx – [in] User context forwarded to callbacks.
-
void ecu_sm_set_timeout(ecu_sm_t *sm, ecu_time_t timeout_ms)
Set a timeout for the current state.
- Parameters:
sm – [inout] State machine context.
timeout_ms – [in] Timeout in milliseconds (0 = disable).
-
bool ecu_sm_timed_out(const ecu_sm_t *sm, ecu_time_t now)
Check whether the current state has exceeded its timeout.
- Parameters:
sm – [in] State machine context.
now – [in] Current timestamp (ms).
- Returns:
trueif timed out.
-
ECU_STATE_COUNT
Total number of states.
-
struct ecu_sm_t
- #include <ecu_sm.h>
State machine runtime context.
Public Members
-
ecu_state_t current
Current state.
-
ecu_time_t state_enter_time
Timestamp of last entry.
-
ecu_time_t timeout_ms
Per-state timeout (0 = none).
-
ecu_sm_callback_t on_enter
Entry callback (may be NULL).
-
ecu_sm_callback_t on_exit
Exit callback (may be NULL).
-
void *cb_ctx
User context for callbacks.
-
ecu_state_t current