How to Use Simulink Real-Time for Real-Time Simulation
To use
Simulink Real-Time, first create a Simulink model, then configure it for real-time execution by selecting a target computer. Next, build and download the model to the real-time hardware, and finally start the real-time simulation to interact with your model in real time.Syntax
The basic workflow to use Simulink Real-Time involves these steps:
- Create Model: Build your system model in Simulink.
- Configure Target: Set up the real-time target computer using
slrealtimeor Simulink Real-Time Explorer. - Build Model: Compile the model for the real-time target.
- Download Model: Transfer the compiled model to the target hardware.
- Run Simulation: Start and control the real-time execution on the hardware.
Each step uses specific commands or GUI actions to prepare and run your model in real time.
matlab
tg = slrealtime; load_system('your_model'); set_param('your_model', 'HardwareBoard', 'Speedgoat'); rtp = slrealtime.build('your_model'); slrealtime.upload(tg, rtp); slrealtime.start(tg); slrealtime.stop(tg);
Example
This example shows how to run a simple real-time simulation of a sine wave generator on Speedgoat hardware using MATLAB commands.
matlab
tg = slrealtime; % Connect to target computer model = 'sine_wave_model'; load_system(model); % Load your Simulink model set_param(model, 'HardwareBoard', 'Speedgoat'); % Set target hardware rtp = slrealtime.build(model); % Build real-time application slrealtime.upload(tg, rtp); % Upload to target slrealtime.start(tg); % Start real-time simulation pause(10); % Run for 10 seconds slrealtime.stop(tg); % Stop simulation slrealtime.shutdown(tg); % Disconnect
Output
Connecting to target computer...
Building model 'sine_wave_model' for Speedgoat...
Uploading real-time application...
Starting real-time simulation...
Simulation running for 10 seconds...
Stopping simulation...
Shutting down connection.
Common Pitfalls
Common mistakes when using Simulink Real-Time include:
- Not setting the correct target hardware in model configuration, causing build errors.
- Failing to connect to the target computer before uploading the model.
- Trying to run the simulation without building the real-time application first.
- Ignoring real-time constraints like sample time, which can cause timing errors.
Always verify hardware connection and model settings before building and running.
matlab
tg = slrealtime; % Wrong: Trying to upload before building % slrealtime.upload(tg, 'your_model'); % This causes error % Correct sequence: rtp = slrealtime.build('your_model'); slrealtime.upload(tg, rtp);
Quick Reference
Here is a quick cheat-sheet for main Simulink Real-Time commands:
| Command | Description |
|---|---|
| slrealtime | Connect to real-time target computer |
| slrealtime.build('model') | Build real-time application from Simulink model |
| slrealtime.upload(target, app) | Upload application to target hardware |
| slrealtime.start(target) | Start real-time simulation on target |
| slrealtime.stop(target) | Stop real-time simulation |
| slrealtime.shutdown(target) | Disconnect from target computer |
Key Takeaways
Always configure your Simulink model for the correct real-time target hardware before building.
Use the proper sequence: connect to target, build model, upload, then start simulation.
Monitor real-time constraints like sample time to avoid timing errors during execution.
Use
slrealtime MATLAB commands or Simulink Real-Time Explorer GUI to manage real-time simulations.Stop and shutdown the target connection properly to avoid communication issues.