Signal Tracing With a Host Scope in Freerun Mode

This example shows how to do freerun signal tracing using an 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 application is started and the host scope is used for data acquisition and display. Note:

  • The model sample time is 250 usec.

  • The scope is set to acquire 200 samples with a decimation factor of 4.

  • This corresponds to a display length of 250e-6 * 200 * 4 = 0.2 seconds.

The scope is started in Freerun mode, and its status is monitored until it reaches the 'Finished' state. 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.

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');
tg = slrt('TargetPC1');
load(tg,'xpcosc');
### Starting Simulink Real-Time build procedure for model: xpcosc
Warning: This model contains blocks that do not handle sample time
changes at runtime. To avoid incorrect results, only change
the sample time in the original model, then rebuild the model. 
### 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 (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

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.

  • Start scope object

  • Wait until scope-object has 'finished' acquiring the data.

Task 5: 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'); % create host scope and plot
signals(1) = getsignalid(tg, 'Integrator1');
signals(2) = getsignalid(tg, 'Signal Generator');
sc = addscope(tg, 'host'); % define host scope object
addsignal(sc, signals);
sc.NumSamples  = 200; % set number of samples and other settings
sc.Decimation  = 4;
sc.TriggerMode = 'Freerun';
figh = findobj('Name', 'scfreerundemo'); % check for plot figure
if isempty(figh)
  figh = figure;
  set(figh, 'Name', 'scfreerundemo', '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); % change gain periodically
  end
  m = rem(m + 1,  5);
  start(sc); % start scope object
  while ~strcmpi(sc.Status,'finished')
  end; % wait until scope is finished
  t = sc.Time; % create time vector and display it
  plot(t,sc.Data);
  title(['scfreerundemo: ',num2str(n),' of 25 data packages']);
  set(gca,'XLim',[t(1), t(end)]);
  set(gca,'YLim',[-10,  10]);
  drawnow;
end
if ~flag, title('scfreerundemo: 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);