Skip to content

Ready-to-Drive (R2D) FSM

The R2D FSM (r2d_fsm.c) implements the strict safety sequence required for a driver to transition the vehicle from an inactive state to an active driving state.

Flow Diagram

States

StateDescription
SYSTEM_R2D_IDLEFSM is inactive.
SYSTEM_R2D_FIRST_BTNThe driver has pressed the R2D button for the first time to initiate the sequence.
SYSTEM_R2D_PRECH_ONGOINGWaiting for the precharge circuit to close and stabilize via a non-blocking step sequencer.
SYSTEM_R2D_SECOND_BTNPrecharge is complete; waiting for the driver to press the R2D button a second time while holding the brake pedal.
SYSTEM_R2D_BUZZINGThe R2D sequence is successful. The vehicle sounds the R2D buzzer for a mandated duration to alert bystanders.

Transitions & Events

The sequence flows linearly through the states if conditions are met:

  1. First Button Press: Initiates the transition from IDLE to FIRST_BTN, which immediately falls through to PRECH_ONGOING.
  2. Precharge Completion: While in PRECH_ONGOING, the FSM executes a non-blocking step-sequencer using HAL_GetTick() to manage contactor and relay settling times (AIR- closed → 300ms → Precharge closed → wait for voltage comparator (>1s minimum) → AIR+ closed → 300ms → Precharge opened → 300ms → done). Once complete, it moves to SECOND_BTN.
    • Timeout: If precharge takes too long (precharge_timeout of 15 seconds), the FSM flags an ECU_FSM_REASON_FAULT_PRECHARGE_TIMEOUT and forces the ECU FSM to SYSTEM_SHUTDOWN.
  3. Second Button & Brake: In SECOND_BTN, the FSM waits for button_pressed and brake_ok to be true simultaneously. When this happens, it transitions to BUZZING.
  4. Buzzer Completion: In BUZZING, it waits for buzzer_elapsed. Once finished, it requests the ECU FSM to transition to SYSTEM_CONTROL (vehicle is now active).

Released under the MIT License.