-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathID_derivatives.m
101 lines (77 loc) · 2.32 KB
/
ID_derivatives.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
function [dtau_dq, dtau_dqd] = ID_derivatives( model, q, qd, qdd )
if ~isfield(model,'nq')
model = postProcessModel(model);
end
if ~iscell(q) || ~iscell(qd) || ~iscell(qdd)
[q, qd, qdd] = confVecToCell(model,q,qd,qdd);
end
if sum(model.has_rotor) > 1
error('ID_derivatives does not support rotors');
end
a_grav = get_gravity(model);
IC = model.I;
I = model.I;
for i = 1:model.NB
[ XJ, S{i} ] = jcalc( model.jtype{i}, q{i} );
Xup{i} = XJ * model.Xtree{i};
if model.parent(i) == 0
v{i} = zeros(6,1);
a{i} = -a_grav;
Xup0{i} = Xup{i};
else
Xup0{i} = Xup{i}*Xup0{model.parent(i)};
v{i} = v{model.parent(i)};
a{i} = a{model.parent(i)};
end
Xdown0{i} = inv(Xup0{i});
S{i} = Xdown0{i}*S{i};
vJ{i}= S{i}*qd{i};
aJ{i} = crm(v{i})*vJ{i} + S{i}*qdd{i};
Sd{i} = crm(v{i})*S{i};
Sdd{i}= crm(a{i})*S{i} + crm(v{i})*Sd{i};
Sj{i} = 2*Sd{i} + crm(vJ{i})*S{i};
v{i} = v{i} + vJ{i};
a{i} = a{i} + aJ{i};
IC{i} = Xup0{i}.'*I{i}*Xup0{i};
BC{i} = 2*factorFunctions(IC{i},v{i});
f{i} = IC{i}*a{i} + crf(v{i})*IC{i}*v{i};
end
dtau_dq = q{1}(1)*0 + zeros(model.NV,model.NV);
dtau_dqd = q{1}(1)*0 + zeros(model.NV,model.NV);
J = myCell2Mat(model,S);
Jd = myCell2Mat(model,Sd);
Jdd= myCell2Mat(model,Sdd);
Jj = myCell2Mat(model,Sj);
tmp1 = repmat(0*q{1}(1) , 6, model.NV);
tmp2 = repmat(0*q{1}(1) , 6, model.NV);
tmp3 = repmat(0*q{1}(1) , 6, model.NV);
tmp4 = repmat(0*q{1}(1) , 6, model.NV);
dtau_dq = repmat(0*q{1}(1), model.NV,model.NV);
dtau_dqd = repmat(0*q{1}(1), model.NV,model.NV);
for i = model.NB:-1:1
ii = model.vinds{i};
tmp1(:,ii) = IC{i}*S{i};
tmp2(:,ii) = BC{i}*S{i} + IC{i}*Sj{i};
tmp3(:,ii) = BC{i}*Sd{i} + IC{i}*Sdd{i} + icrf(f{i})*S{i};
tmp4(:,ii) = BC{i}.'*S{i};
jj = model.subtree_vinds{i};
dtau_dq(ii,jj) = J(:,ii).'*tmp3(:,jj);
dtau_dq(jj,ii) = tmp1(:,jj).'*Jdd(:,ii)+tmp4(:,jj).'*Jd(:,ii);
dtau_dqd(ii,jj) = J(:,ii).'*tmp2(:,jj);
dtau_dqd(jj,ii) = tmp1(:,jj).'*Jj(:,ii)+tmp4(:,jj).'*J(:,ii);
if model.parent(i) > 0
p = model.parent(i);
IC{p} = IC{p} + IC{i};
BC{p} = BC{p} + BC{i};
f{p} = f{p} + f{i};
end
end
end
function M = myCell2Mat(model,S)
% import casadi.*
% M = SX.zeros(6,model.NV);
% for i = 1:length(S)
% M(:,model.vinds{i}) = S{i};
% end
M = cell2mat(S);
end