Tune PI Controllers Using Field Oriented Control Autotuner Block on Real-Time Systems
This example computes the gain values of proportional-integral (PI) controllers within the speed and current controllers by using the Field Oriented Control Autotuner block. For details about field-oriented control, see Field-Oriented Control (FOC).
This model supports both simulation and code generation. When you run the model, it uses the simple values of gains for the PI controllers to achieve the steady state of the speed-control operation.
The model begins tuning only in the steady state. It introduces disturbances in the controller output depending on the controller goals (bandwidth and phase margin). The model uses the system response to disturbances to calculate the optimal controller gain.
Model
The example includes the model mcb_pmsm_foc_autotuner_speedgoat.
You can use this model for both simulation and code generation.
For details on the supported hardware configuration, see the Required Hardware section under Generate Code and Deploy Model to Target Hardware.
Required MathWorks Products
Motor Control Blockset™
Simulink® Control Design™
Simulink Real-Time™
Additional Product
Speedgoat I/O Blockset (from Speedgoat)
Prerequisites
1. The motor parameters available in the example model are for the motor that comes with the Speedgoat Electric Motor Control Kit (Baseline real-time target machine with IO397 I/O module and Electric Motor Control Kit)
. You can modify these parameters for any other motor by replacing them with values from either the motor datasheet or other sources.
2. If you obtain the motor parameters from the datasheet or other sources, update the motor and inverter parameters in the model initialization script associated with the Simulink® models. For instructions, see Model Initialization Script.
Simulate Model
This example supports simulation. Follow these steps to simulate the model.
1. Open the model included with this example.
2. Check the reference speed profile configured in the signal builder (available in mcb_pmsm_foc_autotuner_speedgoat/System Inputs/Speed Reference).
3. Check and update the FOC Autotuner parameters in the Field Oriented Control Autotuner block mask (available in the Control Algorithms/FOC_AutoTuner subsystem). For details about the Field Oriented Control Autotuner block, see Field Oriented Control Autotuner.
4. Check and update the simple gain values in the model initialization script associated with the model.
5. Click Run on the Simulation tab to simulate the model.
6. Verify that the motor reaches steady state operation for at least half of the rated speed using the simple gain values that you entered. The model begins field-oriented control (FOC) tuning (using the Field Oriented Control Autotuner block) at the seventeenth second.
7. After tuning completes, observe the computed PI controller gain values in the Display PI Params block available in the Control Algorithms subsystem.
8. Observe the system response with the newly computed PI parameters by using the Simulation Data Inspector.
For more details, see Tune PI Controllers Using Field Oriented Control Autotuner.
Generate Code and Deploy Model to Target Hardware
This section instructs you on how to generate code and run the FOC algorithm on the target hardware.
Required Hardware
This example supports Speedgoat Electric Motor Control Kit (Baseline real-time target machine with IO397 I/O module and Electric Motor Control Kit)
that includes these components:
Three-phase inverter rated for 48 V and 20 A from Speedgoat
100 W three-phase brushless DC motor from Maxon Motor
Quadrature encoder with 4096 impulses
150 W 254 V DC power supply
NOTE: Contact Speedgoat for the bit stream file that is valid for your hardware.
For details about Speedgoat hardware setup, navigate to Speedgoat Software Configuration Guide
from this page.
Generate Code and Run Model on Target Hardware
1. Simulate the model and verify that you are obtaining the desired controller response.
2. Complete the hardware connections for the Speedgoat Electric Motor Control Kit.
Calibrate current offset
1. In the model, set Operating Mode to Current Offset Calibration.
2. In the Real-Time tab on the Simulink toolstrip, select the Speedgoat kit under Target Platform.
3. In the Real-Time tab on the Simulink toolstrip, click Build Model in the Run on Target drop-down menu to build the model.
NOTE: Do not click Run on Target because this example model does not support real-time execution in external mode.
4. Navigate to the folder where Simulink built the model. Double click the file mcb_pmsm_foc_autotuner_speedgoat.mldatx
to open the Simulink Real-Time Application dialog box.
5. In the Simulink Real-Time Application dialog box, select the target computer to which you are connected. Click OK to load the application file to the hardware.
6. Enter these commands (in the same order) at the MATLAB® command prompt to execute the loaded application on the hardware.
tg = slrealtime;
tg.start;
7. After the model runs successfully, use Data Inspector on the Simulation tab to see the logged signals. The stabilized Iab_offset
signals are the current offsets.
8. Update the current offset values in the inverter.CtSensAOffset
and inverter.CtSensBOffset
variables available in the model initialization script associated with the Simulink model.
Run motor in open-loop control
1. In the model, set Operating Mode to Open Loop Speed Control.
2. In the Real-Time tab on the Simulink toolstrip, select the Speedgoat kit under Target Platform.
3. In the Real-Time tab on the Simulink toolstrip, click Build Model in the Run on Target drop-down menu to build the model.
4. Navigate to the folder where Simulink built the model. Double click the file mcb_pmsm_foc_autotuner_speedgoat.mldatx
to open the Simulink Real-Time Application dialog box.
5. In the Simulink Real-Time Application dialog box, select the target computer to which you are connected. Click OK to load the application file to the hardware.
6. Enter these commands (in the same order) at the MATLAB command prompt to execute the loaded application on the hardware.
tg = slrealtime;
tg.start;
7. After the model executes, use Data Inspector on the Simulation tab to see the logged signals. Verify that speed feedback (Speed_fb
) follows the reference speed (Speed_Ref
) signal.
For example, verify that the positive reference speed has a positive speed feedback, and the position signal (Pos_PU
) has a positive ramp.
If there is a mismatch in the sign of the reference speed and speed feedback signals, change the A leads B parameter (of the Inverter and Plant model/SpeedGoatDrivers/Condition Encoder block) either from 0 to 1 or from 1 to 0. Then follow steps 2 to 6 in this section to execute the model again on the hardware.
NOTE: In the Open Loop Speed Control mode, the motor speed is limited between 500 rpm and 1200 rpm.
Run motor in closed-loop control
1. In the model, set Operating Mode to Closed Loop Speed Control.
2. Set the FOC Autotuner button on the model to Disable to disable the field-oriented control (FOC) Autotuner.
3. In the Real-Time tab on the Simulink toolstrip, select the Speedgoat kit under Target Platform.
4. In the Real-Time tab on the Simulink toolstrip, click Build Model in the Run on Target drop-down menu to build the model.
5. Navigate to the folder where Simulink built the model. Double-click the file mcb_pmsm_foc_autotuner_speedgoat.mldatx
to open the Simulink Real-Time Application dialog box.
6. In the Simulink Real-Time Application dialog box, select the target computer to which you are connected. Click OK to load the application file to the hardware.
7. Enter these commands (in the same order) at the MATLAB command prompt to execute the loaded application on the hardware and run the motor.
tg = slrealtime;
tg.start;
The motor runs in closed-loop control at a speed that is configured in the signal builder.
8. Verify that the motor reaches steady state operation because the FOC Autotuner will not work if the motor speed is unstable.
If the motor fails to reach the steady state, change the PI parameters manually in the model initialization script (associated with the model), until the motor speed stabilizes to half the base speed of the motor.
NOTE: When tuning the PI parameters in the model initialization script, the motor may show a slow speed response.
9. If the motor reaches a stable speed, follow the steps to run FOC Autotuner.
Run FOC Autotuner
1. Set the FOC Autotuner button on the model to Enable to enable the field-oriented control autotuner.
2. Verify if Operating Mode is set to Closed Loop Speed Control.
3. Check and update the FOC Autotuner parameters (such as autotuner trigger timing and controller target) in the Field Oriented Control Autotuner block mask (available inside Control Algorithms/FOC_AutoTuner subsystem). For details about the Field Oriented Control Autotuner block, see Field Oriented Control Autotuner.
4. In the Real-Time tab on the Simulink toolstrip, select the Speedgoat kit under Target Platform.
5. In the Real-Time tab on the Simulink toolstrip, click Build Model in the Run on Target drop-down menu to build the model.
6. Navigate to the folder where Simulink built the model. Double click the file mcb_pmsm_foc_autotuner_speedgoat.mldatx
to open the Simulink Real-Time Application dialog box.
7. In the Simulink Real-Time Application dialog box, select the target computer to which you are connected. Click OK to load the application file to the hardware.
8. Enter these commands (in the same order) at the MATLAB command prompt to execute the loaded application on the hardware and run the motor.
tg = slrealtime;
tg.start;
The model begins field-oriented control (FOC) tuning (using the Field Oriented Control Autotuner block) at the seventeenth second after model execution begins on the hardware. It logs the PI controller gain values (kp_Id
, ki_Id
, kp_Iq
, ki_Iq
, kp_speed
, ki_speed
) in the Simulation Data Inspector.
9. Observe and compare the system response with the PI parameters before tuning and after tuning in the Simulation Data Inspector.
10. If the system response after tuning is satisfactory, update the gain values in the model initialization script associated with the model. For consecutive model executions, you can disable the FOC tuning using the FOC Autotuner button in the model and continue with the closed-loop testing using the new PI parameters.
NOTE: Do not reconfigure or change the reference speed value in the signal builder such that the reference speed changes during the tuning process.