Main Content

Simulate FMU with Enum Type Ports using FMU Import Block

The example demonstrates how to use the FMU import block to simulate FMUs that have enumerated data type ports. The FMU import block enables the import of functional mockup units (FMUs) in Simulink.

Export Simulink Model with Enumeration Type Ports to Standalone FMU

This section describes how to export Simulink model enumModel to a standalone FMU, the data type of inport in the model is defined as enumeration settingOption. After exporting this model to standalone FMU, the data type of input ports is preserved in the generated FMU as enumeration type. For basic information about enumeration data type in models, see Use Enumerated Data in Simulink Models.

% load example model
open_system("enumModel");

Screenshot_2023-06-13_00-08-21.png

In the example folder, the file settingOption.m defines the enumeration class, settingOptions, which controls the model's output.

classdef settingOption < Simulink.IntEnumType
    enumeration
        option1(1)
        option2(2)
    end % enumeration
    
    methods (Static)
        function retVal = getDefaultValue()
          retVal = settingOption.option1;
        end
    end % methods    
end % classdef

The following command exports the enumModel to a standalone FMU. This process requires a Simulink compiler license and installation of the FMU Builder for Simulink support package.

% export the model to Standalone FMU
exportToFMU2CS("enumModel", "CreateModelAfterGeneratingFMU", "on");

To adhere to the FMI standard, the definition of this enumeration is generated in the modelDescription.xml under node TypeDefinitions.

  <TypeDefinitions>
    <SimpleType name="settingOption">
      <Enumeration>
        <Item name="option1" value="1"/>
        <Item name="option2" value="2"/>
      </Enumeration>
    </SimpleType>
  </TypeDefinitions>

Simulate FMU with Enum Type

To simulate an FMU that has enumerated data type ports, import the FMU into Simulink with the FMU import block. By default, the data type of the FMU with enumeration ports is set with the type declared by the signal attributes. If the port needs to be simulated as an int32 data type, the field in TypeObject should be set as empty.

Screenshot_2023-06-13_00-02-39.png

To automatically generate the enumeration type defined in the FMU, use command fmudialog.createTypeObject.

fmudialog.createTypeObject(gcb, "CreateClassDefinitionTo", "File")
% To generated enumeration type in the current MATLAB session, set argument 'CreateClassDefinitionTo' to 'Session'.

Screenshot_2023-06-13_00-06-44.png