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')
  error(message('xPCTarget:examples:Connection'));
end

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.

open_system(fullfile(matlabroot,'toolbox','rtw','targets','xpc','xpcdemos','xpcosc'));

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

  • Configure for a non-Verbose build.

  • Build and download application.

set_param('xpcosc','RTWVerbose','off');
rtwbuild('xpcosc');
### Starting Simulink Real-Time build procedure for model: xpcosc
Warning: This model contains blocks that do not handle sample time
changes at runtime. To avoid incorrect results, only change
the sample time in the original model, then rebuild the model. 
### Successful completion of build procedure for model: xpcosc
### Created MLDATX ..\xpcosc.mldatx
### 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.

  • Create a Simulink Real-Time target object

  • Set sample time to 250us

  • Set stop time to a high value (10000s)

tg = SimulinkRealTime.target;
tg.SampleTime = 0.000250;
tg.StopTime   = 10000;
  • 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'

  • Get index of signal 'Integrator1'

  • Get index of signal 'Signal Generator'

  • Add signals to the scope objects

signals(1) = getsignalid(tg, 'Integrator1');
signals(2) = getsignalid(tg, 'Signal Generator');
addsignal(scs, signals);

Use the SET command to simultaneously set properties for each element of a scope vector

  • Set decimation factor

  • Set scope 1 properties.

  • Set scope 3 properties.

  • Set scope 6 properties.

  • Set scope 7 properties.

  • Set Y axis limits for each scope in the vector.

set(scs,'Decimation',1)
set(scs(1), ...
    {'NumSamples', 'TriggerMode', 'Grid', 'DisplayMode',   'YLimit'}, ...
    {200,          'FreeRun',     'On',   'Redraw', [-10, 10]});
set(scs(2), ...
    {'NumSamples', 'TriggerMode', 'TriggerSignal', 'TriggerLevel', ...
     'TriggerSlope', 'Grid', 'DisplayMode'}, ...
    {500,  'Signal',  getsignalid(tg, 'Signal Generator'),    0.0, ...
     'Rising',       'Off',  'Redraw'});
set(scs(3), 'NumSamples',100, 'TriggerMode', 'Software', 'DisplayMode', 'Numerical');
set(scs(4), ...
    {'NumSamples', 'TriggerMode', 'TriggerScope', 'Grid', 'DisplayMode'}, ...
    {2000,         'Scope',       3,              'On',   'Redraw'});
set(scs([1,2,4]), 'YLimit', 'Auto');
  • Start acquisition of every scope

  • Start simulation

  • Software trigger scope 6

start(scs);
start(tg);
trigger(scs(3));

Capture an Image of the Target Computer Video Display

  • Wait for 1 sec after the run

  • Snapshot of target computer video display

pause(1);
tg.viewTargetScreen;

Close Model

When done, close the model.

  • Close model

close_system('xpcosc',0);