Implementing Field Oriented Control (FOC) for motors using STM32 is an efficient control method for Permanent Magnet Synchronous Motors (PMSM). The core of this method is to convert three-phase AC into direct axis (d-axis) and quadrature axis (q-axis) DC through coordinate transformation, achieving independent control of torque and magnetic flux. Below are the implementation steps, key technologies, and code framework for FOC based on STM32:
1. Hardware Preparation
- Main Control Chip: STM32G431/STM32F407/STM32H7, etc. (must have high-speed ADC and advanced timers TIM1/TIM8)
- Power Driver: Three-phase full bridge (e.g., IR2104 driving MOSFET/IGBT)
- Position Sensor: Hall sensor (low cost) or encoder/rotary transformer (high precision)
- Current Sensing: Shunt resistor + operational amplifier (for sampling three-phase current) or integrated chip (e.g., ACS712)
- DC Bus: Power supply + capacitor filtering
2. Core Principles of FOC
- Clark Transformation: Converts three-phase stationary coordinate system (ABC) to two-phase stationary coordinate system (αβ)
- Park Transformation: Converts two-phase stationary coordinate system (αβ) to two-phase rotating coordinate system (dq)
- PI Regulation: Closed-loop control of d/q axis currents, outputting voltage commands
- Inverse Park Transformation: Converts dq axis voltages to αβ axis voltages
- SVPWM Modulation: Generates three-phase PWM waves to drive the inverter
3. STM32 Software Implementation Steps
1. Initialization Configuration
- Clock Configuration: System clock (e.g., 180MHz), ADC clock, timer clock
- GPIO Configuration: PWM output (TIM1_CH1~CH3), sensor interface (Hall/encoder)
- ADC Configuration: Sampling three-phase current (DMA + multi-channel conversion)
- Timer Configuration: TIM1/TIM8 generates SVPWM (center-aligned mode, dead time settings)
- Interrupt Configuration: Timer update interrupt (FOC control cycle), sensor interrupt
2. Rotor Position Detection
- Hall Sensor: Calculate electrical angle from Hall signals (60° resolution, interpolation optimization required)
- Encoder: Obtain position through quadrature decoding mode (TIMx_Encoder), calculate electrical angle
- Sensorless FOC: Estimate position using back EMF observers (e.g., sliding mode observer, Luenberger observer)
3. Current Sampling and Clark Transformation
// Sampling three-phase current (assuming I_a, I_b are sampled, I_c = -I_a - I_b)
float I_alpha = I_a;
float I_beta = (I_a + 2*I_b)/sqrt(3); // Clark transformation (simplified version)
4. Park Transformation (αβ→dq)
// theta is the rotor electrical angle
float I_d = I_alpha*cos(theta) + I_beta*sin(theta);
float I_q = -I_alpha*sin(theta) + I_beta*cos(theta);
5. PI Controller (Current Loop)
// d-axis current PI control (target I_d_ref=0, can be set to negative for flux weakening)
float U_d = PI_Controller(I_d_ref - I_d, &PI_d);
// q-axis current PI control (I_q_ref output from speed loop)
float U_q = PI_Controller(I_q_ref - I_q, &PI_q);
6. Inverse Park Transformation (dq→αβ)
// Calculate sector and duty cycle
SVPWM_Calc(U_alpha, U_beta, &svpwm);
// Set timer compare values
TIM_SetCompare1(TIM1, svpwm.Ta);
TIM_SetCompare2(TIM1, svpwm.Tb);
TIM_SetCompare3(TIM1, svpwm.Tc);
7. SVPWM Modulation
// Calculate sector and duty cycle
SVPWM_Calc(U_alpha, U_beta, &svpwm);
// Set timer compare values
TIM_SetCompare1(TIM1, svpwm.Ta);
TIM_SetCompare2(TIM1, svpwm.Tb);
TIM_SetCompare3(TIM1, svpwm.Tc);
8. Speed Loop Control (Optional)
// Speed PI control (target Speed_ref)
float I_q_ref = PI_Controller(Speed_ref - Speed_meas, &PI_speed);
4. Key Code Modules
1. PI Controller Implementation
typedef struct {
float Kp;
float Ki;
float integral;
float limit;
} PI_Params;
float PI_Controller(float error, PI_Params* pi) {
pi->integral += error * pi->Ki;
pi->integral = fminf(fmaxf(pi->integral, -pi->limit), pi->limit);
return error * pi->Kp + pi->integral;
}
2. SVPWM Calculation
typedef struct {
float Ta, Tb, Tc;
} SVPWM_Params;
void SVPWM_Calc(float U_alpha, float U_beta, SVPWM_Params* svpwm) {
float Udc = 3.3f; // Bus voltage
float Uref = sqrtf(U_alpha*U_alpha + U_beta*U_beta);
float theta = atan2f(U_beta, U_alpha);
// Sector determination and duty cycle calculation (detailed implementation omitted)
// ...
}
5. Debugging and Optimization
- Current Loop Debugging: Start with open-loop testing, then closed-loop, observe current waveforms
- Parameter Tuning: Adjust PI parameters from large to small to avoid oscillation
- Dead Time Compensation: Adjust dead time based on power device characteristics
- Flux Weakening Control: Increase negative d-axis current at high speeds to extend speed range
6. Reference Resources
- ST Official Libraries: STM32CubeF4/STM32CubeG4
- AN4774: “STM32 Permanent Magnet Synchronous Motor FOC Development Guide”
- Open Source Project: SimpleFOC (Arduino/STM32 compatible)
By following the above steps, FOC control can be implemented on STM32, suitable for high precision and high efficiency motor drive scenarios (such as servo systems, drones, electric vehicles, etc.)