Main Content

Use Motor Control Blockset to Generate Code for Custom Target

This example shows how to use Motor Control Blockset™ with any processor.

The example shows you how to simulate and generate code from a system model configured for a Texas Instruments™ C2000™ F28069M processor. The system model uses a Field-Oriented Control (FOC) implementation that you can run on any processor. The algorithm part of the model is separated from the driver layer by using a reference model that you can deploy on any device.

Required Products


  • Simulink®

  • MATLAB® Coder™

  • Simulink® Coder™

  • Motor Control Blockset™

  • Embedded Coder®

  • Fixed-Point Designer™ (only for serial communication)

Verify Algorithm Behavior by Using System Simulation

This section shows you how to verify the controller in a closed-loop system simulation.

The system model mcb_pmsm_foc_system test bench consists of the test inputs, an embedded processor, power electronics, and motor hardware. To see the signals, use the Data Inspector button on the Simulation tab of the Simulink toolstrip. You can use this model to test the controller and explore its expected behavior.

Use this command to open the model.


NOTE: This model supports only floating-point computations.

Run the simulation and see the logged speed reference (Speed_Reference) and measured motor speed (Speed_Motor) signals in the data inspector.

Model Architecture

This section explains the model architecture and includes these sub-sections:

  • Data Specification

  • Controller Partitioning from Test Bench

  • Controller Scheduling

The model architecture facilitates system simulation and algorithmic code generation.

Data Specification

A data definition file creates the data needed for simulation and code generation. This data file is automatically run within the InitFcn callback of the system test bench model.


Another data file mcb_pmsm_foc_qep_f28069LaunchPad_data.m defines the motor and inverter parameters.

Update the motor and inverter parameters for your hardware configuration in this file. For example, update the motor parameters in the function mcb_SetPMSMMotorParameters that is called from this file.

Controller Partitioning from Test Bench

Within the system test bench model, the embedded processor is modeled as a combination of the peripherals and the controller software. The block mcb_pmsm_foc_system/Embedded Processor/Serial Receive implements the reference inputs for simulation.

open_system('mcb_pmsm_foc_system/Embedded Processor');

In this example, a separate model includes the controller software. The controller software model contains the Speed Control and Current Control subsystems of the FOC algorithm.


Controller Scheduling

The primary control method is field-oriented control. The controller has a low rate outer loop that controls the speed. It also has a higher rate inner loop that controls the current. Speed Control subsystem implements the PI controller for speed. The Current Control subsystem converts the ADC signals (or the current feedback) to per-unit values and passes them to the core controller algorithm. In addition, it also measures the speed and position values from the quardature encoder pulses.

The controller algorithm calculates the voltages. The voltages are then converted to a driver signal. The speed controller outer loop executes after each instance of the time period used to run the current control loop. You can view the variables that specify the speed and current control loop sample times by using these commands:

fprintf('Current loop sample time = %f seconds\n', Ts)
fprintf('Speed loop sample time  = %f seconds\n', Ts_speed)

Generate C Code to Integrate Controller into Embedded Application

This section shows you how to generate and visually inspect the C code function for the controller.

The generated code consists of three generated global functions:

  • void Controller_Init(void): This function should be called to perform initialization routines.

  • void Current_Controller(void): This function implements the current controller and should be called from a task running at 50e-6 seconds.

  • void Speed_Controller(void): This function implements the speed controller and should be called from a task running at 500e-6 seconds.

To specify the function prototype, see Configure C Code Generation for Model Entry-Point Functions.

Inputs to FOC Control Algorithm:

  • ExternalInputs_mcb_pmsm_foc: This is a structure with the speed reference and signal to enable the motor.

  • SensorSigs: This is an array with ${I_a}$ ADC counts, ${I_b}$ ADC counts, quadrature encoder position counts, and quadrature encoder index latch.

Outputs of FOC Control Algorithm:

  • PWM Duty: This is an array with the PWM Duty Cycles for three phases and the signal to enable PWM.

  • DebugSignals: This is an array of signals that you can log while executing the control algorithm.

Parameters for FOC Control Algorithm:

  • PI_params: This is a structure that contains the PI gains Kp_i, Ki_i, Kp_speed, and Ki_speed.

  • IsOffset, IbOffset: These are datastore variables that contain the ADC calibration offsets.

Hardware Peripheral Integration

  • Hardware peripherals are integrated with the control algorithm inside the mcb_pmsm_foc_system/Embedded Processor subsystem.

  • The ADC interrupt is used to schedule the generated code. The interrupt triggers at 50e-6 seconds.

  • The subsystem mcb_pmsm_foc_system/Embedded Processor/Hardware Init finds the ADC calibration offsets and provides them to the control algorithm.

  • The subsystem mcb_pmsm_foc_system/Embedded Processor/Sensor Driver Blocks implements the ADC and Quadrature Encoder peripherals.

  • The subsystem mcb_pmsm_foc_system/Embedded Processor/Serial Receive has the serial blocks to receive user inputs from a host model when the generated code is executing on the target.

  • The subsystem mcb_pmsm_foc_system/Embedded Processor/Inverter Driver Peripherals has the PWM driver peripherals and the Serial Transmit block to send data to the host computer. All these peripherals are used from the Texas Instruments™ C2000™ Support Package.

If you are using a custom processor, you can implement the driver logic using a custom code. You can integrate the generated code for the control algorithm with your own driver code in your preferred Integrated Development Environment (IDE).

Test Behavior of Generated Code

For details of the required hardware connections, see Hardware Connections.

  • Build and load the executable file to the target for the mcb_pmsm_foc_system model.

  • Open the host model mcb_pmsm_foc_host_model_f28069m using the host model link available in the mcb_pmsm_foc_system model.

  • Update the COM port name for the target in the Host Serial Setup block of the host model.

  • Click Run in the Simulation tab to run the host model.

  • Change the Motor Start / Stop switch position to On, to start running the motor.

  • Change the Reference Speed and monitor the effects in the scope window.