Main Content

Implement Field-Oriented Control on FPGA SoC

This example shows how to deploy a field-oriented control (FOC) algorithm for brushless DC motors to an SoC device by using a custom board target. A custom board target for the Trenz Electronic™ Motor Control Development Kit, based on Xilinx® Zynq® UltraScale+ MPSoC, allows you to deploy FOC application as a mix of software to the ARM® Cortex-A processor and hardware to the programmable logic of the device. This example uses the model and control algorithm partitioning from the Hardware-Software Partitioning of a Motor Control Algorithm (SoC Blockset) example.


Motor Control Development Kit with Xilinx Zynq UltraScale+ ZU2CG-1E MPSoC Module

Note: The example only supports the rev 3 version of this Motor Control Development Kit. If you need assistance with using the rev 4 and 5 versions, contact MathWorks® support.

Add Trenz Custom Board Support

This example includes the createCustomBoard_te0820 script and premade te0820_ps.tcl file. Use the following command to access the folder containing these files.


Add the Trenz custom board definition to the SoC Blockset board registry by running createCustomBoard_te0820 script.

Board registration files are generated in the same folder. For more information on how to create and register custom board support with SoC Blockset, see Add a New Hardware Board to SoC Blockset (SoC Blockset).

Using the SoC Model Creator (SoC Blockset) tool, you can create an empty model configured to the selected target board and reference design. For information on how to create the reference design, see Define Custom Board and Reference Design for Zynq Workflow (HDL Coder). You can then add the other elements of the model such as algorithm.

This example includes the soc_trenz_top model created with the SoC Model Creator tool based on a shipping reference design, Trenz TE0820 with CR00140, containing the ADC, encoder, and PWM components.

The model includes the design elements of the FOC application from the Hardware-Software Partitioning of a Motor Control Algorithm (SoC Blockset) example. Run this command at the MATLAB® command prompt to open the model.


The model automatically computes the ADC (or current) offset values after deployment. To disable this functionality (enabled by default), update the value of the variable paramAdcEnablecalibration to 0. If you disable this functionality, you can compute the ADC offset manually and update it in the paramAdcCountAtZeroAmpere variable.

To access these variables using the data dictionary, open the model soc_trenz_proc and click Modeling > Model Explorer to open the Model Explorer window. Navigate to soc_trenz_proc > External Data > TrenzData > Design Data to update these variables. Alternatively, you can also update these variables in the model initialization script mcb_foc_fpga_data.m. Use this command to access this script.

edit mcb_foc_fpga_data.m

Follow this procedure to determine the ADC offset values manually.

  • Navigate to the System_Input subsystem in the soc_trenz_top model model and open the block parameters dialog box for the Input_Source block.

  • Set the Value parameter to inputSourceEnum.Interactive UserInput.

  • Navigate to the soc_trenz_top/Model (soc_trenz_proc) subsystem and measure the signals highlighted in the following figure. The measured values indicate the ADC offsets. Update these values in the variable paramAdcCountAtZeroAmpere.

Note: Make sure that you power OFF the inverter before calibrating the ADC offsets for phase A and phase B.

The script mcb_foc_fpga_data.m included in the example automatically computes the PI controller gains.

In addition, the script mcb_update_data_dictionary.m, included in the example, computes and updates the data dictionary variables.

The example supports two plant models for simulation. The first plant model uses the motor block from Motor Control Blockset™. The second plant uses the Simscape™ Electrical™ motor block. You can use the Plant Selection Value button on the soc_trenz_top.slx model to select one of these plant models.

In the System On Chip tab, the current Hardware Board option is set to Trenz Electronic TE0820 Custom SoC Board.

Set up the Trenz Electronic Motor Control Development Kit

Connect parts of the Trenz Electronic Motor Control Development Kit as shown in this image.

  1. 5V DC power supply

  2. SD card

  3. Micro USB cable for UART and JTAG

  4. Ethernet cable

  5. Encoder connector

  6. 24V DC power supply

  7. Motor power cable (A, B, C)

  8. 24V brushless DC motor

  9. Switch 1 (S1) controls power to the driver board

Download the prebuilt Linux image Zip archive from MathWorks GitHub repository. The Linux image is built with Buildroot and kernel version 4.9. Extract the zip archive onto the microSD card and insert the card into the SD Card slot on the hardware board.

Implement and Run on Hardware

Click Configure, Build & Deploy to launch SoC Builder from soc_trenz_top model and follow the steps.

In the Select Build Action step, select Build and load for external mode to monitor the motor operation from Simulink®. Open the Simulation Data Inspector and observe the velocity command and motor velocity response.

Other Things to Try

You can use Dashboard blocks to interactively set the reference speed from the generated software model. The soc_trenz_top_sw_exmode model is provided as a reference.