Main Content

Python API Example for Deployed Real-Time Applications

This example shows how to build a Python package by using the MATLAB® Compiler SDK. The package lets you create instrumentation for a Simulink® Real-Time™ model and interface with the instrumentation from your Python application. The MATLAB Compiler SDK supports building a standalone package that is dependent on the MATLAB Compiler Runtime (MCR). The MCR is freely available, is downloadable, and does not require a MATLAB license at run time. To create the package with the compiler, a license is required. You also can control Simulink Real-Time applications from external language interfaces without the need for creating a Compiler SDK project by using MATLAB External Interfaces. For more information, see Run Real-Time Application by Using Python Script.

Connect to Target Computer

Created a target object tg to represent the target computer. Connect to the target computer and identify the system target file (STF) for the target computer.

tg = slrealtime;
connect(tg);
modelSTF = getSTFName(tg);

Open Model

Open the model slrt_ex_pendulum_100Hz.

model = 'slrt_ex_pendulum_100Hz';
modelPath = 'simulink-real-time-python-api/instrumentation-model/slrt_ex_pendulum_100Hz';
open_system(modelPath);

Configure and Build Model

Configure the model for the STF that corresponds to the target computer. Build the real-time application from the model.

set_param(model,"SystemTargetFile",modelSTF);
evalc('slbuild(model)');
bdclose(model);

Build Python Package

Use mkdir and mcc functions to create a build folder and run the MATLAB® Compiler. You can perform this operation interactively by using the library builder app in the deployment project slrealtimePythonDeployment.prj.

mkdir slrealtimePythonDeployment\for_testing
mcc -W python:slrealtimePythonAPI -T link:lib -d ...
    .\slrealtimePythonDeployment\for_testing ...
    -v .\slrealtime-api\slrealtimeCaptureSignals.m ...
    .\slrealtime-api\slrealtimeDisableWarnings.m ...
    .\slrealtime-api\slrealtimeConnect.m ...
    .\slrealtime-api\slrealtimeDisconnect.m ...
    .\slrealtime-api\slrealtimeGetCapturedSignal.m ...
    .\slrealtime-api\slrealtimeGetModelStatus.m ...
    .\slrealtime-api\slrealtimeGetParam.m ...
    .\slrealtime-api\slrealtimeInitTarget.m ...
    .\slrealtime-api\slrealtimeLoad.m ...
    .\slrealtime-api\slrealtimeRemoveInstrument.m ...
    .\slrealtime-api\slrealtimeSetParam.m ...
    .\slrealtime-api\slrealtimeSetStopTime.m ...
    .\slrealtime-api\slrealtimeStart.m ...
    .\slrealtime-api\slrealtimeStop.m ...
    -a .\slrt_ex_pendulum_100Hz.mldatx ...
    -Z 'Simulink Real-Time Target Support Package'

You can use the MATLAB Compiler SDK to package the application. For more information, see Create Python Application Using Library Compiler App (MATLAB Compiler SDK).

Run Python Script

Before you run the Python script, update the Python script with the IP address of the target computer to which you are connecting. Edit line 73 of file CallingSLRealTimeFromPythonUsingCompiler.py to set the IP address for your target computer. You can run the script from the Command Window by typing:

winopen('CallingSLRealTimeFromPythonUsingCompiler.py')