Time- and Value-Equidistant Data Logging

This example shows how to do time- and value-equidistant data logging with Simulink® Real-Time™. After the script builds and downloads the oscillator model, xpcosc, to the target computer, it runs the application and logs data for 0.2 sec. The option to log states is turned off for this example.

At the end of the first run, the time and output logs (tg.TimeLog and tg.OutputLog) are retrieved and plotted on the development computer. Initially the logging mode is time-equidistant and every sample is logged. Subsequently, the logging mode is set to value-equidistant with values between 0.02 and 0.2 in steps of 0.02. If the PARAM variable is set to 1, the damping gain 'Gain1/Gain' is randomly selected and set to a new value before starting each run. Otherwise, 'Gain1/Gain' is held constant.

Note: For the case with a random gain setting, the results may appear incorrect due to the combined effects of changing both the gain and the value-equidistant logging parameter.

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

Determine Whether xpcosc is Open

systems = find_system('type', 'block_diagram');
if all(~strcmp('xpcosc', systems))
  mdlOpen = 0;
  open_system(fullfile(matlabroot,'toolbox','rtw','targets','xpc','xpcdemos','xpcosc'));
else
  mdlOpen = 1;
end
if (mdlOpen), stateOption = get_param('xpcosc', 'SaveState'); end

Turn State logging off for this example.

set_param('xpcosc', 'SaveState', 'off');

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

Close or Reset Model

Close the model if we opened it or reset the state if it was already open.

if (mdlOpen)
  set_param('xpcosc', 'SaveState', stateOption);
else
  bdclose('xpcosc');
end

Run Model, Set Value-Equidistant Logging Parameter, Plot Logged 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.

  • Create a Simulink Real-Time Object

  • Set sample time to 250us

  • Set stop time to 0.2s

  • Time-equidistant logging

tg.SampleTime = 0.000250;
tg.StopTime   = 0.2;
tg.LogMode = 'normal';
  • Start model execution

  • Get index of parameter 'Gain1/Gain'

start(tg);
tPar = getparamid(tg, 'Gain1','Gain');

Does the plot figure exist?

  • If no, create figure.

  • If yes, make it the current figure.

figh = findobj('Name', 'dataloggingdemo');
if isempty(figh)
  figh = figure;
  set(figh, 'Name', 'dataloggingdemo', 'NumberTitle', 'off');
else
  figure(figh);
end

Wait until the run is complete.

while strcmp(tg.Status, 'running')
  pause(0.05);
end

Retrieve the logged data and plot it.

tm = tg.TimeLog;
op = tg.OutputLog;
plot(tm, op);
set(gca, 'XLim', [tm(1), tm(end)], 'YLim', [-10, 10]);
title(['Time equidistant logging, ' num2str(length(tm)) ' samples']);
drawnow;
PARAM = 1; flag = 0;

Loop over the equidistant logging

  • Change parameter Gain1/Gain to random value between 0 and 2000.

  • Set value-equidistant logging parameter to n.

  • Start model execution

  • Wait until the application is complete.

  • Retrieve the logged data and plot it.

for vep = 0.02 : 0.02 : 0.2
  if isempty(find(get(0, 'Children') == figh, 1)), flag = 1; break; end
  if PARAM == 1
    setparam(tg, tPar, 2*1000*rand);
  end
  set(tg, 'LogMode', vep);
  start(tg);
  while strcmp(tg.Status, 'running')
    pause(0.05);
  end
  tm = tg.TimeLog;
  op = tg.OutputLog;
  plot(tm, op);
  set(gca, 'XLim', [tm(1), tm(end)], 'YLim', [-10, 10]);
  title(['Value equidistant logging (' num2str(vep) '): ' ...
       num2str(length(tm)) ' samples']);
  drawnow;
end

Stop Application

When done, stop the application from running.

if ~flag, title('dataloggingdemo: finished'); end
stop(tg);