-
Notifications
You must be signed in to change notification settings - Fork 448
/
URDF.m
231 lines (196 loc) · 7.53 KB
/
URDF.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
% Copyright (C) 1993-2017, by Peter I. Corke
%
% This file is part of The Robotics Toolbox for MATLAB (RTB).
%
% RTB is free software: you can redistribute it and/or modify
% it under the terms of the GNU Lesser General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% RTB is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU Lesser General Public License for more details.
%
% You should have received a copy of the GNU Leser General Public License
% along with RTB. If not, see <http://www.gnu.org/licenses/>.
%
% http://www.petercorke.com
classdef URDF
properties
joints
links
transmissions
props
attr
jseq
end
methods
function urdf = URDF(urdffile)
if nargin == 0
urdffile = 'katana_400_6m180.urdf.xacro';
end
dom = xmlread(urdffile);
links = dom.getElementsByTagName('link');
joints = dom.getElementsByTagName('joint');
properties = dom.getElementsByTagName('property');
robot = dom.getElementsByTagName('robot');
urdf.attr = urdf.getattr(robot.item(0))
% get the properties
fprintf('%d properties\n', properties.getLength);
Props = [];
for k = 0:properties.getLength-1
property = properties.item(k)
if property.hasAttributes
attributes = property.getAttributes;
att = [];
for i=1:attributes.getLength
attribute = attributes.item(i-1);
n = char(attribute.getName);
v = char(attribute.getValue);
att.(n) = v
end
Props = [ Props; att];
end
end
urdf.props = Props;
% get the joints
urdf.joints = URDF.get_elements(dom, 'joint', Props);
% get the links
urdf.links = URDF.get_elements(dom, 'link', Props);
% get the transmissions
urdf.transmissions = URDF.get_elements(dom, 'transmission', Props);
p = zeros(1, length(urdf.links));
for j=1:length(urdf.joints)
i = urdf.ln2i( urdf.joints{j}.parent.link);
p(i) = p(i) + 1;
i = urdf.ln2i( urdf.joints{j}.child.link);
p(i) = p(i) - 1;
end
[~,base_link] = max(p);
base_link_name = urdf.links{base_link}.name;
link = base_link_name;
for j=1:length(urdf.joints)
jj = urdf.findnextjoint(link);
if isempty(jj)
break;
end
urdf.jseq(j) = jj;
link = urdf.joints{jj}.child.link;
end
end
function r = robot(urdf)
for i=1:urdf.njoints
% create link objects
% attach the STL model to them
end
r = SerialLink(links, 'name', urdf.attr.name);
end
function n = nlinks(urdf)
n = length(urdf.links);
end
function n = njoints(urdf)
n = length(urdf.joints);
end
% map joint/link name to an index
function joint = findnextjoint(urdf, link)
joint = [];
for j=1:length(urdf.joints)
if strcmp(link, urdf.joints{j}.parent.link)
joint = j;
return
end
end
end
function idx = ln2i(urdf, name)
idx = [];
for i=1:length(urdf.links)
if strcmp(urdf.links{i}.name, name)
idx = i;
return
end
end
end
function display(urdf)
for j=1:length(urdf.joints)
joint = urdf.joints{j};
fprintf('j%d: %s -> %s (%s)\n', j, joint.parent.link, joint.child.link, joint.type);
end
end
end
methods (Static)
function List = get_elements(doc, elname, props)
elements = doc.getElementsByTagName(elname);
% get the links
List = {};
fprintf('%d %s\n', elements.getLength, elname);
% step through the list of elements found
for k = 1:elements.getLength
element = elements.item(k-1);
info = URDF.descend(element, props);
info = URDF.getattr(element, info, props);
List{k} = info;
end
end
function t = descend(node, props)
if node.hasChildNodes
t = [];
nodeChildren = node.getChildNodes;
for i=1:nodeChildren.getLength
child = nodeChildren.item(i-1);
if child.getNodeType == 3
continue;
end
result = URDF.descend(child, props);
if ~isempty(result)
n = char(child.getNodeName);
t.(n) = result;
end
end
elseif node.hasAttributes
t = URDF.getattr(node, [], props);
else
t = [];
end
end
function att = getattr(node, att, props)
if nargin < 2
att = [];
end
if nargin < 3
props = [];
end
attributes = node.getAttributes;
for i=1:attributes.getLength
attribute = attributes.item(i-1);
n = char(attribute.getName);
v = char(attribute.getValue);
% skip properties with colon in them
n = strrep(n, ':', '_');
%do simple xacro type substitution
%xyz="0 0 ${-base_height}"
if ~isempty(props)
% is there a ${...} substring
[s,e] = regexp(v, '\${[^}]*');
if ~isempty(s)
macro = v(s+2:e);
%fprintf('XACRO: %s --> ', v);
% if yes, then regexprep replace within it for every property in the list
for k=1:length(props)
macro = strrep(macro, props(k).name, props(k).value);
end
% stick the macro back into the original string
macro = strrep(macro, ' ', ''); % remove all blanks
v = [v(1:s-1) macro v(e+2:end)];
%fprintf('%s\n', v);
end
end
% attempt to convert it to a scalar or vector, fallback is a string
att.(n) = str2num(v);
if isempty( att.(n) )
att.(n) = v;
end
end
end
end
end