Main Content

Export Simscape Model as FMU with Tunable Parameters and Internal Variables

This example shows how to export a Simscape model as an FMU with tunable parameters and internal variables.

We first configure the model to export Simscape block parameters as tunable parameters of the FMU that can be changed during simulation of the FMU. We then configure the model to export variables of simscape blocks as internal variables of the FMU that can be observed from the imported model. The model is then exported as FMU and imported and simulated in Simulink.

Open Simscape Model

Open the simscape model and set the solver to fixed-type. This simscape model describes a mass-spring-damper system with translational friction. An ideal velocity source is provided as input to this system.

open_system('mechanical_system_local');
set_param('mechanical_system_local','SolverType','Fixed-step','fixedstep','0.001');

simscapeFMU_model.png

Configure model to export tunable parameters

You can export simscape block parameters as tunable parameters. To do so,

  1. Create a base workspace variable or Simulink.Parameter object for each of the desired tunable parameter to hold its initial value.

  2. Double-click on the block to open the block dialog box.

  3. Assign the variable as the value of this parameter and specify its desired unit.

  4. Set the Configurability setting of the parameter as Run-time.

In this example, the mass of the object, the damping coefficient of the damper, and the spring rate of the spring are set a configured to be exported as tunable parameters. The following code implements the aforementioned steps programmatically.

mass_val = 0.1;
set_param('mechanical_system_local/Mass','mass','mass_val','mass_conf','runtime','mass_unit','kg');

damping_coeff_val = 20;
set_param('mechanical_system_local/Damper','D','damping_coeff_val','D_conf','runtime','D_unit','N*s/m');

spring_rate_val = 250;
set_param('mechanical_system_local/TranslationalSpring','spr_rate','spring_rate_val','spr_rate_conf','runtime','spr_rate_unit','N/m');

tunable_parameter_mass.png

Configure model to export internal variables

Simscape variables associated with simscape blocks can be exported as internal variables of the FMU.

  1. You need to first extract these variables using a Probe block of the simscape library and bind it to the variable that you want to export. The probe block now converts this variable to a simulink signal.

  2. You can now export this simulink signal as an internal variable of the FMU. For more information on exporting simulink signals are internal variables, see Configure Model to Export as FMU with Internal Variables.

In this example, a probe block is used to extract the deformation of the spring. The probe block converts it to a simulink signal. The signal is set for logging. It is associated with a Simulink.Signal object of the same name as the signal and they are resolved. The signal object sets the initial value for the signal and sets the scope of the signal as Exported Global.

springDeformation = Simulink.Signal; 
springDeformation.InitialValue = '0';
springDeformation.CoderInfo.StorageClass = 'ExportedGlobal';

Export the model as FMU

The model is ready to be exported as FMU with tunable parameters and internal variables. Use the exportToFMU function to export the model as FMU.

exportToFMU('mechanical_system_local','FMIVersion','2','FMUType','CS','EnableFMUState','off','ExportedInternals',{'springDeformation'});
Setting System Target to FMU 'Co-Simulation' for model 'mechanical_system_local'.
Setting Hardware Implementation > Device Type to 'MATLAB Host' for model 'mechanical_system_local'.
### Searching for referenced models in model 'mechanical_system_local'.
### Total of 1 models to build.
### 'GenerateComments' is disabled for 'Co-Simulation' FMU Export.
### Generating code for Physical Networks associated with solver block 'mechanical_system_local/Velocity Input/Solver Configuration' ...
done.

Build Summary

Top model targets:

Model                    Build Reason                                         Status                        Build Duration
==========================================================================================================================
mechanical_system_local  Information cache folder or artifacts were missing.  Code generated and compiled.  0h 0m 17.42s  

1 of 1 models built (0 models already up to date)
Build duration: 0h 0m 17.718s
### Model was successfully exported to 'Co-Simulation' FMU: '/tmp/Bdoc25a_2864802_2304439/tp823db898/simulinkcompiler-ex49904336/mechanical_system_local.fmu'.

Import and Simulate FMU in Simulink

Import the generated FMU back into Simulink.

open_system('simscapeFMU_simulation');

simscapeFMU_simulationModel.png

Double click on the FMU Import block to open the FMU block dialog and check if the parameters are exported as tunable. You can change the value of the parameters according to your simulation requirements.

Expose the internal variable as an output port by selecting its corresponding Visibility check-box to see the spring deformation values.

fmuSimscape_parameters.pngfmuSimscape_internalvariables.png

Simulate the model with FMU with different mass and damping coefficient values and view the internal variable output for the specified FMU parameters.

set_param('simscapeFMU_simulation/FMU','mass_val','0.1','damping_coeff_val','20');
simData_1 = sim('simscapeFMU_simulation');
set_param('simscapeFMU_simulation/FMU','mass_val','10','damping_coeff_val','15');
simData_2 = sim("simscapeFMU_simulation");

plotData = figure(1);
plot(simData_1.yout{1}.Values,'LineWidth',1.5);
hold on 
plot(simData_2.yout{1}.Values,'LineWidth',1.5,'LineStyle','--')
xlabel('Time(s)');
ylabel('Spring Deformation (m)');
legend('0.1 kg, 20 Ns/m', '10 kg, 15 Ns/m','Location','southwest')
title('Spring Deformation for different FMU parameters')
grid minor;

Figure contains an axes object. The axes object with title Spring Deformation for different FMU parameters, xlabel Time(s), ylabel Spring Deformation (m) contains 2 objects of type stair. These objects represent 0.1 kg, 20 Ns/m, 10 kg, 15 Ns/m.

See Also

|