Contenuto principale

Generate a Basic Standalone FMU from C/C++ Code

Generate standalone co-simulation FMU from simple existing C/C++ functions using the Code to FMU tool. You can integrate your code, define FMU algorithm, and customize the FMU package using the Code to FMU tool.

Export C/C++ Function Defining a Brake Model as a Standalone Co-Simulation FMU

This example shows how you can export a simple C/C++ function as a standalone Functional Mockup Unit (FMU). In this example, the Code to FMU tool is used to integrate C/C++ code, specify interface for the FMU, configure tunable parameters, and customize the FMU package.

Create Code to FMU Project

Create a new Code to FMU project or open an existing project. To create a new project, on the HOME tab of the MATLAB toolstrip, click Simulink. This opens the Simulink Start Page. Click Code to FMU from the Simulink FMU Builder section.

Doing so opens the Create Project dialog. Specify the name and folder for your project and click OK. This opens a project in MATLAB along with a blank Code to FMU window.

You can also open and access the Code to FMU window from the Project tab in the MATLAB toolstrip.

Integrate C/C++ Source Code into Code to FMU

This example integrates an existing C/C++ function that defines a simple brake model. The model is defined by the following equations:

T=0whenP<Pdb

T=min[Tmax,μnpadsAp(P-Pdb)re]whenP>Pdb

where:

T is the brake torque

P is the line pressure

Pdb is the pressure deadband

Tmax is the maximum brake torque

μ is the friction coefficient

npads is the number of pads

Ap is the effective piston area

re is the effective rotor radius

Specify the C/C++ header file calculateBrakeTorque.hpp for this brake model.

#ifndef BRAKE_MODEL
#define BRAKE_MODEL

extern double brakeTorque(
double pressureInput,
double pressureDeadband,
double pistonArea,
double frictionCoefficient,
double effectiveBrakeRadius,
int numberOfBrakePads,
double maxTorque
); 

#endif

Specify the C/C++ source file calculateBrakeTorque.cpp for the brake model.

#include "calculateBrakeTorque.hpp"

double brakeTorque(
    double pressureInput,
    double pressureDeadband,
    double pistonArea,
    double frictionCoefficient,
    double effectiveBrakeRadius,
    int numberOfBrakePads,
    double maxTorque
    )
{
    if (pressureInput <= pressureDeadband || frictionCoefficient <= 0.0 
        || pistonArea <= 0.0 || effectiveBrakeRadius <= 0.0 || numberOfBrakePads <= 0.0)
    {
        return 0.0; 
    }    

    double torque {frictionCoefficient*numberOfBrakePads*pistonArea*(pressureInput - pressureDeadband)*effectiveBrakeRadius}; 

    if (torque < maxTorque)
        return torque;
    else 
        return maxTorque;
}

You can create and organize these files into src and include directories in the project. To create a new folder in the project, right click on the project pane, select New and click on Folder.

Create an include folder to store calculateBrakeTorque.hpp and src folder to store calculateBrakeTorque.cpp.

Open the Code to FMU window from the Project tab in the MATLAB toolstrip.

Specify the FMU Name, Language, and FMU Type in the CODE TO FMU toolstrip.

Add the library and source directories to the Custom Code table in the Code to FMU window. Click the button to add items to the table. Specify the source directory, source file, include directory, and library file.

Configure the input and output ports and specify desired tunable parameters for the FMU block. Click the button to add these elements to the Ports and Parameters table.

Specify General and FMU settings in the Settings pane.

Define the required FMU algorithm that uses the imported C/C++ functions within the callbacks in the Code To FMU Editor pane. In this example, the brakeComponent_Outputs_wrapper callback is defined as:

torqueOut[0] = brakeTorque(pressureIn[0], p_db[0], a_p[0], mu[0], r_eff[0], n_pad[0], t_max[0]);

Generate FMU

To the generate the FMU, click Build on the CODE TO FMU toolstrip.

The generated FMU is stored in the buildOutput folder of the project.

openProject("brakeModel");

brakeModelProjObject = FMUBuilder.CodeToFMU('brakeModel.prj');
brakeModelProjObject.build();
Generating 'brakeComponent.fmu'. .... Please wait.
FMU 'brakeComponent.fmu' created successfully.

Import and Simulate FMU

Open the Simulink model simModel that imports the generated FMU into Simulink.

open_system("simModel.slx");

Double-click on the FMU block to open the block dialog box. Specify the values of the FMU parameters.

Simulate the model and plot the output.

simOut = sim("simModel.slx");

figure(1)
plot(simOut.yout{1}.Values,'r','LineWidth',1.5);
grid minor
xlabel('Time (s)')
ylabel('Brake Torque (N-m)')
title('Brake Torque output')

Figure contains an axes object. The axes object with title Brake Torque output, xlabel Time (s), ylabel Brake Torque (N-m) contains an object of type stair.