State Machines
The motor control software uses three distinct state machines operating at different priorities. Understanding their execution context and interaction is essential for customizing or debugging the application.
Execution Architecture
The two main execution contexts are the high-priority ADC ISR and the low-priority
background loop in main().
The ADC ISR is triggered by the MCPWM peripheral: each PWM cycle generates an ADC
start-of-conversion (SOC) trigger via the PWM XBAR. When the ADC conversion completes,
ADC0_INT1_IRQHandler fires. The ISR frequency therefore equals the PWM switching
frequency — at the default 15 kHz PWM setting the ISR runs every 66 µs, and the
frequency can be scaled up to 100 kHz by increasing USER_M1_PWM_FREQ_kHz in
user_mtr1.h and matching the MCPWM period in SysConfig.
The ISR counter timerCntFOCCtrlISR is incremented every ISR tick and checked by the
background loop. When it exceeds LOW_PRIORITY_WAIT_TIME_1ms (= 1000 / PWM_kHz ticks),
the low-priority state machine runs once and the counter resets.
Startup and Calibration Sequence
Before the motor can run, two calibration phases must complete. Both are initiated in
main() before the background loop starts.
Why Two Calibration Phases?
Phase |
Method |
What it calibrates |
|---|---|---|
Comparator DAC offset |
Blocking sweep before ISR starts |
CMPSS DAC trip-point offsets (hardware over-current protection) |
ADC IIR offset |
ISR-driven, PWM-synchronous |
ADC current/voltage channel offsets using IIR filter with live PWM data |
The ADC calibration must run with the PWM active (at 0% duty) so that the ADC samples are synchronized to the actual switching noise environment. This is why it runs inside the ISR rather than in a blocking loop.
High-Priority State Machine
FOC_runStateMachine() is called every ISR tick (every PWM cycle). It determines the
FOC angle source and current references based on the current motor state.
State Descriptions
State |
Angle Source |
Current Reference |
Description |
|---|---|---|---|
|
— |
— |
ADC IIR offset calibration running in ISR |
|
— |
0 |
Motor stopped, PWM disabled, waiting for command |
|
Observer |
Flying start logic |
Re-synchronize to a spinning motor; exits to CTRL_RUN if speed > threshold, else to ALIGN or IPD |
|
IPD result |
Injection pulses |
Initial Position Detection — determines rotor angle at standstill |
|
Fixed |
|
Force rotor to a known angle before open-loop ramp |
|
|
|
Open-loop speed ramp — angle generated internally |
|
|
|
Transition: observer begins tracking open-loop angle |
|
Observer (FAST/ESMO) |
Speed PI output |
Closed-loop FOC — observer provides angle and speed |
|
EST angle (FAST) |
EST trajectory |
InstaSPIN or TiTune parameter identification (outlier) |
|
— |
0 |
Fault active, PWM disabled (outlier) |
Low-Priority State Machine
FOC_runLowPriorityStateMachine() runs in the background loop approximately every 1 ms.
It handles non-time-critical management tasks: fault checking, stop commands, motor
monitoring, and trajectory management.
Fault Flags
The FAULT_MTR_BITS structure in motor_common.h
tracks all active faults as individual bits in a 16-bit register:
Bit |
Fault |
Detection |
|---|---|---|
0 |
|
DC bus > |
1 |
|
DC bus < |
4 |
|
Hardware CMPSS trip |
5 |
|
Software current limit |
6 |
|
Torque limit exceeded |
7 |
|
Phase current missing |
9 |
|
Speed = 0 with current applied |
10 |
|
OL→CL transition failed |
11 |
|
Speed > |
12 |
|
FAST estimator error |
13 |
|
Gate driver nFAULT pin |
TiTune State Machine
When flagEnableRunAndIdentify is set and motorParamIdentifyMethod == MOTOR_PARAM_IDENTIFY_TITUNE,
the motor enters MOTOR_PARAM_IDENT state and StateMachine_handleTiTune() runs
TiTune_Run_stateMachine() every ISR tick.
TiTune State Descriptions
State |
What Runs |
Key Output |
|---|---|---|
|
HF voltage injection at standstill |
|
|
DC current injection, measure voltage drop |
|
|
Open-loop speed ramp to estimation speed |
Observer lock |
|
Closed-loop, flux estimation active |
|
|
Coast-down frequency sweep |
|
|
|
PI gains updated, motor stops |
What Happens at TiTune_STATE_DONE
When identification completes, TiTune_applyFOCControllers() in
application.h
performs the following in sequence:
Writes identified
Ld,Lq,Rs,λ_PMintoUSER_ParamsRecomputes
Kctrl_Wb_p_kgm2from identified inertiaCalls
setupControllers()to recompute current loop PI gains (Kp = L × BWc,Ki = 0.25 × R/L × T)Overrides speed PI gains with
TiTune_PI_Speed(computed from J/Kt sweep)Syncs all gains to
MOTOR_SetVars_tfor watch-window displayUpdates observer parameters (FAST or ESMO) with new Rs/Ld/Lq
Returns motor to
MOTOR_STOP_IDLE
ISR Execution Budget
The ADC ISR runs at the configured PWM frequency (USER_M1_PWM_FREQ_kHz in user_mtr1.h).
The available ISR budget per tick is therefore:
PWM Frequency |
ISR Period (budget) |
|---|---|
15 kHz (default) |
66 µs |
20 kHz |
50 µs |
40 kHz |
25 µs |
100 kHz (max) |
10 µs |
The motor control algorithm itself (FOC feedback, observers, state machine, feedforward)
consumes approximately 6–10 µs. The remaining budget is available for custom code
added to application_run(). At higher PWM frequencies the available margin shrinks
significantly — plan custom code accordingly.
The CPU_TIME_Obj (cpuTime) in main.h
can be used to measure actual ISR execution time.
Note
The ISR is placed in RAM (.motor_ctrl_ramfunc section) via the MOTOR_CTRL_RAMFUNC
attribute to minimize execution latency. Any custom code added to application_run()
must also be RAM-resident or fast enough to fit within the available budget for the
configured PWM frequency.