Borrar filtros
Borrar filtros

How to pass non-numeric value as parameter to Matlab function block in Simulink

3 visualizaciones (últimos 30 días)
I am trying to communicate with a motor controls program, written in C and compiled into an excutable. Simulink, via a Matlab function block, writes to the stdin of the program things like DC voltage, phase currents, and angle, and the program reads that data, performs some computations, and writes to its stdout things like duty cycles and dq current.
Right now, I start the process in the InitFcn callback:
% Initialize process structure
process = System.Diagnostics.Process;
process.StartInfo.FileName = strcat(projectPath, '\\', 'inverter.exe');
process.EnableRaisingEvents = true;
process.StartInfo.CreateNoWindow = true;
% Set UseShellExecute to false for redirection
process.StartInfo.UseShellExecute = false;
% Redirect process stdout
process.StartInfo.RedirectStandardOutput = true;
% Redirect process stdin
process.StartInfo.RedirectStandardInput = true;
% Start process
process.Start();
% Initialize process input writer
processWriter = process.StandardInput;
And access it in a Level-2 S-Function:
function pipe(block)
setup(block);
%endfunction
function setup(block)
%% Register number of input and output ports
block.NumInputPorts = 4;
block.NumOutputPorts = 1;
%% Setup functional port properties to dynamically inherited
block.SetPreCompInpPortInfoToDynamic;
block.SetPreCompOutPortInfoToDynamic;
%% Set number of dialog params
block.NumDialogPrms = 3;
%% Set number and dimension of input ports
block.InputPort(1).Dimensions = 1;
block.InputPort(1).DirectFeedthrough = false;
block.InputPort(2).Dimensions = 1;
block.InputPort(2).DirectFeedthrough = false;
block.InputPort(3).Dimensions = [1, 3];
block.InputPort(3).DirectFeedthrough = false;
block.InputPort(4).Dimensions = 1;
block.InputPort(4).DirectFeedthrough = false;
%% Set number and dimension of output ports
block.OutputPort(1).Dimensions = [1, 18];
%% Set block sample time to inherited sample time
block.SampleTimes = [block.DialogPrm(3).Data 0];
%% Set the block simStateCompliance to default (i.e., same as a built-in block)
block.SimStateCompliance = 'DefaultSimState';
%% Register methods
block.RegBlockMethod('Outputs', @Output);
%endfunction
function Output(block)
%% Create output string
sendData = sprintf("%f %f %f %f %f", ...
block.InputPort(3).Data(3), ... % phaseA 3 (forward) 2 (reversed)
block.InputPort(3).Data(2), ... % phaseB 2 (forward) 1 (reversed)
block.InputPort(1).Data(1), ... % dcBus
block.InputPort(2).Data(1), ... % angle
block.InputPort(4).Data(1)); % trq request
%% Send output string
processWriter = block.DialogPrm(2).Data;
processWriter.WriteLine(sendData);
%% Read input string
process = block.DialogPrm(1).Data;
recvData = string(process.StandardOutput.ReadLine());
%% Parse input string
block.OutputPort(1).Data = sscanf(recvData, "%f", [1, 18]);
%endfunction
Which works, but it is slow. I would like to move to using a Matlab Function Block, as this should speed things up. I wrote some equivalent code for a Matlab Function Block, which is masked with two parameters, process and processWriter:
function out = fcn(dcBus, A, Iabc, Tr, process, processWriter)
% Send data to inverter process
sendData = sprintf("%f %f %f %f %f", Iabc(3), Iabc(2), dcBus, A, Tr);
processWriter.WriteLine(sendData);
% Recieve data from inverter process
recvData = string(process.StandardOutput.ReadLine());
out = sscanf(recvData, "%f", [1, 18]);
end
However this fails with the error:
Error:Expression 'process' for initial value of data 'process' must evaluate to logical or supported numeric type.
How can I resolve this?
Attached is the model.

Respuestas (1)

Walter Roberson
Walter Roberson el 3 de En. de 2024
You are not going to be able to pass processWriter as a signal .
You just might be able to construct processWriter inside the function block, set as a persistent variable, with the setup protected by isempty()

Productos


Versión

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by