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.

% Is xpcosc already 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.

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
### Successful completion of build procedure for model: xpcosc
### 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);

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.

tg = slrt;                              % Create a Simulink Real-Time Object
tg.SampleTime = 0.000250;               % Set sample time to 250us
tg.StopTime   = 0.2;                    % Set stop time to 0.2s
tg.LogMode = 'normal';                  % Time-equidistant logging

start(tg);                              % Start model execution

tPar = getparamid(tg, 'Gain1','Gain');  % Get index of parameter 'Gain1/Gain'

figh = findobj('Name', 'dataloggingdemo');% Does the plot figure exist?
if isempty(figh)
  figh = figure;                        % No: Create figure
  set(figh, 'Name', 'dataloggingdemo', 'NumberTitle', 'off');
  figure(figh);                         % Yes: Make it the current figure

% Wait until the run is complete.
while strcmp(tg.Status, 'running')

% 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']);

PARAM = 1; flag = 0;
for vep = 0.02 : 0.02 : 0.2
  % Loop over the equidistant logging
  if isempty(find(get(0, 'Children') == figh, 1)), flag = 1; break; end

  % Change parameter Gain1/Gain to random value between 0 and 2000.
  if PARAM == 1
    setparam(tg, tPar, 2*1000*rand);
  set(tg, 'LogMode', vep);              % Set value-equidistant logging parameter to n.

  start(tg);                            % Start model execution

  % Wait until the application is complete.
  while strcmp(tg.Status, 'running')

  % 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(['Value equidistant logging (' num2str(vep) '): ' ...
         num2str(length(tm)) ' samples']);
if ~flag, title('dataloggingdemo: finished'); end

Stop Application

When done, stop the application from running.

Was this topic helpful?