Documentation

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.

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——工厂提供:

  • 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金宝app

  • Select the Blank Project template from the start page.

  • Create themodelBasedDesignCodeGenProjectproject.

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');elseproj_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.打开project from the selection on theRecent Simulink Projectslist.

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 theProject Files Viewbutton and selectAll files View

6.Select the foldersball_tracking,controller,harnesses,plant, andutilities, right-click the icon for one of the folders, and selectAdd 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 theworkfolder, right-click the icon for one of the folders, and selectProject Path>Add to the Project Path (Including Subfolders)。如果你选择这个文件夹添加到路径,通用电气nerated 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. ClickAll files Viewand selectProject Files View。打开utilitiesfolder node.

9.Select the filesclean_up_project.m,generate_controller_code.m, andset_up_project.m, right-click the icon for one of the files, and selectAdd Shortcut to>General。Select theShortcut Managementview.

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

  • Select theset_up_project.mscript.

  • Right-click the file.

  • SelectSet 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 theclean_up_project.mscript.

  • Right-click the file.

  • SelectSet 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 theStartupshortcut.

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: /tmp/Bdoc17b_716109_27120/publish_examples2/tp5348a400/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: /tmp/Bdoc17b_716109_27120/publish_examples2/tp5348a400/ex97559686/ModelBasedDesignCodeGen/utilities/set_up_project.m

Generate Code for the Controller from Dependency Analysis View

1.打开Dependency Analysisview.

2.ClickAnalyze。Simulink Project analyzes file dependencies in the project and updates the view of the dependencies.

3.Use theZoombutton 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 theMBDController.slxmodel on the dependency map. Right-click the model and selectOpen

5.When the model opens in Simulink, click theBuildbutton to generate code for the model. Or, use thegenerate_controller_codeshortcut in the project to generate code.

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

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

bdcloseall; run('clean_up_project'); close(simulinkproject); clearall;
Running: /tmp/Bdoc17b_716109_27120/publish_examples2/tp5348a400/ex97559686/ModelBasedDesignCodeGen/utilities/clean_up_project.m Closing Project Models Clearing: Project Path Closing working folders

See Also

addFile,addFolderIncludingChildFiles,addPath,addReference,addShortcut,addLabel,close,createCategory,createLabel,removeLabel(was detachLabelFromFile ),export,findCategory,findFile,findLabel,findLabelDefinition(renamed to findLabel),isLoaded,listModifiedFiles,listRequiredFiles,refreshSourceControl,reload,removeCategory,removeFile,removeLabel,removePath,removeReference,removeShortcut

More Information

Related Examples

Was this topic helpful?