Running system call from simulink

2 views (last 30 days)
Sergio Mesa Martín
Sergio Mesa Martín on 8 Jul 2021
Commented: Prasanth Sunkara on 18 Aug 2021
I have tryed to implement a system call on simulink to execute a python script, using this solution, implemented on a Matlab system block:
So my files are like this:
python_script.m:
classdef python_script < matlab.System ...
& coder.ExternalDependency ...
& matlab.system.mixin.Propagates ...
& matlab.system.mixin.CustomIcon
properties
% Public, tunable properties.
end
properties (Nontunable)
% Public, non-tunable properties.
end
properties (Access = private)
% Pre-computed constants.
end
methods
% Constructor
function obj = python_script_trigger(varargin)
% Support name-value pair arguments when constructing the object.
setProperties(obj,nargin,varargin{:});
end
end
methods (Access=protected)
function setupImpl(obj) %#ok<MANU>
end
function stepImpl(obj,turn_on) %%#ok<INUSD>
if isempty(coder.target)
% Place simulation output code here
else
% Call C-function implementing device output
coder.cinclude('python_script.h');
coder.ceval('python_script_command',turn_on);
end
end
function releaseImpl(obj) %#ok<MANU>
if isempty(coder.target)
% Place simulation termination code here
else
% Call C-function implementing device termination
%coder.ceval('sink_terminate');
end
end
end
methods (Access=protected)
%%Define input properties
function num = getNumInputsImpl(~)
num = 1;
end
function num = getNumOutputsImpl(~)
num = 0;
end
function flag = isInputSizeLockedImpl(~,~)
flag = true;
end
function varargout = isInputFixedSizeImpl(~,~)
varargout{1} = true;
end
function flag = isInputComplexityLockedImpl(~,~)
flag = true;
end
function icon = getIconImpl(~)
% Define a string as the icon for the System block in Simulink.
icon = 'python_script';
end
end
methods (Static, Access=protected)
function simMode = getSimulateUsingImpl(~)
simMode = 'Interpreted execution';
end
function isVisible = showSimulateUsingImpl
isVisible = false;
end
end
methods (Static)
function name = getDescriptiveName()
name = 'python_script';
end
function b = isSupportedContext(context)
b = context.isCodeGenTarget('rtw');
end
function updateBuildInfo(buildInfo, context)
if context.isCodeGenTarget('rtw')
% Update buildInfo
srcDir = fullfile(fileparts(mfilename('fullpath')),'../src'); %%#ok<NASGU>
includeDir = fullfile(fileparts(mfilename('fullpath')),'../include');
addIncludePaths(buildInfo,includeDir);
addSourceFiles(buildInfo,'python_script.c',srcDir);
end
end
end
end
python_script.c
#include <python_script.h>
char command[512];
void python_script_command(boolean_T turn_on)
{
if (turn_on == 1)
{
strcpy( command, "C:\ti\simplelink_cc13x2_26x2_sdk_5_10_00_48\tools\ble5stack\rtls_agent\.venv\Scripts\python C:\ti\simplelink_cc13x2_26x2_sdk_5_10_00_48\tools\ble5stack\rtls_agent\examples\rtls_aoa_iq_with_rtls_util_export_into_csv.py" );
system(command);
}
}
python_script.h
#ifndef _PYTHON_SCRIPT_H_
#define _PYTHON_SCRIPT_H_
#include "rtwtypes.h"
void python_script_command(boolean_T turn_on);
#endif //_PYTHON_SCRIPT_H_
Whenever I execute this script, it should create a log file, but when I execute it running simulink, it doesn't, so I suppose it is not working properly. Is there any other better solution or am I implementing this incorrectly?
I need the system call to start when I execute the simulink model (only once, and then the script should keep running), because I want to synchronize it with a csv reading script.

Answers (1)

Sergio Mesa Martín
Sergio Mesa Martín on 8 Jul 2021
I found out it was as simple as this on a matlab function:
function fcn()
coder.extrinsic('system');
system(['C:\ti\simplelink_cc13x2_26x2_sdk_5_10_00_48\tools\' ...
'ble5stack\rtls_agent\.venv\Scripts\python C:\\ti\\simplelink_cc13x2'...
'_26x2_sdk_5_10_00_48\\tools\\ble5stack\\rtls_agent\\examples\\rtls_example_'...
'with_rtls_util.py'])
end
  3 Comments
Prasanth Sunkara
Prasanth Sunkara on 18 Aug 2021
Using MATLAB Function block was good choice.
It calls at every sample time. You can avoid that by using a persistent variable and call the system command only once based on whether the variable was intialized or not.
Thanks,
Prasanth
Prasanth Sunkara
Prasanth Sunkara on 18 Aug 2021
Or even better, you could place this system, cmd in the Model initialize function.
https://www.mathworks.com/help/simulink/ug/initialization-function.html

Sign in to comment.

Products


Release

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!