forked from petercorke/robotics-toolbox-matlab
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathurdfparse.m
276 lines (245 loc) · 9.56 KB
/
urdfparse.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
function urdf = urdfparse(filename)
root = xmlread(filename);
robot = root.getElementsByTagName('robot').item(0);
%% process all the materials elements
% - these are optional but we do them first because link elements
% reference them
materialNodes = getChildrenByTagName(robot, 'material');
materials = [];
materialNames = [];
if ~isempty(materialNodes)
for i = 1:length(materialNodes)
node = materialNodes(i);
materials(i).name = string(node.getAttribute('name'));
materials(i).rgba = str2num(node.getElementsByTagName('color').item(0).getAttribute('rgba'));
materials(i).node = node;
end
urdf.materials = materials;
materialNames = [materials.name];
end
%% process all the link elements
% create a vector of joint structures, fields are:
% name name of the joint
% type type of joint (fixed, continuous, prismatic)
% T SE3 object for pose of joint wrt link frame
% xyz position of joint wrt link frame
% rpy RPY angles of joint frame wrt link frame
% node reference to XML DOM node for this <joint>
% child link index for downstream link
% parent link index for downstream link
linkNodes = getChildrenByTagName(robot, 'link');
for i = 1:length(linkNodes)
node = linkNodes(i);
links(i).name = string(node.getAttribute('name'));
try
links(i).geometry = node.getElementsByTagName('geometry').item(0);
links(i).mesh = links(i).geometry.getElementsByTagName('mesh').item(0).getAttribute('filename');
end
try
links(i).material = find(strcmp((node.getElementsByTagName('material').item(0).getAttribute('name')), materialNames));
end
try
inertial = node.getElementsByTagName('inertial').item(0);
try
links(i).m = str2num(inertial.getElementsByTagName('mass').item(0).getAttribute('value'));
end
try
I = inertial.getElementsByTagName('inertia').item(0);
links(i).I.xx = str2num(I.getAttribute('ixx'));
links(i).I.xy = str2num(I.getAttribute('ixy'));
links(i).I.xz = str2num(I.getAttribute('ixz'));
links(i).I.yy = str2num(I.getAttribute('iyy'));
links(i).I.yz = str2num(I.getAttribute('iyz'));
links(i).I.zz = str2num(I.getAttribute('izz'));
end
try
t = str2num( node.getElementsByTagName('origin').item(0).getAttribute('xyz'));
rpy = str2num( node.getElementsByTagName('origin').item(0).getAttribute('rpy'));
links(i).com = SE3(t) * SE3.rpy(rpy);
end
end
t = [0 0 0]; rpy = [0 0 0]; % in case not provided
try
t = str2num( node.getElementsByTagName('origin').item(0).getAttribute('xyz'));
rpy = str2num( node.getElementsByTagName('origin').item(0).getAttribute('rpy'));
end
if isempty(t)
t = [0 0 0]; % in case the tag has no attribute
end
if isempty(rpy)
rpy = [0 0 0]; % in case the tag has no attribute
end
links(i).T = SE3(t) * SE3.rpy(rpy);
links(i).node = node;
links(i).children = [];
links(i).parent = [];
end
linkNames = [links.name]; % a list of link names
%% process all the joint elements
% create a vector of link structures, fields are are:
% name name of the link
% geometry reference to XML DOM node
% T SE3 object for origin of visual wrt link frame
% node reference to XML DOM node for this <link>
% children list of joint indexes attaching this link to its children
% parent index of joint attaching this link to its parent
jointNodes = getChildrenByTagName(robot, 'joint');
joints = [];
for i = 1:length(jointNodes)
node = jointNodes(i);
joints(i).name = string(node.getAttribute('name'));
joints(i).type = string(node.getAttribute('type'));
try
joints(i).axis = str2num(node.getElementsByTagName('axis').item(0).getAttribute('xyz'));
end
p = find(strcmp(node.getElementsByTagName('parent').item(0).getAttribute('link'), linkNames));
c = find(strcmp(node.getElementsByTagName('child').item(0).getAttribute('link'), linkNames));
joints(i).parent = p;
joints(i).child = c;
links(p).children = [links(p).children i];
links(c).parent = i;
t = [0 0 0]; rpy = [0 0 0]; % in case not provided
try
t = str2num( node.getElementsByTagName('origin').item(0).getAttribute('xyz'));
rpy = str2num( node.getElementsByTagName('origin').item(0).getAttribute('rpy'));
end
if isempty(t)
t = [0 0 0]; % in case the tag has no attribute
end
if isempty(rpy)
rpy = [0 0 0]; % in case the tag has no attribute
end
joints(i).T = SE3(t) * SE3.rpy(rpy);
joints(i).xyz = t;
joints(i).rpy = rpy;
joints(i).node = node;
end
urdf.joints = joints;
urdf.links = links;
urdf.endlinks = find( cellfun('isempty', {links.children}) );
urdf.ets = [];
%% create a transform list
% the mechanism might have multiple endpoints (if branched), for each
% endpoint compute an ETS chain and return a string array
for endlink = urdf.endlinks
transforms = "";
joint = joints(links(endlink).parent);
while true
transforms = xform2s(joint) + axis2s(joint) + transforms;
joint = joints(links(joint.parent).parent);
if isempty(joint)
break;
end
end
transforms = strtrim(transforms);
urdf.ets = [urdf.ets transforms];
end
%% display a summary
fprintf('\nLinks::\n');
for i=1:length(links)
name = links(i).name;
if ismember(i, urdf.endlinks)
name = "*" + name;
end
geomlist = "";
if ~isempty(links(i).geometry)
g = links(i).geometry.getChildNodes;
for k=0:g.getLength-1
c = g.item(k);
if c.getNodeType == 1 % skip text nodes
if geomlist == ""
geomlist = "shape=";
else
geomlist = geomlist + ", ";
end
geomlist = geomlist + string(c.getNodeName);
end
end
end
fprintf('%2d: %10s; parentjoint=%s, childjoints=%s; %s\n', i, name, ...
formatj(links(i).parent), formatj(links(i).children), geomlist);
end
if ~isempty(joints)
fprintf('\nJoints::\n');
for i=1:length(joints)
fprintf('%2d: %10s; type=%s, parentlink=%d, childlink=%d\n', i, joints(i).name, joints(i).type, joints(i).parent, joints(i).child);
end
end
if ~isempty(materials)
fprintf('\nMaterials::\n');
for i=1:length(materials)
fprintf('%10s: %f %f %f %f\n', materials(i).name, materials(i).rgba);
end
end
if strlength(transforms) > 0
fprintf('\nForward kinematics::\n %s\n', transforms);
end
end
function s = formatj(j)
if isempty(j)
s = '-';
else
s = strip(num2str(j, '%d,'), 'right', ',');
end
end
function t = xform2s(joint)
t = "";
if joint.xyz(1) ~= 0
t = t + sprintf('Tx(%.12g) ', joint.xyz(1));
end
if joint.xyz(2) ~= 0
t = t + sprintf('Ty(%.12g) ', joint.xyz(2));
end
if joint.xyz(3) ~= 0
t = t + sprintf('Tz(%.12g) ', joint.xyz(3));
end
if joint.rpy(3) ~= 0
t = t + sprintf('Rz(%.12g) ', round(joint.rpy(3)*180/pi, 3));
end
if joint.rpy(2) ~= 0
t = t + sprintf('Ry(%.12g) ', round(joint.rpy(2)*180/pi,3));
end
if joint.rpy(1) ~= 0
t = t + sprintf('Rx(%.12g) ', round(joint.rpy(1)*180/pi, 3));
end
end
function t = axis2s(joint)
t = "";
switch joint.type
case {'revolute', 'continuous'}
assert(length(find(joint.axis)) == 1, 'Joint axis must be about x, y or z only');
if joint.axis(1) ~= 0
t = t + sprintf('Rx(%s) ', joint.name);
end
if joint.axis(2) ~= 0
t = t + sprintf('Ry(%s) ', joint.name);
end
if joint.axis(3) ~= 0
t = t + sprintf('Rz(%s) ', joint.name);
end
case 'prismatic'
assert(length(find(joint.axis)) == 1, 'Joint axis must be about x, y or z only');
if joint.axis(1) ~= 0
t = t + sprintf('Tx(%s) ', joint.name);
end
if joint.axis(2) ~= 0
t = t + sprintf('Ty(%s) ', joint.name);
end
if joint.axis(3) ~= 0
t = t + sprintf('Tz(%s) ', joint.name);
end
case 'fixed'
end
end
function children = getChildrenByTagName(parent, name)
% return array of immediate child nodes with node name equal to name
% REF https://stackoverflow.com/questions/18776408/get-all-the-children-for-a-given-xml-in-java
children = [];
childnodes = parent.getChildNodes;
for i=1:childnodes.getLength
node = childnodes.item(i-1);
if node.getNodeType == node.ELEMENT_NODE && strcmp(node.getNodeName, name)
children = [children node];
end
end
end