-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathpassivederive.m
296 lines (211 loc) · 9.16 KB
/
passivederive.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
%%%% derive passive walker with round feet
%%% Pranav A. BHounsule, 21 April 2009. [email protected]
clc;
clear all;
syms q1 q2 real %Angles as defined in figures
syms u1 u2 real %Angular velocity
syms ud1 ud2 real%Angular Acceleration
syms gam g real %slope of ramp,, gravity
syms c w l r real % Distances as defined in figures
syms M m I real%Mass Hip, leg, Inertia
syms xp1 real
syms Th real%Th motor torques
syms v1 v2 real %angles before heelstrike
syms r1 r2 real %velocities before heelstrike
i=[1 0 0];
j=[0 1 0];
k=[0 0 1];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Reference Frames %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
X1 = sin(q1)*i-cos(q1)*j;
Y1 = cos(q1)*i+sin(q1)*j;
X2 = sin(q1-q2)*i - cos(q1-q2)*j;
Y2 = cos(q1-q2)*i + sin(q1-q2)*j;
J = -sin(gam)*i+cos(gam)*j; %Fictitious ramp
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Position Vectors %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Original position vectors
r_H_G1 = c*X1 + w*Y1;
r_H_G2 = c*X2 + w*Y2;
r_P1_H = r*j - l*X1;
r_P2_H = r*j - l*X2;
%Derived position vectors
r_P1_G1 = r_P1_H + r_H_G1;
r_P1_G2 = r_P1_H + r_H_G2;
r_P1_P2 = r_P1_H - r_P2_H;
r_H_P2 = -r_P2_H;
%Position vectors for heelstrike
r_P2_G1 = r_P2_H + r_H_G1;
r_P2_G2 = r_P2_H + r_H_G2;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Angular Velocities and Accelerations %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
om1 = u1*k;
om2 = (u1-u2)*k;
al1 = ud1*k;
al2 = (ud1-ud2)*k;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Constraints, Linear Velocities and Accelerations %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
q = [q1; q2];
u = [u1; u2];
ud = [ud1; ud2];
xh = -l*sin(q1) - r*q1 + xp1;
yh = l*cos(q1) + r;
xhdot = jacobian(xh,q)*u;
xhddot = jacobian(xhdot,q)*u + jacobian(xhdot,u)*ud;
yhdot = jacobian(yh,q)*u;
yhddot = jacobian(yhdot,q)*u + jacobian(yhdot,u)*ud;
v_H = xhdot*i+yhdot*j;
a_H = xhddot*i+yhddot*j;
v_G1 = v_H +cross(om1,r_H_G1);
v_G2 = v_H +cross(om2,r_H_G2);
a_G1 = a_H +cross(om1,cross(om1,r_H_G1))+cross(al1,r_H_G1);
a_G2 = a_H +cross(om2,cross(om2,r_H_G2))+cross(al2,r_H_G2);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Collision Condition (Events) %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
CD = dot(r_P1_P2,j);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Change of Angular Momentum & External Moments %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
M_P1 = dot(m*g*cross(r_P1_G1,-J)+m*g*cross(r_P1_G2,-J)+M*g*cross(r_P1_H,-J),k);
Hdot_P1 = dot(m*cross(r_P1_G1,a_G1)+m*cross(r_P1_G2,a_G2)+M*cross(r_P1_H,a_H)+I*(al1+al2),k);
M_H = dot(m*g*cross(r_H_G2,-J)+Th*k,k); %Th
Hdot_H = dot(m*cross(r_H_G2,a_G2)+I*al2,k);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Angular Momentum, (Before and After Heelstrike %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
H_P1p = dot(m*cross(r_P1_G1,v_G1)+m*cross(r_P1_G2,v_G2)+M*cross(r_P1_H,v_H)+I*(om1+om2),k);
H_P2n = subs(dot(m*cross(r_P2_G2,v_G2)+m*cross(r_P2_G1,v_G1)+M*cross(r_P2_H,v_H)+I*(om1+om2),k),...
{u1,u2,q1,q2},{v1,v2,r1,r2});
H_Hp = dot(m*cross(r_H_G2,v_G2)+I*om2,k);
H_Hn = subs(dot(m*cross(r_H_G1,v_G1)+I*om1,k),...
{u1,u2,q1,q2},{v1,v2,r1,r2});
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% EOM:[M-Hdot=0](Single Stance and Double Stance) %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
AMB_P1 = M_P1-Hdot_P1;
AMB_H = M_H-Hdot_H;
%%% Single stance equations %%%
RHSs1 = -subs(AMB_P1,[ud1 ud2],[0 0]);
Ms11 = subs(AMB_P1,[ud1 ud2],[1 0]) + RHSs1;
Ms12 = subs(AMB_P1,[ud1 ud2],[0 1]) + RHSs1;
RHSs2 = -subs(AMB_H,[ud1 ud2],[0 0]);
Ms21 = subs(AMB_H,[ud1 ud2],[1 0]) + RHSs2;
Ms22 = subs(AMB_H,[ud1 ud2],[0 1]) + RHSs2;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Heelstrike:[H+ = H-](Single Stance and Double Stance) %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Heelstrike: H+
AM_P1 = H_P1p;
AM_H = H_Hp;
%%%% Heelstrike: H-, Transition to Single stance
RHSh1 = H_P2n;
RHSh2 = H_Hn;
Mh11 = subs(AM_P1,[u1 u2],[1 0]);
Mh12 = subs(AM_P1,[u1 u2],[0 1]);
Mh21 = subs(AM_H,[u1 u2],[1 0]);
Mh22 = subs(AM_H,[u1 u2],[0 1]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Energies %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Get required positions of masses
%wrt ramp frame
xH = xh - xp1;
yH = yh;
xG1 = xH+dot(r_H_G1,i);
yG1 = yH+dot(r_H_G1,j);
xG2 = xH+dot(r_H_G2,i);
yG2 = yH+dot(r_H_G2,j);
%wrt to global frame
Y_H = yH*cos(gam) - xH*sin(gam);
Y_G1 = yG1*cos(gam) - xG1*sin(gam);
Y_G2 = yG2*cos(gam) - xG2*sin(gam);
X_H = xH*cos(gam) + yH*sin(gam);
X_G1 = xG1*cos(gam) + yG1*sin(gam);
X_G2 = xG2*cos(gam) + yG2*sin(gam);
KE = 0.5*(simplify(m*dot(v_G1,v_G1) + m*dot(v_G2,v_G2) + M*dot(v_H,v_H) + I*(dot(om1,om1) + dot(om2,om2))));
PE = simplify(m*g*Y_G1+m*g*Y_G2+M*g*Y_H);
TE = simplify(KE + PE);
DTE = jacobian(TE,q)*u + jacobian(TE,u)*ud;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ground Reaction Forces %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
R = m*(a_G1+a_G2) + M*a_H + (2*m+M)*g*j;
Rx = dot(R,i); %Reaction from front foot on the ground in x direction
Ry = dot(R,j); %Reaction from front foot on the ground in y direction
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Writing the right hand side automatically %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Write single_stance.m %%%
fid=fopen( 'single_stance.m','w');
fprintf(fid, 'function zdot=single_stance(t,z,flag,GL_DIM) \n\n');
fprintf(fid, 'q1 = z(1); u1 = z(2); \n');
fprintf(fid, 'q2 = z(3); u2 = z(4); \n');
fprintf(fid, 'xh = z(6); vxh = z(7); \n');
fprintf(fid, 'yh = z(8); vyh = z(9); \n\n');
fprintf(fid, 'M = walker.M; m = walker.m; I = walker.I; \n');
fprintf(fid, 'l = walker.l; c = walker.c; w = walker.w; \n');
fprintf(fid, 'r = walker.r; g = walker.g; gam = walker.gam; \n\n');
fprintf(fid, 'Th=0; \n\n');
fprintf(fid,'M11 = %s; \n', char(simplify(Ms11)) );
fprintf(fid,'M12 = %s; \n', char(simplify(Ms12)) );
fprintf(fid,'\n');
fprintf(fid,'M21 = %s; \n', char(simplify(Ms21)) );
fprintf(fid,'M22 = %s; \n', char(simplify(Ms22)) );
fprintf(fid,'\n');
fprintf(fid,'RHS1 = %s; \n', char(simplify(RHSs1)) );
fprintf(fid,'RHS2 = %s; \n', char(simplify(RHSs2)) );
fprintf(fid,'\n');
fprintf(fid,'MM = [M11 M12; \n');
fprintf(fid,' M21 M22]; \n\n');
fprintf(fid,'RHS = [RHS1; RHS2]; \n\n');
fprintf(fid,'X = MM \\ RHS; \n\n');
fprintf(fid,'ud1 = X(1); \n');
fprintf(fid,'ud2 = X(2); \n\n');
fprintf(fid,'DTE = %s; \n', char(simplify(DTE)) );
fprintf(fid,'axh = %s; \n', char(simplify(xhddot)) );
fprintf(fid,'ayh = %s; \n', char(simplify(yhddot)) );
fprintf(fid,'\n');
fprintf(fid,'zdot = [u1 ud1 u2 ud2 ... \n');
fprintf(fid,' DTE vxh axh vyh ayh]''; \n');
fclose(fid);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Write heelstrike.m %%%
%%% Heelstrike transitioning to single stance
fid=fopen( 'heelstrike.m','w');
fprintf(fid, 'function zplus=heelstrike(t,z,GL_DIM) \n\n');
fprintf(fid, 'r1 = z(1); v1 = z(2); \n');
fprintf(fid, 'r2 = z(3); v2 = z(4); \n');
fprintf(fid, 'xh = z(6); yh = z(8); \n\n');
fprintf(fid, 'q1 = r1 - r2; \n');
fprintf(fid, 'q2 = -r2; \n\n');
fprintf(fid, 'M = walker.M; m = walker.m; I = walker.I; \n');
fprintf(fid, 'l = walker.l; c = walker.c; w = walker.w; \n');
fprintf(fid, 'r = walker.r; g = walker.g; gam = walker.gam; \n\n');
fprintf(fid,'M11 = %s; \n', char(simplify(Mh11)) );
fprintf(fid,'M12 = %s; \n', char(simplify(Mh12)) );
fprintf(fid,'\n');
fprintf(fid,'M21 = %s; \n', char(simplify(Mh21)) );
fprintf(fid,'M22 = %s; \n', char(simplify(Mh22)) );
fprintf(fid,'\n');
fprintf(fid,'RHS1 = %s; \n', char(simplify(RHSh1)) );
fprintf(fid,'RHS2 = %s; \n', char(simplify(RHSh2)) );
fprintf(fid,'\n');
fprintf(fid,'MM = [M11 M12; \n');
fprintf(fid,' M21 M22]; \n\n');
fprintf(fid,'RHS = [RHS1; RHS2]; \n\n');
fprintf(fid,'X = MM \\ RHS; \n\n');
fprintf(fid,'u1 = X(1); \n');
fprintf(fid,'u2 = X(2); \n\n');
fprintf(fid,'TE = %s; \n', char(simplify(TE)) );
fprintf(fid,'vxh = %s; \n', char(simplify(xhdot)) );
fprintf(fid,'vyh = %s; \n', char(simplify(yhdot)) );
fprintf(fid,'\n');
fprintf(fid,'zplus = [q1 u1 q2 u2 TE xh vxh yh vyh]; \n');
fclose(fid);
%%%%%%%%%%%%%%%
disp('Files written');