Main Content

Generate Motor Control Models for Selected Algorithm and Hardware

This example shows how to use Motor Control Blockset™ along with other MathWorks products to generate host and target Simulink® models that are configured for a specific hardware and motor control technique.

Motor Control Blockset provides examples that help you design motor control applications using different microcontrollers and hardware boards. 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 and sensor-based position computation.

To help you find the right solution for your motor control application with ease, this workflow provides a unified interface that can generate host and target Simulink models preconfigured 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

  • Stateflow® (You need a license for this toolbox only if you are using the ParamEstQEP and ParamEstSensorless control algorithms)

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)

  • Stateflow (You need a license for this toolbox only if you are using the ParamEstQEP and ParamEstSensorless control algorithms)

Open Live Script

Click mcbExampleConfigurator or the Open Live Script button available on this page to open the example working directory and a live script that is identical to this doc page.

Load List of Hardware Targets and Control Algorithms from Data 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 generate host and target Simulink models that contain field-oriented control (FOC) motor control algorithm with sensorless position detection. For more details about this algorithm, see Sensorless Field-Oriented Control of PMSM (Motor Control Blockset).

Select Control Algorithm

ControlAlgorithm    = AlgorithmList(1);

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

Find and Open Models

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

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 (Motor Control Blockset).

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 model, 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 options under the HardwareTarget and ControlAlgorithm fields.

Add Hardware Option to List

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

  1. Add the Simulink models (including the host model, if any) and model initialization script for the new hardware in the example working directory (this is the directory that opens when you click mcbExampleConfigurator or the Open Live Script button).

  2. Ensure that MATLAB Current Folder is set to the example working directory and then 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 (for the new hardware target) that you want to add to the HardwareTarget field.

  • 'modelName' - Name of the target Simulink model (without '.slx' file extension) that you can deploy to the new hardware target.

  • 'modelParams' - Name of the model initialization script (without '.m' file extension) that defines the hardware parameters (for example, ADC, PWM, communication baud rate, and so on).

  • 'hostExists' - If the host Simulink model is available in the example working directory (for communication with hardware target), specify 'true'. Otherwise, specify 'false'.

  • 'hostName' - Name of the host Simulink model (without '.slx' file extension, if available in the example working directory). If unavailable, specify 'NA'.

  • 'templateRepoName' - Name of the Simulink data dictionary (with '.sldd' file extension) used by this workflow.

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

The following arguments are applicable only if you set 'IntModelled' to 'true.' 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 host and target Simulink models to function.

For example, to add the TI F28069M Launchpad 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 (such as a motor control algorithm) 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 (this is the directory that opens when you click mcbExampleConfigurator or the Open Live Script button).

  2. Ensure that MATLAB Current Folder is set to the example working directory and then 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 (for the new algorithm reference) that you want to add to the ControlAlgorithm field.

  • ’algorithm_init_script’ - Name of the algorithm initialization script (without '.m' file extension) for the new algorithm reference. The script defines the motor and algorithm reference parameters.

  • ’subsystem_ref1’, ’subsystem_ref2’, … - Names of the Simulink subsystem references (without '.slx' file extension) for the new algorithm.

  • ’host_model1’,’host_model2’, … - Names of the host models. A host model, which runs on a host computer, acts as a user interface for the algorithm reference running on the controller hardware board. For more details about the host-target communication, see Host-Target Communication (Motor Control Blockset).

  • ’simulink_data_dictionary.sldd’ - Name of the Simulink data dictionary (with '.sldd' file extension) used by this workflow.

For example, to add the FOC QEP 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 (this is the directory that opens when you click mcbExampleConfigurator or the Open Live Script button) 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 target that you want to remove.

  • ’simulink_data_dictionary.sldd’ - Name of the Simulink data dictionary (with '.sldd' file extension) used by this workflow.

For example, the following command removes TI F28069M Launchpad option from the HardwareTarget field:

mcbRemoveHardwareFromList('TI F28069M Launchpad','mcbExampleConfigRepo.sldd');

Remove Algorithm Reference from List

You can run the following command in the example working directory (this is the directory that opens when you click mcbExampleConfigurator or the Open Live Script button) 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 reference that you want to remove.

  • ’simulink_data_dictionary.sldd’ - Name of the Simulink data dictionary (with '.sldd' file extension) used by this workflow.

For example, the following command removes FOC QEP option from the ControlAlgorithm field:

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