Azzera filtri
Azzera filtri

Custum level-2 MATLAB S-function problem with desktop real-time simulink

6 visualizzazioni (ultimi 30 giorni)
Hi,
I try to develop a real-time simulink model to stream data to workspace of MATLAB from delsys trigno EMG system. I made a model that works perfectly when I simulate the model by using a custum level-2 MATLAB S-function to communicate with delsys Trigo and get the data. But as I need the model to run in real-time, I build the model using Simulink desktop real-time. The time steps of my custum level-2 MATLAB S-function is set up to 0.05s and I use a Unbuffer blocks to obtain the original frequency sampling at 2000Hz, (or 0.0005s time steps).
When I run in real-time my model, I do not receive any error message but EMG data are replace by 0 values. After adding some disp to my code, I notice that the model do not use anymore the function 'Outputs' of my custum level-2 MATLAB S-function. Then, the model do not ask anymore for EMG data but simply return 0 value instead.
Here my custum level-2 MATLAB S-function :
function DelsysTrignoSamplerBuffer(block)
% The function that implements the
% Sampling block in simulink. This functions is a Matlab Level 2 S-function.
tcpipCommand = NaN;
tcpipEMG = NaN;
is_started = false;
rateAdjustedEmgBytesToRead = 0;
buffer = [];
at_frame_buffer = 1;
parameters = struct();
setup(block);
function setup(block)
% Register the number of ports.
block.NumInputPorts = 0;
block.NumOutputPorts = 1;
block.NumContStates = 0;
block.NumDworks = 0;
% Set output ports
block.SetPreCompOutPortInfoToDynamic();
block.OutputPort(1).Dimensions = [100 16];
block.OutputPort(1).SamplingMode = 'Frame';
% Number of parameters
block.NumDialogPrms = 1;
% Block time steps
block.SampleTimes = [0.05 0];
% run block in interpreted mode even w/ Acceleration
block.SetAccelRunOnTLC(false);
block.SimStateCompliance = 'DefaultSimState';
block.RegBlockMethod('CheckParameters', @checkParameters);
block.RegBlockMethod('Start', @Start);
block.RegBlockMethod('Outputs', @Outputs);
block.RegBlockMethod('Terminate', @Terminate);
end
function checkParameters(block)
parameters.ipAddress = block.DialogPrm(1).Data;
end
%
function Start(block)
% This function is called once, when the simulation is started.
% Création d'une connexion TCP/IP
tcpipCommand = tcpclient(parameters.ipAddress, 50040);
tcpipEMG = tcpclient(parameters.ipAddress, 50041);
%Setup interface object to read chunks of data
rateAdjustedEmgBytesToRead=100*16;
writeline(tcpipCommand,sprintf('START\r\n\r'))
is_started = true;
bytesReady = tcpipEMG.NumBytesAvailable;
bytesReady = bytesReady - mod(bytesReady, rateAdjustedEmgBytesToRead);
while bytesReady == 0
bytesReady = tcpipEMG.NumBytesAvailable;
bytesReady = bytesReady - mod(bytesReady, rateAdjustedEmgBytesToRead);
end
data = read(tcpipEMG,bytesReady,'single');
buffer = double(reshape(data,16,[])');
at_frame_buffer = 1;
disp('okstart')
end
function Outputs(block)
disp('okoutput')
if size(buffer, 2) < at_frame_buffer
bytesReady = tcpipEMG.NumBytesAvailable;
bytesReady = bytesReady - mod(bytesReady, rateAdjustedEmgBytesToRead);
while bytesReady == 0
bytesReady = tcpipEMG.NumBytesAvailable;
bytesReady = bytesReady - mod(bytesReady, rateAdjustedEmgBytesToRead);
end
data = read(tcpipEMG,bytesReady,'single');
buffer = double(reshape(data,16,[])');
at_frame_buffer = 1;
end
block.OutputPort(1).Data = buffer(at_frame_buffer:at_frame_buffer+99,:);
at_frame_buffer = at_frame_buffer + 100;
end
function Terminate(block)
% Cleans up the TMSi library.
writeline(tcpipCommand,sprintf('STOP\r\n\r'))
flush(tcpipEMG)
is_started = false;
end
end
How can I resolve this and have the function Outputs block works even with desktop real-time ?

Risposta accettata

Jan Houska
Jan Houska il 29 Lug 2023
Hi Julien,
Level-2 MATLAB S-functions do not generate C code, and therefore cannot be used by Simulink Desktop Real-Time in Run In Kernel mode. You have two options. Either you can run the model in Connected IO mode, where the S-function will be run as-is without compilation. There's a good chance it will work if you sample rate is 20 Hz. Or, you can re-implement the TCP/IP communication using the Packet Input and Packet Output blocks and the TCP/IP protocol driver. This way, C code will be generated and you will be able to run in Run In Kernel mode.
Good Luck, Jan

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by