Main Content

Open-Loop Control and ADC Offset Calibration

This is the first workflow that uses an algorithm to run a three-phase permanent magnet synchronous motor (PMSM) using open-loop control. 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 open_loop folder to access these files.

  • open_loop_algorithm.slx (target model)

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

  • open_loop_host.slx (host model)

Generate Code For Control Algorithm Using Embedded Coder

  1. After you open the MATLAB® project, double-click the open_loop_algorithm.slx file in the open_loop 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 corresponding configuration interface dialog box.

  9. Use the Configure C Step 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. The 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 open_loop\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 run a three-phase PMSM using open-loop control:

  1. Click the host model hyperlink in the target model to open the associated host model. You can also double-click the open_loop_host.slx file in the open_loop 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 Motor slider switch to the Start position to start running the motor.

  5. Update the reference speed value in the Speed Reference (RPM) field. It is recommended that you set the speed to a value that is approximately half the rated speed of the motor.

  6. After the motor runs, observe the ADC counts for the Ia and Ib currents in the time scope.

  7. Stop the host model simulation and turn the Motor slider switch to the Stop position to stop the motor.

Follow these steps to determine the ADC offsets for the current sensors:

  1. Disconnect the DC voltage supply from the hardware.

  2. Disconnect the motor wires for three phases from the hardware board terminals and then reconnect the DC voltage supply to the hardware.

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

  4. Observe the ADC counts for the Ia and Ib currents in the time scope. The average values of the ADC counts are the ADC offset corrections for the currents Ia and Ib. Follow these steps to obtain the average (median) values of ADC counts:

    • In the Scope window, navigate to Tools > Measurements and select Signal Statistics to display the Trace Selection and Signal Statistics areas.

    • Under Trace Selection, select a signal (Ia or Ib). The time scope displays the characteristics of the selected signal in the Signal Statistics area. You can see the median value of the selected signal in the Median field.

    Update this ADC (or current) offset value in the inverter.CtSensAOffset and inverter.CtSensBOffset variables in the model initialization script linked to Field-Oriented Control.

To compute the offset of the quadrature encoder position sensor attached to the motor, see Quadrature Encoder Offset Calibration.