Problem with call back function

Guys, I have a program which obtains some data from UR5 robot via TCP/IP. I want to implement a call back function to enable it continuously obtaining this data if possible upto 10hours because it stops at some point (like 10minutes). But the call back is new to me and what I have also stops after the first reading of the specified bytes. Here are the programs:
CALLBACK FUNCTION
function urcallback(obj, event)
global t;
pause(t);
t = 0;
ur5_sample = fread(obj, 2216, 'uint8');
timestamp = now;
raw_data = uint8(ur5_sample');
X = typecast(fliplr(raw_data(445:452)), 'double');
Y = typecast(fliplr(raw_data(453:460)), 'double');
Z = typecast(fliplr(raw_data(461:468)), 'double');
Rx = typecast(fliplr(raw_data(469:476)), 'double');
Ry = typecast(fliplr(raw_data(477:484)), 'double');
Rz = typecast(fliplr(raw_data(485:492)), 'double');
Gripper_state = raw_data(1045);
timestring = datestr(timestamp,'HH:MM:SS.FFF');
[~,~,~,hours,minutes,seconds] = datevec(timestring);
Time = 1000*(3600*hours + 60*minutes + seconds);
global log_table;
table_row = table(Time, Gripper_state, X, Y, Z, Rx, Ry, Rz);
log_table = [log_table; table_row];
ACTUAL PROGRAM % Initialize variables % Unprocessed data global log_table log_table = table();
global t
t = 5;
% Setup connection to UR5
UR5_ADDRESS = '192.168.233.3';
UR5_PORT = 30003;
s = tcpip(UR5_ADDRESS, UR5_PORT, 'NetworkRole', 'client', 'Timeout', 10);
s.InputBufferSize = 102400;
s.BytesAvailableFcnCount = 1108;
s.BytesAvailableFcnMode = 'byte';
s.BytesAvailableFcn = {@urcallback};
fopen(s);
pause(5);
fclose(s);
delete(s);
clear s; % Close connection
filename = strcat('log_table.csv');
writetable(log_table, filename);
Find attached, the result obtained after 1108 byte was read. Your help is welcomed please?

6 comentarios

Geoff Hayes
Geoff Hayes el 28 de Ag. de 2018
Editada: Geoff Hayes el 28 de Ag. de 2018
Deyire - why do you open the connection and then pause for five seconds before closing the connection? Don't you want to keep the it open? As soon as you close it, the callback will no longer fire. I suspect that you will want a while loop of some kind to keep the keep your program running and then terminate it after some condition has been met. Something like
fopen(s);
pause(5);
while true
if <some condition has been met>
break;
end
end
fclose(s);
delete(s);
clear s; % Close connection
filename = strcat('log_table.csv');
writetable(log_table, filename);
Also, try and avoid using global variables. Either pass variables into your callback or nest the callback within the body of you main function so that it has access to the (say) log_table variable.
DEYIRE UMAR
DEYIRE UMAR el 28 de Ag. de 2018
Editada: DEYIRE UMAR el 28 de Ag. de 2018
Thank you. I thought when I open, it will collect the data and for it to go all over again may need some delay. I am actually new to this. I actually have a version which uses while loop but stops reading at some point. I even implemented if else in it but the frequency dropped, so I am actually looking for a replacement of while loop, and I thought callback function could be used for the task. I will consider your input and try
Geoff Hayes
Geoff Hayes el 28 de Ag. de 2018
well you would still use the callback to collect the data...the while loop just makes sure that your program doesn't end...
DEYIRE UMAR
DEYIRE UMAR el 28 de Ag. de 2018
Editada: DEYIRE UMAR el 28 de Ag. de 2018
ok. seriously need this reconnection after over and over again.
Deyire's answer moved here
I still wonder why this program which is without the callback but using While loop, due stop after some minutes of running.
raw_data = [];
host = '10.53.106.139';
port = 30003;
s = tcpip(host, port,'NetworkRole', 'client', 'Timeout', 10);
% Setup connection to UR5
s.InputBufferSize = 102400;
iteration = 0;
T = table();
MASK0 = 1;
fopen(s);
s.ReadAsyncMode = 'continuous';
while (exist('.//lock.txt', 'file') == 2)
ur5_sample = fread(s, 1108, 'uint8');
timestamp = now;
raw_data = uint8(ur5_sample');
X = typecast(fliplr(raw_data(445:452)), 'double')*1000;
Y = typecast(fliplr(raw_data(453:460)), 'double')*1000;
Z = typecast(fliplr(raw_data(461:468)), 'double')*1000;
Rx = typecast(fliplr(raw_data(469:476)), 'double')* 180 / 3.1459;
Ry = typecast(fliplr(raw_data(477:484)), 'double')* 180 / 3.1459;
Rz = typecast(fliplr(raw_data(485:492)), 'double')* 180 / 3.1459;
digit_out= typecast(fliplr(raw_data(1045:1052)), 'double');
Gripper_state = bitand(digit_out, MASK0);
if (Gripper_state == MASK0)
Gripper_state = 1;
else
Gripper_state = 0;
end
timestring = datestr(timestamp,'HH:MM:SS.FFF');
[~,~,~,hours,minutes,seconds] = datevec(timestring);
Time1 = 1000*(3600*hours + 60*minutes + seconds);
iteration = iteration + 1;
% fprintf('Time = %d sec Gripper_state =%d X = %f mm Y = %f mm Z = %f mm Rx = %f deg Ry = %f deg Rz = %f deg \n',Time, Gripper_state, X, Y, Z, Rx, Ry, Rz);
T = [T; table(Time1,Gripper_state, X, Y, Z, Rx, Ry, Rz)];
tcp_coordinates = T;
end
writetable(T,'robotData.csv','Delimiter',',');
fclose(s);
delete(s);
clear s; % Close connection
And displays this error:
Warning: The specified amount of data was not returned within the Timeout period.
'tcpip' unable to read all requested data. For more information on possible reasons, see TCPIP
Read Warnings.
Index exceeds array bounds.
Error in ur_data_acquisition (line 25)
X = typecast(fliplr(raw_data(441:448)), 'double')*1000;
Is this your line of code?
X = typecast(fliplr(raw_data(441:448)), 'double')*1000;
If so, then the warning and error messages are telling you what is wrong - not enough data was returned before the timeout expired and then you tried to access more data than what was available in the (presumably) raw_data array. You may need to check the size of raw_data before trying to extract data from it...

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 28 de Ag. de 2018

Comentada:

el 30 de Ag. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by