The motor control examples and algorithms presented in this guide are provided by the ctrl-elec project, an educational initiative from INSA Lyon (France).
Attribution:
License: Educational use - Please refer to the ctrl-elec website for specific licensing terms.
This content is included in the MPLAB Device Blocks for Simulink documentation to demonstrate real-world motor control applications. Microchip Technology and the MPLAB Device Blocks toolbox are separate from the ctrl-elec project.
This guide demonstrates how to use the MPLAB Device Blocks for Simulink toolbox for motor control applications, using examples and educational materials from the ctrl-elec project (INSA Lyon).
The examples showcase:
Prerequisites:
Understanding of motor control theory (vector control, Park/Clarke transforms)
Familiarity with Simulink and Model-Based Design
Microchip development hardware (e.g., MCLV-2, dsPICDEM MCLV)
Field-Oriented Control (FOC) provides precise control of PMSM torque and speed by independently controlling flux and torque-producing currents in the rotating reference frame (d-q frame).
| Component | Function | MPLAB Blocks Used |
|---|---|---|
| Clarke Transform | Converts 3-phase currents (a,b,c) to stationary frame (α,β) | Math blocks (Simulink standard) |
| Park Transform | Converts stationary frame (α,β) to rotating frame (d,q) | Math blocks with rotor position input |
| PI Controllers | Regulate d-axis and q-axis currents independently | Discrete PID blocks with anti-windup |
| Inverse Park | Converts voltage commands (Vd, Vq) to stationary frame | Math blocks with angle input |
| Space Vector Modulation | Generates 3-phase PWM duty cycles | MCHP_PWM_HS or MCHP_PWM_HS_FEP |
| Current Sensing | Measures motor phase currents | MCHP_ADC with PWM-synchronized sampling |
| Position/Speed Estimation | Encoder or sensorless observer | MCHP_QEI (encoder) or observer algorithm |
Hardware Configuration
Configure PWM outputs using MCHP_PWM_HS_FEP block
Set PWM frequency (typically 10-20 kHz)
Enable complementary mode with dead-time insertion
Configure center-aligned PWM for reduced harmonics
ADC Setup
Use MCHP_ADC block for current sensing
Trigger ADC from PWM for synchronous sampling
Sample at PWM valley or peak for accurate measurements
Enable ADC interrupt for time-critical control loop
Control Loop Structure
Outer loop: Speed controller (lower rate, e.g., 1 kHz)
Inner loop: Current controller (higher rate, e.g., 10 kHz)
Use multi-rate model with appropriate task priorities
Parameter Tuning
Enable External Mode for real-time tuning
Use picgui for lightweight data visualization
Adjust PI gains using Ziegler-Nichols or model-based methods
| Block Name | Configuration | Purpose in FOC |
|---|---|---|
| MCHP_Master | Select target device, enable multi-tasking | System configuration and scheduler |
| MCHP_PWM_HS_FEP | 3-phase complementary, center-aligned, 10 kHz | Drive motor inverter (6 IGBTs/MOSFETs) |
| MCHP_ADC | 2 channels (phase currents), PWM-triggered | Measure Ia, Ib (Ic calculated) |
| MCHP_QEI | Quadrature encoder interface, 1024 PPR | Rotor position and speed feedback |
| MCHP_UART_Config | 115200 baud, XCP protocol | External Mode communication |
| MCHP_Interrupt | Timer or ADC interrupt, highest priority | Trigger fast current control loop |
Safety Considerations:
Implement overcurrent protection using ADC limits and emergency shutdown
Add dead-time compensation to prevent shoot-through
Test control algorithm in simulation before hardware deployment
Use current limiting during startup and parameter tuning
Monitor MCU load to prevent scheduler overruns
Sensorless control eliminates the need for mechanical position sensors by estimating rotor position and speed from electrical measurements (voltages and currents). This example uses a Sliding Mode Observer (SMO) for robust position estimation.
| Component | Function | Implementation Notes |
|---|---|---|
| Current Observer | Estimates stator currents using motor model | Uses measured voltages and motor parameters (Rs, Ls) |
| Back-EMF Estimation | Extracts back-EMF from current error | Sliding mode function with adaptive gain |
| Position Calculation | Computes rotor angle from back-EMF (α,β) | Arctangent function with phase compensation |
| Speed Estimation | Differentiates position or uses PLL | Low-pass filter to reduce noise |
| Startup Strategy | Open-loop alignment then transition to sensorless | Forced angle ramp until sufficient back-EMF |
| Block Name | Configuration | Purpose in Sensorless Control |
|---|---|---|
| MCHP_ADC | 4 channels: Ia, Ib, Vdc, temperature | Measure currents and DC bus voltage |
| MCHP_PWM_HS_FEP | Same as FOC example | Motor drive PWM generation |
| Observer Subsystem | Fixed-point or floating-point math | Sliding mode observer implementation |
| MCHP_Interrupt | ADC interrupt at PWM frequency | Execute observer at high rate (10-20 kHz) |
Advantages of Sensorless Control:
Reduced hardware cost (no encoder)
Higher reliability (no mechanical sensor to fail)
Simplified wiring and installation
Suitable for harsh environments
Limitations:
Poor performance at zero/low speed
Requires accurate motor parameters
Sensitive to parameter variations (temperature, saturation)
More complex tuning process
Field weakening extends the speed range of PMSM beyond the base speed by injecting negative d-axis current. This reduces the flux linkage, allowing higher speeds while maintaining voltage within DC bus limits.
| Operating Region | Control Strategy | Id Command | Iq Command |
|---|---|---|---|
| Constant Torque (ω < ωbase) | Maximum torque per ampere (MTPA) | 0 A (or slightly negative), Limited by max current | |
| Field Weakening (ωbase < ω < ωmax) | Voltage-limited operation | Negative (increases with speed) | Adjusted to maintain voltage limit |
| Constant Power (ω ≈ ωmax) | Maximum speed operation | Maximum negative Id | Reduced for voltage constraint |
Voltage Limit Calculation
Measure DC bus voltage using MCHP_ADC
Calculate maximum voltage: Vmax = Vdc / √3 (for SVPWM)
Account for voltage drops and modulation overhead
Field Weakening Controller
Input: Speed feedback, voltage magnitude, current limits
Output: Id_ref (d-axis current command)
Algorithm: PI controller on voltage error or lookup table
Current Limiting
Enforce circular current limit: √(Id² + Iq²) ≤ Imax
Priority given to Id (flux control) at high speeds
Reduce Iq if total current exceeds limit
| Parameter | Typical Range | Effect |
|---|---|---|
| Base Speed (ωbase) | Rated speed or design speed | Transition point to field weakening |
| Max Id Current | 50-80% of rated current | Maximum field weakening capability |
| Voltage Margin | 5-10% below Vmax | Safety buffer for control headroom |
| FW Controller Gain | Tuned for stability | Response speed vs. oscillations |
Field Weakening Considerations:
Increased copper losses due to higher total current
Reduced efficiency at high speeds
Risk of demagnetization with excessive negative Id
Requires precise voltage and current limiting
Test carefully with thermal monitoring
Website:[https://www.ctrl-elec.fr/]
Comprehensive tutorials on motor control theory
Detailed explanations of FOC, sensorless control, and field weakening
Step-by-step lab exercises with MATLAB/Simulink
GitHub Repository:[https://github.com/rdelpoux/ctrl-elec]
Complete Simulink models for motor control examples
Reference implementations and test cases
Documentation and supplementary materials
RCP Platform:[http://rcp.ctrl-elec.fr/]
Rapid Control Prototyping platform documentation
Hardware setup guides for various development boards
Application notes and best practices
The ctrl-elec examples can be adapted to use MPLAB Device Blocks by:
Replace generic peripheral blocks with MCHP-specific blocks:
Generic PWM → MCHP_PWM_HS_FEP
Generic ADC → MCHP_ADC
Generic encoder → MCHP_QEI
Configure MCHP Master block for target device and enable multi-tasking
Adjust sample times to match MCHP scheduler rates
Enable External Mode using MCHP UART blocks for real-time tuning
Leverage MCHP-specific features:
PWM-triggered ADC for precise current sampling
DMA transfers for high-performance data movement
Assembly code replacement for optimized math operations
This guide demonstrated how to implement advanced motor control algorithms using the MPLAB Device Blocks for Simulink, leveraging educational resources from the ctrl-elec project (INSA Lyon).
Key Takeaways:
Next Steps:
Motor control algorithms, theory, and examples presented in this guide are provided by:
ctrl-elec project - Real-time Control & Embedded Systems EducationINSA Lyon (Institut National des Sciences Appliquées de Lyon), France
Website:[https://www.ctrl-elec.fr/]GitHub:[https://github.com/rdelpoux/ctrl-elec]RCP Platform:[http://rcp.ctrl-elec.fr/]
This documentation is part of the MPLAB Device Blocks for Simulink toolbox. The ctrl-elec project and INSA Lyon are independent educational initiatives. Microchip Technology provides the MPLAB blocks hardware interface, while ctrl-elec provides motor control algorithms and educational materials.
MPLAB Device Blocks for Simulink © Microchip Technology Inc. All rights reserved.