-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtree_ALL.m
341 lines (273 loc) · 9.54 KB
/
tree_ALL.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
close all
clear all;
clear
%% Rigid-body bar link parameters
COM_init = [0 0]; % center of mass
w = 1; % width of rigid body
h = 5; % height of rigid body
j = 0.25; % joint hole size
d = 2; % distance of the joint holes away from COM, the joint holes on
% both sides are the same distance away from the COM
m = 1; % mass of the objects
ks = 100;%spring and damping constants
kd = 10;
%% System definition! Change all parameters here.
hTree = 3; % height of the binary tree
% 2^0 + 2^1 + ... + 2^h = 2^(h+1)-1
totBod = 2^(hTree+1)-1;
root = [-pi/2,0,0]';
relth = zeros(totBod-1,1); % initial position of the system (6x1)
%th = pi/4;
q0 = zeros(totBod*3,1);%generateInitCoords(root, relth, d);
%q0 = generateInitTreeCoords(root, th, d, hTree);
numBod = totBod; length(q0)/3;
% Apply a tip force
Fext = zeros(numBod*3,1);
Fext(end-2:end) = [0 -25 50]';
dt = 0.025; % timestep size [s]
time = 5; % total simulation time [s]
solverType = 3; % 1: A\b, 2: sparse, 3: dense
auxConstraint = 2; % 1: no aux constraints, 2: auxillary constraints
visualize = 1; % 1=visualize, otherwise=no
%% Get the Jacobian
% joint coordinates in the body frame, one constraint between each joint
% NOTE: Add more joint coordinates as you add more constraints!
pa = sym('pa', [(numBod-1) 2], 'real');
pb = sym('pb', [(numBod-1) 2], 'real');
q = sym('q', [numBod 3], 'real'); % gen coords for rigid body, COM position and orientation about the COM
qdot = sym('qdot', [numBod 3], 'real'); % angular velocity and velocity of the rigid body COM in the world frame
qddot = sym('qddot', [numBod 3], 'real'); % angular acc and acc of the rigid body COM in the world frame
R2 = @(theta) [cos(theta) -sin(theta);
sin(theta) cos(theta)];
% transform the joint coordinates in the body frame to the world frame
xa = [];
xb = [];
for i = 0:hTree -1 % for every level of the tree
for k = 1:2^i % for every block node
ind = (k-1)*2;
par = 2^i+k-1; % parent
ln = 2^(i+1)+ind;
rn = 2^(i+1)+ind+1;
% left branch
xa = cat(1,xa,R2(q(par,1))*pa(par,:)' + q(par,2:3)');
xb = cat(1,xb,R2(q(ln,1))*pb(ln,:)' + q(ln,2:3)');
% right branch
xa = cat(1,xa,R2(q(par,1))*pa(par,:)' + q(par,2:3)');
xb = cat(1,xb,R2(q(rn,1))*pb(1,:)' + q(rn,2:3)');
end
end
% C(q) = 0
% Cdot(q) = dC/dq * qdot = 0
% Cddot(q) = dC/dq * qddot + dCdot/dq * qdot = 0
Csym = xa - xb;
% flatten everything to vectors
pa = reshape(pa',(numBod-1)*2,1);
pb = reshape(pb',(numBod-1)*2,1);
q = reshape(q',numBod*3,1);
qdot = reshape(qdot',numBod*3,1);
qddot = reshape(qddot',numBod*3,1);
Jsym = jacobian(Csym', q);
Cdot_sym = Jsym * qdot;
c_sym = jacobian(Cdot_sym, q) * qdot + ks * Csym + kd * Cdot_sym;
cfuncmod = matlabFunction(c_sym, 'Vars', {pa, pb, q, qdot});
Jfuncmod = matlabFunction(Jsym, 'Vars', {pa, pb, q});
%% Jacobian of aux constraint
% constrain the COM of the first joint
aend = R2(q(1)) * [pa(1); -pa(2)] + q(2:3);
a = root(2:3) + R2(root(1))*[0; -d];
Ca = aend - a;
Ja = jacobian(Ca, q);
Ca_dot = Ja * qdot;
ca_sym = jacobian(Ca_dot, q) * qdot + ks * Ca + kd * Ca_dot;
Ca_ddot = Ja * qddot + jacobian(Ca_dot, q) * qdot;
Jafunc = matlabFunction(Ja, 'Vars', {pa, pb, q, qdot});
ca_symfunc = matlabFunction(ca_sym, 'Vars', {pa, pb, q, qdot});
%% Time Integration
tall = 1:dt:time;
stps = time/dt;
I = m * (w * w + h * h) / 12.0;
M = eye(numBod*3)*m;
for i = 0:numBod-1
M(i*3+1,i*3+1) = I;
end
Asym = Jsym*inv(M)*Jsym';
Afunc = matlabFunction(Asym, 'Vars', {pa, pb, q});
% Give the positions of the joint that are being constrained
pa = zeros((numBod-1)*2*2,1);
pb = zeros((numBod-1)*2*2,1);
% NOTE: this is for a connection between each individual joint that forms a
% serial chain
for i = 0:(numBod-1)*2-1
if(rem(i, 2)==0)
pa(i*2+1:i*2+2) = [0; d];
pb(i*2+1:i*2+2) = [0; -d];
else
pa(i*2+1:i*2+2) = [0; -d];
pb(i*2+1:i*2+2) = [0; d];
end
end
%%
% make bodies
bodies = [];
constraints = [];
currparent = -1;
z = {};
% for tree structures
for i = 0:hTree % for every level of the tree
Mi = M(i*3+1:i*3+3, i*3+1:i*3+3);
if i==hTree % we are all the leaf of the tree
children = [];
for k = 1:2^(i)
ind = 2^i+k-1;
b = make_test_body(ind, Mi, numBod + ind -1, children);
bodies = [bodies b];
z = [z; zeros(3, 1)];
end
else % another level of the tree
for k = 1:2^i % for every block node
ind = 2^i+k-1;
children = [totBod + ind*2-1, totBod + ind*2];% there are two child constraints
cl = make_constraint(totBod + ind*2-1, zeros(2, 3), ind, ind*2);
cr = make_constraint(totBod + ind*2, zeros(2, 3), ind, ind*2+1);
constraints = [constraints cl cr];
currparent = numBod+ind-1;
if(i==0)
currparent = - 1;
end
b = make_test_body(ind, Mi, currparent, children);
bodies = [bodies b];
z = [z; zeros(3, 1)];
end
end
end
allnodes = [bodies constraints];
for i=1:size(constraints, 2)-1
z = [z; zeros(2, 1)];
end
%% Run
q = q0; % initial position of the system
qdot = zeros(numBod*3,1); % initial velocity of the system
% w vx vy ...
allConstrF = zeros(numBod*3,stps);
% external force only applies for an initial period of time
extF = zeros(numBod*3,stps);
for t=1:1
extF(:, t) = Fext;
end
if visualize == 1
vid = VideoWriter('video.avi');
open(vid);
figure(1)
hold on
xlim([-15 15])
ylim([-15 15])
grid on
end
Tsolve = zeros(size(tall, 2),1);
for i=1:size(tall, 2)
i
t = tall(i);
[allCOM, allBars, allax] = getAllBars(q,w,h,j,d,m);
if visualize == 1
visualizeAllBars(allCOM, allBars, allax);
end
% Get J, c, b for primary constraint
J = Jfuncmod(pa, pb, q);
c = cfuncmod(pa, pb, q, qdot);
b = -(J*inv(M)*extF(:, i) + c);
% (1) solve lambda for primary constraint
if(solverType == 1) % (1a) A\b NAIEVE SOLVE
tic
A = Afunc(pa, pb, q);
lambda = inv(A)*b;%A\b; %
Tsolve(i) = toc;
elseif(solverType == 2) % (1b) SPARSE SOLVE
for bi=0:size(constraints, 2)-1
par = floor(bi/2);
child = bi+1;
Jic = J(bi*2+1:bi*2+2, par*3+1:par*3+3);
Jip = J(bi*2+1:bi*2+2, child*3+1:child*3+3);
allnodes(numBod+bi+1).D = [Jic Jip];
z{numBod+bi+1} = -b(bi*2+1:bi*2+2);
end
tic
[H, forwards] = sparsefactor(allnodes);
ylamb = sparsesolve(H, z, allnodes, forwards);
Tsolve(i) = toc;
lambda = cell2mat(ylamb(size(bodies, 2)+1:end));
elseif(solverType == 3) % (1c) DENSE SOLVE
for bi=0:size(constraints, 2)-1
par = floor(bi/2);
child = bi+1;
Jic = J(bi*2+1:bi*2+2, par*3+1:par*3+3);
Jip = J(bi*2+1:bi*2+2, child*3+1:child*3+3);
allnodes(numBod+bi+1).D = [Jic Jip];
z{numBod+bi+1} = -b(bi*2+1:bi*2+2);
end
tic
[H, H_tree, forwards] = densefactor(allnodes);
ylamb = densesolve(H, z, forwards);
Tsolve(i) = toc;
lambda = cell2mat(ylamb(size(bodies, 2)+1:end));
end
if(auxConstraint == 1)
% no aux constraints
qddot = inv(M)*J'*lambda + inv(M)*extF(:, i); % (6x1) update the velocity
elseif(auxConstraint == 2)
% (2) evaluate Ja K ca
vdot_aux = inv(M) * (J'*lambda + extF(:, i));
Ja = Jafunc(pa, pb, q, qdot);
K = Ja';
ca = ca_symfunc(pa, pb, q, qdot);
MhatK = zeros(size(K));
if(solverType == 1)
% (3) solve for M_hat * K
for k=1:size(K, 2)
b = -J * inv(M) * K(:, k);
lambda = A\b;
MhatK(:, k) = inv(M) * (J' * lambda + K(:, k));
end
% (4) solve for mu (O (K^3))
mu = (Ja*MhatK) \ (a - Ja*vdot_aux - ca);
% (5) solve for primary response to Fext + K*mu
b = -(J*inv(M)*(K*mu + extF(:, i)) + c);
lambda = A\b;
% aux constraints
qddot = inv(M)*(K*mu + J'*lambda + extF(:, i)); % (6x1) update the velocity
else % Sparse or Dense Solve
% (3) solve for M_hat * K
MhatK = zeros(size(K));
for k=1:size(K, 2)
b = -J * inv(M) * K(:, k);
for bi=0:size(constraints, 2)-1
z{numBod+bi+1} = -b(bi*2+1:bi*2+2);
end
ylamb = sparsesolve(H, z, allnodes, forwards);
lambda = cell2mat(ylamb(size(bodies, 2)+1:end));
MhatK(:, k) = inv(M) * (J' * lambda + K(:, k));
end
% (4) solve for mu (O (K^3))
mu = (Ja*MhatK) \ (a - Ja*vdot_aux - ca);
% (5) solve for primary response to Fext + K*mu
b = -(J*inv(M)*(K*mu + extF(:, i)) + c);
for bi=0:size(constraints, 2)-1
z{numBod+bi+1} = -b(bi*2+1:bi*2+2);
end
ylamb = sparsesolve(H, z, allnodes, forwards);
lambda = cell2mat(ylamb(size(bodies, 2)+1:end));
end
% aux constraints
qddot = inv(M)*(K*mu + J'*lambda + extF(:, i)); % (6x1) update the velocity
end
allConstrF(:,i) = J'*lambda;
[q, qdot] = forwardeuler(q, qdot, qddot, dt);
if visualize == 1
frame = getframe(gcf);
writeVideo(vid,frame);
cla
end
end
time = sum(Tsolve)/size(tall, 2)
if visualize == 1
close(vid);
end