This example shows how to:
Configure a Simulink® Real-Time™ Scope block to acquire signal data during a real-time simulation on target hardware.
Use TLCOptions settings to execute through CPU overloads on your target hardware.
Use a Simulink target object to check for overruns and find the minimum solver step size for avoiding overruns.
Verify that results from a simulation that uses a larger time step are acceptably accurate.
This example requires an active connection between your development computer and a real-time target machine. For information on configuring and connecting your development computer to target hardware, see Simulink Real-Time Setup and Configuration (Simulink Real-Time).
An overrun occurs when a target machine cannot compute the results for an individual time step in a simulation because the processing tasks for the step overload the CPU in the target machine. By default, when the Simulink Real-Time kernel detects a CPU overload, it stops model execution on the real-time target machine. You can use Simulink and Simulink Real-Time tools to determine the task execution time (TET) for the step that causes the overrun so that you can adjust your sample time (Ts) accordingly. However, because it is possible that a larger TET occurs later in the simulation, your model might generate another overrun when you next simulate.
You can direct the Simulink Real-Time environment using TLCOptions settings to allow CPU overloads so the target machine can execute through overruns. When your target machine executes to completion, you can determine the maximum task-execution time (TETmax) for the entire simulation. You can then use TETmax to determine the minimum step size (Tsmin) that your target machine requires to execute your model in real time without generating an overrun. For information on using TETmax to determine Tsmin, see Estimate Computation Costs.
The tools that you can use to check for overruns or determine the maximum TET include:
Simulink targets objects
Simulink Real-Time Explorer
Simulink Real-Time target management blocks
Simulink Real-Time Explorer and Simulink Real-Time target management blocks allow you to track the data graphically throughout the simulation, while the target object only reports the number of overruns and the maximum and minimum TET values. However, because the target management blocks rely on I/O processing during the simulation, they have a higher computational cost and, therefore, larger TETs than target objects do. For Simscape™ models, which tend to have long execution times, it is generally better to use a target object to check for overruns and determine TET.
The optimal time step for your simulation is one that balances speed with accuracy. If the executable version of your model overruns on your target machine, you can increase the size of the solver time step to give the CPU enough time to avoid an overload.
Generate reference results from the model on your development computer so that you have a benchmark for verifying the accuracy of results from simulating on target hardware.
To open the reference model, at the MATLAB® command prompt, enter:
ssc_resistive_ac_circuit
Simulate the model.
To see the results, open the Development Scope block.
The current through the resistor is 0.3 A.
To prepare your Simscape development model for a real-time simulation on target hardware, configure it to build an executable that tracks TET and allows you to see simulation results.
Add a Simulink Real-Time Scope block to the Simscape plant model.
In the Simulink library browser, navigate to the Simulink-Real Time > Displays and Logging library.
Add a Scope block to the Simscape model.
Connect the scope to the output signal from the PS-Simulink Converter block.
Configure Simulink Real-Time Scope block as a host scope.
Specify the Scope type parameter
as Host
.
Set the Number of samples to 100000
.
Click OK.
Configure the Simscape model configuration parameters for TET analysis using Simulink Real-Time.
Open the model configuration parameters.
In the left pane of the Model Parameter Configuration window, select Code Generation.
Click Browse...
In the System Target File Browser, for the System
Target File, select slrt.tlc
.
Click OK.
In the left pane of the Model Parameter Configuration window, select Code Generation > Verification.
Select the Measure task execution time check box.
Click OK.
Connect your development computer to a real-time target machine before you can complete these steps. For information on configuring and connecting your development computer to target hardware, see Simulink Real-Time Setup and Configuration (Simulink Real-Time).
You can use a Simulink Real-Time target object to check for overruns and determine TET after you simulate in real time on target hardware. The target object, which communicates with the real-time target machine, reports the status of the real-time application to the development computer.
Run your real-time application on the target hardware.
To generate code from your model, click the Build
button .
To set the simulation mode for executing on a target
machine, in the Simulink window, in the Modeling select
External
.
To connect your development computer and target machine
and to transfer your model parameters to the target machine, click
the Connect to Target button .
To execute your real-time application on the target
machine, click the Run button .
To use the target object to print the simulation status, on your development computer, at the MATLAB command prompt, enter:
tg = slrt
Target: SLRTLABTGT2 Connected = Yes Application = ssc_resistive_ac_circuit Mode = Real-Time Single-Tasking Status = stopped CPUOverload = detected ExecTime = 0.0000 SessionTime = 11.8761 StopTime = 1.000000 SampleTime = 0.000010 AvgTET = 0.000021 MinTET = 0.000021 MaxTET = 0.000021 ViewMode = 0 TimeLog = Vector(2) StateLog = Off OutputLog = Off TETLog = Vector(2) MaxLogSamples = 50000 NumLogWraps = 0 LogMode = Normal Scopes = 1 NumSignals = 3 ShowSignals = off NumParameters = 2 ShowParameters = off
In this case, when the task execution time (maxTET = 0.000021) exceeds the step size (SampleTime = 0.000010), the Simulink Real-Time kernel detects an overrun (CPUOverload = detected) and stops the simulation.
The simulation stops at the very beginning of the execution even though it has a specified
stop time of 1 second (ExecTime = 0,
StopTime = 1.000000). In order to determine the
maximum TET for the entire simulation time, use the Simulink
Real-Time
TLCOptions
Properties function to configure the
real-time application to continue to run after it detects an overrun.
The function allows you to specify target computer overload values for
the Number of acceptable overloads property
programmatically. The application executes through the number of
overloads that you specify, but stops executing if it detects an
additional
overload.
set_param('ssc_resistive_ac_circuit',... 'TLCOptions', '-axPCMaxOverloads=200000')
To regenerate the code with the new property setting, click the Build button.
To reconnect to the target machine, click the Connect To Target button.
Run the simulation.
Obtain the statistics.
tg = slrt
Target: SLRTLABTGT2 Connected = Yes Application = ssc_resistive_ac_circuit Mode = Real-Time Single-Tasking Status = stopped CPUOverload = none ExecTime = 1.0000 SessionTime = 6.3129 StopTime = 1.000000 SampleTime = 0.000010 AvgTET = 0.000016 MinTET = 0.000014 MaxTET = 0.000032 ViewMode = 0 TimeLog = Vector(50000) StateLog = Off OutputLog = Off TETLog = Vector(50000) MaxLogSamples = 50000 NumLogWraps = 2 LogMode = Normal Scopes = 1 NumSignals = 3 ShowSignals = off NumParameters = 2 ShowParameters = off
The simulation executes to completion with a maximum TET of 0.000032.
Adjust the sample time, rerun the simulation, check for overruns and for the accuracy of the results.
To avoid simulation overruns, specify a step size that is at least 20% larger than the TETmax from the simulation on your real-time target machine. At the MATLAB command prompt, enter:
ts = 4e-5;
Remove the overload override.
set_param('ssc_resistive_ac_circuit', 'TLCOptions', '')
Regenerate the code.
Connect to the target machine.
Run the simulation.
Obtain the statistics.
tg = slrt
Target: SLRTLABTGT2 Connected = Yes Application = ssc_resistive_ac_circuit Mode = Real-Time Single-Tasking Status = stopped CPUOverload = none ExecTime = 1.0000 SessionTime = 8.7715 StopTime = 1.000000 SampleTime = 0.000040 AvgTET = 0.000016 MinTET = 0.000015 MaxTET = 0.000028 ViewMode = 0 TimeLog = Vector(25001) StateLog = Off OutputLog = Off TETLog = Vector(25001) MaxLogSamples = 50000 NumLogWraps = 0 LogMode = Normal Scopes = 1 NumSignals = 3 ShowSignals = off NumParameters = 2 ShowParameters = off
The simulation executes to completion with a maximum TET of 0.000028.
To see the results, open the Development Scope block.
The current through the resistor is 0.3 A. The results match the reference results and the simulation now runs without yielding an overrun.
Get Overload Counter | Scope | Set Overload Counter | SimulinkRealTime.target
| TLC Command-Line Options | Task Execution Time | slrt
| xPCIsOverloaded