Signal Tracing Using Signal Triggering

This example shows how to trace signals using 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).

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

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. The model has been configured to build for Simulink Real-Time, and building the model creates an executable image, xpcosc.mldatx, that can be run on a target 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 = slrt;                              % Create a Simulink Real-Time Object
tg.SampleTime = 0.000250;               % Set sample time to 250us
tg.StopTime   = 10000;                  % Set stop time to a high value (10000s)
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'

sc = addscope(tg, 'host');              % Define (add) a host scope object
addsignal(sc, signals);                 % Add signals to signal list of scope object

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

figh = findobj('Name', 'scsignaldemo');
if isempty(figh)
  figh = figure; set(figh, 'Name', 'scsignaldemo', 'NumberTitle', 'off');
figh = findobj('Name', 'scsignaldemo'); % Does the plot figure exist?
if isempty(figh)
  figh = figure;                        % No: Create figure
  set(figh, 'Name', 'scsignaldemo', '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);

  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
  plot(t, sc.Data);                     % Upload acquired data and plot
  title(['scsignaldemo: ', num2str(n), ' of 25 data packages']);
  set(gca,'XLim',[t(1), t(end)], 'YLim', [-10, 10]);
if ~flag, title('scsignaldemo: 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
Was this topic helpful?