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 | def load(path_or_xml, *, base_link: str | None = None, ee_link: str | None = None) -> Robot:
"""Build a Robot from a URDF file path or a URDF string.
The chain runs from base_link (default: the one link that is nobody's child) to ee_link
(default: the one link that is nobody's parent). Each moving joint's <origin> is
T_{i-1,i}(0); its <axis> in its own frame is A_i = (axis, 0) or (0, axis); the screw
axis in {s} is S_i = [Ad_{T_0i(0)}] A_i, and M is the product carried through the
fixed joints to ee_link. Fixed joints add no column. <axis> is normalised. <limit>
bounds are recorded; a joint without them (e.g. continuous) gets (-inf, inf) when any
other joint has limits.
Inertia follows MR 8.3: link frame {i} sits at link i's centre of mass with the
<inertial><origin> axes, so G_i = diag(I_b, m I); link_frames[i-1] is M_{i-1,i} and
link_frames[n] carries the last link frame to {b}. A moving link without <inertial>
leaves link_frames and link_inertias as None (a kinematic-only robot).
Notes 4.3; MR 4.2, 8.3.
"""
text = str(path_or_xml)
if "<robot" in text:
root = ET.fromstring(text)
else:
if not os.path.exists(text):
raise FileNotFoundError(f"no URDF at {text}")
root = ET.parse(text).getroot()
if root.tag != "robot":
raise ValueError(f"expected a <robot> root element, got <{root.tag}>")
joints = [_parse_joint(j) for j in root.findall("joint")]
inertials = {link.get("name"): _parse_inertial(link) for link in root.findall("link")}
by_parent: dict[str, list[_Joint]] = {}
parents, children = set(), set()
for j in joints:
by_parent.setdefault(j.parent, []).append(j)
parents.add(j.parent)
children.add(j.child)
roots = sorted(parents - children)
leaves = sorted(children - parents)
base = _pick("base link", roots, base_link)
ee = _pick("ee link", leaves, ee_link)
# Walk from base to ee along the unique parent->child path.
path: list[_Joint] = []
link = base
seen = set()
while link != ee:
if link in seen:
raise ValueError("the URDF's joint graph has a cycle")
seen.add(link)
nxt = [j for j in by_parent.get(link, []) if _reaches(j.child, ee, by_parent)]
if not nxt:
raise ValueError(f"no chain of joints from {base!r} to {ee!r}")
path.append(nxt[0])
link = nxt[0].child
T = np.eye(4)
S_cols, types, names, limits, joint_frames = [], [], [], [], []
moving_links: list[tuple[str, np.ndarray]] = [] # (child link, T_0i(0))
for j in path:
T = T @ j.origin
if j.type == "fixed":
continue
joint_frames.append(T.copy())
if j.type in _REVOLUTE:
S_cols.append(adjoint(T) @ revolute_axis(np.zeros(3), j.axis))
types.append("revolute")
else:
S_cols.append(adjoint(T) @ prismatic_axis(j.axis))
types.append("prismatic")
names.append(j.name)
limits.append(j.limits)
moving_links.append((j.child, T.copy()))
M = T
n = len(S_cols)
S = np.column_stack(S_cols) if n else np.zeros((6, 0))
joint_limits = None
if n and any(lim is not None for lim in limits):
joint_limits = np.array(
[lim if lim is not None else (-np.inf, np.inf) for lim in limits], dtype=float
)
link_frames = link_inertias = None
if n and all(inertials.get(name) is not None for name, _ in moving_links):
com_frames = [T0i @ inertials[name].origin for name, T0i in moving_links]
link_frames, link_inertias = [], []
prev = np.eye(4)
for F, (name, _) in zip(com_frames, moving_links):
link_frames.append(transform_inv(prev) @ F)
inert = inertials[name]
G = np.zeros((6, 6))
G[:3, :3] = inert.inertia
G[3:, 3:] = inert.mass * np.eye(3)
link_inertias.append(G)
prev = F
link_frames.append(transform_inv(prev) @ M)
return Robot(
name=root.get("name", "robot"),
M=M,
S=S,
joint_types=tuple(types),
joint_names=tuple(names),
joint_limits=joint_limits,
joint_frames_home=tuple(joint_frames),
link_frames=None if link_frames is None else tuple(link_frames),
link_inertias=None if link_inertias is None else tuple(link_inertias),
)
|