Active Magnetic Bearing Controller
Software Architecture and Function Reference
This document was generated from the supplied
main.c. It documents the overall architecture, state machine, module interactions, and the principal functions. It is suitable as a Blogger article and can be extended as the project evolves.
1. Overview
The firmware implements a closed-loop Active Magnetic Bearing (AMB) controller on an STM32H753. The software controls five magnetic bearing axes using AD7606 ADCs, an AD5361 16-channel DAC, two SH1106 OLED displays, and a finite state machine for safe operation and parameter tuning.
2. Hardware Interfaces
| Peripheral | Purpose |
|---|---|
| SPI2 | AD7606 ADC acquisition |
| SPI6 | AD5361 DAC output |
| I2C2/I2C3 | Dual SH1106 OLED displays |
| TIM14 | 300 µs control loop scheduler |
| TIM13 | 250 ms heartbeat, menu scan and long-press detection |
| GPIO EXTI | Immediate state transitions |
3. Software Architecture
main()
|
------------------------
| |
Hardware Init Variable Init
| |
-----------+------------
|
SAFE_MAIN_MENU
|
+-----------+-----------+
| |
TIM13 Interrupt Main Loop
| |
| OLED Update
|
Menu Navigation
|
+-----> RUN_STATE
|
TIM14 Interrupt
|
Read ADC -> Control -> DAC
4. Finite State Machine
SAFE_MAIN_MENU
|
+--> Axis Menu
|
+--> Parameter Menu
|
+--> Parameter Edit
SAFE_MAIN_MENU
|
+--> Orbit
|
+--> Manual
|
+--> ADC Display
|
+--> PA Gain
Long SW4
|
V
RUN_STATE
SW1 during RUN
|
V
SAFE_MAIN_MENU
5. Main Program Flow
- HAL initialization
- Clock configuration
- Peripheral initialization
- SPI speed configuration
- Timer configuration
- OLED initialization
- DAC reset and initialization
- ADC reset
- Controller parameter initialization
- Enter infinite loop
6. Function Call Hierarchy
main()
├── initializeAxisControlValueDefaults()
├── write16valuesToDac5361_v2()
├── HAL_TIM_Base_Start_IT()
└── while()
├── displayRunState()
└── displaySafeState()
TIM13 ISR
├── readAdc1Spi3BrdSpi2StmCh8ToCh1()
├── tuneParameterFromPot1()
├── updateSafeMenuSelection()
├── initializeControllerStates()
├── controlUpdateCompute()
└── write16valuesToDac5361_v2()
TIM14 ISR
├── readAdc1Spi3BrdSpi2StmCh8ToCh1()
├── controlUpdateCompute()
└── write16valuesToDac5361_v2()
controlUpdateCompute()
└── updateOneAxis()
├── updateErrorHistory()
└── PD_Compute()
7. Major Functions
| Function | Description |
|---|---|
| initializeAxisControlValueDefaults() | Loads default controller gains and bias currents. |
| initializeControllerStates() | Synchronizes controller history before entering RUN. |
| processSwInp1interrPress() | Processes FSM transitions from SW1. |
| enterParameterEdit() | Loads parameter limits and current value. |
| updateSafeMenuSelection() | Maps POT1 voltage into menu indices. |
| controlUpdateCompute() | Updates all five AMB axes. |
| updateOneAxis() | Performs one axis position-control update. |
| PD_Compute() | Computes proportional-derivative controller output. |
| write16valuesToDac5361_v2() | Writes all 16 DAC channels then pulses LDAC. |
| readAdc1Spi3BrdSpi2StmCh8ToCh1() | Reads eight ADC channels from AD7606. |
| displayRunState() | Displays live controller status. |
| displaySafeState() | Dispatches menu drawing based on FSM state. |
| drawMainMenu() | Main safe-state menu. |
| drawAxisMenu() | Axis selection screen. |
| drawParameterMenu() | Displays parameter list and current values. |
| drawParameterEdit() | Interactive parameter tuning screen. |
| saveEditedParameter() | Stores edited parameter into the selected axis. |
8. Control Loop
Sensor mV | sensorGain | Position (mm) | Reference | Error | PD Controller | Bias Current | Current-to-Voltage Gain | DAC Voltage | Power Amplifier
9. Safety Features
- System always powers up in SAFE mode.
- RUN mode requires a continuous 3-second SW4 press.
- SW1 immediately exits RUN mode.
- DAC outputs are forced to ground in SAFE mode using the AD5361 CLEAR input.
- Controller histories are initialized before closed-loop operation.
10. Future Extensions
- Orbit control implementation.
- Manual actuator drive mode.
- PA gain calibration.
- Derivative filtering.
- Integral control.
- Persistent parameter storage in Flash.
- DMA-based ADC and DAC transfers.