How to update workspace whenever Matlab gets data from server
Show older comments
Hi, guys. I have question. I made server in Unity using C# and client in matlab(TCP/IP). The server sends data to Matlab and Matlab compute transferfuntion after gets data(P, I, D) from Unity. Then, Matlab needs to make data.csv file and Unity read it. I made a Cylinder in Unity and it reads data.csv. Then, Cylinder operates when it reads data.csv. My quesition is that the data.csv file is only generated when TCP/IP is disconnected. I want my data.csv to be updated when TCP/IP is still running.
I think I need to fix while loop in matlab. But, I don't know how to fix it. I hope you guys understand what I'm saying.
cod is below.
while
((s == 2 && data_s(1) == 113) == 0)
type = fscanf(tclient,'%c', 1);
data = fscanf(tclient,'%d');
if type == 'P'
p = data;
elseif type == 'I'
i = data;
elseif type == 'D'
d = data;
elseif type == 'T'
Kavq = 4.17e-6;
Kce = 6.94e-12*1.1;
Ap = 1.62e-4;
Vt = 97e-6;
Mt = 15.7;
Be = 10000e5;
Bp = 1.955e3;
Kf = 50;
del_h = Kce/Ap*sqrt(Be*Mt/Vt)+Bp/(4*Ap)*sqrt(Vt/(Be*Mt));
Wh = sqrt((4*Ap^2*Be)/(Mt*Vt));
num0 = Kavq/Ap;
den3 = 1/Wh^2;
den2 = 2*del_h/Wh;
den1 = 1;
den0 = 0;
num = [ num0 ];
den = [ den3 den2 den1 den0 ];
pnum = [ p ];
pden = [ 1 ];
inum = [ i ];
iden = [ 1 0 ];
dnum = [ d 0 ];
dden = [ 1 ];
P1 = tf(pnum,pden);
I1 = tf(inum,iden);
D1 = tf(dnum,dden);
PI = parallel(P1,I1);
PID = parallel(PI,D1);
g1 = tf(num, den);
G = series(PID, g1);
Gt = feedback(G, Kf);
t = 0:0.001:5;
step(Gt)
y = step(Gt,t);
u=y';
Y = round(u,5);
A= [t;Y];
fileID = fopen('data.csv','w');
fprintf(fileID,'%1s,%1s\n','t','Y');
fprintf(fileID,'%4.4f,%4.4f\n',A);
fclose(fileID);
else
fprintf("Unhandled data.");
end
end
Answers (0)
Categories
Find more on TCP/IP Communication in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!