-
Notifications
You must be signed in to change notification settings - Fork 7
/
urdf2eomFD.m
67 lines (59 loc) · 1.63 KB
/
urdf2eomFD.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
function [qdd] = urdf2eomFD(file,simplifyflag)
%Generates equation of motion in symbolic form from urdf file
%Based on articulated body forward dynamics code by Roy Featherstone, 2015
%http://royfeatherstone.org/spatial/v2/index.html
%Load urdf and convert to SMDS format
smds = urdf2smds(file);
%Initialize variables
q = sym('q',[1,smds.NB],'real')';
qd = sym('qd',[1,smds.NB],'real')';
tau = sym('tau',[1,smds.NB],'real')';
syms g;
%Gravity
a_grav = [0;0;0;0;0;g];
I = smds.I;
%Articulated body algorithm
for i = 1:smds.NB
[ XJ, S{i} ] = jcalc( smds.jtype{i}, q(i) );
vJ = S{i}*qd(i);
Xup{i} = XJ * smds.Xtree{i};
if smds.parent(i) == 0
v{i} = vJ;
c{i} = zeros(size(a_grav));
else
v{i} = Xup{i}*v{smds.parent(i)} + vJ;
c{i} = crm(v{i}) * vJ;
end
IA{i} = I(:,:,i);
pA{i} = crf(v{i}) * I(:,:,i) * v{i};
end
for i = smds.NB:-1:1
U{i} = IA{i} * S{i};
d{i} = S{i}' * U{i};
u{i} = tau(i) - S{i}'*pA{i};
if smds.parent(i) ~= 0
Ia = IA{i} - U{i}/d{i}*U{i}';
pa = pA{i} + Ia*c{i} + U{i} * u{i}/d{i};
IA{smds.parent(i)} = IA{smds.parent(i)} + Xup{i}' * Ia * Xup{i};
pA{smds.parent(i)} = pA{smds.parent(i)} + Xup{i}' * pa;
end
end
for i = 1:smds.NB
if smds.parent(i) == 0
a{i} = Xup{i} * -a_grav + c{i};
else
a{i} = Xup{i} * a{smds.parent(i)} + c{i};
end
qdd(i,1) = (u{i} - U{i}'*a{i})/d{i};
a{i} = a{i} + S{i}*qdd(i);
end
if simplifyflag == 1
qdd = simplify(expand(qdd));
end
%Write to file
file = fopen('qdd.txt', 'w');
for i = 1:smds.NB
fprintf(file, '%s\r\n\n', char(qdd(i)));
end
fclose(file);
end