Simulink Real-Time


Parameter Tuning and Data Logging

This example shows how to do real-time parameter tuning and data logging with Simulink® Real-Time™. After the script builds and downloads the oscillator model, xpcosc, to the target computer, it makes multiple runs with the gain 'Gain1/Gain' changed (tuned) before each run. The gain is swept from 0.1 to 0.7 in steps of 0.05. The parameter index of 'Gain1/Gain' is retrieved from the Simulink Real-Time target object using the function GETPARAMID.

The data logging capabilities of Simulink Real-Time are used to capture signals of interest during each run. The logged signals are uploaded to the development computer and plotted. Finally, a 3-D plot of the oscillator output vs. time vs. gain is displayed.

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. Building the model will create an executable image, xpcosc.dlm, that can be run on a computer booted with the Simulink Real-Time kernel.

open_system('xpcosc');

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: TargetPC1
### Download model onto target: TargetPC1

Run Model, Sweep 'Gain' 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 target object
tg.SampleTime = 0.000250;               % Set sample time to 250us
tg.StopTime   = 0.2;                    % Set stop time to 0.2s

Run the model, sweeping through and changing the gain (damping parameter) before each run. Plot the results as you go.

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

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

y = []; flag = 0;
for z = 0.1 : 0.05 : 0.7                % Loop over damping factor z
  if isempty(find(get(0, 'Children') == figh, 1)), flag = 1; break; end
  setparam(tg,tPar,2 * 1000 * z);       % Set damping factor (Gain1/Gain)
  start(tg);                            % Start model execution
  pause(2*tg.StopTime);
  outp = tg.OutputLog;                  % Upload output and
  y    = [y, outp(:, 1)];               % store in a matrix
  t    = tg.TimeLog;                    % Upload time vector
  plot(t, outp(:, 1));                  % Plot data for current run
  set(gca, 'XLim', [t(1), t(end)], 'YLim', [-10, 10]);
  title(['parsweepdemo: Damping Gain = ', num2str(z)]);
  xlabel('Time'); ylabel('Output');
  drawnow;
end

Create 3-D Plot (Oscillator Output vs. Time vs. Gain)

Create a plot of oscillator output vs. time vs. gain.

if ~flag,
  delete(gca);                          % Create 3-D plot
  surf(t(1 : 200), 0.1 : 0.05 : 0.7, y(1 : 200, :)');
  colormap cool
  shading interp
  h = light;
  set(h, 'Position', [0.0125, 0.6, 10], 'Style', 'local');
  lighting gouraud
  title('parsweepdemo: finished');
  xlabel('Time'); ylabel('Damping Gain'); zlabel('Output');
end

Close Model

When done, close the model.

close_system('xpcosc',0);               % Close model