matlab editor to simulink communication using UDP
6 次查看(过去 30 天)
显示 更早的评论
I have a matlab script that runs in a loop and updates two vectors say x and y for every iteration. I need to send this vectors to simulink in realtime for every iteration. I use UDP to communicate between matlab editor and simulink. Each iteration takes 2.5 seconds in the editor.
This is the code i use in matlab
clc
clear all
close all
u = udp('127.0.0.55', 'RemotePort', 12345);
u.OutputBufferSize = 8192;
fopen(u);
for i=1:1:5000
... my code
...I get the x and y vectors of size 1 by n
... n varies for each iteration.
sendData = [x,y];
fwrite(u, sendData, 'double');
pause(1);
... more of my code
end
The attached image shows my simulink model
I use the matlab function block in simulink to process the data to a required size. The following is the code i use in matlab function block in simulink.
function [xdata_musicf2f, ydata_musicf2f] = processReceivedData(u)
coder.extrinsic('scatter', 'figure', 'clf');
coder.varsize('xdata_musicf2f', [1, 1000], [false, true]);
coder.varsize('ydata_musicf2f', [1, 1000], [false, true]);
xdata_musicf2f = zeros(1, 1); % Initialize to a default size
ydata_musicf2f = zeros(1, 1); % Initialize to a default size
receivedDataSingle = u;
u
n = length(receivedDataSingle) / 2;
if n > 0
xdata_musicf2f = receivedDataSingle(1:n);
ydata_musicf2f = receivedDataSingle(n+1:end);
end
figure(1);
clf;
scatter(xdata_musicf2f, ydata_musicf2f);
xlim([-5,5]);
ylim([0,38]);
end
The data I receive in simulink makes no sense and also is of different size. Also I am unable to make the simulink wait until the next data is ready to be sent from the editor. Please let me know what is the mistake i am making. Is there any other method to effeciently send such variable sized vectors in realtime to simulink.
Thanks in Advance.
0 个评论
回答(1 个)
Yatharth
2023-9-20
Hi Praanesh,
I understand that using the "UDP" function you are trying to send X and Y values to Simulink from MATLAB, however there is a mismatch in the data received in Simulink.
From your Simulink Model's screenshot, I think since there is no "Enable port”, Simulink is unable to detect the start of the UDP connection.
Also "udp" will be removed in a future release therefore you should use "udpport" instead.
I request you to try this example to understand the workflow, same can be easily replicated for your given problem. https://www.mathworks.com/help/instrument/read-data-from-matlab-using-udp-receive-block.html
Notice that in the Simulink model, the subsystem contains an "Enable port" this is used as a trigger for the blocks inside the subsystem to run, whenever UDP receive Block Status is true so that the connection is in sync.
I hope this helps.
Yatharth Taneja
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Development Computer Setup 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!