forked from ShuoYangRobotics/QuadrupedSim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inverse_kinematics.m
48 lines (45 loc) · 1.11 KB
/
inverse_kinematics.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
function [inv_s,inv_u,inv_k] = inverse_kinematics(p, body)
% from center of the shoulder
x = p(1);
y = p(2);
z = p(3);
l1 = body.upper_length;
l2 = body.lower_length;
tmp = y/sqrt(y*y+z*z);
if tmp > 1
tmp = 1;
elseif tmp < -1
tmp = -1;
end
inv_s = asin(tmp);
tmp = (x*x+y*y+z*z-l1*l1-l2*l2)/(2*l1*l2);
if tmp > 1
tmp = 1;
elseif tmp < -1
tmp = -1;
end
inv_k = -acos(tmp); % always assume knee is minus
if abs(inv_k) > 1e-4
if abs(inv_s) > 1e-4
temp1 = y/sin(inv_s)+(l2*cos(inv_k)+l1)*x/l2/sin(inv_k);
else
temp1 = -z/cos(inv_s)+(l2*cos(inv_k)+l1)*x/l2/sin(inv_k);
end
temp2 = -l2*sin(inv_k)-(l2*cos(inv_k)+l1)*(l2*cos(inv_k)+l1)/(l2*sin(inv_k));
tmp = temp1/temp2;
if tmp > 1
tmp = 1;
elseif tmp < -1
tmp = -1;
end
inv_u = asin(tmp);
else
tmp = x/(-l1-l2);
if tmp > 1
tmp = 1;
elseif tmp < -1
tmp = -1;
end
inv_u = asin(tmp);
end
end