Main Content

Generate Motor Control Models for Selected Algorithm and Hardware

This example shows how to use Motor Control Blockset™ to generate a Simulink® model that is configured for a specific hardware and motor control technique.

Motor Control Blockset provides examples that help you design motor control applications using hardware boards, including TI C2000 and Speedgoat. These examples support open-loop or scalar control as well as closed-loop or vector control techniques such as field-oriented control (FOC). The most common motor-type that these examples support is the Permanent magnet synchronous motor (PMSM). In addition, these examples support both sensorless as well sensor-based position computation.

To help you find the right solution for your motor control application with ease, this workflow provides you a unified interface that configures a Simulink model for the hardware and motor control algorithm that you want to use. You can select the hardware and algorithm by using the HardwareTarget and ControlAlgorithm fields in the live script.

Required MathWorks Products

To simulate Simulink models:

  • Motor Control Blockset™

To generate code and deploy Simulink models:

  • Motor Control Blockset

  • Embedded Coder®

  • C2000™ Microcontroller Blockset (You need a license for this toolbox only if you are using the TI F28379D Launchpad and TI F280039C Launchpad hardware targets)

Open Live Script

Click mcbExampleConfigurator to open the example working directory and a live script that is identical to this doc page.

Load Hardware and Control Algorithm from Dictionary

[HardwareList, AlgorithmList] = mcbLoadHardwareAlgoInfo('mcbExampleConfigRepo.sldd');

Select Hardware Target and Control Algorithm

Use this procedure to specify your hardware and control algorithm.

In the Select Hardware section of the live script, set the HardwareTarget field to one of these options:

Select Hardware

HardwareTarget = HardwareList(2);

In the Select Control Algorithm section of the live script, set the ControlAlgorithm field to one of these options:

Select Control Algorithm

ControlAlgorithm    = AlgorithmList(2);

After you have selected the hardware and the algorithm, run this live script section.

Find and Open Models

Run this section to generate the target and host Simulink models for the selected hardware and algorithm reference.

mcbOpenExample(HardwareTarget,ControlAlgorithm,'mcbExampleConfigRepo.sldd');
Loading template model...
Setting algorithm references...
Setting model intialization callbacks...
Opening the model...


Files used :
F280039CTargetMotorControl.slx
F280039CTargetMotorControlData.m
ApplicationOpenLoop.slx
ApplicationAbstractionOpenLoop.slx
MotorPMSMOpenLoop.slx
mcbOpenloopData.m
SCIInAbstractionOpenLoop.slx
SCIOutAbstractionOpenLoop.slx
F280039CHostMotorControl.slx
UserInputsOpenLoopHost.slx
SCIRXOpenLoopHost.slx

For details about the microcontroller architecture of the generated motor control models, see Software Architecture of Generated Motor Control Models.

Parameterize Generated Models

The generated target model includes the following two buttons that enable you to parameterize the model:

  • Edit hardware parameters - Click this button to open the hardware initialization script associated with the target model in the editor. Use this script to update the device driver specifications (for example, ADC, PWM, communication baud rate, and so on) as well as the inverter configuration parameters (for example, inverter voltage, current sense gain, and so on).

  • Edit motor and algorithm parameters - Click this button to open the algorithm initialization script associated with the target model in the editor. Use this script to update the parameters related to the control algorithm and the motor.

Simulate Target Model

  1. On the target model, click Run on the Simulation tab to simulate the model.

  2. Click Data Inspector on the Simulation tab to view and analyze the simulation results.

Generate Code and Depoly Model to Target Hardware

  1. Complete the hardware connections.

  2. On the target hardware, click Build, Deploy & Start on the Hardware tab to deploy the target model to the hardware.

  3. Click the Open host model button on the target model to open the associated host model.

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

  5. Observe the debug signals received from the target hardware, in the Selected_Signals scope of the host model.

You can use the following optional procedure to add and remove HardwareTarget and ControlAlgorithm field options.

Add Hardware Option to List

