Main Content

Field-Oriented Control of PMSM Using Quadrature Encoder

This example implements the field-oriented control (FOC) technique to control the speed of a three-phase permanent magnet synchronous motor (PMSM). The FOC algorithm requires rotor position feedback, which is obtained by a quadrature encoder sensor. For details about FOC, seeField-Oriented Control (FOC).

This example uses the quadrature encoder sensor to measure the rotor position. The quadrature encoder sensor consists of a disk with two tracks or channels that are coded 90 electrical degrees out of phase. This creates two pulses (A and B) that have a phase difference of 90 degrees and an index pulse (I). Therefore, the controller uses the phase relationship between A and B channels and the transition of channel states to determine the direction of rotation of the motor.

Models

The example includes these models:

You can use these models for both simulation and code generation. You can also use the open_system command to open the Simulink® models. For example, use this command for a F28069M based controller.

open_system('mcb_pmsm_foc_qep_f28069m.slx');

For the model names that you can use for different hardware configurations, see the Required Hardware topic in the Generate Code and Deploy Model to Target Hardware section.

Required MathWorks® Products

To simulate model:

1.For the models:mcb_pmsm_foc_qep_f28069mandmcb_pmsm_foc_qep_f28069LaunchPad

  • Motor Control Blockset™

  • Fixed-Point Designer™

2.For the modelmcb_pmsm_foc_qep_f28379d

  • Motor Control Blockset™

To generate code and deploy model:

1.For the models:mcb_pmsm_foc_qep_f28069mandmcb_pmsm_foc_qep_f28069LaunchPad

  • Motor Control Blockset™

  • Embedded Coder®

  • C2000™ Microcontroller Blockset

  • Fixed-Point Designer™

2.For the modelmcb_pmsm_foc_qep_f28379d

  • Motor Control Blockset™

  • Embedded Coder®

  • C2000™ Microcontroller Blockset

  • Fixed-Point Designer™ (only needed for optimized code generation)

Prerequisites

1.Obtain the motor parameters. We provide default motor parameters with the Simulink® model that you can replace with the values from either the motor datasheet or other sources.

However, if you have the motor control hardware, you can estimate the parameters for the motor that you want to use, by using the Motor Control Blockset parameter estimation tool. For instructions, seeEstimate PMSM Parameters Using Recommended Hardware.

The parameter estimation tool updates themotorParamvariable (in the MATLAB® workspace) with the estimated motor parameters.

2.If you obtain the motor parameters from the datasheet or other sources, update the motor parameters and inverter parameters in the model initialization script associated with the Simulink® models. For instructions, seeEstimate Control Gains and Use Utility Functions.

If you use the parameter estimation tool, you can update the inverter parameters, but do not update the motor parameters in the model initialization script. The script automatically extracts motor parameters from the updatedmotorParamworkspace variable.

Simulate Model

This example supports simulation. Follow these steps to simulate the model.

1.Open a model included with this example.

2.ClickRunon theSimulationtab to simulate the model.

3.ClickData Inspectoron theSimulationtab to view and analyze the simulation results.

Generate Code and Deploy Model to Target Hardware

This section instructs you to generate code and run the FOC algorithm on the target hardware.

This example uses a host and a target model. The host model is a user interface to the controller hardware board. You can run the host model on the host computer. The prerequisite to use the host model is to deploy the target model to the controller hardware board. The host model uses serial communication to command the target Simulink® model and run the motor in a closed-loop control.

Required Hardware

This example supports these hardware configurations. You can also use the target model name to open the model for the corresponding hardware configuration, from the MATLAB® command prompt.

For connections related to the preceding hardware configuration, seeF28069 control card configuration.

NOTE:When using BOOSTXL-3PHGANINV inverter, ensure that proper insulation is available between bottom layer of BOOSTXL-3PHGANINV and the LAUNCHXL board.

For connections related to the preceding hardware configurations, seeLAUNCHXL-F28069M and LAUNCHXL-F28379D Configurations.

在目标硬件生成代码并运行模型

1.Simulate the target model and observe the simulation results.

2.Complete the hardware connections.

3.The model automatically computes the ADC (or current) offset values. To disable this functionality (enabled by default), update the value 0 to the variable inverter.ADCOffsetCalibEnable in the model initialization script.

Alternatively, you can compute the ADC offset values and update it manually in the model initialization scripts. For instructions, seeRun 3-Phase AC Motors in Open-Loop Control and Calibrate ADC Offset.

4.Compute the quadrature encoder index offset value and update it in the model initialization scripts associated with the target model. For instructions, see正交编码器抵消校准为永磁同步电动机非常贴切or.

NOTE:Verify the number of slits available in the quadrature encoder sensor attached to your motor. Check and update the variablepmsm.QEPSlitsavailable in the model initialization script. This variable corresponds to theEncoder slitsparameter of the quadrature encoder block. For more details about theEncoder slitsandEncoder counts per slitparameters, seeQuadrature Decoder.

5.Open the target model for the hardware configuration that you want to use. If you want to change the default hardware configuration settings for the model, seeModel Configuration Parameters.

6.Load a sample program to CPU2 of LAUNCHXL-F28379D, for example, program that operates the CPU2 blue LED by using GPIO31 (c28379D_cpu2_blink.slx), to ensure that CPU2 is not mistakenly configured to use the board peripherals intended for CPU1.

7.ClickBuild, Deploy & Starton theHardwaretab to deploy the target model to the hardware.

8.Click thehost modelhyperlink in the target model to open the associated host model. You can also use the open_system command to open the host model. For example, use this command for a F28069M based controller.

open_system('mcb_host_model_f28069m.slx');

For details about the serial communication between the host and target models, seeHost-Target Communication.

9.In the model initialization script associated with the target model, specify the communication port using the variabletarget.comport. The example uses this variable to update the Port parameter of the Host Serial Setup, Host Serial Receive, and Host Serial Transmit blocks available in the host model.

10.更新参考速度值主机模型.

11.ClickRunon theSimulationtab to run the host model.

12.Change the position of the Start / Stop Motor switch to On, to start running the motor.

13.Observe the debug signals from the RX subsystem, in the Time Scope of host model.

Note: If you are using a F28379D based controller, you can also select the debug signals that you want to monitor.