Main Content

Algorithm-Export Workflows for Custom Hardware

This example enables you to use any custom motor-control hardware (hardware not used in the Motor Control Blockset™ examples) to run a three-phase permanent magnet synchronous motor (PMSM) using field-oriented control (FOC). Using the algorithm export workflows, which involve generating code for the control algorithm by using Simulink® and Embedded Coder® and then integrating it with either manually written or externally generated hardware driver code. This example explains the algorithm export workflows along with the intermediate steps.

The example uses the following hardware as a reference, but you can use any motor-control hardware:

  • Controller: STMicroelectronics® STM32F302R8

  • Inverter: STMicroelectronics® X-NUCLEO-IHM07M1

  • Motor: BLY171D (includes quadrature encoder sensor)

You can use this example to customize the control algorithm and integrate it with the drivers for your motor-control hardware. In this example, we use the STM32 Cube MX software to configure and generate code for the hardware drivers. This example supports any three-phase PMSM.

Implementing the FOC algorithm needs real-time rotor position feedback. This example uses a quadrature encoder sensor to measure the rotor position. For details about FOC, see Field-Oriented Control (FOC).

The example includes three workflows.

1. Open-loop control and ADC offset calibration — This workflow uses an algorithm that runs a PMSM using open-loop control (also known as scalar control or Volts/Hz control). You can use this workflow to check the integrity of the hardware connections and calculate the ADC offsets for the current sensors available on the hardware.

2. Quadrature encoder offset calibration — This workflow uses an algorithm that calculates the offset between the d-axis of the rotor and the index pulse position as detected by the quadrature encoder sensor. The control algorithm (available in the field-oriented control workflow) needs this offset to accurately compute the rotor position, which is necessary to implement FOC.

3. Field-oriented control — This workflow uses an algorithm that runs a PMSM using closed-loop field-oriented control (FOC). The workflow uses the ADC and quadrature sensor offsets as inputs.

Each workflow includes these steps to prepare, deploy, and run the algorithm on your hardware:

1. Generate code for the control algorithm using Embedded Coder®

2. Obtain C Code For Hardware Drivers

3. Integrate control algorithm code with the driver code

4. Deploy the integrated code to hardware

5. Control the motor using a host Simulink® model.

Open MATLAB Project

Use one of these methods to open the MATLAB® project:

  • Click Open Project.

  • Run the following command to access the supporting files of the example and open the MATLAB project.


The project contains three folders, one for each workflow required to run the final FOC algorithm. Each folder contains these contents:

  • Data script containing motor, inverter, and target hardware details.

  • Algorithm model for generating code for the control algorithm. The generated code will be available in the folder [project_root]/work/code.

  • Host model for communication with the target hardware.

  • C code which shows how to integrate the generated algorithm code and the hardware driver code (specific to STM32F302R8 & X-NUCLEO-IHM07M1).

In addition to the three folders, the project also includes an .IOC file. You can use this file with STM32 Cube MX to configure the peripherals of the target and generate code. The .IOC file available in the project is specific to the STM32F302R8 and X-NUCLEO-IHM07M1 hardware.

Workflows for Custom Hardware

Follow these workflows in sequence.

1. Open-Loop Control and ADC Offset Calibration

2. Quadrature Encoder Offset Calibration

3. Field-Oriented Control