Simulink Real-Time

Pre- and Post-Triggering of a Host Scope

This example shows pre- and post-triggering of a signal-triggered Simulink® Real-Time™ host scope. After the script builds and downloads the oscillator model, xpcosc, to the target computer, it adds a scope of type 'host' to the target application and the signals 'Integrator1' and 'Signal Generator' to the scope. The scope is then configured to trigger on the signal 'Signal Generator' when it reaches 0.0 on a rising slope (signal value goes negative to positive). Pre-triggering is set to 12 samples, meaning that the 12 samples collected prior to the trigger are also stored in the host scope data vector.

Once the trigger condition is met, the scope is monitored to determine when its data acquisition is complete. Next, the scope data is uploaded to the host computer and plotted. 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) and the scope toggles between a 12 sample pre-trigger and post-trigger mode.

Rationale for choosing 12 samples: The model sample rate is 250 usec. Since the decimation factor for the scope is set to 4, the difference between two acquired samples is 1 ms. The 'Signal Generator' block outputs a square wave at 20 Hertz (50 msec per wave). Therefore, the acquisition will be shifted by 12 ms (approximately 1/4 of a square wave) each time the scope is updated.

Check Connection Between Host and Target Computer

Use 'slrtpingtarget' to test for a proper host-target connection.

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 options settings, the system target file has been set to slrt.tlc. Hence, building the model will create an executable image, xpcosc.dlm, that can be run on a computer booted with the Simulink Real-Time kernel.



### Successful completion of build procedure for model: xpcosc
### Looking for target: TargetPC
### Download model onto target: TargetPC

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

Create the MATLAB® variable, tg, containing the Simulink Real-Time target object. This object contains all information needed 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
start(tg);                              % Start model execution




sc.NumSamples  = 200;                   % Set number of samples
sc.Decimation  = 4;                     % Set decimation factor
sc.TriggerMode   = 'Signal';            % Set trigger mode
sc.TriggerSignal = signals(2);          % Set trigger signal to 'Signal Generator'
sc.TriggerLevel  = 0.0;                 % Set trigger level
sc.TriggerSlope  = 'rising';            % Set trigger slope
sc.NumPrePostSamples = -12;             % Set pre-triggering to 12 samples

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

% Loop to acquire 25 data packages from the scope object.
m = 1; flag = 0;
for n = 1 : 25
  if ~m
    % Change parameter Gain1/Gain every fifth acquisition loop
    % to a random value between 0 and 2000.

    % Toggle between pre- and post-triggering
    sc.NumPrePostSamples = -sc.NumPrePostSamples;
  m = rem(m + 1, 5);

  start(sc);                            % Start scope object

  % Wait until scope object has 'finished' state.
  while ~strcmpi(sc.Status,'finished'), end;

  % Create time vector, upload scope data and display it.
  t = sc.Time;                          % Upload time vector

  if (sc.NumPrePostSamples < 0)
    textString = '(Pre-Triggered)';
    textString = '(Post-Triggered)';

  title(['scprepostdemo: ', num2str(n), ' of 25 data packages ' textString]);
if ~flag, title('scprepostdemo: finished'); end

Stop and Close Model

When done, stop the application and close the model.

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