This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

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 real-time 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 development 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 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 options 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 contains the information required 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)

  • 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) a host scope object

  • Add signals to signal list of scope object

  • Set number of samples

  • Set decimation factor

  • Set trigger mode

  • Set trigger signal to 'Signal Generator'

  • Set trigger level

  • Set trigger slope

  • Set pre-triggering to 12 samples

Task 3: Check for Plot Figure

Does the plot figure exist?

  • If no, create figure

  • If yes, make it the current figure

Task 4: 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.

  • Toggle between pre- and post-triggering

  • Start scope object

Task 5: Wait until scope object has 'finished' state.

  • 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');
sc = addscope(tg, 'host'); % define scope object
addsignal(sc, signals);
sc.NumSamples  = 200;
sc.Decimation  = 4;
sc.TriggerMode   = 'Signal';
sc.TriggerSignal = signals(2);
sc.TriggerLevel  = 0.0;
sc.TriggerSlope  = 'rising';
sc.NumPrePostSamples = -12;
figh = findobj('Name', 'scprepostdemo'); % check for plot figure
if isempty(figh)
  figh = figure;
  set(figh, 'Name', 'scprepostdemo', 'NumberTitle', 'off');
else
  figure(figh);
end
m = 1; flag = 0; % loop to acquire data packages
for n = 1 : 25
  if isempty(find(get(0, 'Children') == figh, 1)), flag = 1; break; end
  if ~m
    setparam(tg, tPar, 2*1000*rand);
    sc.NumPrePostSamples = -sc.NumPrePostSamples;
  end
  m = rem(m + 1, 5);
  start(sc);
while ~strcmpi(sc.Status,'finished'), end; % wait for scope finished
  t = sc.Time;
  plot(t, sc.Data);
  if (sc.NumPrePostSamples < 0)
    textString = '(Pre-Triggered)';
  else
    textString = '(Post-Triggered)';
  end
  title(['scprepostdemo: ', num2str(n), ' of 25 data packages ' textString]);
  set(gca,'XLim',[t(1), t(end)], 'YLim', [-10, 10]);
  drawnow;
end
if ~flag, title('scprepostdemo: 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);