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

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.mldatx, that can be run on a computer booted with the Simulink Real-Time kernel.


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
Warning: This model contains blocks that do not handle sample time
changes at runtime. To avoid incorrect results, only change
the sample time in the original model, then rebuild the model. 
### Successful completion of build procedure for model: xpcosc
### Created MLDATX ..\xpcosc.mldatx
### Looking for target: TargetPC1
### Download model onto target: TargetPC1

Run Model, Sweep 'Gain' Parameter, Plot Logged Data

This code accomplishes a number of tasks.

Task 1: Create Target Object

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

  • Set sample time to 250us.

  • Set stop time to 0.2s.

Task 2: Run the Model and Plot Results

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

  • Get index of parameter 'Gain1/Gain'

  • Does the plot figure exist?

  • If no, create figure

  • If yes, make it the current figure

Task 3: Loop over damping factor z

  • Set damping factor (Gain1/Gain)

  • Start model execution

  • Upload output and store in a matrix

  • Upload time vector

  • Plot data for current run

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

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

  • Create 3-D plot

tg = slrt; % create target object
tg.SampleTime = 0.000250;
tg.StopTime   = 0.2;
tPar = getparamid(tg, 'Gain1', 'Gain'); % run the model
figh = findobj('Name', 'parsweepdemo');
if isempty(figh)
  figh = figure;
  set(figh, 'Name', 'parsweepdemo', 'NumberTitle', 'off');
y = []; flag = 0; % loop over damping factor
for z = 0.1 : 0.05 : 0.7
  if isempty(find(get(0, 'Children') == figh, 1)), flag = 1; break; end
  setparam(tg,tPar,2 * 1000 * z);
  outp = tg.OutputLog;
  y    = [y, outp(:, 1)];
  t    = tg.TimeLog;
  plot(t, outp(:, 1));
  set(gca, 'XLim', [t(1), t(end)], 'YLim', [-10, 10]);
  title(['parsweepdemo: Damping Gain = ', num2str(z)]);
  xlabel('Time'); ylabel('Output');
if ~flag % 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');

Close Model

When done, close the model.