forked from DeepakParamkusam/urdf2eom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
urdf2eomFD.m
78 lines (65 loc) · 2.21 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
68
69
70
71
72
73
74
75
76
77
78
function [forwardDynamicsFunction] = urdf2eomFD(file,geneate_c_code)
%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 = my_urdf2smds(file);
import casadi.*;
%Initialize variables
q = SX.sym('q',[1,smds.NB])';
qd = SX.sym('qd',[1,smds.NB])';
qdd = SX.sym('qdd', [smds.NB,1]);
tau = SX.sym('tau',[1,smds.NB])';
g = SX.sym('g',[3,1]);
%Gravity
a_grav = [0;0;0;g(1);g(2);g(3)];
%Articulated body algorithm
for i = 1:smds.NB
[ XJ, S{i} ] = jcalc( smds.jaxis{i},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} = smds.I{1,i};
pA{i} = crf(v{i}) * smds.I{1,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
% Define the symbolic function and set its input and output in poper order
forwardDynamicsFunction = Function('forwardDynamics',{q,qd,g,tau},{qdd},{'joints_position','joints_velocity','gravity','joints_torque'},{'joints_acceleration'});
%% Code generation option
if geneate_c_code
opts = struct('main', true,...
'mex', true);
forwardDynamicsFunction.generate('forwardDynamics.c',opts);
mex forwardDynamics.c -DMATLAB_MEX_FILE
% Test the function
a=forwardDynamics('forwardDynamics');
A=full(a);
if (A~=zeros(smds.NB,1))
error('The compiled smds returns non null torques for all null inputs (gravity included)');
end
end