Signal Tracing Using Scope Triggering

This example shows how to trace signals with a scope triggered Simulink® Real-Time™ host scope. After the script builds and downloads the oscillator model, xpcosc, it adds two scopes of type 'host' to the real-time application. The first scope is configured to trigger on the signal 'Signal Generator' (the only signal added to this scope). The 'Integrator1' signal is also added to the second scope. Scope 2 is configured to be triggered by the first scope (i.e., it is triggered at the same time the first scope is triggered). This synchronizes the scopes.

Next, the scopes are started and monitored to determine when data acquisition is complete. Data from both scopes are then uploaded to the development computer and plotted. Although both scopes begin data acquisition at the same time, Scope 2 acquires data over a longer time record by increasing the decimation factor from 4 to 10. This process repeats 25 times. After every fifth run, the damping gain 'Gain1/Gain' is set to a new random value (between 0 and 2000).

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 slrt.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
### Generated code for 'xpcosc' is up to date because no structural, parameter or code replacement library changes were found.
### Successful completion of build procedure for model: xpcosc
### Looking for target: TargetPC1
### Download model onto target: TargetPC1

Run Model, Randomize 'Gain' Parameter, Plot Host 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
start(tg);                              % Start model execution

Create, configure, and plot to the host scope during each run.

tPar = getparamid(tg, 'Gain1', 'Gain');             % Get index of parameter 'Gain1/Gain'
signals(1) = getsignalid(tg, 'Integrator1');        % Get index of signal 'Integrator1'
signals(2) = getsignalid(tg, 'Signal Generator');   % Get index of signal 'Signal Generator'

scs    = addscope(tg, 'host');          % Define (add) first host scope object
scs(2) = addscope(tg, 'host');          % Define (add) second host scope object

% Set properties of first scope object
addsignal(scs(1), signals(2));          % Add 'Signal Generator' to signal list
scs(1).NumSamples    = 200;             % Set number of samples
scs(1).Decimation    = 4;               % Set decimation factor
scs(1).TriggerMode   = 'Signal';        % Set trigger mode
scs(1).TriggerSignal = signals(2);      % Set trigger signal to 'Signal Generator'
scs(1).TriggerLevel  = 0.0;             % Set trigger level
scs(1).TriggerSlope  = 'Rising';        % Set trigger slope

% Set properties of second scope object
addsignal(scs(2),signals);              % Add both signals to signal list
scs(2).NumSamples    = 200;             % Set number of samples
scs(2).Decimation    = 10;              % Set decimation factor
scs(2).TriggerMode   = 'Scope';         % Set trigger mode
scs(2).TriggerScope  = scs(1).ScopeId;  % Set trigger scope to first scope object

figh = findobj('Name', 'scscopedemo');  % Does the plot figure exist?
if isempty(figh)
  figh = figure;                        % No: Create figure
  set(figh, 'Name','scscopedemo','NumberTitle','off');
  figure(figh);                         % Yes: Make it the current figure

% Loop to acquire 25 data packages from the scope object.
m = 1; flag = 0;
for n = 1 : 25
  if isempty(find(get(0, 'Children') == figh, 1)), flag = 1; break; end
  % Change parameter Gain1/Gain every fifth acquisition loop
  % to a random value between 0 and 2000.
  if ~m
    setparam(tg, tPar, 2*1000*rand);
  m = rem(m + 1, 5);

  scs(2).start; % Start second scope (waits until triggered by first scope)
  scs(1).start; % Start first scope

  % Wait until both scope objects have 'finished' state.
  while ~strcmpi(scs(1).Status,'finished') || ...

  % First scope object: create time vector, upload scope data and display it.
  subplot(2, 1, 1);
  t1 =  scs(1).Time;                    % Upload time vector
  plot(t1, scs(1).Data, 'g');           % Upload acquired data and plot
  set(gca, 'XLim', [t1(1), t1(end)], 'YLim', [-10, 10]); ylabel('Scope 1');

  title(['scscopedemo: ', num2str(n), ' of 25 data packages']);

  % Second scope object: create time vector, upload scope data and display it.
  t2 =  scs(2).Time;                    % Upload time vector
  plot(t2, scs(2).Data);                % Upload acquired data and plot
  set(gca,'XLim',[t2(1),t2(end)],'YLim',[-10,10]);  ylabel('Scope 2');

if ~flag
    subplot(2, 1, 1);
    title('scscopedemo: finished');

Stop and Close Model

When done, stop the application and close the model.

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