Create and Run Real-Time Application from Simulink Model
This tutorial begins with a non-real-time Simulink® model of a damped oscillator, slrt_ex_osc_nrt
. To open the
model, in the MATLAB Command Window, type:
open_system(fullfile(matlabroot, 'toolbox', 'slrealtime', ... 'examples', 'slrt_ex_osc_nrt'))
To complete the tutorial, you must already know how to create, configure, and simulate
such a model. As you go through the tutorial, you transform
slrt_ex_osc_nrt
into a Simulink
Real-Time™ model configured to build as a real-time
application. You then build,
download, and execute the real-time
application on your
target computer.
The final model is named slrt_ex_osc_rt
. To open the model, in the
MATLAB Command Window, type:
open_system(fullfile(matlabroot, 'toolbox', 'slrealtime', ... 'examples', 'slrt_ex_osc_rt'))
Transform Simulink Model to Real-Time Application
To run a Simulink model as a real-time application under Simulink Real-Time, add and configure a real-time File Log block and set configuration parameters for code generation and target execution.
Add Simulink Real-Time File Log Block
Simulink Real-Time supports a real-time File Log block. This tutorial uses the File Log block, which logs signal data for display in the Simulation Data Inspector after the simulation run.
In the MATLAB Command Window, type
slrt_ex_osc_nrt
. MATLAB® loads the oscillator model and displays the Simulink block diagram.In Simulink Editor, from the Apps tab, click Simulink Real-Time.
From the Real-Time tab Prepare section, click Library Browser.
In the Simulink left pane, browse to and double-click the node Simulink Real-Time.
From the list of I/O block categories, click node Logging.
Click and drag the File Log block to the Simulink block diagram. Simulink adds a new real-time File Log block to the model.
Connect the Mux output to the File Log block input.
On the Simulation tab, from the Save button, click Save As. Enter a file name. For example, enter
slrt_ex_osc_ucf
, and then click OK. To open the unconfigured modelslrt_ex_osc_ucf
, in the MATLAB Command Window, type:open_system(fullfile(matlabroot, 'toolbox', 'slrealtime', ... 'examples', 'slrt_ex_osc_ucf'))
Set File Log Block Parameters
File Log block parameters define the decimation for logged signals. After the simulation run, the signals that you log by using this block are available in the Simulation Data Inspector. For this tutorial, configure a File Log block.
In the MATLAB Command Window, type
slrt_ex_osc_ucf
. MATLAB loads the oscillator model and displays the Simulink block diagram.Double-click the File Log block.
In the Decimation text box, type
1
. This value means that data is collected at each sample interval.Click OK, and then from the Simulation tab, click Save.
Set Configuration Parameters
The example model, slrt_ex_osc_ucf
, is a non-real-time model of
a damped oscillator. You enter the simulation and real-time run parameters in the
Configuration Parameters dialog box. These parameters provide information to
Simulink
Coder™ on how to build a real-time
application from the
Simulink model.
After you open a Simulink model and start the target computer, you can enter the simulation parameters.
In the MATLAB Command Window, type
slrt_ex_osc_ucf
.In Simulink Editor, from the Apps tab, click Simulink Real-Time.
From the Real-Time tab, click Hardware Settings.
In the Configuration Parameters dialog box, click the Solver node.
In the Solver selection section, from the Type list, select
Fixed-step
.From the Solver list, select a solver. For example, select the general-purpose solver
ode4 (Runge-Kutta)
.Under Solver details, in the Fixed-step size (fundamental sample time) box, enter the sample time for the real-time application. For example, enter
0.00025
seconds (250
microseconds). After creating the real-time application, you can change this value.If you find that a value overloads the CPU on the target computer, try a larger Fixed-step size value, such as
0.0002
seconds.The sample times of the model blocks can be multiples only of Fixed-step size. If you enter
'auto'
in Fixed-step size, the fundamental sample time is calculated from the sample times of the model blocks.In the Configuration Parameters dialog box, click the Code Generation node.
To build a real-time application, in the Target selection section, click Browse at the System target file list. Click
slrealtime.tlc
, and then click OK.Click OK.
In the Simulation tab, from the Save button, click Save As.
Enter a file name. For example, enter
slrt_ex_osc_rt
.
Start Target Computer and Display Status Monitor
Starting the target computer boots the RTOS. The target computer waits for the Simulink Real-Time software to download and start a real-time application from the development computer or to start a real-time application that was previously downloaded.
After you have configured the Simulink Real-Time product by using the Simulink Real-Time Explorer, you can start the target computer and connect the development computer to the target computer. Before building the real-time application, you start the target computer. After the build process is complete, you download the real-time application to the target computer.
The status monitor application on the target computer displays status of the real-time application and other target computer status information. The target computer display supports multiple sessions. You can choose to display the status monitor (default, session 1) or display the target computer command line interface (session 2). To display the target computer status monitor:
Start the target computer.
The target computer displays session 1 (default) and the target computer status monitor.
To view the status monitor from the development computer, you can use PuTTY to open
an SSH client and start the status monitor application
/usr/target/bin/statusmonitor
on the target computer.
For more information, see Target Computer Status Monitor.
Build and Download Real-Time Application by Using Run on Target
The example model is a real-time model of a damped oscillator,
slrt_ex_osc_rt
. To open the model, in the MATLAB Command Window,
type:
open_system(fullfile(matlabroot, 'toolbox', 'slrealtime', ... 'examples', 'slrt_ex_osc_rt'))
To generate C code, compile, link, and download the real-time application to the target computer, use the Simulink Real-Time build process available from the Simulink Editor Real-Time tab.
After you enter changes in the Configuration Parameters dialog box, you can build the real-time application. For information about real-time application options, see Simulink Real-Time Options Pane.
In the MATLAB Command Window, type
slrt_ex_osc_rt
.MATLAB loads the oscillator model and displays the Simulink block diagram.
In the Simulink Editor, from the targets list on the Real-Time tab, select the target computer on which to run the real-time application.
Click Run on Target.
On the development computer, in the Diagnostics Viewer, you can view the build status. On the Debug tab, click Diagnostics. If the software detects an error during build and download, see Troubleshooting in Simulink Real-Time.
If you have a monitor connected to your target computer, the status monitor screen displays information about the application name. For more information, see Target Computer Status Monitor. You can also monitor the event status of a
Target
object by installing a listener for object events.
Execute Real-Time Application in Simulink External Mode by Using Step-by-Step Commands
Control of the real-time application with Simulink is limited to connecting a Simulink model to a real-time application through external mode, and then starting the real-time application. Using Simulink external mode is one method to tune parameters.
The model connects to the target computer that you have selected in the Simulink Editor on the Real-Time tab.
Note
To avoid target computer communication issues, do not use Simulink external mode while Simulink Real-Time Explorer is running. Use only one interface.
After you build and download a real-time
application to the
target computer, you can run the
real-time application. This
procedure uses the Simulink model slrt_ex_osc_rt
. To open the model, in the MATLAB
Command Window, type:
open_system(fullfile(matlabroot, 'toolbox', 'slrealtime', ... 'examples', 'slrt_ex_osc_rt'))
For more information about building applications, see Build and Download Real-Time Application by Using Run on Target.
You must already have specified the required target computer environment. Specifically, in the Simulink Editor, from the target computers list on the Real-Time tab, you must select the target computer to which you want to connect.
In the Simulink Editor, connect to the target computer. In the Real-Time tab, click Toggle target computer connection
. The connection status toggles to the connected state
.
From the Run on Target pull down, start the Step by Step Commands by selecting Build Application.
After the model build completes, from the Run on Target pull down, select Deploy to Target.
After the real-time application deploys to the target, from the Run on Target pull down, select Connect Model.
After you connect the model to the real-time application, the status bar displays External to indicate that the model is ready for external mode simulation. In this mode, the model serves as the interface to the application. This connection permits tuning parameters in the real-time application without rebuilding the application. This connection also permits observing signal activity in the real-time application by using, for example, Simulink Scope blocks in the model.
From the Run on Target pull down, select Start Application.
The current Simulink model parameters are downloaded from the development computer to the real-time application. The real-time application begins running and the Simulink Scope block displays signal data.
To stop the execution, from the Run on Target pull down, select Stop Application.
When the real-time application is deployed to the target computer, the step-by-step commands provide additional workflow options.
Commands | Step-by-Step Operations |
---|---|
| In the Step by Step Commands, after you deploy the real-time application to the target computer, you can connect the model to the real-time application. The Connect Model operation enables you to use the model as an interface to the real-time application for external mode simulation. |
| In the Step by Step Commands, after you connect the model to the real-time application on the target computer, you can disconnect the model from the real-time application. The Disconnect Model operation enables you to keep the real-time simulation running on the target computer and reconnect to the model at a later time. |
| In the Step by Step Commands, after you deploy the real-time application to the target computer and connect the model to the real-time application, you can start the real-time application. The Start Application operation enables you to start the real-time application for external mode simulation. Before you start the application, you can select
the AutoImportFileLog option from the Run
on Target button. This checkbox corresponds to the
|
| When you are connected in external mode, to restart the real-time
application from time |