MATLAB Examples

Generate Code and Simulate Models with Simulink Project API

This example shows how to add files to a new project, set up project shortcuts, generate code, and simulate the harness model. The models in this example demonstrate some principles of Model-Based Design (MBD).

This example also shows how to use the Simulink Project API to perform many project operations.

Contents

Manage a Model-Based Design Project with Simulink Project

The example models provide a controller that positions a camera fixed to a DC motor that tracks a green ball.

The MBD approach leads to modeling the controller operations:

  • Model plant: The plant is a DC motor that rotates to reposition the camera.
  • Model controller: The design uses a PID controller with feedback on angular position for positioning the DC motor.
  • Model high-level ball tracking scheduler: The scheduler checks for validity of the position of the green ball and the saturation of the reference angle. The scheduler handles the instances of no ball on the screen to track.

The design and Simulink Project have the following sections (folders).

Plant -- The plant provides:

  • Models with continuous-time dynamic systems that use basic Simulink blocks.
  • Models with linear time invariant (LTI) systems that use the LTI system block.
  • Import and export of simulation data to and from the workspace
  • Loading of parameters into the base workspace that use data dictionary and model callbacks.

Controller -- The controller provides:

  • Models with discrete-time dynamic systems that use basic Simulink blocks.
  • Models with discrete-time dynamic systems that use the PID Controller Block.
  • Enforcement of signal saturation that use a PWM Conversion subsystem.

Ball Tracking -- The ball tracking provides:

  • The basic ball tracking algorithm.
  • The terminator and ground blocks as placeholders.
  • A hybrid controller that generates motor reference angles using Stateflow.

Harnesses -- The harnesses provide top-level simulation of the full system.

Create a Simulink Project and Add Files

1. Create a Simulink project from the Blank Project template, then close the project.

To create this project from the Simulink start page:

  • In the Command Window, type simulink.
  • Select the Blank Project template from the start page.
  • Create the modelBasedDesignCodeGenProject project.

To create this project using the Simulink Project API, in the Command Window, type:

checkForProject = exist('./ModelBasedDesignCodeGen/ModelBasedDesignCodeGenProject.prj');
if (checkForProject ~= 2)
   proj_modelBasedDesignCodeGen = Simulink.createFromTemplate('blank_project.sltx','Name','modelBasedDesignCodeGenProject','Folder','./ModelBasedDesignCodeGen');
else
   proj_templateCodeGen = simulinkproject('./ModelBasedDesignCodeGen/ModelBasedDesignCodeGenProject.prj');
end
Initializing: Project Path
Setting up working folders
Identifying shadowed project files

2. Copy the example files to folders within the Simulink project. In the Command Window, type:

copyfile(fullfile(matlabroot,'toolbox','rtw','rtwdemos','slproject_codegen_demo'), ...
fullfile(pwd),'f');

3. Close the Simulink project from the Simulink Project window.

To close this project using the Simulink Project API, in the Command Window, type:

close(simulinkproject);
Closing Project Models
Clearing: Project Path
Closing working folders

4. Open the project from the selection on the Recent Simulink Projects list.

To open this project using the Simulink Project API, in the Command Window, type:

simulinkproject(fullfile('./ModelBasedDesignCodeGen/ModelBasedDesignCodeGenProject.prj'));
Initializing: Project Path
Setting up working folders
Identifying shadowed project files

5. To add existing files to the project, click the Project Files View button and select All files View.

6. Select the folders ball_tracking, controller, harnesses, plant, and utilities, right-click the icon for one of the folders, and select Add Folder to Project (Including Child Files).

To add these files to the project using the Simulink Project API, in the Command Window, type:

proj_modelBasedDesignCodeGen = simulinkproject;
if (checkForProject ~= 2)
   addFolderIncludingChildFiles(proj_modelBasedDesignCodeGen,'ball_tracking');
   addPath(proj_modelBasedDesignCodeGen,'ball_tracking');
   addFolderIncludingChildFiles(proj_modelBasedDesignCodeGen,'controller');
   addPath(proj_modelBasedDesignCodeGen,'controller');
   addFolderIncludingChildFiles(proj_modelBasedDesignCodeGen,'harnesses');
   addPath(proj_modelBasedDesignCodeGen,'harnesses');
   addFolderIncludingChildFiles(proj_modelBasedDesignCodeGen,'plant');
   addPath(proj_modelBasedDesignCodeGen,'plant');
   addFolderIncludingChildFiles(proj_modelBasedDesignCodeGen,'utilities');
   addPath(proj_modelBasedDesignCodeGen,'utilities');
end

7. Select the work folder, right-click the icon for one of the folders, and select Project Path > Add to the Project Path (Including Subfolders). If you choose to add this folder to the path, generated files (artifacts) from simulation and code generation are available on the project path. If you choose not to include the generated files in the project, the Dependency Analysis view does not display these files.

