Simulink Real-Time

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 host 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 for all runs.

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 Host and Target Computer

Use 'slrtpingtarget' to test for a proper host-target connection.

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.dlm, 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;
  open_system('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.dlm, 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: TargetPC
### Download model onto target: TargetPC

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.

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');
else
  figure(figh);                         % Yes: Make it the current figure
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;
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);
  end
  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')
    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(['Value equidistant logging (' num2str(vep) '): ' ...
         num2str(length(tm)) ' samples']);
  drawnow;
end
if ~flag, title('dataloggingdemo: finished'); end

Stop Application

When done, stop the application from running.

stop(tg);