Aller au contenu principal

Integrate a Matlab or a Python function

When operations require calculation, we use Matlab/Simulink® blocks, or Python codes, that we integrate as operations in our UML description, as it is more performant than UML.

To integrate an external function, you first have to generate (or write) a xml file to ease the integration in the UML model.

remarque

The integration of external functions is only possible for the version pro of Designer.

INTEGRATE A PYTHON FUNCTION

From a python file containing useful functions, it is possible.

astuce

Python function integration requires Python 3.7, which was installed when the libraries were installed with Cygwin. If you forgot this step: (go to the page Cygwin installation)

Generate the integration file

The file automotive.py contains the following python function:

def compute_braking_distance(speed, adhesionCoeff):
gravitationConst = 9.81
squareSpeed = speed**2
result = squareSpeed / (2*gravitationConst*adhesionCoeff)
return result

You can analyze this file to extract relevant informations to integrate this function in a model by using dedicated command.

run command to generate integration file

This opens a dialog which enables to analyse the python file.

integration app
  • Select the file to analyse
  • Tick default to generate a file with the same name than your input file
  • Click on Analyse Entry File

This will offer you the possibility to describe with more details the function and its parameters.

file analysis
  • Tick Generated case for each function you would use in the model.
  • Click Update to complete the data about this function.
  • Fill the data for each parameter and the function. This will help to integrate in the model.
function details
  • Validate the changes
  • Click on Generate XML File to obtain the integration file

Integrate a Python function in the model

  • Right click on an activity in a sequence diagram
activity in sequence diagram
  • Select Edit > Complete Activity
  • Chose Python function
  • Click on Next
activity integration
  • Select the wanted python function. You can check the details and the parameters of the function
  • Click on Next
function choice
  • Update the mapping between inputs and outputs by chosing the variables of the model which have to be related to the variables of the function to call.
  • Then, click on Finish
function choice

These steps will generate the code to use the Python function at runtime.

Additional build settings to use Python functions

With Cygwin (on Windows)

You need to use the Python libraries deployed via Cygwin for your code to compile correctly.

Right-click on the folder of the generated C++ project in the Project Explorer, then select "Properties".

astuce

The folder name is <Name_of_Model>_DefaultNode_<Name_Of_Variant>.

  • Navigate to C++ Build > Settings, then in the Tool Settings tab, select Cygwin C++ Compiler > Includes (refer to the screenshot below),
  • Add the path to your local Python includes in Include paths (-I): C:\cygwin64\usr\include\python3.7m (adapt based on your Cygwin installation),
  • Add Python.h to Include files (-include),
python_include
  • Go to Cygwin C++ Linker,
  • Add python3.7 to Libraries.
python_lib