1
- % Visualize Walking Gait and Inverse Kinematics
2
- % Copyright 2019 The MathWorks, Inc.
3
-
4
- close all
5
- robotParametersCtrl ;
6
-
7
- %% Plot the foot trajectory
8
- gaitPeriod = 1 ;
9
- stepLength = 0.1 ;
10
- stepHeight = 0.025 ;
11
- numPoints = 150 ;
12
-
13
- tVec = linspace(0 ,gaitPeriod ,numPoints );
14
- foot_height_offset = sqrt( (lower_leg_length + upper_leg_length )^2 ...
15
- - ((stepLength / 2 )*100 )^2 ) - 1e-3 ;
16
-
17
- figure , hold on
18
- x = zeros(numPoints ,1 );
19
- y = zeros(numPoints ,1 );
20
- for idx = 1 : numPoints
21
- [x(idx ),y(idx )] = evalFootGait(tVec(idx ),stepLength ,stepHeight ,gaitPeriod );
22
- end
23
-
24
- plot(x ,y ,' .-' );
25
- axis equal
26
- title(' Foot Gait' );
27
- xlabel(' x [m]' )
28
- ylabel(' y [m]' )
29
-
30
- %% Calculate joint angles
31
- theta_hip = zeros(numPoints ,1 );
32
- theta_knee = zeros(numPoints ,1 );
33
- theta_ankle = zeros(numPoints ,1 );
34
-
35
- for idx = 1 : numPoints
36
-
37
- pitch = 0 ; % Assume zero body pitch
38
-
39
- % Calculate inverse kinematics
40
- theta = legInvKin(upper_leg_length / 100 , lower_leg_length / 100 , ...
41
- x(idx ), y(idx ) - (foot_height_offset / 100 ));
42
-
43
- % Address multiple solutions by preventing knee bending backwards
44
- if size(theta ,1 ) == 2
45
- if theta(1 ,2 ) > 0
46
- t1 = theta(2 ,1 );
47
- t2 = theta(2 ,2 );
48
- else
49
- t1 = theta(1 ,1 );
50
- t2 = theta(1 ,2 );
51
- end
52
- else
53
- t1 = theta(1 );
54
- t2 = theta(2 );
55
- end
56
-
57
- % Pack the results. Ensure the ankle angle is set so the foot always
58
- % lands flat on the ground
59
- theta_hip(idx ) = t1 ;
60
- theta_knee(idx ) = t2 ;
61
- theta_ankle(idx ) = -(t1 + t2 );
62
-
63
- end
64
-
65
- % Display joint angles
66
- figure
67
- subplot(311 )
68
- plot(tVec ,rad2deg(theta_hip ))
69
- title(' Hip Angle [deg]' );
70
- subplot(312 )
71
- plot(tVec ,rad2deg(theta_knee ))
72
- title(' Knee Angle [deg]' );
73
- subplot(313 )
74
- plot(tVec ,rad2deg(theta_ankle ))
75
- title(' Ankle Angle [deg]' );
76
-
77
- %% Animate the walking gait
78
- figure(3 ), clf , hold on
79
-
80
- % Initialize plot
81
- plot(x ,y - foot_height_offset / 100 ,' k:' ,' LineWidth' ,1 );
82
- h1 = plot([0 0 ],[0 0 ],' r-' ,' LineWidth' ,4 );
83
- h2 = plot([0 0 ],[0 0 ],' b-' ,' LineWidth' ,4 );
84
-
85
- % Calculate knee and ankle (x,y) positions
86
- xKnee = sin(theta_hip )*upper_leg_length / 100 ;
87
- yKnee = - cos(theta_hip )*upper_leg_length / 100 ;
88
- xAnkle = xKnee + sin(theta_hip + theta_knee )*lower_leg_length / 100 ;
89
- yAnkle = yKnee - cos(theta_hip + theta_knee )*lower_leg_length / 100 ;
90
-
91
- % Define axis limits
92
- xMin = min([xKnee ;xAnkle ]) - 0.025 ;
93
- xMax = max([xKnee ;xAnkle ]) + 0.025 ;
94
- yMin = min([yKnee ;yAnkle ]) - 0.025 ;
95
- yMax = max([0 ;yKnee ;yAnkle ]) + 0.025 ;
96
-
97
- % Animate the walking gait
98
- numAnimations = 5 ;
99
- for anim = 1 : numAnimations
100
- for idx = 1 : numPoints
101
- set(h1 ,' xdata' ,[0 xKnee(idx )],' ydata' ,[0 yKnee(idx )]);
102
- set(h2 ,' xdata' ,[xKnee(idx ) xAnkle(idx )],' ydata' ,[yKnee(idx ) yAnkle(idx )]);
103
- xlim([xMin xMax ]), ylim([yMin yMax ]);
104
- title(' Walking Gait Animation' );
105
- axis equal
106
- drawnow
107
- end
1
+ % Visualize Walking Gait and Inverse Kinematics
2
+ % Copyright 2019 The MathWorks, Inc.
3
+
4
+ close all
5
+ robotParametersCtrl ;
6
+
7
+ %% Plot the foot trajectory
8
+ gaitPeriod = 1 ;
9
+ stepLength = 0.1 ;
10
+ stepHeight = 0.025 ;
11
+ numPoints = 150 ;
12
+
13
+ tVec = linspace(0 ,gaitPeriod ,numPoints );
14
+ foot_height_offset = sqrt( (lower_leg_length + upper_leg_length )^2 ...
15
+ - ((stepLength / 2 )*100 )^2 ) - 1e-3 ;
16
+
17
+ figure , hold on
18
+ x = zeros(numPoints ,1 );
19
+ y = zeros(numPoints ,1 );
20
+ for idx = 1 : numPoints
21
+ [x(idx ),y(idx )] = evalFootGait(tVec(idx ),stepLength ,stepHeight ,gaitPeriod );
22
+ end
23
+
24
+ plot(x ,y ,' .-' );
25
+ axis equal
26
+ title(' Foot Gait' );
27
+ xlabel(' x [m]' )
28
+ ylabel(' y [m]' )
29
+
30
+ %% Calculate joint angles
31
+ theta_hip = zeros(numPoints ,1 );
32
+ theta_knee = zeros(numPoints ,1 );
33
+ theta_ankle = zeros(numPoints ,1 );
34
+
35
+ for idx = 1 : numPoints
36
+
37
+ pitch = 0 ; % Assume zero body pitch
38
+
39
+ % Calculate inverse kinematics
40
+ theta = legInvKin(upper_leg_length / 100 , lower_leg_length / 100 , ...
41
+ x(idx ), y(idx ) - (foot_height_offset / 100 ));
42
+
43
+ % Address multiple solutions by preventing knee bending backwards
44
+ if size(theta ,1 ) == 2
45
+ if theta(1 ,2 ) > 0
46
+ t1 = theta(2 ,1 );
47
+ t2 = theta(2 ,2 );
48
+ else
49
+ t1 = theta(1 ,1 );
50
+ t2 = theta(1 ,2 );
51
+ end
52
+ else
53
+ t1 = theta(1 );
54
+ t2 = theta(2 );
55
+ end
56
+
57
+ % Pack the results. Ensure the ankle angle is set so the foot always
58
+ % lands flat on the ground
59
+ theta_hip(idx ) = t1 ;
60
+ theta_knee(idx ) = t2 ;
61
+ theta_ankle(idx ) = -(t1 + t2 );
62
+
63
+ end
64
+
65
+ % Display joint angles
66
+ figure
67
+ subplot(311 )
68
+ plot(tVec ,rad2deg(theta_hip ))
69
+ title(' Hip Angle [deg]' );
70
+ subplot(312 )
71
+ plot(tVec ,rad2deg(theta_knee ))
72
+ title(' Knee Angle [deg]' );
73
+ subplot(313 )
74
+ plot(tVec ,rad2deg(theta_ankle ))
75
+ title(' Ankle Angle [deg]' );
76
+
77
+ %% Animate the walking gait
78
+ figure(3 ), clf , hold on
79
+
80
+ % Initialize plot
81
+ plot(x ,y - foot_height_offset / 100 ,' k:' ,' LineWidth' ,1 );
82
+ h1 = plot([0 0 ],[0 0 ],' r-' ,' LineWidth' ,4 );
83
+ h2 = plot([0 0 ],[0 0 ],' b-' ,' LineWidth' ,4 );
84
+
85
+ % Calculate knee and ankle (x,y) positions
86
+ xKnee = sin(theta_hip )*upper_leg_length / 100 ;
87
+ yKnee = - cos(theta_hip )*upper_leg_length / 100 ;
88
+ xAnkle = xKnee + sin(theta_hip + theta_knee )*lower_leg_length / 100 ;
89
+ yAnkle = yKnee - cos(theta_hip + theta_knee )*lower_leg_length / 100 ;
90
+
91
+ % Define axis limits
92
+ xMin = min([xKnee ;xAnkle ]) - 0.025 ;
93
+ xMax = max([xKnee ;xAnkle ]) + 0.025 ;
94
+ yMin = min([yKnee ;yAnkle ]) - 0.025 ;
95
+ yMax = max([0 ;yKnee ;yAnkle ]) + 0.025 ;
96
+
97
+ % Animate the walking gait
98
+ numAnimations = 5 ;
99
+ for anim = 1 : numAnimations
100
+ for idx = 1 : numPoints
101
+ set(h1 ,' xdata' ,[0 xKnee(idx )],' ydata' ,[0 yKnee(idx )]);
102
+ set(h2 ,' xdata' ,[xKnee(idx ) xAnkle(idx )],' ydata' ,[yKnee(idx ) yAnkle(idx )]);
103
+ xlim([xMin xMax ]), ylim([yMin yMax ]);
104
+ title(' Walking Gait Animation' );
105
+ axis equal
106
+ drawnow
107
+ end
108
108
end
0 commit comments