RTW Gs
RTW Gs
R2017a
How to Contact MathWorks
Phone: 508-647-7000
The bug reports are an integral part of the documentation for each release. Examine
periodically all bug reports for a release, as such reports may identify inconsistencies
between the actual behavior of a release you are using and the behavior described in this
documentation.
In addition to reviewing bug reports, you should implement a verification and validation
strategy to identify potential bugs in your design, code, and tools.
Contents
Product Overview
1
Simulink Coder Product Description . . . . . . . . . . . . . . . . . . . 1-2
Key Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-2
vii
Build and Run Executable . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-11
Configure Model to Output Data to MAT-File . . . . . . . . . . . 2-11
Build Executable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-13
Run Executable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-14
View Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-14
viii Contents
1
Product Overview
Key Features
• ANSI/ISO C and C++ code and executables for discrete, continuous, or hybrid
Simulink and Stateflow models
• Incremental code generation for large models
• Integer, floating-point, and fixed-point data type support
• Code generation for single-rate, multirate, and asynchronous models
• Single-task, multitask, and multicore code execution with or without an RTOS
• External mode simulation for parameter tuning and signal monitoring
1-2
Code Generation Technology
Code generation technology and related products provide tooling that you can apply
to the V-model for system development. The V-model is a representation of system
development that highlights verification and validation steps in the development process.
For more information, see “Validation and Verification for System Development” on page
1-4.
To learn model design patterns that include Simulink blocks, Stateflow charts, and
MATLAB functions, and map to commonly used C constructs, see “Modeling Patterns for
C Code” (Embedded Coder).
1-3
1 Product Overview
In this section...
“V-Model for System Development” on page 1-4
“Types of Simulation and Prototyping in the V-Model” on page 1-6
“Types of In-the-Loop Testing in the V-Model” on page 1-7
“Mapping of Code Generation Goals to the V-Model” on page 1-8
1-4
Validation and Verification for System Development
Simulation Hardware-in-the-loop
Rapid simulation (HIL) testing
System Integration
System Specification
and Calibration
Software Detailed
Software Integration
Design
Coding
Depending on your application and its role in the process, you might focus on one or more
of the steps called out in the V-model or repeat steps at several stages of the V-model.
Code generation technology and related products provide tooling that you can apply to
the V-model for system development. For more information about how you can apply
MathWorks code generation technology and related products provide tooling to the V-
model process, see:
1-5
1 Product Overview
1-6
Validation and Verification for System Development
1-7
1 Product Overview
Reduced hardware
cost
Real-time Not real time Not real time Not real time Hard real time
capability (between samples) (between samples)
1-8
Validation and Verification for System Development
“Simulation” (Simulink)
“Acceleration” (Simulink)
Run standalone rapid “Accelerate, Refine, and Test “Run Rapid Simulations Over
simulations Hybrid Dynamic System on Host Range of Parameter Values”
Computer by Using RSim System
Run batch or Monte-Carlo Target File” “Run Batch Simulations
simulations Without Recompiling
“Set Up and Use Host/Target Generated Code”
Repeat simulations with Communication Channel”
varying data sets, interactively “Use MAT-Files to Feed Data
or programmatically with to Inport Blocks for Rapid
scripts, without rebuilding the Simulations”
model
1-9
1 Product Overview
1-10
Validation and Verification for System Development
“Asynchronous Events”
Specify function boundaries of “Subsystems” rtwdemo_atomic
systems rtwdemo_ssreuse
rtwdemo_filepart
rtwdemo_exporting_functions
Specify components and “Component-Based Modeling” rtwdemo_mdlreftop
boundaries for design and
incremental code generation “Component-Based Modeling”
(Embedded Coder)
1-11
1 Product Overview
“Construction of Generated
Identifiers” (Embedded Coder)
Create a zip file that contains “Relocate Code to Another rtwdemo_buildinfo
generated code files, static Development Environment”
files, and dependent data to
build generated code in an
environment other than your
host computer
Export models for validation “Package Generated Code as rtwdemo_shrlib
in a system simulator using Shared Libraries” (Embedded
shared libraries Coder)
Refine component and “Deployment”
environment model designs
by rapidly iterating between “Deployment” (Embedded
algorithm design and Coder)
prototyping
1-12
Validation and Verification for System Development
Test hardware
Generate code for rapid “Function and Class Interfaces” rtwdemo_counter
prototyping rtwdemo_counter_msvc
“Entry-Point Functions and rtwdemo_async
Scheduling”
1-13
1 Product Overview
“Run-Time Environment
Configuration” (Embedded
Coder)
Design model variants “Define, Configure, and
Activate Variants” (Simulink)
1-14
Validation and Verification for System Development
1-15
1 Product Overview
1-16
Validation and Verification for System Development
“Code Replacement
Customization” (Embedded
Coder)
Control the format and style of “Control Code Style” rtwdemo_parentheses
generated code (Embedded Coder)
Control comments inserted into “Add Custom Comments to rtwdemo_comments
generated code Generated Code” (Embedded
Coder)
Enter special instructions “Customize Post-Code- rtwdemo_buildinfo
or tags for postprocessing by Generation Build Processing”
third-party tools or processes
Include requirements links in “Review of Requirements rtwdemo_requirements
generated code Links” (Simulink Verification
and Validation)
1-17
1 Product Overview
violations
“Polyspace Bug Finder”
1-18
Validation and Verification for System Development
a. MISRA® and MISRA C are registered trademarks of MISRA Ltd., held on behalf of the MISRA Consortium.
“Code Replacement
Customization” (Embedded
Coder)
Customize and control the build “Build Process Customization” rtwdemo_buildinfo
process
1-19
1 Product Overview
“Code Replacement
Customization” (Embedded
Coder)
Enter special instructions “Customize Post-Code- rtwdemo_buildinfo
or tags for postprocessing by Generation Build Processing”
third-party tools or processes
Integrate existing externally “Block Creation” (Simulink) rtwdemos, select Integrating
written code with code with C Code or Integrating
generated for a model “External Code Integration” with C++ Code
Connect to data interfaces “Data Exchange Interfaces” rtwdemo_capi
for the generated C code data rtwdemo_asap2
structures “Data Exchange Interfaces”
(Embedded Coder)
Schedule the generated code “Timers” rtwdemos, select Multirate
Support
“Time-Based Scheduling”
“Event-Based Scheduling”
1-20
Validation and Verification for System Development
“Configure Processor-In-The-
Loop (PIL) for a Custom Target”
(Embedded Coder)
“Create a Target
Communication Channel for
Processor-In-The-Loop (PIL)
Simulation” (Embedded Coder)
1-21
1 Product Overview
1-22
Target Environments and Applications
In this section...
“About Target Environments” on page 1-23
“Types of Target Environments Supported By Simulink Coder” on page 1-23
“Applications of Supported Target Environments” on page 1-25
The code generator provides built-in system target files that generate, build, and execute
code for specific target environments. These system target files offer varying degrees
of support for interacting with the generated code to log data, tune parameters, and
experiment with or without Simulink as the external interface to your generated code.
1-23
1 Product Overview
The generated code runs in real time and behaves deterministically. The
exact nature of execution varies based on the particular behavior of the
system hardware and RTOS.
a. UNIX is a registered trademark of The Open Group in the United States and other countries.
b. Linux is a registered trademark of Linus Torvalds.
Note Compile the code produced by the code generator for target hardware by using a
two's complement representation for signed integer values. The code generator does not
provide a check for this configuration in the generated code.
1-24
Target Environments and Applications
In addition, you can deploy different parts of a Simulink model on different target
environments. For example, it is common to separate the component (algorithm or
controller) portion of a model from the environment (or plant). Using Simulink to model
an entire system (plant and controller) is often referred to as closed-loop simulation and
can provide many benefits, such as early verification of components.
The following figure shows example target environments for code generated for a model.
System model
generation
generation
Code
Code
Code
Host computer(s)
Application Description
Host Computer
Accelerated simulation You apply techniques to speed up the execution of model
simulation in the context of the MATLAB and Simulink
environments. Accelerated simulations are especially
useful when run time is long compared to the time
1-25
1 Product Overview
Application Description
associated with compilation and checking whether the
target is up to date.
Rapid simulation You execute code generated for a model in nonreal time on
the host computer, but outside the context of the MATLAB
and Simulink environments.
System simulation You integrate components into a larger system. You
provide generated source code and related dependencies
for building a system in another environment or in a host-
based shared library to which other code can dynamically
link.
Model intellectual property protection You generate a Simulink shareable object library for a
(Embedded Coder) model or subsystem for use by a third-party vendor in
another Simulink simulation environment.
Real-Time Simulator
Rapid prototyping You generate, deploy, and tune code on a real-time
simulator connected to the system hardware (for example,
physical plant or vehicle) being controlled. This design
step is crucial for validating whether a component can
control the physical system.
System simulation You integrate generated source code and dependencies
for components into a larger system that is built in
another environment. You can use shared library files for
intellectual property protection.
On-target rapid prototyping You generate code for a detailed design that you can run
in real time on an embedded microprocessor while tuning
parameters and monitoring real-time data. This design
step allows you to assess, interact with, and optimize code,
using embedded compilers and hardware.
Embedded Microprocessor
Production code generation From a model, you generate code that is optimized
for speed, memory usage, simplicity, and potentially,
compliance with industry standards and guidelines.
1-26
Target Environments and Applications
Application Description
“Software-in-the-Loop Simulation” You execute generated code with your plant model
(Embedded Coder) within Simulink to verify conversion of the model to code.
You might change the code to emulate target word size
behavior and verify numerical results expected when the
code runs on an embedded microprocessor. Or, you might
use actual target word sizes and just test production code
behavior.
“Processor-in-the-Loop Simulation” You test an object code component with a plant or
(Embedded Coder) environment model in an open- or closed-loop simulation
to verify model-to-code conversion, cross-compilation, and
software integration.
Hardware-in-the-loop (HIL) testing You verify an embedded system or embedded computing
unit (ECU), using a real-time target environment.
1-27
1 Product Overview
• By developing Simulink models and Stateflow charts, and then generating C/C++ code
from the models and charts with the Simulink Coder product
• By integrating MATLAB code for code generation in MATLAB Function blocks in a
Simulink model, and then generating C/C++ code with the Simulink Coder product
You can generate code for most Simulink blocks and many MathWorks products on page
1-3. The following figure shows the product workflow for code generation with Simulink
Coder. Other products that support code generation, such as Stateflow software, are
available.
MATLAB Simulink
Simulink
Coder
C or C++
code
Compiler or
IDE toolchain
Executable program
(runs in target environment)
The code generation workflow is a part of the V-model for system development. The
process includes code generation, code verification, and testing of the executable program
in real-time. For rapid prototyping of a real-time application, typical tasks are:
1-28
Code Generation Workflow with Simulink Coder
• Configure the model for code generation in the model configuration set
• Check the model configuration for execution efficiency using the Code Generation
Advisor
• Generate and view the C code
• Create and run the executable of the generated code
• Verify the execution results
• Build the target executable
• Run the external model target program
• Connect Simulink to the external process for testing
• Use signal monitoring and parameter tuning to further test your program.
A typical workflow for applying the software to the application development process is:
1-29
1 Product Overview
For more information on how to perform these tasks, see the Getting Started with
Simulink Coder tutorials:
1-30
2
Simulink Coder generates standalone C/C++ code for Simulink models for deployment
in a wide variety of applications. The Getting Started with Simulink Coder includes
three tutorials. It is recommended that you complete Generate C Code for a Model
first, and then the following tutorials: “Build and Run Executable” on page 2-11 and
“Tune Parameters and Monitor Signals During Execution” on page 2-17.
rtwdemo_secondOrderSystem
To generate code for a model, you must configure a solver. Simulink Coder generates only
standalone code for a fixed-step solver. On the Solver pane, select a solver that meets
2-2
Generate C Code for a Model
the performance criteria for real-time execution. For this model, observe the following
settings.
To specify a target configuration for the model, choose a system target file, a template
makefile, and a make command. You can use a ready-to-run Generic Real-Time Target
(GRT) configuration.
1 In the Configuration Parameters dialog box, select the Code Generation pane.
2 To open the System Target File Browser dialog box, click the System target file
parameter Browse button. The System Target File Browser dialog box includes a
list of available targets. This example uses the system target file grt.tlc Generic
Real-Time Target.
2-3
2 Getting Started Examples
3 Click OK.
You can specify that the code generation process automatically generates an HTML
report that includes the generated code and information about the model.
1 In the Configuration Parameters dialog box, select the Code Generation > Report
pane.
2 For this example, the following configuration parameters are selected:
After the code generation process is complete, an HTML code generation report appears
in a separate window.
1 In the Configuration Parameters dialog box, select the Code Generation pane.
2 Under Code generate objectives, select the following and then click Apply:
After the advisor runs, there are two warnings indicated by a yellow triangle.
5 On the left pane, click Check model configuration settings against code
generation objectives.
6 On the right pane, click Modify Parameters. The configuration parameters that
caused the warning are changed to the software-recommended setting.
2-4
Generate C Code for a Model
7 On the right pane, click Run This Check. The check now passes. The Code
Generation Advisor lists the parameters and their recommended settings for
Execution efficiency. Close the Code Generation Advisor.
Ignore the warning for the Identify questionable blocks within the specified
system. This warning is for production code generation which is not the goal for this
example.
2-5
2 Getting Started Examples
Leave these results in the Simulation Data Inspector. Later, you can compare the
simulation data to the output data generated from the executable shown in “Build and
Run Executable” on page 2-11.
Generate Code
1 In the Configuration Parameters dialog box, on the Code Generation pane, select
the Generate code only check box.
2 Click Apply.
3 In the Simulink Editor, press Ctrl+B.
2-6
Generate C Code for a Model
2-7
2 Getting Started Examples
• Summary
• Subsystem Report
• Code Interface Report
• Generated Code
In the left navigation pane, click Code Interface Report to open the report. The code
interface report provides information on how an external main program can interface
with the generated code. There are three entry point functions to initialize, step, and
terminate the real-time capable code.
2-8
Generate C Code for a Model
2-9
2 Getting Started Examples
Generated Code
The next example shows how to build an executable. See “Build and Run Executable” on
page 2-11.
2-10
Build and Run Executable
In this section...
“Configure Model to Output Data to MAT-File” on page 2-11
“Build Executable” on page 2-13
“Run Executable” on page 2-14
“View Results” on page 2-14
The code generation target that you select for your model determines the build process
controls that are presented to you. The example model uses the GRT code generation
target, which enables the toolchain based controls. This example shows how to build an
executable using the toolchain controls and then test the executable results.
1 In the Configuration Parameters dialog box, select the All Parameters tab and
search for MAT-file.
2 Select rt_ for the MAT-file variable name modifier value and check the MAT-
file logging box.
2-11
2 Getting Started Examples
3 On the Commonly Used Parameters tab, click the Data Import/Export pane
and specify the Save to workspace or file parameters, as shown here.
4 Click Apply.
2-12
Build and Run Executable
Build Executable
The internal MATLAB function make_rtw executes the code generation process for a
model. make_rtw performs an update diagram on the model, generates code, and builds
an executable.
1 On the All Parameters tab, search for the Toolchain parameter and set the value
to Automatically locate an installed toolchain.
2 Click Validate to verify your toolchain.
As the code generator builds the executable, Building appears on the bottom left of
the Simulink Editor window. The process is complete when the text reads Ready and
the Code Generation Report appears.
The code generator places the executable in the working folder. On Windows the
executable is rtwdemo_secondOrderSystem.exe. On Linux the executable is
rtwdemo_secondOrderSystem.
2-13
2 Getting Started Examples
Run Executable
In the MATLAB command window, run the executable using one of the following
commands:
!rtwdemo_secondOrderSystem
For Linux, type
!./rtwdemo_secondOrderSystem
View Results
This example shows you how to import data into the Simulation Data Inspector and then
compare the executable results with the simulation results. If you have not already sent
logged data from the workspace to the simulation data to the Simulation Data Inspector,
follow the instructions in “Simulate the Model” on page 2-5.
1 If the Simulation Data Inspector is not already open, in the Simulink Editor, click
the Simulation Data Inspector button.
2 To open the Import dialog, click Import from the Visualize tab on the Simulation
Data Inspector toolstrip.
3 In the Import dialog, for Import from, select the MAT-file option button.
2-14
Build and Run Executable
Click Import.
4 Click the Compare tab.
5 Select Run 1: rtwdemo_secondOrderSystem from the Baseline list and Run 2:
Imported_Data from the Compare To list.
6 Click Compare.The Simulink Data Inspector indicates that the output from
the executed code is within a reasonable tolerance of the simulation data output
previously collected in “Generate C Code for a Model” on page 2-2.
2-15
2 Getting Started Examples
The next example shows how to run the executable on your machine using Simulink as
an interface for testing. See “Tune Parameters and Monitor Signals During Execution”
on page 2-17.
2-16
Tune Parameters and Monitor Signals During Execution
In this section...
“Configure Data Accessibility” on page 2-17
“Build Standalone Executable” on page 2-19
“Run Executable” on page 2-19
“Connect Simulink to Executable” on page 2-20
“Tune Parameter” on page 2-20
“More Information” on page 2-21
This example shows how to access parameter and signal data while a generated
executable runs. Use this approach to experiment with parameters and signal inputs
during rapid prototyping.
To learn about the example model and how to generate code, see the tutorials “Generate
C Code for a Model” on page 2-2 and “Build and Run Executable” on page 2-11.
rtwdemo_secondOrderSystem
2-17
2 Getting Started Examples
2 Set Configuration Parameters > Optimization > Signals and Parameters >
Default parameter behavior to Tunable.
2-18
Tune Parameters and Monitor Signals During Execution
With this setting, by default, block parameters (such as the Gain parameter of a
Gain block) are tunable in the generated code.
3 Clear the Configuration Parameters > All Parameters > Signal storage reuse
check box.
With this setting, by default, the generated code allocates storage for signal lines.
The external mode simulation can access the values of these signals so that you can
monitor the signals, for example, by using a Scope block in the model.
1 Select the Configuration Parameters > Code Generation > Interface >
External mode check box.
This option enables the generated executable to later communicate with Simulink.
2 Generate code from the model. For example, in the model, press Ctrl+B.
Run Executable
At the command prompt, run the generated executable. Use the option -tf to override
the stop time so that the executable runs indefinitely.
2-19
2 Getting Started Examples
1 In the Simulink Editor toolbar, set the Simulation mode drop-down list to
External.
2
Click the Connect to Target button .
3 In the model, double-click the Scope block. The scope displays the values of the
system output signals.
Tune Parameter
Experiment with the value of a block parameter during execution. Observe the impact of
the change.
2-20
Tune Parameters and Monitor Signals During Execution
The Scope block shows the effect of the change on the signal values.
More Information
For more information, the following table includes common capabilities and resources for
generating and executing C and C++ code for your model.
To... See....
Configure data accessibility for rapid “Access Signal, State, and Parameter Data
prototyping During Execution”
Model multirate systems “Scheduling”
2-21
2 Getting Started Examples
To... See....
Create multiple model configuration sets “Configuration Reuse” (Simulink)
and share configuration parameter settings
across models
Control how signals are stored and “Signals”
represented in the generated code
Generate block parameter storage “Override Default Parameter Behavior by
declarations and interface block Creating Global Variables in the Generated
parameters to your code Code”
Store data separate from the model “Data Objects” (Simulink)
Interface with legacy code for simulation “External Code Integration”
and code generation
Generate separate files for subsystems and “File Packaging”
model
Configure code comments and reserve “Code Appearance”
keywords
Generate C++ compatible code “Programming Language”
Export an ASAP2 file containing “Export ASAP2 File for Data Measurement
information about your model during the and Calibration”
code generation process
Write host-based or target-based code that “Exchange Data Between Generated and
interacts with signals, states, root-level External Code Using C API”
inputs/outputs, and parameters in your
target-based application code
Create a protected model that hides all “Model Protection”
block and line information to share with
third-party
Customize the build process “Build Process Customization”
Create a custom block “Block Authoring and Customization”
Create your own target “Target Development”
2-22