Main Content

Quadrature Encoder Offset Calibration

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.

Expand the qep_calibration folder to access these files.

  • qep_calibration_algorithm.slx (target model)

  • qep_calibration_data.m (model initialization script associated with the target model)

  • qep_calibration_host.slx (host model)

Generate Code For Control Algorithm Using Embedded Coder

  1. After you open the MATLAB® project, double-click the qep_calibration_algorithm.slx file in the qep_calibration folder.

  2. Select Modeling > Model Settings > Model Settings to open the Configuration Parameters dialog box.

  3. In the Solver Selection area of the Solver tab, update the Type and Solver fields.

  4. In the Hardware Implementation tab of the Configuration Parameters dialog box, configure the parameters according to your hardware.

  5. In the Simulink toolstrip of the target model, select Apps > Embedded Coder to open the Embedded Coder application.

  6. In the Simulink toolstrip, select C Code > Code Interface > Default Code Mappings to open the Code Mappings - C dialog box.

  7. In the Code Mappings - C dialog box, open the Functions tab.

  8. For a listed C function, click the hyperlink under the Function Preview column to open the Configure C Initialize Function Interface dialog box.

  9. Use the Configure C Initialize Function Interface dialog box to configure the interface and arguments of the C function.

  10. Click Apply and OK to complete configuring the C function.

  11. Repeat steps 10 to 12 for all the listed functions.

  12. In the Simulink toolstrip of the target model, select C Code > Generate Code > Build to build the model and generate a .c file for the target model.

This image shows an example of the generated code for a C function.

Note

  • 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.

Obtain C Code For Hardware Drivers

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 hardware.

Alternatively, you can also use a manually written driver code.

Integrate Control Algorithm Code With Driver Code

  1. 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.

  2. 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.

Deploy Integrated Code to Hardware

  1. Complete the hardware connections.

  2. 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.

Control Motor Using Host Simulink Model

Follow these steps to determine the offset of the quadrature encoder sensor attached to a three-phase PMSM:

  1. 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 qep_calibration folder.

  2. In the Serial 1 tab of the block parameters dialog boxes for the Host Serial Setup, Host Serial Receive, and Host Serial Transmit blocks, 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.

  3. Click Run on the Simulation tab to run the host model.

  4. Turn the Calibration slider switch to the Start position to start the calibration process by running the motor.

  5. 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.

  6. 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.