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

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;
  mdlOpen = 1;
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.

tg = slrt('TargetPC1');
### 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: xpcosc
### Created MLDATX ..\xpcosc.mldatx

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

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'

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

Does the plot figure exist?

  • If no, create figure.

  • If yes, make it the current figure.

Wait until the run is complete and then retrieve the logged data and plot it.

while strcmp(tg.Status, 'running')
figh = findobj('Name', 'dataloggingdemo');
if isempty(figh)
  figh = figure;
  set(figh, 'Name', 'dataloggingdemo', 'NumberTitle', 'off');
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']);
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);
  set(tg, 'LogMode', vep);
  while strcmp(tg.Status, 'running')
  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']);

Stop Application

When done, stop the application from running.

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