forked from s-kajita/IntroductionToHumanoidRobotics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculate_zmp.m
100 lines (82 loc) · 2.32 KB
/
calculate_zmp.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
%%% calculate_zmp.m
%%% ZMP(IZMP) calculation and display (3.4.1 Derivation of ZMP)
close all
clear
global uLINK G M
G = 9.8; % Gravity acceleration [m/s^2]
SetupBipedRobot2; % Biped robot in Fig.2.19, Fig.20 with center of mass and inertia tensor for each link
M = TotalMass(1);
%%%%%% Initial condition %%%%%
uLINK(BODY).p = [0.0, 0.0, 0.5]';
uLINK(BODY).R = eye(3);
uLINK(BODY).v = [0, 0, 0]';
uLINK(BODY).w = [0, 0, 0]';
Rfoot.p = [0, -0.2, 0.1]';
Rfoot.R = eye(3);
Rfoot.v = [0 0 0]';
Rfoot.w = [0 0 0]';
Lfoot.p = [-0.04, 0.0, 0.05]';
Lfoot.R = eye(3);
Lfoot.v = [0 0 0]';
Lfoot.w = [0 0 0]';
InverseKinematicsAll(RLEG_J5, Rfoot);
InverseKinematicsAll(LLEG_J5, Lfoot);
ForwardVelocity(1);
com = calcCoM; % Center of mass
Zc = com(3); % Height of the linear inverted pendulum (LIPM)
Tc = sqrt(Zc/G); % Time constant of LIPM
cx0 = com(1);
cy0 = com(2);
P1 = calcP(1);
L1 = calcL(1);
global Dtime
Dtime = 0.01;
EndTime = 0.5;
time = 0:Dtime:EndTime;
tsize = length(time);
com_m = zeros(tsize,3);
zmp_m = zeros(tsize,2);
figure
k = 1;
zmpz = 0.0;
for k = 1:tsize
[px,vx] = LIPM(time(k),Lfoot.p(1),cx0,Tc);
[py,vy] = LIPM(time(k),Lfoot.p(2),cy0,Tc);
uLINK(BODY).p = [px, py, 0.5]';
uLINK(BODY).v = [vx, vy, 0.0]';
InverseKinematicsAll(LLEG_J5, Lfoot); % Keep support foot on the ground
ForwardVelocity(1);
%%% Calculate ZMP
com = calcCoM; % Center of mass
P = calcP(1); % Linear momentum
L = calcL(1); % Angular momentum
dP = (P-P1)/Dtime;
dL = (L-L1)/Dtime;
[zmpx,zmpy] = calcZMP(com,dP,dL,zmpz);
P1 = P;
L1 = L;
com_m(k,:) = com';
zmp_m(k,:) = [zmpx, zmpy];
hold off
newplot
DrawAllJoints(1);
h(1) = DrawMarker([zmpx,zmpy,zmpz]','r');
h(2) = DrawMarker([com(1),com(2),0]','b');
legend(h,'IZMP','CoM');
axis equal
set(gca,...
'CameraPositionMode','manual',...
'CameraPosition',[4,4,1],...
'CameraViewAngleMode','manual',....
'CameraViewAngle',15,...
'Projection','perspective',...
'XLimMode','manual',...
'XLim',[-0.5 0.5],...
'YLimMode','manual',...
'YLim',[-0.5 0.5],...
'ZLimMode','manual',...
'ZLim',[0 1.5])
grid on
text(0.5, -0.4, 1.4, ['time=',num2str(time(k),'%5.3f')])
drawnow;
end