Real-Time Simulation and Testing

Real-time simulation and testing is used in two ways, differing only in whether the design or the prototype is being modeled in Simulink®software.

  • Rapid control prototyping (RCP) — Model a design by using Simulink Real-Time™ software and connect it to a physical system by using a target computer. You can work out the design flaws before investing in a physical prototype and uncover requirements for new applications.

  • Hardware-in-the-loop (HIL) simulation — Connect a Simulink Real-Time model that is a physical prototype of the design to a software plant model that represents the hardware system. You can test the prototype for safety and performance without expensive downtime for the rest of the system. You can test operation and failure conditions that are difficult to replicate and substitute for unavailable parts of the hardware system.

Real-Time Simulation and Testing Workflow

A workflow process for real-time simulation and testing leads from model creation to regression and stress testing.

Create a Simulink or Stateflow model.

  1. In the Simulink Editor, on the Simulation tab, from New, select Model.

  2. From the Simulink start page, select a model template or click Blank Model.

  3. Create block diagrams in Simulink by dragging blocks to your model. If possible, set model and block parameters to use a fixed-step solver and specify a sample time compatible with the real-time requirements of your model.

Simulate the model as a non-real-time application.

Simulink uses a computed time vector to step the model. After computing the outputs for a given time value, Simulink immediately repeats the computation for the next time value until it reaches the stop time.

Because the computed time vector is not connected to a physical clock, the outputs are calculated as fast as your computer can run. The elapsed time of the simulation can differ significantly from the elapsed time of the real system.

You can log simulation results for later comparison.

Configure the development and target computers.

  1. Configure the communication method between the development and target computers.

  2. Configure the development and target computers by using:

Prepare the model for real-time execution. Configure the build environment.

  1. Set the model Configuration Parameters to values compatible with real-time execution:

    • On the Apps tab, click Simulink Real-Time. This selection sets the System target file model parameter to slrt.tlc and sets the Type model parameter to Fixed-step.

    • On the Real-Time tab, click Hardware Settings. In Configuration Parameters > Solver > Fixed-step size select a step size that is compatible with the real-time requirements of your model.

    • On the Real-Time tab, click Hardware Settings. Select options for Configuration Parameters > Code Generation > Simulink Real-Time Options.

  2. Add Simulink Real-Time I/O blocks representing your I/O boards to the model.

  3. To visualize the simulation results, mark signals in the model for data logging with the Simulation Data Inspector. See View Data with the Simulation Data Inspector (Simulink).

Connect to the physical hardware. Restart the target computer. Configure the build environment.

  1. Connect the development computer to the Speedgoat target machine. Connect the IO Modules in the target computer to the physical hardware where applicable.

  2. Restart the target computer with the Simulink Real-Time real-time kernel by using:

Connect to the target computer. Build and download the real-time application.

  1. On the Real-Time tab, toggle the Toggle target computer connection to Connected and click Run on Target. For more information about building and downloading the real-time application, see:

Execute the real-time application.

  1. Execute the real-time application under command from the development computer or by restarting the target computer in standalone mode.

    The Simulink Real-Time software uses real-time resources on the target computer. Based on your sample rate, the Simulink Real-Time software uses interrupts to step the model at the sample rate. With each new interrupt, the real-time application computes the block outputs from your model.

  2. Execute using:

Visualize signals.

  1. Create real-time scopes and Simulink Real-Time Explorer instruments. Use them to acquire and display signal data from the real-time application. You can filter and group hierarchical signals in Explorer.

  2. Scopes created by real-time scope blocks acquire data according to Simulink sample time rules. Scopes can gather data at the top level or in an enabled or triggered subsystem. Scopes created dynamically (from the MATLAB® Command Window or the API) sample at the base rate, irrespective of the sample time of their signals.

  3. To create instrument panels, use Simulink Real-Time Explorer to create instrument panels. You can drag graphical instruments to the instrument panels and drag signals to the instruments to display signal data.

    Visualize signals using:

Tune parameters.

  1. Tune observable parameters such as time delays, input and output amplitudes, and input and output frequencies. You can filter and group hierarchical signals in Simulink Real-Time Explorer.

    Note

    Simulink Real-Time does not support parameters of multiword data types.

  2. Tune parameters using:

Prepare regression and stress tests.

Write MATLAB scripts that perform parameter sweep and extreme-value testing in a repeatable manner, accumulating results as known good data.

Special Purpose Tasks

In addition to the typical real-time simulation and testing workflow process steps, some special purpose tasks could support your development process.

  • Visualize the model with Simulink Real-Time Explorer instruments.

    Configure Simulink Real-Time Explorer by using instrument panels configured with real-time scopes and with signal display and parameter tuning graphical instruments.

  • Run the model where the physical device runs.

    Configure the real-time application to run outside MATLAB, for example in a moving vehicle. Export the Simulink Real-Time Explorer configuration as a standalone control program.

  • Prepare regression and product tests with Simulink Test™.

    Extend the regression and stress tests to cover functionality over the full parameter range.

  • Integrate the model into a third-party test environment.

    Use MATLAB Coder™ to translate MATLAB regression test scripts into C for integration into a third-party test environment.

Related Topics