-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw3problem1.m
209 lines (159 loc) · 6.06 KB
/
hw3problem1.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
clear, clc, close all
addpath('utils');
plotOn = true;
nTests = 20;
%% Create the manipulator
% Link length values (meters)
L1 = 0.3;
L2 = 0.3;
L3 = 0.3;
robot = SerialLink([Revolute('a', 0, 'd', L1, 'alpha', pi/2, 'offset', pi/2), ...
Revolute('a', L2, 'd', 0, 'alpha', 0), ...
Revolute('a', L3, 'd', 0, 'alpha', pi/2, 'offset', -pi/2)], ...
'name', 'RRR Manipulator');
% Joint limits
qlim = [-pi/2 pi/2; % q(1)
-pi/4 pi/2; % q(2)
-pi/12 pi/3]; % q(3)
% Display the manipulator in the home configuration
q = zeros(1,3);
robot.teach(q);
%% Part A - Forward Kinematics via PoE in the body frame
% Let us calculate the screw axis for each joint
% Put all the axes into a 6xn matrix S, where n is the number of joints
S_space = [0 0 1 0 0 0;
1 0 0 -cross([1 0 0], [0 0 L1]);
1 0 0 -cross([1 0 0], [0 L2 L1])]';
% Let us calculate the homogeneous transformation matrix M for the
% home configuration
R_home = [0 0 -1; 1 0 0; 0 -1 0]';
t_home = [0 L2 L1-L3]';
M = [R_home t_home; 0 0 0 1];
T=fkine(S_space,M,q,"space");
R=T(1:3,1:3);
P=T(1:3,4);
Pb=[0,-P(3),P(2);P(3),0,-P(1);-P(2),P(1),0];
o=[0,0,0;0,0,0;0,0,0];
A=[R,o;Pb*R,R];
S_body=pinv(A)*S_space
fprintf('---------------------Forward Kinematics Test---------------------\n');
fprintf(['Testing ' num2str(nTests) ' random configurations.\n']);
fprintf('Progress: ');
nbytes = fprintf('0%%');
% Test the forward kinematics for 10 random sets of joint variables
for ii = 1 : nTests
fprintf(repmat('\b',1,nbytes));
nbytes = fprintf('%0.f%%', ceil(ii/nTests*100));
% Generate a random configuration
q = [qlim(1,1) + (qlim(1,2) - qlim(1,1)) * rand(), ...
qlim(2,1) + (qlim(2,2) - qlim(2,1)) * rand(), ...
qlim(3,1) + (qlim(3,2) - qlim(3,1)) * rand()];
% Calculate the forward kinematics
T = fkine(S_body,M,q,"body");
if plotOn
robot.teach(q);
title('Forward Kinematics Test');
end
assert(all(all(abs(double(robot.fkine(q)) - T) < 1e-10)));
end
fprintf('\nTest passed successfully.\n');
%% Part B - Calculate the Body Jacobian of the manipulator
fprintf('-------------------Differential Kinematics Test------------------\n');
fprintf(['Testing ' num2str(nTests) ' random configurations.\n']);
fprintf('Progress: ');
nbytes = fprintf('0%%');
% Test the correctness of the Jacobian for 10 random sets of joiny
% variables
for ii = 1 : nTests
fprintf(repmat('\b',1,nbytes));
nbytes = fprintf('%0.f%%', ceil(ii/nTests*100));
% Generate a random configuration
q = [qlim(1,1) + (qlim(1,2) - qlim(1,1)) * rand(), ...
qlim(2,1) + (qlim(2,2) - qlim(2,1)) * rand(), ...
qlim(3,1) + (qlim(3,2) - qlim(3,1)) * rand()];
% Calculate the Jacobian in the body frame
J_b = jacobe(S_space,M,q);
if plotOn
robot.teach(q);
title('Differential Kinematics Test');
end
% Test the correctness of the Jacobian
J_test = [J_b(4:6,:); J_b(1:3,:)]; % swap the rotation and translation components
assert(all(all(abs(double(robot.jacobe(q)) - J_test) < 1e-10)));
end
fprintf('\nTest passed successfully.\n');
% Part C - Calculate the Analyical Jacobian of the manipulator
fprintf('---------------------Analytical Jacobian Test--------------------\n');
fprintf(['Testing ' num2str(nTests) ' random configurations.\n']);
fprintf('Progress: ');
nbytes = fprintf('0%%');
% Test the correctness of the Analytical Jacobian for 10 random sets of joint
% variables
for ii = 1 : nTests
fprintf(repmat('\b',1,nbytes));
nbytes = fprintf('%0.f%%', ceil(ii/nTests*100));
% Generate a random configuration
q = [qlim(1,1) + (qlim(1,2) - qlim(1,1)) * rand(), ...
qlim(2,1) + (qlim(2,2) - qlim(2,1)) * rand(), ...
qlim(3,1) + (qlim(3,2) - qlim(3,1)) * rand()];
% Calculate the Analytical Jacobian
J_a = jacoba(S_space,M,q);
if plotOn
robot.teach(q);
title('Analytical Jacobian Test');
end
% Test the correctness of the Jacobian
Jref = robot.jacob0(q);
Jref = Jref(1:3,:);
assert(all(all(abs(double(Jref) - J_a) < 1e-10)));
end
fprintf('\nTest passed successfully.\n');
%% Part D - Inverse Kinematics
fprintf('----------------------Inverse Kinematics Test--------------------\n');
fprintf(['Testing ' num2str(nTests) ' random configurations.\n']);
fprintf('Progress: ');
nbytes = fprintf('0%%');
% Set the current joint variables
currentQ = zeros(1,3);
% Calculate the Analytical Jacobian at the current configuration
J_a = jacoba(S_space,M,currentQ);
% Generate path to follow
t = linspace(0, 2*pi, nTests);
x = 0.25 * cos(t);
y = 0.25 * sin(t);
z = 0.2 * ones(1,nTests);
path = [x; y; z];
if plotOn
robot.teach(currentQ);
h = plot_ellipse(J_a*J_a');
title('Inverse Kinematics Test');
hold on
scatter3(path(1,:), path(2,:), path(3,:), 'filled');
end
% Iterate over the target points
for ii = 1 : nTests
fprintf(repmat('\b',1,nbytes));
nbytes = fprintf('%0.f%%', ceil(ii/nTests*100));
% Select the next target point
targetPose = path(:,ii);
T = fkine(S_body, M, currentQ, 'body');
currentPose = T(1:3,4);
while norm(targetPose - currentPose) > 1e-3
% YOUR INVERSE KINEMATICS CODE HERE
% Necessary variables:
% Current Robot Pose -> currentPose
% Target Robot Pose -> targetPose
% Current Joint Variables -> currentQ
J = jacoba(S_space,M,currentQ);
deltaQ = pinv(J)*(targetPose - currentPose);
currentQ = currentQ + deltaQ';
T = fkine(S_body, M, currentQ, 'body');
currentPose = T(1:3,4);
if plotOn
robot.teach(currentQ);
plot_ellipse(J*J',currentPose,'alter',h);
title('Inverse Kinematics Test');
end
end
end
fprintf('\nTest passed successfully.\n');