FSM Architecture
This page documents the FSM’s architecture and how it handles the unpredictability of the BMS while still providing state guarantees.
--- title: AMS State Diagram --- stateDiagram-v2 direction LR [*] --> Init # Core States Init --> WaitForBMS WaitForBMS --> Safe Safe --> Connecting Connecting --> Enabled Connecting --> Safe Enabled --> Safe Safe --> WaitForBMS # Error States Connecting --> Failure Enabled --> Failure Failure --> WaitForBMS
Inputs & Timeouts
The FSM is driven by two inputs, both of which expire if not refreshed:
| Input | Source | Timeout | On timeout |
|---|---|---|---|
| BMS info (state & faults) | BMS BMSInfo frames | BMS_RX_TIMEOUT | BMS state is treated as unknown |
| AMS enabled | Controller enable heartbeat | AMS_ENABLE_TIMEOUT | AMS is treated as disabled |
An unknown BMS state never satisfies a transition that requires a specific BMS state, and while Connecting or Enabled it is treated as a fault.
“BMS faults” means any fault flag reported by the BMS, including CONTROL_TIMEOUT.
The fault relay is driven high while the BMS info is current and reports no faults, and low otherwise.
Re-arming after a failure
Entering Failure latches a rearm required flag, which is only cleared once the AMS is disabled (explicitly or by its enable heartbeat timing out).
While it is set the AMS cannot progress from Safe → Connecting, so the accumulator never reconnects after a failure without a fresh enable request.
Detailed State Explanations
| State | Section |
|---|---|
Init | Firmware Initialisation |
WaitForBMS | Wait For BMS |
Safe | Safe |
Connecting | Connecting |
Enabled | Enabled |
Failure | Failure |
Firmware Initialisation
Enters this state on startup. This is where the firmware initialises its memory heap, logging, peripherals and watchdog.
| Transitions | Requirements |
|---|---|
| Init → WaitForBMS | Firmware initialisation was successful |
Wait For BMS
The firmware will request the BMS to enter the idle state and wait until it enters such a state. Will wait until the BMS has booted if necessary.
| Transitions | Requirements |
|---|---|
| WaitForBMS → Safe | BMS is reporting an idle state |
Safe
The AMS will sit in this state until it has been requested to connect the BMS.
It will regress to the WaitForBMS state if the BMS reports it isn’t in an idle, connect or precharge state (e.g. still enabled after leaving Enabled), or if BMS info is lost.
| Transitions | Requirements |
|---|---|
| Safe → Connecting | BMS in idle & no BMS faults & AMS enabled & not awaiting re-arm |
| Safe → WaitForBMS | BMS not in idle, connect or precharge, or BMS state unknown |
Connecting
The BMS is connecting & undergoing its precharge sequence. Waits for the BMS to report success or failure before progressing to the relevant state. Regresses to safe if the AMS is disabled. Fails if the BMS leaves the connect sequence (idle → connect → precharge → enabled) without being asked to, even without reporting a fault.
| Transitions | Requirements |
|---|---|
| Connecting → Enabled | BMS is enabled & no BMS faults & AMS enabled |
| Connecting → Safe | No BMS faults & AMS disabled |
| Connecting → Failure | BMS faults, BMS state unknown, or BMS outside the connect sequence; AMS either enabled/disabled |
Enabled
The BMS is enabled and is able to drive the car.
Can return to the safe state if requested or progresses to the Failure state if faults occur or the BMS leaves the enabled state unrequested.
| Transitions | Requirements |
|---|---|
| Enabled → Safe | No BMS faults & AMS disabled |
| Enabled → Failure | BMS faults, BMS state unknown, or BMS not enabled; AMS either enabled/disabled |
Failure
The BMS has encountered a fault (or behaved unexpectedly) and has automatically disengaged contactors.
The AMS requests idle and waits until the BMS has continuously reported no faults for a fixed period (FAILURE_TIMEOUT) before automatically progressing to the WaitForBMS state.
The period restarts if faults reappear or BMS info is lost.
The AMS must be disabled and re-enabled before it will connect again, see Re-arming after a failure.
| Transitions | Requirements |
|---|---|
| Failure → WaitForBMS | No BMS faults continuously for the fixed timeout |