Running system call from simulink

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.

回答 (1 件)

Sergio Mesa Martín
Sergio Mesa Martín 2021 年 7 月 8 日

0 投票

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 件のコメント

Sergio Mesa Martín
Sergio Mesa Martín 2021 年 7 月 8 日
Anyway, I have doubts on how this works on simulink. When I execute the simulink model, it calls the system, but, does it call the system everytime while being executed or only once per run?
Prasanth Sunkara
Prasanth Sunkara 2021 年 8 月 18 日
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 2021 年 8 月 18 日
Or even better, you could place this system, cmd in the Model initialize function.
https://www.mathworks.com/help/simulink/ug/initialization-function.html

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCreate System Objects についてさらに検索

製品

リリース

R2020a

質問済み:

2021 年 7 月 8 日

コメント済み:

2021 年 8 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by