Issue with UDP socket continous read
显示 更早的评论
I am trying to run distributed network control algorithm. I am sending the states of the system from MATLAB to a machine in the network and receiving the control input from the network and trying to solve the non linear differential equation using ODE45.
I do not have any issues when the controller program is run on the same computer where MATLAB is running. But if I send to any other remote hosts it still runs for a short period and then fscanf times out, though I am able to see the packet being sent from the remote host. I am attaching my code.
Main Program
clc;clear all;instrreset;
global ip;
ip = [];
server = udp('152.14.125.203',9090,'LocalPort',9091);
%server = udp('10.138.37.223',9090,'LocalPort',9091);
%client = udp('192.168.0.12',9091,'LocalPort',9090);
fopen(server)
%fopen(client)
tspan = [1:1:10];
%x0 = [0;1];
x0 = [0;0.526;0;0];
%x0 = zeros(4,1);
[t,x] = ode45(@(t,x) power_sys(t,x,server),tspan,x0);
fwrite(server,'end')
delete(server);
Differential Equation function:
function dx = power_sys(t, x,server)
global ip;
% [t,x] = ode45(@(t,x) sys(t,x,server,client),tspan,x0)
format long
dx = zeros(4,1);
% Machine Parameters
M1 = 4.7;M2 = 4.7;
E1 = 1;E2 = 0.98;
x12 = 0.01;
k12 = E1*E2/x12;
K =[-0.403068278527916 0.455718526403815 0.072750403407375 0.067255905604533;0.063999385331140 0.067255905604533 0.352416905715348 0.374150309862755];
s ='';
temp_str ='';
for i=1:length(x)
temp_str = num2str(x(i));
s = strcat(s,temp_str);
if i ~= length(x)
s = strcat(s,'//');
end
end
fwrite(server,s)
u_str = fscanf(server,'%s')
u_cell_array = strsplit(u_str,'//');
for i = 1:length(u_cell_array)
temp_char = char(u_cell_array(i));
u(i) = str2num(temp_char);
end
ip = [ip;u];
%Non Linear state space Model
dx(1) = x(2);
dx(2) = 1/M1*(u(1)-k12*sin(x(1)-x(3)));
dx(3) = x(4);
dx(4) = 1/M2*(u(2)-k12*sin(x(3)-x(1)));
end
1 个评论
Gitesh Nandre
2015-6-23
Try a basic example and see if it is working: Let's say you have two machines: A(local) and B(remote). Start the echo server on remote machine B by executing following command on remote machine's MATLAB.
echoudp('on',4012)
Create UDP object on local machine A where x.x.x.x is IP address of remote machine B:
u = udp('x.x.x.x',4012);
Connect the UDP object to the host on A machine's MATLAB.
fopen(u)
Write to the host and read from the host.
fwrite(u,65:74)
A = fscanf(u,'%s');
A
Stop the echo server and disconnect the UDP object from the host.
echoudp('off')
fclose(u)
See that if the value of 'A' is correctly populated.
回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Interface-Based Instrument Communication 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!