You can use the procedure in this section to add a new hardware option to the existing list under the HardwareTarget field in the Select Hardware section.

  1. Add the Simulink models (including the host model) and model initialization script for the new hardware in the example working directory.

  2. In the example working directory, run the function mcbAddHardwareToList with these arguments:

mcbAddHardwareToList(‘list_item_name’,’model1’,’model2’, … ,’model_init_script’,true,’host_model’,’simulink_data_dictionary.sldd’);

where:

  • ‘list_item_name’ - Name of the new list item that you want to add to the HardwareTarget field.

  • ‘model1’,‘model2’,… - Names of the Simulink models for the new hardware.

  • ’model_init_script’ - Name of the model initialization script for the new hardware.

  • ’host_model’ - Name of the host model.

  • ’simulink_data_dictionary.sldd’ - Name of the Simulink data dictionary used by this workflow.

For example, to add the Infineon option to the HardwareTarget field, you can run the following command.

mcbAddHardwareToList('Infineon','InfineonMotorControl','InfineonMotorControlData',true,'InfineonHostMotorControl','mcbExampleConfigRepo.sldd');

Add Algorithm Reference to List

You can use the following procedure to add a new algorithm reference to the existing list under the ControlAlgorithm field in the Select Control Algorithm section:

  1. Add the subsystem references, algorithm initialization script, and host models for the new algorithm reference in the example working directory.

  2. In the example working directory, run the function mcbAddAlgorithmToList with these arguments:

mcbAddAlgorithmToList(‘list_item_name’,’algorithm_init_script’,’subsystem_ref1’, ’subsystem_ref2’, … ,’host_model1’,’host_model2’, … ,’simulink_data_dictionary.sldd’);

Use the following arguments in the function.

  • ‘list_item_name’ - Name of the new list item that you want to add to the ControlAlgorithm field.

  • ’algorithm_init_script’ - Name of the initialization script for the new algorithm.

  • ’subsystem_ref1’, ’subsystem_ref2’, … - Names of the Simulink subsystem references for the new algorithm.

  • ’host_model1’,’host_model2’, … - Names of the host models.

  • ’simulink_data_dictionary.sldd’ - Name of the Simulink data dictionary used by this workflow.

For example, to add the QEP Calibration option to the ControlAlgorithm field, you can run the following command.

mcbAddAlgorithmToList('FOC QEP','mcbFocQepData','ApplicationFocQep','ApplicationAbstractionFocQep','SCIInAbstractionFocQep','SCIOutAbstractionFocQep','MotorFocQep','SystemInputsSimFocQep','UserInputsFocQepHost','SCIRXFocQepHost','mcbExampleConfigRepo.sldd');

Remove Hardware Option from List

You can run the following command in the example working directory to remove a hardware option from the existing list under the HardwareTarget field in the Select hardware section:

mcbRemoveHardwareFromList(‘list_item_name’,’simulink_data_dictionary.sldd’);

where:

  • ‘list_item_name’ - Name of the list item for the hardware that you want to remove.

  • ’simulink_data_dictionary.sldd’ - Name of the Simulink data dictionary used by this workflow.

For example, to remove the Infineon option from the HardwareTarget field, you can run the following command:

mcbRemoveHardwareFromList('Infineon','mcbExampleConfigRepo.sldd');

Remove Algorithm Reference from List

You can run the following command in the example working directory to remove an algorithm reference from the existing list under the ControlAlgorithm field in the Select control algorithm section:

mcbRemoveAlgorithmFromList(‘list_item_name’,’simulink_data_dictionary.sldd’);

where:

  • ‘list_item_name’ - Name of the list item for the algorithm that you want to remove.

  • ’simulink_data_dictionary.sldd’ - Name of the Simulink data dictionary used by this workflow.

For example, to remove the QEP Calibration option from the ControlAlgorithm field, you can run the following command:

mcbRemoveAlgorithmFromList('FOC QEP','mcbExampleConfigRepo.sldd');