Problem with call back function

조회 수: 2 (최근 30일)
DEYIRE UMAR
DEYIRE UMAR 2018년 8월 28일
댓글: Geoff Hayes 2018년 8월 30일
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
Geoff Hayes
Geoff Hayes 2018년 8월 30일
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;
Geoff Hayes
Geoff Hayes 2018년 8월 30일
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...

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by