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 Texas Instruments® C2000, Speedgoat®, and STM32G4 series of microcontrollers. 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, TI F280039C Launchpad, TI F28P650D Launchpad, and TI F2800137 Launchpad hardware targets)

  • Embedded Coder® Support Package for STMicroelectronics® STM32 Processors (You need this support package only if you are using the STM32G4xx Based 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(1);

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

Note: STM32G4xx Based hardware target does not support ParamEstSensorless control algorithm.

  • FOCSensorless - Select this option if you want to use the field-oriented control (FOC) motor control algorithm using sensorless position detection. For more details about this algorithm, see Sensorless Field-Oriented Control of PMSM.

Select Control Algorithm

ControlAlgorithm    = AlgorithmList(1);

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 initialization callbacks...
Opening the model...


Files used :
C28379dTargetMotorControl.slx
C28379dTargetMotorControlData.m
ApplicationOpenLoop.slx
ApplicationAbstractionOpenLoop.slx
MotorPMSMOpenLoop.slx
mcbOpenloopData.m
SCIInAbstractionOpenLoop.slx
SCIOutAbstractionOpenLoop.slx
C28379dHostMotorControl.slx
UserInputsOpenLoopHost.slx
SCIRXOpenLoopHost.slx


Creating a temp directory...
Copying files to temp directory...
Saving generated models to temp directory...
Opening generated models...

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 Deploy 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, modelName, modelParams, ...
    hostExists, hostName, templateRepoName, IntModelled, SciInPath,...
    SciOutPath,ApplAbPath,ApplPath,ApplHighPrioPath,ApplLowPrioPath,...
    ApplSysControlPath, SupportFile1, SupportFile2);

where:

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

  • 'modelName' - Name of the model (target model) that is deployed to the hardware.

  • 'modelParams' - Name of the model initialization script.

  • 'hostExists' - If the host file exists for communication with hardware, specify 'true'. Otherwise, specify 'false'.

  • 'hostName' - Name of the host model (if available). If unavailable, specify 'NA'.

  • 'templateRepoName' - Name of the Simulink data dictionary used by this workflow.

  • 'IntModelled' - If ADC hardware interrupt is modelled in the target model, specify 'true'. Otherwise, specify 'false'.

If you set 'IntModelled' to 'false', set the following arguments to 'NA'.

  • 'SciInPath' - Relative path of the subsystem where the user inputs should be processed.

  • 'SciOutPath' - Relative path of the subsystem where the output debug data should be processed.

  • 'ApplAbPath' - Relative path of the subsystem where application abstraction should be implemented.

  • 'ApplPath' - Relative path of the subsystem where application algorithm is implemented.

  • 'ApplHighPrioPath' - Relative path of the subsystem where ADC interrupt service routine should be implemented.

  • 'ApplLowPrioPath' - Relative path of the subsystem where slow task for the application should be implemented.

  • 'AplSysControlPath' - Relative path of the subsystem where the system state control algorithm should be implemented. Generally, this algorithm runs at a slower rate than that of 'ApplHighPrioPath' and 'ApplLowPrioPath' algorithms.

  • 'SupportFile1' and 'SupportFile2' - Supporting files (if any) required for the models to function.

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

mcbAddHardwareToList('TI F28069M Launchpad', 'C28069mTargetMotorControl',   'C28069TargetMotorControlData', true, 'C28069mHostMotorControl', 'mcbExampleConfigRepo.sldd', true, '/Serial Receive/SCI In Abstraction', '/FastTask/SCI Out Abstraction','/FastTask/Application Abstraction','NA', '/FastTask/FastTask','/SlowTask','/SystemControl', 'NA', 'NA');

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’, ‘subsystem_ref’);

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',  'ApplicationFocQepHighPrio', 'ApplicationFocQepLowPrio', 'ApplicationFocQepSysControl');

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');