To add this folder to the project path using the Simulink Project API, in the Command Window, type:

if (checkForProject ~= 2)
    addPath(proj_modelBasedDesignCodeGen,'work');
end

8. To make the Simulink Project more accessible, define and set properties for project shortcuts. Click All files View and select Project Files View. Open the utilities folder node.

9. Select the files clean_up_project.m, generate_controller_code.m, and set_up_project.m, right-click the icon for one of the files, and select Add Shortcut to > General. Select the Shortcut Management view.

10. To set the current working folder, set the simulation cache folder, and run other setup scripts when you open the project:

  • Select the set_up_project.m script.
  • Right-click the file.
  • Select Set Shortcut Action > Startup.

To add this setup script to project startup using the Simulink Project API, in the Command Window, type:

if (checkForProject ~= 2)
    addStartupFile(proj_modelBasedDesignCodeGen,fullfile(pwd,'/utilities/set_up_project.m'));
end

11. To set the restore previous settings and run other clean-up scripts when you close the project:

  • Select the clean_up_project.m script.
  • Right-click the file.
  • Select Set Shortcut Action > Shutdown.

To add this clean-up script to project shutdown using the Simulink Project API, in the Command Window, type:

if (checkForProject ~= 2)
    addShutdownFile(proj_modelBasedDesignCodeGen,fullfile(pwd,'/utilities/clean_up_project.m'));
end

12. Close the project. When you reopen the project, the Simulink Project runs the Startup shortcut.

To complete these operations using the Simulink Project API, in the Command Window, type:

close(simulinkproject);
simulinkproject(fullfile('./ModelBasedDesignCodeGen/ModelBasedDesignCodeGenProject.prj'));
run('set_up_project');
Running: /private/tmp/Bdoc17b_685977_71560/publish_examples2/tp2586a51b/ex97559686/ModelBasedDesignCodeGen/utilities/clean_up_project.m
Closing Project Models
Clearing: Project Path
Closing working folders
Initializing: Project Path
Setting up working folders
Identifying shadowed project files
Running: /private/tmp/Bdoc17b_685977_71560/publish_examples2/tp2586a51b/ex97559686/ModelBasedDesignCodeGen/utilities/set_up_project.m

Generate Code for the Controller from Dependency Analysis View

1. Open the Dependency Analysis view.

2. Click Analyze. Simulink Project analyzes file dependencies in the project and updates the view of the dependencies.

3. Use the Zoom button to zoom into the dependencies map. Use arrow keys or click-drag with the mouse roller wheel to move around in the map.

4. Select the MBDController.slx model on the dependency map. Right-click the model and select Open.

5. When the model opens in Simulink, click the Build button to generate code for the model. Or, use the generate_controller_code shortcut in the project to generate code.

evalc('rtwbuild(''MBDController'')');

6. Observe that the contents of the work folder do not appear in the Dependency Analysis view. This setup simplifies the view to indicate the dependencies in model files and .m script file.

bdclose all;
run('clean_up_project');
close(simulinkproject);
clear all;
Running: /private/tmp/Bdoc17b_685977_71560/publish_examples2/tp2586a51b/ex97559686/ModelBasedDesignCodeGen/utilities/clean_up_project.m
Closing Project Models
Clearing: Project Path
Closing working folders

See Also

docid:simulink_ref.btpdlzu-1_2, docid:simulink_ref.buzy2cr-1, docid:simulink_ref.bvm6h9b, docid:simulink_ref.bvnagj4-1, docid:simulink_ref.bvnaieh-1, docid:simulink_ref.btpdlzt-1_4, docid:simulink_ref.btpdlzu-1_1, docid:simulink_ref.btpdlzu-1, docid:simulink_ref.btpdlzt-1_13, docid:simulink_ref.btpdlzt-1_3, docid:simulink_ref.btpdlzt-1_12, docid:simulink_ref.btpdlzt-1_11, docid:simulink_ref.btpdlzt-1_10, docid:simulink_ref.btpdlzt-1_2, docid:simulink_ref.btpdlzt-1_1, docid:simulink_ref.btpdlzt-1_9, docid:simulink_ref.bu4j88b, docid:simulink_ref.bvo7m1y, docid:simulink_ref.bu4knz7-1, docid:simulink_ref.btpdlzt-1_8, docid:simulink_ref.btpdlzt-1_7, docid:simulink_ref.btpdlzt-1_6, docid:simulink_ref.btpdlzt-1_5, docid:simulink_ref.bvm91v0-1, docid:simulink_ref.bvnahqu-1, docid:simulink_ref.bvnaiq2-1

More Information

Related Examples