Signal Tracing With a Target Scope

This example shows how to trace signals with an Simulink® Real-Time™ target scope. Target scopes are used to trace or display signals on a video monitor attached to the target computer. After the script builds and downloads the oscillator model, xpcosc, to the target computer, it adds four scopes of type 'target' to the application, each scope having a different acquisition DisplayMode. The four scopes are identified by the following scope numbers: 1, 3, 6, and 7. The signals 'Signal Generator' (oscillator input) and 'Integrator1' (oscillator output) are added to and displayed on each scope.

  • scs(1): Scope # 1 is set to Redraw, Grid, FreeRun.

  • scs(2): Scope # 3 is set to Redraw, NoGrid, Signal.

  • scs(3): Scope # 6 is set to Numerical, - , Software.

  • scs(4): Scope # 7 is set to Redraw, Grid, Scope.

This example shows how to use the SET function to set multiple object property values in one command.

Check Connection Between Development and Target Computers

Use 'slrtpingtarget' to test the connection between the development and target computers.

if ~strcmp(slrtpingtarget, 'success')

Open, Build, and Download Model to the Target Computer

Open the oscillator model xpcosc. Under the model's configuration parameter Simulink Real-Time option settings, the system target file has been set to xpctarget.tlc. Hence, building the model will create an executable image, xpcosc.mldatx, that can be run on a computer booted with the Simulink Real-Time kernel.


Build the model and download the image, xpcosc.mldatx, to the target computer.

set_param('xpcosc','RTWVerbose','off'); % Configure for a non-Verbose build.
rtwbuild('xpcosc');                     % Build and download application.
### Starting Simulink Real-Time build procedure for model: xpcosc
### Successful completion of build procedure for model: xpcosc
### Looking for target: TargetPC1
### Download model onto target: TargetPC1

Run Model, Plot Target Scope Data

Create the MATLAB® variable, tg, containing the Simulink Real-Time target object. This object allows you to communicate with and control the target computer.

tg =;           % Create a Simulink Real-Time target object
tg.SampleTime = 0.000250;               % Set sample time to 250us
tg.StopTime   = 10000;                  % Set stop time to a high value (10000s)

% Define target scope objects 1, 3, 6 and 7: vectorization is used.
scs = addscope(tg, 'target', [1,3,6,7]);

% Get indices of signals 'Integrator1', 'Signal Generator'
signals(1) = getsignalid(tg, 'Integrator1');        % Get index of signal 'Integrator1'
signals(2) = getsignalid(tg, 'Signal Generator');   % Get index of signal 'Signal Generator'

addsignal(scs, signals);                % Add signals to the scope objects

% Use the SET command to simultaneously set properties for each element of a scope vector
set(scs,'Decimation',1)                 % Set decimation factor

% Set scope 1 properties.
set(scs(1), ...
    {'NumSamples', 'TriggerMode', 'Grid', 'DisplayMode',   'YLimit'}, ...
    {200,          'FreeRun',     'On',   'Redraw', [-10, 10]});

% Set scope 3 properties.
set(scs(2), ...
    {'NumSamples', 'TriggerMode', 'TriggerSignal', 'TriggerLevel', ...
     'TriggerSlope', 'Grid', 'DisplayMode'}, ...
    {500,  'Signal',  getsignalid(tg, 'Signal Generator'),    0.0, ...
     'Rising',       'Off',  'Redraw'});

% Set scope 6 properties.
set(scs(3), 'NumSamples',100, 'TriggerMode', 'Software', 'DisplayMode', 'Numerical');

% Set scope 7 properties.
set(scs(4), ...
    {'NumSamples', 'TriggerMode', 'TriggerScope', 'Grid', 'DisplayMode'}, ...
    {2000,         'Scope',       3,              'On',   'Redraw'});

% Set Y axis limits for each scope in the vector.
set(scs([1,2,4]), 'YLimit', 'Auto');

start(scs);                             % Start acquisition of every scope
start(tg);                              % Start simulation
trigger(scs(3));                        % Software trigger scope 6

Capture an Image of the Target Computer Video Display

pause(1);                               % Wait for 1 sec after the run
tg.viewTargetScreen;                    % Snapshot of target computer video display

Close Model

When done, close the model.

close_system('xpcosc',0);               % Close model
Was this topic helpful?