Main Content

Quadrature Encoder Offset Calibration for PMSM Motor

This example calculates the offset between the d-axis of the rotor and encoder index pulse position as detected by the quadrature encoder sensor. The control algorithm (available in the field-oriented control and parameter estimation examples) uses this offset value to compute an accurate and precise position of the d-axis of rotor. The controller needs this position to implement the field-oriented control (FOC) correctly in the rotor flux reference frame (d-q reference frame), and therefore, run the permanent magnet synchronous motor (PMSM) correctly.


The example includes this model:

You can use these models only for code generation. You can use the open_system command to open the Simulink® model. For example, use this command for a F28035 based controller:


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 generate code and deploy model:

For the models: mcb_pmsm_qep_offset_f28035

  • Motor Control Blockset™

  • Embedded Coder®

  • Embedded Coder Support Package for Texas Instruments™ C2000™ Processors

  • Fixed-Point Designer™

Generate Code and Deploy Model to Target Hardware

This section instructs you to generate code and run the motor by using open-loop control.

The 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 model and run the motor in an open-loop configuration. You can use the host model to control the motor rotations and validate the direction of rotation of motor. The Incorrect motor direction LED in the host model turns red to indicate that the motor is running in the opposite direction. When the LED turns red, you must reverse the motor phase connections (from ABC to CBA) to change the direction of rotation. The host model displays the calculated offset value.

Required Hardware

This example supports these hardware configurations. Use the target model name (highlighted in bold) to open the model for the corresponding hardware configuration, from the MATLAB® command prompt.

  1. Complete the hardware connections.

  2. 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 target model, see Model Configuration Parameters (Motor Control Blockset).

  3. Update these motor parameters in the Configuration panel of the target model.

    • Number of Pole Pairs

    • QEP Slits

    • PWM Frequency [Hz]

    • Vd Ref in Per Unit voltage

  4. Click Build, Deploy & Start on the Hardware tab to deploy the target model to the hardware.

  5. Click the host model hyperlink 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 F28035 based controller:


    For details about the serial communication between the host and target models, see Host-Target Communication (Motor Control Blockset).

    You can use the Time Scope in the host model to monitor the rotor position and offset values.

  6. In the Host Serial Setup block mask of the host model, select a Port name.

  7. Click Run on the Simulation tab to run the host model. The motor runs and calibration begins when you start simulation. After calibration completes, simulation ends and the motor stops automatically.

  8. See the Calibration Status section to know the status of calibration process:

    • Calibration in progress - LED turns orange when the motor starts running. Notice the rotor position and offset value variations in the Time Scope (the position signal indicates a ramp signal with an amplitude between 0 and 1). After calibration completes this LED turns grey.

    • Calibration complete - LED turns green when calibration completes. Then the Calibration Output field displays the computed offset value.

    • Incorrect motor direction - LED turns red if the motor runs in the opposite direction. Then the Calibration Output field displays the value "NaN." Turn off the DC power supply (24V) and reverse the motor phase connections from ABC to CBA. Repeat steps 5 to 8 and check if the Calibration complete LED is green. Verify that the Calibration Output field displays the offset value.


This example does not support simulation.

During emergency, click the Emergency Motor Stop button to stop the motor immediately.

For examples that implement FOC using a quadrature encoder sensor, you must update the computed quadrature encoder offset value in the pmsm.PositionOffset parameter in the model initialization script linked to the example. For instructions, see Estimate Control Gains from Motor Parameters (Motor Control Blockset).

Related Topics