Timing in Hardware Interface Models
Simulation Time
When blocks in your Simulink® model must interface with hardware devices, you might have to consider how long the simulation takes to run in real time versus simulation time and how often and how many times the hardware interface blocks execute during a simulation. Usually your hardware communication rates are relative to real-world or "wall clock" time. You can adjust the duration of a simulation, the execution rate of the blocks, and the pacing of the simulation to accommodate your hardware requirements. This topic discusses basic timing concepts in hardware interface models, using fixed steps for block execution.
A simulation has a duration defined by a start time and a stop time. The default duration is 10 units of simulation time (or simulated seconds). These simulation seconds are not necessarily equivalent to real-time seconds as measured by a wall clock.
To adjust the model duration, open the model Configuration Parameters dialog box. In
the Simulink Toolstrip, on the Modeling tab, click Model
Settings. In the left pane, select Solver. The
Start time and Stop time settings define
the duration. In most cases, the Start time should be
0.0
, and you can set the Stop time to
reflect the duration you want the simulation to have.
As a simulation runs, the clocking for block execution is performed by a series of timesteps. With a setting for an automatic solver with fixed time step sizes, during compilation Simulink calculates the timestep frequency to accommodate the Sample time parameter settings of all the blocks in the model. For example, if all the timed blocks in the model have a Sample time setting of 0.01 or a multiple of that, then a time step size of 0.01 works for the whole model.
Block Sample Time
For models that interface with hardware devices, you might prefer fixed time steps of a specified rate. For example, you might need millisecond resolution to control the timing relationship of your blocks. Set the timing options as follows:
Start time:
0.0
Stop time:
10.0
Type:
Fixed-step
Solver:
discrete
Fixed-step size:
0.001
The image shows the dialog box with these settings.
In this model, a block with a default Sample time setting of
0.01
executes every tenth time step, or 1001 times in a 10 second
simulation. Another block that must run at twice the rate should have Sample
time set to 0.005
.
Note
In most cases, you can leave the Fixed-step size setting to
auto
, allowing Simulink to calculate the appropriate fundamental sample time based on all the
block settings.
Because the simulation duration is 10 simulated seconds, and the Sample time period of the block is 0.01 simulated seconds, that block executes 1001 times in a complete simulation (including first and last step). The simulation runs as fast as its blocks can perform, and those 1001 executions might take significantly less than 10 seconds of wall clock time. So the simulation in real time is determined by how fast it can execute the blocks in the model for the required number of iterations. Often the purpose of simulation is to model behavior in a way that takes less time than it would in a real-world situation. In these cases, the sequence and repetition of block execution is important, while the actual span of real-world time might not be.
Pacing Model Simulation
You might have a requirement for a model to interact with a hardware device by repeating some operation at fixed intervals of real-world time. For example, a block might repeatedly read data from a thermometer or send triggers for an external signal generator to output a pulse train.
Setting the block Sample time to 0.1
controls
the rate of block execution only in simulation time. To correlate simulation time to
real time, you can use simulation pacing to slow down a simulation to run at the pace of
real-world time. To access the Simulation Pacing Options dialog box, in the Simulink Toolstrip, on the Simulation tab, click the
Run button arrow then select Simulation
Pacing.
To pace the simulation, select Enable pacing to slow down
simulation. By default, the ratio of simulation time to real time is
1
, which means that simulating a model for one second of
simulation time is paced to take approximately one second of real time.
With this pacing setting, a block Sample time of
0.1
is approximately equal to 0.1 wall clock seconds, resulting
in ten block executions per second. So a block that generates a device output pulse
every 0.1 simulation seconds, now generates 10 pulses per wall clock second.
For information on cases requiring more precise pacing to wall clock time, see Real-Time Simulation and Testing.
See Also
Tools
- Simulation Pacing Options (Simulink)
Related Topics
- What Is Sample Time? (Simulink)