Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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:

InputSourceTimeoutOn timeout
BMS info (state & faults)BMS BMSInfo framesBMS_RX_TIMEOUTBMS state is treated as unknown
AMS enabledController enable heartbeatAMS_ENABLE_TIMEOUTAMS 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

StateSection
InitFirmware Initialisation
WaitForBMSWait For BMS
SafeSafe
ConnectingConnecting
EnabledEnabled
FailureFailure

Firmware Initialisation

Enters this state on startup. This is where the firmware initialises its memory heap, logging, peripherals and watchdog.

TransitionsRequirements
Init → WaitForBMSFirmware 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.

TransitionsRequirements
WaitForBMS → SafeBMS 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.

TransitionsRequirements
Safe → ConnectingBMS in idle & no BMS faults & AMS enabled & not awaiting re-arm
Safe → WaitForBMSBMS 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.

TransitionsRequirements
Connecting → EnabledBMS is enabled & no BMS faults & AMS enabled
Connecting → SafeNo BMS faults & AMS disabled
Connecting → FailureBMS 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.

TransitionsRequirements
Enabled → SafeNo BMS faults & AMS disabled
Enabled → FailureBMS 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.

TransitionsRequirements
Failure → WaitForBMSNo BMS faults continuously for the fixed timeout