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

Introduction

The AMS Controller is the subsystem responsible for the supervision and management of the Prohelion BMS.

The intent of the AMS Controller is to:

  • Ensure the accumulator is engaged and providing power to the motor when required.
  • Ensure the accumulator is only engaged when it is asked too and the relevant safety requirements are met.
  • Provide feedback to the other car subsystems as to whether the accumulator is engaged.

Why not integrate this into the SCUM?

In short: Complexity and Safety.

Expanding on this, having everything handled by one monolithic system (e.g. the SCUM) is fine until it isn’t. When a finite state machine isn’t used, a system can get stuck in weird cases and lockups potentially causing a preventable incident to occur. This is especially important in safety-critical systems like the accumulator where it has the potential to maim or cause death if it isn’t adequetely respected.

From a complexity perspective, having so many moving parts inside a system can cause breakage unintentionally and makes it hard when multiple incompatible standards need to be supported. In an embedded environment, MCUs have a limited number of peripherals, GPIO power than can be drawn by other components and system clock speed. From a safety perspective, having one small system which is wholely focused on a single task is vastly easier to implement correctly, safely and auditably.

Setup

This section will guide you through how to setup the AMS Controller with the BMS and test it to ensure that it works.

Hardware Items

  • STM32 Nucleo G431KB
  • CAN Transceiver (e.g. Microchip MCP2561)
  • Prohelion D1000 Gen 2 w/ Modules

Flashing the Firmware

Important

If the Nucleo has already been flashed with the correct firmware then this step isn’t needed.

If you’re building from source then see building from source, otherwise this section is assuming you’re flashing prebuilt firmware to the board.

To flash the prebuilt firmware, you can use any tool which supports flashing ELF binaries. This tutorial will use probe-rs as the tool to flash since it can automatically hook into the logging outputs. To install probe-rs, see their website.

Once you’ve downloaded the firmware into a directory, open up a terminal and flash the board by running:

probe-rs run --chip STM32G431KB --preverify --verify [[PATH TO FIRMWARE]]

Note

To quickly explain this command:

  • The run subcommand tells probe-rs to flash the board and attach to the output.
  • The --chip flag tells probe-rs what the microcontroller is.
  • The --preverify flag tells probe-rs to not write if the firmware is identical.
  • The --verify flag tells probe-rs to read back what was written and ensure it is correct.

Once you start getting logging output from the board which will be indicated by output which has INFO, you can then Ctrl+C to exit from probe-rs and disconnect.

Setting up the Hardware

Note

This assumes you’re using a breadboard and not a perf board.

On the breadboard, add both the Nucleo board and CAN transceiver and then grab some breadboard wires to connect them up. The wiring is as follows:

Nucleo PinTransceiver PinLine Description
D2 (PA12)TXDCAN TX Line
D10 (PA11)RXDCAN RX Line
+3V3VDD3.3V Power Supply
GND (choose any one)VSSGround

Additionally, the Nucleo board is connected to the ready to drive (R2D) line, which terminates at the A0 pin. For testing purposes, connecting this line to ground will set R2D to low (preventing engagement) and connecting it to power (i.e. floating) will set R2D to high.

Important

The firmware only cares whether the R2D line is driven low or not as it assumes the line will be floating when R2D is enabled.

Now that the board is setup, it can be connected up to the BMS and used.

Caution

Neither the BMS or the AMS Board should be powered during this process as the firmware will enable the accumulator if the R2D line is not pulled low.

The BMS provides a male DB9 connection for use by the AMS or other peripherals, to connect the CAN transceiver to the BMS, the wiring is as follows:

Transceiver PinDB9 Connector
CANHPin 7 (CAN_H)
CANLPin 2 (CAN_L)
GND (From Nucleo Board)Pin 3 (GND)

Once everything is connected, connect the R2D line to ground to prevent automatic engagement of the accumulator. After this, power up the BMS and the AMS board and then connect the R2D line to the +3V3 line on the Nucleo to allow it to engage the accumulator.

Building From Source

The firmware is built on Rust with the flip-link linker.

To install Rust and flip-link.

To build:

  • Get the source code from either the repo or a tarball
  • cargo build - This will grab the nightly toolchain and build the firmware and output it to target/thumbv7em-none-eabihf/debug as ams_controller
  • Flash it as per the instructions in setup

If you’re debugging and continually testing changes to the code, replace cargo build with cargo run and it will automatically call probe-rs

Controlling the Controller

CAN Messages

The controller is controlled over CAN messages. This page documents what CAN messages can be sent to and from the controller.

EnableMsg

ID: 0x300

Length: 1

Input/Output: Input

Description: Controls whether the AMS Controller should enable the BMS

NameCommentTypeStart ByteLength (Bytes)
EnabledWhether to enable to BMSbool01

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

Glossary

TermExpandedDefinition
AMSAccumulator Management SystemThe subsystem of the car responsible for the monitoring and safe usage of the BMS both internally and externally facing.
BMSBattery Management SystemThe subsystem of the car responsible for the management of the actual battery segments and modules themself.
SCUMShutdown, Control and Universal Monitoring UnitThe subsystem responsible for looking after the bigger picture. It has the final say in the matter and is responsible for the external safety alerts.