Real-Time Workshop in MATLAB

Real-Time Workshop in MATLAB
Jitkomut Songsiri
July 5, 2011
This note provides a short guideline for setting up a MATLAB simulink file to acquire
data from a hardware. To be able to use this feature, you must have:
1. a data acquisition board and its driver installed properly in your PC,
2. included Real-Time Workshop toolbox while installing MATLAB (or you may
add it later).
In this document, we have tested with NI (National Instruments) PCI-6024E card with
16 analog inputs at up to 200 kS/s, 16-bit resolution and up to 2 analog output at 10
Creating a model
To create a Simulink file, follow these steps:
1. Open the Simulink library browser and browse for Real-Time Window Target
2. The two important blocks are Analog Output and Analog Input. Insert these
two in your Simulink file.
3. Analog input is used for receiving data from an external source. Double click at
this block, you will see the dialog box as shown in the follwing figure. Click at
Install new board and select the device you use. MATLAB should recognize a
wide range of boards from various companies. In box Sample Time, we put a
variable Ts (and will assign a value to it later in the Command window.) In box
Input Channels, put a vector containing the numbers of channels you want to
acquire data from. Here we use channel 1,2,3, and 4. Other parameters (such as
input range) should be automatically recognized from MATLAB after we added
the board.
If you click at the Board Setup, it shows the configuration of the board. We
basically change nothing, since the default setting works fine.
4. Analog output is used for generating a signal from MATLAB and sending it to
an external sink. Double click at this block, the parameters are similar to the
analog input block. Select your card, define the sample time (use Ts) and select
the output channel. Here we use the first channel, so we put 1.
5. You may build a simple scheme to test how it works by using a scope as a sink to
Analog input block, and use a signal generator as a source to Analog output.
6. For example, we use a sine signal with amplitude 1 and frequency 0.2 Hz (make
sure it is not faster than the sampling rate of your card). In scope block, click at
Parameters and set parameters in Data History tab. Uncheck the box Limit
data points to last and check the box Save data to workspace. We use
variable ys in array format (for some reason, we got an error while running realtime with ys in structure format). With this setting, after we run the simulation,
the variable ys will appear in the current workspace and has 5 columns; the first
column is the time vector and the remaining 4 columns are the data from the 4
Setting Simulation Parameters
On your Simulink window, click at Simulation/Configuration Parameters and set
these parameters:
1. Select Solver from the left menu. Change Solver options to Fixed-Step type
and specify a fixed-step size. Here we put a variable Ts (same as the sample time.)
This may not be necessary, but the step size should not be less than the sample
2. Select Data Import/Export from the left menu. We uncheck the boxes in
Save to workspace since we already set data logging in the scope. If you use a
workspace to get the data instead of using a scope, you may want to uncheck the
box Save option/Limit data points to last.
3. Go to Real-Time Workshop menu on the left. At Target Selection, click at
Browse to choose System target file. Another dialog box will appear. Choose
Real-Time Windows Target.
4. After setting these parameters properly. Close the dialog box of Configuration
5. On your simulink window, change from normal to external mode. In external
mode, you are not able to use some keyboard shortcuts such as oopy (Ctrl-C) or
paste (Ctrl-V) as in the normal mode.
Data logging (optional)
The number of data points from the simulation obviously depends on the simulation
time and the step size we use. We often prefer not to limit the data points to last at
a certain number. Apart from the settings described previously, this parameter is also
dependent of the setting in the menu Tools/External Mode Control Panel in the
simulink window. This is for running a simulation in real-time only.
1. Click at Signal & Triggering button.
2. The dialog box of External Signal & Triggering will appear. In the Duration
box, enter the number of sample points in a data buffer.
The Duration value is related to the Limit data points to last value in the
scope parameters box. The smaller of either value limits the number of sample
points saved to the MATLAB workspace.
Running the simulation
Suppose we have connected the hardware properly. Since we have set the parameter
sample time as Ts, we assign a value 0.01 to Ts in the command window.
1. Before running a simulation, the simulink file must be built by pressing the button
shown in the figure (black arrow). Some codes will be executed in the Command
2. If there is no error on the Command window, next, Connect to a target by
clicking at the button next to the blue arrow. Now you are ready to run the
Input Channels
This is for my own reference only. We use 4 input channels from NI-PCI 6024E card
and the 4 probes have the description as follows.
Probe color
reference signal
output signal
control signal
error signal
light blue
light blue
[1] Real-Time Windows Target User’s guide: For Use with Real-Time Workshop, The
Mathworks, version 2, page 3-33.
Download PDF