Motor Data Structures
The motor control software uses a layered set of data structures. Understanding how data flows from static motor nameplate parameters through to live runtime control variables is essential for tuning, debugging, and extending the application.
Data Flow Overview
Primary Structures
Motor_ParamsType — Motor Nameplate Data
File: motor_params.h,
motor_params.c
This is the source of truth for motor electrical and mechanical parameters. It is a
const table — one entry per supported motor. The active motor is selected by the
motorParams_M1 pointer in motor_params.c.
Field |
Unit |
Description |
|---|---|---|
|
— |
Number of pole pairs |
|
Ω |
Phase resistance |
|
H |
D-axis inductance |
|
H |
Q-axis inductance |
|
V/Hz |
Rated flux linkage |
|
A |
Rated phase current |
|
kg·m² |
Rotor inertia |
|
Hz |
Rated electrical speed |
|
Hz |
Open-loop → closed-loop transition speed |
|
A |
Open-loop torque current |
|
A |
Rotor alignment current |
|
— |
ESMO sliding mode gain range |
|
— |
ESMO PLL proportional gain range |
|
— |
ESMO PLL integral gain |
To add a new motor, add a new Motor_ParamsType constant in motor_params.c and update
the motorParams_M1 pointer:
/* motor_params.c */
const Motor_ParamsType Motor_myNewMotor = {
.NumPolePairs = 4,
.Rs_Ohm = 0.85f,
.Ls_d_H = 0.00135f,
.Ls_q_H = 0.00135f,
.RatedFlux_VpHz = 0.0415f,
/* ... */
};
/* Set active motor */
const Motor_ParamsType *motorParams_M1 = &Motor_myNewMotor;
USER_Params — Algorithm Configuration
File: userParams.h (SDK), populated from user_mtr1.h
USER_Params holds the algorithm-level configuration derived from board constants in
user_mtr1.h. It is computed once at init by initMotor1CtrlParameters() and is
read-only during operation.
Key fields populated from user_mtr1.h macros:
|
Source Macro |
Description |
|---|---|---|
|
|
ISR / control loop frequency |
|
|
Speed controller decimation |
|
|
Pole pairs |
|
|
Stator resistance |
|
|
D-axis inductance |
|
|
Q-axis inductance |
|
Computed from |
Rated flux in Webers |
|
|
Maximum electrical speed |
|
Computed from |
Speed loop gain factor |
MOTOR_SetVars_t — Runtime Setpoints
File: motor_common.h
(global: motorSetVars_M1)
MOTOR_SetVars_t holds the runtime-adjustable setpoints and thresholds. These are
initialized from Motor_ParamsType and user_mtr1.h at startup, but can be modified
at runtime via the watch window for tuning.
Key field groups:
Group |
Key Fields |
Description |
|---|---|---|
Motor parameters |
|
Electrical params (updated by TiTune) |
PI gains |
|
Controller gains (updated by TiTune) |
Current limits |
|
Operating current limits |
Speed limits |
|
Speed ramp parameters |
Fault thresholds |
|
Protection limits |
Timing counts |
|
State timing in ISR ticks |
Note
After TiTune completes, TiTune_applyFOCControllers() writes the identified motor
parameters and recomputed PI gains back into MOTOR_SetVars_t so they are visible
in the watch window.
MOTOR_InputCtrl_t — User Commands
File: motor_common.h
(field inside MOTOR_Vars_t: motorVars_M1.inputCtrl)
MOTOR_InputCtrl_t is the user-facing command interface. These fields are written
by the user (via watch window, UART command, or host interface) and read by the ISR
state machine and speed controller.
Field |
Type |
Description |
|---|---|---|
|
|
Set |
|
|
Pulse |
|
|
Speed setpoint in electrical Hz |
|
|
|
|
|
|
|
|
|
|
|
Enable field weakening control |
|
|
Enable maximum torque per ampere |
|
|
Enable dead-time compensation |
|
|
|
|
|
Speed control or torque control |
Typical Motor Start Sequence (Watch Window)
/* 1. Set speed reference */
motorVars_M1.inputCtrl.speedRef_Hz = 50.0f;
motorVars_M1.inputCtrl.direction = 1.0f;
/* 2. Select estimator */
motorVars_M1.inputCtrl.estimatorMode = ESTIMATOR_MODE_ESMO;
/* 3. Enable run */
motorVars_M1.inputCtrl.flagEnableRunAndIdentify = true;
MOTOR_OutVars_t — FOC Outputs
File: motor_common.h
(field inside MOTOR_Vars_t: motorVars_M1.outVars)
MOTOR_OutVars_t contains the primary outputs of the FOC algorithm — the values
that are written by the ISR and read by the background loop for monitoring.
Field |
Unit |
Description |
|---|---|---|
|
|
Current state machine state |
|
rad |
FOC angle used for Park/iPark transforms |
|
A |
Measured d-q currents (after Park transform) |
|
A |
Current references (d-axis: FWC/MTPA, q-axis: speed PI) |
|
V |
Voltage commands from current PI controllers |
|
V |
Alpha-beta voltage commands (after iPark) |
|
Hz |
Estimated electrical speed |
|
|
Active fault flags (bit field) |
MOTOR_StatusVars_t — Internal Status
File: motor_common.h
(field inside MOTOR_Vars_t: motorVars_M1.statusVars)
MOTOR_StatusVars_t holds internal algorithm state — intermediate values used by
the FOC algorithm that are useful for debugging but not directly commanded by the user.
Field |
Unit |
Description |
|---|---|---|
|
rad |
FAST estimator angle (with delay compensation) |
|
rad |
ESMO PLL angle (with delay compensation) |
|
rad |
Active observer angle (FAST or ESMO, selected by |
|
Hz |
FAST estimator speed |
|
Hz |
ESMO PLL speed |
|
Hz |
Absolute speed (used for fault detection) |
|
Hz |
Speed trajectory integrator output |
|
|
Speed controller enable (managed by state machine) |
|
|
Current controller enable |
|
— |
ADC offset calibration tick counter |
|
— |
Time-in-state counter (used for state transitions) |
MOTOR_Vars_t — Master Motor Object
File: motor_common.h
(global: motorVars_M1)
MOTOR_Vars_t is the top-level motor control object. It contains all sub-structures
and handles as fields. The ISR receives a MOTOR_Handle (pointer to MOTOR_Vars_t) and
accesses everything through it.
SYSTEM_Vars_t — System-Level Flags
File: motor_common.h
(global: systemVars in main.c)
SYSTEM_Vars_t holds system-wide flags shared between the ISR and the background
loop. It is separate from MOTOR_Vars_t to allow multi-motor configurations.
Field |
Description |
|---|---|
|
Set after all init functions complete |
|
Background loop runs while this is |
|
Set by ISR when ADC offset calibration completes |
|
ISR tick counter — used to gate the 1 ms background loop |
|
Relay energize delay before motor start |
|
DAC debug output control (serial plot) |
Structure Initialization Flow
HAL FOC Data Object
HAL_FOC_Data_Obj — Hardware Interface Runtime Data
File: hal_foc_interface.h
HAL_FOC_Data_Obj is the mutable runtime data object that sits at the boundary between
the FOC algorithm and the physical hardware. It is allocated in RAM and passed by pointer
through the HAL_FOC_Interface_t to every vtable function. The ISR reads ADC results from
it, writes PWM duty-cycle values into it, and checks gate-driver fault status through it —
all in a single ISR tick.
Field Reference
Field |
Type |
Direction |
Description |
|---|---|---|---|
|
|
Config |
Motor instance identifier (M1, M2, …) |
|
|
Config |
Number of phase-current ADC channels (1 or 3) |
|
|
Config |
Number of DC-bus voltage ADC channels |
|
|
Config |
Board variant identifier |
|
|
Config |
Sensing topology: three-shunt or single-shunt |
|
|
Written by ISR |
PWM duty-cycle data — see PWM Data |
|
|
Control |
Set |
|
|
Config |
Pointer to over-current trip threshold in Amps |
|
|
Config |
IPD injection current limit in Amps |
|
|
Written by ISR |
ADC phase-current and bus-voltage results — see ADC Data |
|
|
Cal output |
Comparator DAC trip-point offsets in raw counts (0–4095) |
|
|
Read-only |
Gate driver scaling constants and fault thresholds — see GD Data |
|
|
Read |
ECAP capture value for the last IPD pulse width |
ADC Data — HAL_ADCData_t
File: hal_data.h
HAL_ADCData_t is embedded directly inside HAL_FOC_Data_Obj as the adcData field.
Every ISR tick the topology-specific ReadPhaseCurrents and ReadInverterVoltages vtable
functions convert raw ADC counts into physical units and store the results here. The FOC
algorithm then reads adcData.I_A and adcData.VdcBus_V directly.
Field |
Unit |
Description |
|---|---|---|
|
V |
DC bus voltage — used for per-unit voltage normalisation |
|
A |
Three-phase currents (Ia, Ib, Ic) after offset removal and scaling |
|
V |
Three-phase voltages (Va, Vb, Vc) |
|
A |
DC-link current up-count samples (single-shunt only) |
|
A |
DC-link current down-count samples (single-shunt only) |
|
ADC counts |
DC-link current offset — accumulated during offset calibration (single-shunt) |
|
ADC counts |
Per-phase current offsets (Xa, Xb, Xc) — accumulated during offset calibration (three-shunt) |
|
V/count |
Phase voltage offset scale factors |
|
A/count |
Current scale factor — converts raw ADC count to Amps |
|
V/count |
Phase voltage scale factor |
|
V/count |
DC bus voltage scale factor |
ADC Offset Calibration
Before the motor starts, the ISR runs an offset calibration loop controlled by
flagEnableOffsetCalc. During this phase:
HAL_RunOffsetCalInit()zeroes the accumulator registers.HAL_RunOffsetCalStep()is called once per ISR tick foroffsetCalibTimeSet_cntticks (set inMOTOR_SetVars_t).Each call accumulates the raw ADC reading into
offset_I_ad(three-shunt) oroffset_Idc_ad(single-shunt).When the count expires the average is stored and
flagOffsetCalibDoneis set inSYSTEM_Vars_t.
After calibration, every ReadPhaseCurrents call subtracts the stored offset
before applying current_sf, so I_A always represents true zero-mean phase
current.
/* Typical ISR offset calibration sequence */
if (systemVars.flagOffsetCalibDone == FALSE)
{
halFOCDataObj.flagEnableOffsetCalc = TRUE;
HAL_RunOffsetCalInit(&motorVars_M1.halInterface);
/* Called each ISR tick until complete */
uint8_t done = HAL_RunOffsetCalStep(&motorVars_M1.halInterface,
motorVars_M1.statusVars.offsetCalibTimeCnt,
motorSetVars_M1.offsetCalibTimeSet_cnt);
if (done)
{
systemVars.flagOffsetCalibDone = TRUE;
halFOCDataObj.flagEnableOffsetCalc = FALSE;
}
}
PWM Data — HAL_PWMData_t
File: hal_data.h
HAL_PWMData_t is embedded inside HAL_FOC_Data_Obj as the pwmData field.
At the end of every ISR tick the FOC algorithm writes the computed voltage vector
into pwmData.Vabc_pu and then calls HAL_SetPhaseVoltages(), which converts
the per-unit values to hardware compare registers.
Field |
Unit |
Description |
|---|---|---|
|
pu |
Three-phase duty-cycle commands in per-unit (−1.0 … +1.0) — written by the SVPWM/iPark stage |
|
PWM counts |
Computed compare values loaded into MCPWM compare registers |
|
PWM counts |
Compare-A shadow values (used for asymmetric PWM) |
|
PWM counts |
Compare-B shadow values |
|
PWM counts |
ADC SOC trigger point on the up-count (for current sampling) |
|
PWM counts |
ADC SOC trigger point on the down-count (single-shunt second sample) |
|
PWM counts |
Deadband insertion in PWM clock cycles |
|
PWM counts |
Blanking window around switching edge to avoid sampling noise |
|
PWM counts |
Minimum compare value — enforces minimum on-time |
|
PWM counts |
ADC sample timing offset from PWM peak |
|
PWM counts |
PWM period register value |
|
PWM counts |
SOC compare value for the ADC trigger |
|
|
|
|
V |
Pointer to the alpha-beta voltage vector produced by the current PI controllers |
Data flow into pwmData
Gate Driver Data — HAL_GD_DATA_Obj
File: hal_gate_driver.h
gdDataHandle inside HAL_FOC_Data_Obj is a read-only pointer to the
HAL_GD_DATA_Obj that is owned by the gate driver interface (HAL_GD_Interface_t).
It is set once during initMotorControlSystem() and never changes at runtime.
The FOC layer uses gdDataHandle to read board-level scaling constants and protection
thresholds without needing to know which physical gate driver IC is fitted. This keeps
the FOC algorithm independent of the gate driver variant (DRV8323RH, DRV8329A, etc.).
Field |
Unit |
Description |
|---|---|---|
|
A |
Maximum allowable phase current — used to range-check the over-current threshold |
|
A/count |
Current scale factor derived from the gate driver’s sense resistor and amplifier gain |
|
s |
Current sensing propagation delay — used to compensate ADC sample timing |
|
s |
Current sampling delay — additional blanking after the gate driver output |
|
A |
Current offset correction specific to the gate driver hardware |
|
— |
Sign correction ( |
|
V |
Over-voltage fault threshold — compared against |
|
V |
Under-voltage fault threshold — compared against |
|
V/count |
Voltage scale factor for the gate driver’s voltage sense circuit |
|
V/count |
Phase voltage offset scale factor |
|
rad/s |
Pole frequency of the DC bus voltage low-pass filter |
How gdDataHandle communicates fault status
The gdDataHandle pointer provides the FOC layer with the thresholds needed to
detect gate-driver-level faults. The fault detection flow is:
Hardware trip — The gate driver IC asserts its nFAULT pin when an over-current or over-temperature event occurs. This is wired to a PWM trip-zone input, which immediately forces all PWM outputs low in hardware without software intervention.
Software detection — Each ISR tick the motor state machine reads
adcData.VdcBus_Vand compares it againstgdDataHandle->gdOverVolValandgdDataHandle->gdUnderVolValto detect DC bus faults. Phase current magnitude is compared against*pOverCurLimA(which is initialised fromgdDataHandle->gdMaxCurrentAscaled by the user’s over-current factor).Fault latch — When a fault is detected, the state machine sets the corresponding bit in
motorVars_M1.outVars.faultMtrNowand transitions toMOTOR_FAULT_STOPstate.Fault clear —
HAL_clearMtrFaultStatus()calls the vtable function which clears the gate driver’s nFAULT latch (via SPI or GPIO) and resets the PWM trip-zone latch, allowing the motor to restart after the user setsinputCtrl.flagClearFaults = true.
/* Typical fault check pattern in the ISR state machine */
if (motorVars_M1.halInterface.halFOCDataHandle->adcData.VdcBus_V >
motorVars_M1.halInterface.halFOCDataHandle->gdDataHandle->gdOverVolVal)
{
motorVars_M1.outVars.faultMtrNow.bit.overVoltage = 1U;
}
if (motorVars_M1.halInterface.halFOCDataHandle->adcData.VdcBus_V <
motorVars_M1.halInterface.halFOCDataHandle->gdDataHandle->gdUnderVolVal)
{
motorVars_M1.outVars.faultMtrNow.bit.underVoltage = 1U;
}
Note
gdDataHandle is declared const HAL_GD_DATA_Obj * — the FOC layer can only
read the gate driver data. All writes to HAL_GD_DATA_Obj are performed
exclusively by the gate driver initialisation code in initMotorControlSystem().
This enforces a clean ownership boundary: the gate driver layer owns its data;
the FOC layer consumes it.
Comparator DAC Offset Data — compOffsetData[3]
compOffsetData stores the raw DAC count (0–4095) at which each hardware comparator
trips during the comparator offset calibration sweep. These values are used to
compensate for comparator input offset voltage so that the hardware over-current
trip threshold accurately reflects the programmed current limit in Amps.
Index |
Channel |
Description |
|---|---|---|
|
Phase-A / DC-link |
Trip-point DAC count for phase-A shunt (three-shunt) or DC-link shunt (single-shunt) |
|
Phase-B |
Trip-point DAC count for phase-B shunt (three-shunt only) |
|
Phase-C |
Trip-point DAC count for phase-C shunt (three-shunt only) |
The calibration is performed once at startup by calling HAL_CompOffsetCalInit()
followed by repeated calls to HAL_CompOffsetCalStep() until it returns 1.
The found offsets are then added to the nominal DAC threshold when
HAL_SetCompOCLimits() programs the comparator DAC registers.