-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecuteReference2.m
82 lines (58 loc) · 1.79 KB
/
executeReference2.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
function executeReference2(gain, allMsg, joint_sub, robot, pubJointCmd, time_scale, th, is_reverse)
% Take a JointState trajectory and execute it using velocity command.
% allMsg: trajectory
% joint_sub:
% robot: object
% pubJointCmd: publisher
% time_scale: scale
% th: velocity threshold so that robot doesn't make sudden move.
% without gripper
msgJointCmd = rosmessage('intera_core_msgs/JointCommand');
msgJointCmd.Mode = msgJointCmd.VELOCITYMODE;
inds = [1 3 4 5 6 7 8];
msgJointCmd.Names = robot.jointNames(inds)
time = arrayfun(@(x) double(x.Header.Stamp.Sec) + double(x.Header.Stamp.Nsec)*1E-9, allMsg);
time = time - time(1);
refTraj = allMsg;
if is_reverse
refTraj=allMsg(end:-1:1);
end
% arrayfun(@(x) x.Position, allMsg, 'UniformOutput', false)
for i = 2:length(refTraj) %was 1
if length(refTraj(i).Position) < 7
refTraj(i) = refTraj(i-1);
end
end
time = time*time_scale;
vel = 1;
tic
while norm(vel) > .002 || toc < time(end)
msg = joint_sub.receive();
if isempty(msg) || length(msg.Position) < 7
continue
end
curTime = toc;
curTime
ind = find(curTime < time, 1);
if isempty(ind)
ind = length(time);
end
robot.setJointsMsg(msg);
curJoint = robot.getJoints();
curJoint = curJoint(inds);
refmsg = refTraj(ind);
robot.setJointsMsg(refmsg);
refJoint = robot.getJoints();
refJoint = refJoint(inds);
refVel = robot.getJointsVelfromMsg(refmsg);
refVel = refVel(inds)./time_scale;
refVel = refVel*(ind<length(time));
vel = refVel + gain*(refJoint - curJoint);
%ensure vel isn't too high. parameter to play=th
if max(abs(vel) > th)
vel = th*vel./max(abs(vel));
end
msgJointCmd.Velocity = vel;
send(pubJointCmd, msgJointCmd)
end
end