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.
open_loop folder to access these files.
Generate Code For Control Algorithm Using Embedded Coder
After you open the MATLAB® project, double-click the
open_loop_algorithm.slxfile 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
.cfile 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.
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
Alternatively, you can also use a manually written driver code.
Integrate Control Algorithm Code With 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
open_loop\STM32Code folder as a reference.
Deploy Integrated Code to Hardware
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.
Control Motor Using Host Simulink Model
Follow these steps to run a three-phase PMSM using open-loop control:
Click the host model hyperlink in the target model to open the associated host model. You can also double-click the
open_loop_host.slxfile 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 Motor slider switch to the Start position to start running the motor.
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.
After the motor runs, observe the ADC counts for the Ia and Ib currents in the time scope.
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:
Disconnect the DC voltage supply from the hardware.
Disconnect the motor wires for three phases from the hardware board terminals and then reconnect the DC voltage supply to the hardware.
Click Run on the Simulation tab to run the host model.
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.