This is the second workflow that uses an algorithm to calculate the offset between the d-axis of the rotor and the index pulse position as detected by the quadrature encoder sensor attached to the permanent magnet synchronous motor (PMSM). The workflow 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. Before you run the host model on the host computer, build and deploy the target model algorithm (integrated with the hardware drivers) to the controller hardware board. The host model uses serial communication to command the target model algorithm and run the motor.
qep_calibration folder to access these files.
After you open the MATLAB® project, double-click the
qep_calibration_algorithm.slx file in the
Select Modeling > Model Settings > Model Settings to open the Configuration Parameters dialog box.
In the Solver Selection area of the Solver tab, update the Type and Solver fields.
In the Hardware Implementation tab of the Configuration Parameters dialog box, configure the parameters according to your hardware.
In the Simulink toolstrip of the target model, select Apps > Embedded Coder to open the Embedded Coder application.
In the Simulink toolstrip, select C Code > Code Interface > Default Code Mappings to open the Code Mappings - C dialog box.
In the Code Mappings - C dialog box, open the Functions tab.
For a listed C function, click the hyperlink under the Function Preview column to open the Configure C Initialize Function Interface dialog box.
Use the Configure C Initialize Function Interface dialog box to configure the interface and arguments of the C function.
Click Apply and OK to complete configuring the C function.
Repeat steps 10 to 12 for all the listed functions.
In the Simulink toolstrip of the target model, select C Code > Generate Code > Build to build the model and generate a
for the target model.
This image shows an example of the generated code for a C function.
The generated C function uses the interface that you configured.
For details about the per-unit system used in the algorithm, see Per-Unit System.
You can use the code generation software supported by the hardware manufacturer to
generate the code for the hardware drivers. For example, for the reference
STM32F302R8 controller and X-NUCLEO-IHM07M1 inverter, you can use the STM32CubeMX STM32Cube initialization code generator software to
configure the hardware peripherals and generate C code for the hardware drivers.
This example also includes the
FOC_QEP.ioc file (created by
STM32CubeMX software) containing the hardware initialization data for the reference
Alternatively, you can also use a manually written driver code.
Call the control algorithm functions from the driver code using the configured control algorithm function parameters. This image shows a call to the speed control algorithm C function.
Use the return value from the function call to complete integrating the driver with the controller algorithm.
For details about the code structure and program control flow used by the Motor Control Blockset™ examples, see Program Control Flow of Motor Control Blockset Examples.
View the integrated sample code
main.c available in the
qep_calibration\STM32Code folder as a reference.
Complete the hardware connections.
Use the code generation and deployment software supported by the hardware
manufacturer to compile, build, and generate a binary (for example
.HEX) file from the integrated code. Use the software
to flash the binary file to the target hardware.
For example, for the reference STM32F302R8 controller and X-NUCLEO-IHM07M1 inverter, use the STM32CubeMX STM32Cube initialization code generator to generate and flash the binary file.
Follow these steps to determine the offset of the quadrature encoder sensor attached to a three-phase PMSM:
Click the host model hyperlink in the target model
to open the associated host model. You can also double-click the
qep_calibration_host.slx file in the
In the Serial 1 tab of the block parameters dialog box for the Host Serial Setup block, select a Port name and enter a Baud rate for serial communication.
For details about the serial communication between the host and target models, see Host-Target Communication.
Click Run on the Simulation tab to run the host model.
Turn the Calibration slider switch to the Start position to start the calibration process by running the motor.
Observe the position signal in the time scope.
After the calibration process is complete, simulation ends and the motor stops automatically. The model displays the computed encoder offset value in the QEP offset (pu) field.
Check if the
position signal has a positive slope.
Follow these steps, if the slope is negative:
Turn the Calibration slider switch to the Stop position to stop the calibration process and stop running the motor.
Disconnect the DC voltage supply from the hardware.
Interchange any two motor phase connections and then reconnect the DC voltage supply to the hardware.
Follow steps 4 and 5 to run the calibration algorithm again and determine the encoder offset value.
The example automatically saves the computed offset value in the PositionOffset variable available in the base workspace.
Update computed offset value in the pmsm.PositionOffset variable available in the model initialization script linked to Field-Oriented Control.