MATLAB Examples

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).

Contents

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 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.

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
### 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
### Created MLDATX ..\xpcosc.mldatx
### Looking for target: TargetPC1
### Download model onto target: TargetPC1

Run Model, Randomize 'Gain' Parameter, Plot Host Scope Data

This code accomplishes a number of tasks.

Task 1: Create Target Object

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
  • Start model execution

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

  • Get index of parameter 'Gain1/Gain'
  • Get index of signal 'Integrator1'
  • Get index of signal 'Signal Generator'
  • Define (add) first host scope object
  • Define (add) second host scope object

Task 3: Set properties of first scope object

  • Add 'Signal Generator' to signal list
  • Set number of samples
  • Set decimation factor
  • Set trigger mode
  • Set trigger signal to 'Signal Generator'
  • Set trigger level
  • Set trigger slope

Task 4: Set properties of second scope object

  • Add both signals to signal list
  • Set number of samples
  • Set decimation factor
  • Set trigger mode
  • Set trigger scope to first scope object

Task 5: Check for Plot Figure

Does the plot figure exist?

  • If no, create figure
  • If yes, make it the current figure

Task 7: Loop to acquire 25 data packages from the scope object.

  • Change parameter Gain1/Gain every fifth acquisition loop to a random value between 0 and 2000.
  • Start second scope (waits until triggered by first scope)
  • Start first scope
  • Wait until both scope objects have 'finished' state.

Task 8: Scope Objects Display Time Vector

First scope object: create time vector, upload scope data and display it.

  • Upload time vector
  • Upload acquired data and plot

Second scope object: create time vector, upload scope data and display it.

  • Upload time vector
  • Upload acquired data and plot
tg = SimulinkRealTime.target; % create target object
tg.SampleTime = 0.000250;
tg.StopTime   = 10000;
start(tg);
tPar = getparamid(tg, 'Gain1', 'Gain'); % get indexes
signals(1) = getsignalid(tg, 'Integrator1');
signals(2) = getsignalid(tg, 'Signal Generator');
scs    = addscope(tg, 'host'); % add scopes
scs(2) = addscope(tg, 'host');
addsignal(scs(1), signals(2)); % set scope object properties
scs(1).NumSamples    = 200;
scs(1).Decimation    = 4;
scs(1).TriggerMode   = 'Signal';
scs(1).TriggerSignal = signals(2);
scs(1).TriggerLevel  = 0.0;
scs(1).TriggerSlope  = 'Rising';
addsignal(scs(2),signals); % sect scope object properties
scs(2).NumSamples    = 200;
scs(2).Decimation    = 10;
scs(2).TriggerMode   = 'Scope';
scs(2).TriggerScope  = scs(1).ScopeId;
figh = findobj('Name', 'scscopedemo'); % check for plot figure
if isempty(figh)
  figh = figure;
  set(figh, 'Name','scscopedemo','NumberTitle','off');
else
  figure(figh);
end
m = 1; flag = 0; % loop to acquire data
for n = 1 : 25
  if isempty(find(get(0, 'Children') == figh, 1)), flag = 1; break; end
  if ~m
    setparam(tg, tPar, 2*1000*rand);
  end
  m = rem(m + 1, 5);
  scs(2).start;
  scs(1).start;
  while ~strcmpi(scs(1).Status,'finished') || ...
        ~strcmpi(scs(2).Status,'finished')
  end % wait for scope objects to finish
  subplot(2, 1, 1); % first scope object displays time vector
  t1 =  scs(1).Time;
  plot(t1, scs(1).Data, 'g');
  set(gca, 'XLim', [t1(1), t1(end)], 'YLim', [-10, 10]); ylabel('Scope 1');
  title(['scscopedemo: ', num2str(n), ' of 25 data packages']);
  subplot(2,1,2); % second scope object displays time vector
  t2 =  scs(2).Time;
  plot(t2, scs(2).Data);
  set(gca,'XLim',[t2(1),t2(end)],'YLim',[-10,10]);  ylabel('Scope 2');
  drawnow;
end
if ~flag
    subplot(2, 1, 1);
    title('scscopedemo: finished');
end

Stop and Close Model

When done, stop the application and close the model.

  • Stop model
  • Close model
stop(tg);
close_system('xpcosc',0);