Main Content

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 model to accommodate your hardware requirements. This topic discusses basic timing concepts in hardware interface models, using fixed steps for block execution.

A model 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 a real-time second as measured by a wall clock.

To adjust the model duration, open the model Configuration Parameters by clicking the Model Settings icon in the Modeling tab of the model editor toolstrip. Select Solver in the left pane. The Start time and Stop time settings define the duration. In most cases, Start time should be 0.0, and you can set Stop time to reflect the duration you want the model 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 timestep 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 timestep size of 0.01 works for the whole model.

Block Sample Time

For models that interface with hardware devices, you might prefer fixed timesteps 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 dialog settings look like this figure:

Timing options in the model configuration dialog

In this model, a block with a default Sample time setting of 0.01 executes every tenth timestep, 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.

If you set the block Sample time to 0.1, that would control 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. Access the Simulation Pacing Options dialog by clicking Run > Simulation Pacing in the Simulation tab of the model editor toolstrip

Check Enable pacing to slow down simulation, and set the slider ratio to 1 (the default). This causes simulation time to track as closely as possible with wall clock time, so 1 simulation second is approximately equal to 1 wall clock second.

Simulation Pacing Options dialog

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 puts out 10 pulses per wall clock second.

Related Topics