dynibo 0.1.0__py3-none-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
dynibo/__init__.py ADDED
@@ -0,0 +1,26 @@
1
+ """Python interface to the dynibo robot kinematics and dynamics library."""
2
+
3
+ from ._native import (
4
+ DyniboError,
5
+ IkOptions,
6
+ Load,
7
+ ModelError,
8
+ PanicError,
9
+ Pose,
10
+ Robot,
11
+ SolverError,
12
+ Twist,
13
+ )
14
+
15
+ __all__ = [
16
+ "DyniboError",
17
+ "IkOptions",
18
+ "Load",
19
+ "ModelError",
20
+ "PanicError",
21
+ "Pose",
22
+ "Robot",
23
+ "SolverError",
24
+ "Twist",
25
+ ]
26
+ __version__ = "0.1.0"
dynibo/_native.py ADDED
@@ -0,0 +1,379 @@
1
+ """Dependency-free ctypes wrapper around dynibo's stable C ABI."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import ctypes as ct
6
+ import ctypes.util
7
+ import os
8
+ import sys
9
+ from dataclasses import dataclass
10
+ from pathlib import Path
11
+ from typing import Iterable, Sequence
12
+
13
+
14
+ def _library_name() -> str:
15
+ if sys.platform == "win32":
16
+ return "dynibo_c.dll"
17
+ if sys.platform == "darwin":
18
+ return "libdynibo_c.dylib"
19
+ return "libdynibo_c.so"
20
+
21
+
22
+ def _load_library() -> ct.CDLL:
23
+ candidates = []
24
+ override = os.environ.get("DYNIBO_LIBRARY_PATH")
25
+ if override:
26
+ candidates.append(override)
27
+ candidates.append(str(Path(__file__).with_name(_library_name())))
28
+ system = ctypes.util.find_library("dynibo_c")
29
+ if system:
30
+ candidates.append(system)
31
+ errors = []
32
+ for candidate in candidates:
33
+ try:
34
+ return ct.CDLL(candidate)
35
+ except OSError as error:
36
+ errors.append(f"{candidate}: {error}")
37
+ raise ImportError("could not load dynibo native library:\n" + "\n".join(errors))
38
+
39
+
40
+ class _Pose(ct.Structure):
41
+ _fields_ = [("translation", ct.c_double * 3), ("rotation_xyzw", ct.c_double * 4)]
42
+
43
+
44
+ class _Twist(ct.Structure):
45
+ _fields_ = [("angular", ct.c_double * 3), ("linear", ct.c_double * 3)]
46
+
47
+
48
+ class _Load(ct.Structure):
49
+ _fields_ = [
50
+ ("link_id", ct.c_size_t),
51
+ ("torque", ct.c_double * 3),
52
+ ("force", ct.c_double * 3),
53
+ ]
54
+
55
+
56
+ class _IkOptions(ct.Structure):
57
+ _fields_ = [
58
+ ("max_iterations", ct.c_size_t),
59
+ ("translation_tolerance", ct.c_double),
60
+ ("rotation_tolerance", ct.c_double),
61
+ ("damping", ct.c_double),
62
+ ("max_step_norm", ct.c_double),
63
+ ]
64
+
65
+
66
+ @dataclass(frozen=True)
67
+ class Pose:
68
+ """A translation and an `(x, y, z, w)` unit quaternion."""
69
+
70
+ translation: tuple[float, float, float] = (0.0, 0.0, 0.0)
71
+ rotation_xyzw: tuple[float, float, float, float] = (0.0, 0.0, 0.0, 1.0)
72
+
73
+
74
+ @dataclass(frozen=True)
75
+ class Twist:
76
+ """Angular-first spatial velocity or acceleration."""
77
+
78
+ angular: tuple[float, float, float] = (0.0, 0.0, 0.0)
79
+ linear: tuple[float, float, float] = (0.0, 0.0, 0.0)
80
+
81
+
82
+ @dataclass(frozen=True)
83
+ class Load:
84
+ """A link-local external wrench."""
85
+
86
+ link_id: int
87
+ torque: tuple[float, float, float] = (0.0, 0.0, 0.0)
88
+ force: tuple[float, float, float] = (0.0, 0.0, 0.0)
89
+
90
+
91
+ @dataclass(frozen=True)
92
+ class IkOptions:
93
+ """Damped-least-squares inverse-kinematics configuration."""
94
+
95
+ max_iterations: int = 100
96
+ translation_tolerance: float = 1.0e-6
97
+ rotation_tolerance: float = 1.0e-6
98
+ damping: float = 1.0e-3
99
+ max_step_norm: float = 0.5
100
+
101
+
102
+ class DyniboError(RuntimeError):
103
+ """Base class for native dynibo model and calculation errors."""
104
+
105
+
106
+ class ModelError(DyniboError):
107
+ """A robot description could not be loaded or represented."""
108
+
109
+
110
+ class SolverError(DyniboError):
111
+ """An iterative numerical calculation did not produce a valid result."""
112
+
113
+
114
+ class PanicError(DyniboError):
115
+ """The native library caught an unexpected internal panic."""
116
+
117
+
118
+ _lib = _load_library()
119
+ _robot_p = ct.c_void_p
120
+ _workspace_p = ct.c_void_p
121
+ _double_p = ct.POINTER(ct.c_double)
122
+
123
+ _lib.dynibo_last_error_message.restype = ct.c_char_p
124
+ _lib.dynibo_version.restype = ct.c_char_p
125
+ _lib.dynibo_robot_load_urdf.argtypes = [ct.c_char_p, ct.POINTER(_robot_p)]
126
+ _lib.dynibo_robot_load_urdf.restype = ct.c_int
127
+ _lib.dynibo_robot_destroy.argtypes = [_robot_p]
128
+ _lib.dynibo_robot_name.argtypes = [_robot_p]
129
+ _lib.dynibo_robot_name.restype = ct.c_char_p
130
+ _lib.dynibo_robot_joint_count.argtypes = [_robot_p]
131
+ _lib.dynibo_robot_joint_count.restype = ct.c_size_t
132
+ _lib.dynibo_robot_link_count.argtypes = [_robot_p]
133
+ _lib.dynibo_robot_link_count.restype = ct.c_size_t
134
+ _lib.dynibo_robot_link_id.argtypes = [_robot_p, ct.c_char_p, ct.POINTER(ct.c_size_t)]
135
+ _lib.dynibo_robot_link_id.restype = ct.c_int
136
+ _lib.dynibo_workspace_create.argtypes = [_robot_p, ct.POINTER(_workspace_p)]
137
+ _lib.dynibo_workspace_create.restype = ct.c_int
138
+ _lib.dynibo_workspace_destroy.argtypes = [_workspace_p]
139
+ _lib.dynibo_forward_kinematics.argtypes = [
140
+ _robot_p, _workspace_p, _double_p, ct.c_size_t, ct.c_size_t, ct.POINTER(_Pose)
141
+ ]
142
+ _lib.dynibo_forward_kinematics.restype = ct.c_int
143
+ _lib.dynibo_jacobian.argtypes = [
144
+ _robot_p, _workspace_p, _double_p, ct.c_size_t, ct.c_size_t,
145
+ _double_p, ct.c_size_t,
146
+ ]
147
+ _lib.dynibo_jacobian.restype = ct.c_int
148
+ _lib.dynibo_inverse_kinematics.argtypes = [
149
+ _robot_p, _workspace_p, _double_p, ct.c_size_t, ct.c_size_t,
150
+ ct.POINTER(_Pose), _IkOptions, _double_p, ct.c_size_t,
151
+ ]
152
+ _lib.dynibo_inverse_kinematics.restype = ct.c_int
153
+ _lib.dynibo_forward_velocity.argtypes = [
154
+ _robot_p, _workspace_p, _double_p, _double_p, ct.c_size_t, ct.c_size_t,
155
+ ct.POINTER(_Pose), ct.POINTER(_Pose), ct.POINTER(_Twist),
156
+ ]
157
+ _lib.dynibo_forward_velocity.restype = ct.c_int
158
+ _lib.dynibo_forward_acceleration.argtypes = [
159
+ _robot_p, _workspace_p, _double_p, _double_p, _double_p,
160
+ ct.c_size_t, ct.c_size_t, ct.POINTER(_Twist),
161
+ ]
162
+ _lib.dynibo_forward_acceleration.restype = ct.c_int
163
+ _lib.dynibo_gravity.argtypes = [
164
+ _robot_p, _workspace_p, _double_p, ct.c_size_t, ct.POINTER(_Pose),
165
+ ct.POINTER(_Load), ct.c_size_t, _double_p, ct.c_size_t,
166
+ ]
167
+ _lib.dynibo_gravity.restype = ct.c_int
168
+ _lib.dynibo_inverse_dynamics.argtypes = [
169
+ _robot_p, _workspace_p, _double_p, _double_p, _double_p, ct.c_size_t,
170
+ ct.POINTER(_Pose), _Twist, _Twist, ct.POINTER(_Load), ct.c_size_t,
171
+ _double_p, ct.c_size_t,
172
+ ]
173
+ _lib.dynibo_inverse_dynamics.restype = ct.c_int
174
+
175
+
176
+ def _check(status: int) -> None:
177
+ if status != 0:
178
+ raw_message = _lib.dynibo_last_error_message()
179
+ message = raw_message.decode("utf-8", "replace") if raw_message else "unknown dynibo error"
180
+ if status == 1:
181
+ raise ValueError(message)
182
+ if status == 2:
183
+ raise ModelError(message)
184
+ if status == 3:
185
+ raise PanicError(message)
186
+ if status == 4:
187
+ raise SolverError(message)
188
+ raise DyniboError(message)
189
+
190
+
191
+ def _array(values: Sequence[float], name: str) -> ct.Array[ct.c_double]:
192
+ try:
193
+ return (ct.c_double * len(values))(*(float(value) for value in values))
194
+ except (TypeError, ValueError) as error:
195
+ raise TypeError(f"{name} must be a finite-sized sequence of numbers") from error
196
+
197
+
198
+ def _fixed_array(values: Sequence[float], length: int, name: str) -> ct.Array[ct.c_double]:
199
+ if len(values) != length:
200
+ raise ValueError(f"{name} must contain exactly {length} elements")
201
+ return _array(values, name)
202
+
203
+
204
+ def _require_same_length(q: Sequence[float], **states: Sequence[float]) -> None:
205
+ for name, values in states.items():
206
+ if len(values) != len(q):
207
+ raise ValueError(f"q and {name} must have the same length")
208
+
209
+
210
+ def _pose(value: Pose) -> _Pose:
211
+ return _Pose(
212
+ _fixed_array(value.translation, 3, "pose translation"),
213
+ _fixed_array(value.rotation_xyzw, 4, "pose quaternion"),
214
+ )
215
+
216
+
217
+ def _twist(value: Twist) -> _Twist:
218
+ return _Twist(
219
+ _fixed_array(value.angular, 3, "twist angular"),
220
+ _fixed_array(value.linear, 3, "twist linear"),
221
+ )
222
+
223
+
224
+ def _loads(values: Iterable[Load]) -> ct.Array[_Load]:
225
+ values = tuple(values)
226
+ return (_Load * len(values))(*(
227
+ _Load(
228
+ value.link_id,
229
+ _fixed_array(value.torque, 3, "load torque"),
230
+ _fixed_array(value.force, 3, "load force"),
231
+ )
232
+ for value in values
233
+ ))
234
+
235
+
236
+ class Robot:
237
+ """A URDF robot with one reusable, non-thread-safe calculation workspace."""
238
+
239
+ def __init__(self, urdf_path: str | os.PathLike[str]):
240
+ self._robot = _robot_p()
241
+ self._workspace = _workspace_p()
242
+ _check(_lib.dynibo_robot_load_urdf(os.fsencode(urdf_path), ct.byref(self._robot)))
243
+ try:
244
+ _check(_lib.dynibo_workspace_create(self._robot, ct.byref(self._workspace)))
245
+ except Exception:
246
+ _lib.dynibo_robot_destroy(self._robot)
247
+ self._robot = _robot_p()
248
+ raise
249
+
250
+ def close(self) -> None:
251
+ """Release native resources; calling this more than once is safe."""
252
+ if self._workspace:
253
+ _lib.dynibo_workspace_destroy(self._workspace)
254
+ self._workspace = _workspace_p()
255
+ if self._robot:
256
+ _lib.dynibo_robot_destroy(self._robot)
257
+ self._robot = _robot_p()
258
+
259
+ def __enter__(self) -> "Robot":
260
+ return self
261
+
262
+ def __exit__(self, *_: object) -> None:
263
+ self.close()
264
+
265
+ def __del__(self) -> None:
266
+ self.close()
267
+
268
+ @property
269
+ def name(self) -> str:
270
+ value = _lib.dynibo_robot_name(self._robot)
271
+ if value is None:
272
+ raise RuntimeError("robot is closed")
273
+ return value.decode("utf-8")
274
+
275
+ @property
276
+ def joint_count(self) -> int:
277
+ return int(_lib.dynibo_robot_joint_count(self._robot))
278
+
279
+ @property
280
+ def link_count(self) -> int:
281
+ return int(_lib.dynibo_robot_link_count(self._robot))
282
+
283
+ def link_id(self, name: str) -> int:
284
+ result = ct.c_size_t()
285
+ _check(_lib.dynibo_robot_link_id(self._robot, name.encode(), ct.byref(result)))
286
+ return int(result.value)
287
+
288
+ def forward_kinematics(self, q: Sequence[float], target: int) -> Pose:
289
+ q_array = _array(q, "q")
290
+ output = _Pose()
291
+ _check(_lib.dynibo_forward_kinematics(
292
+ self._robot, self._workspace, q_array, len(q), target, ct.byref(output)
293
+ ))
294
+ return Pose(tuple(output.translation), tuple(output.rotation_xyzw))
295
+
296
+ def jacobian(self, q: Sequence[float], target: int) -> tuple[float, ...]:
297
+ """Return a flat, column-major `6 x joint_count` Jacobian."""
298
+ q_array = _array(q, "q")
299
+ output = (ct.c_double * (6 * self.joint_count))()
300
+ _check(_lib.dynibo_jacobian(
301
+ self._robot, self._workspace, q_array, len(q), target,
302
+ output, len(output),
303
+ ))
304
+ return tuple(output)
305
+
306
+ def inverse_kinematics(
307
+ self, initial_q: Sequence[float], target: int, desired: Pose,
308
+ options: IkOptions = IkOptions(),
309
+ ) -> tuple[float, ...]:
310
+ q_array = _array(initial_q, "initial_q")
311
+ desired_c = _pose(desired)
312
+ options_c = _IkOptions(
313
+ options.max_iterations, options.translation_tolerance,
314
+ options.rotation_tolerance, options.damping, options.max_step_norm,
315
+ )
316
+ output = (ct.c_double * self.joint_count)()
317
+ _check(_lib.dynibo_inverse_kinematics(
318
+ self._robot, self._workspace, q_array, len(initial_q), target,
319
+ ct.byref(desired_c), options_c, output, len(output),
320
+ ))
321
+ return tuple(output)
322
+
323
+ def forward_velocity(
324
+ self, q: Sequence[float], qd: Sequence[float], target: int,
325
+ base: Pose = Pose(), tool: Pose = Pose(),
326
+ ) -> Twist:
327
+ _require_same_length(q, qd=qd)
328
+ q_array, qd_array = _array(q, "q"), _array(qd, "qd")
329
+ base_c, tool_c, output = _pose(base), _pose(tool), _Twist()
330
+ _check(_lib.dynibo_forward_velocity(
331
+ self._robot, self._workspace, q_array, qd_array, len(q), target,
332
+ ct.byref(base_c), ct.byref(tool_c), ct.byref(output),
333
+ ))
334
+ return Twist(tuple(output.angular), tuple(output.linear))
335
+
336
+ def forward_acceleration(
337
+ self, q: Sequence[float], qd: Sequence[float],
338
+ qdd: Sequence[float], target: int,
339
+ ) -> Twist:
340
+ _require_same_length(q, qd=qd, qdd=qdd)
341
+ q_array = _array(q, "q")
342
+ qd_array = _array(qd, "qd")
343
+ qdd_array = _array(qdd, "qdd")
344
+ output = _Twist()
345
+ _check(_lib.dynibo_forward_acceleration(
346
+ self._robot, self._workspace, q_array, qd_array, qdd_array,
347
+ len(q), target, ct.byref(output),
348
+ ))
349
+ return Twist(tuple(output.angular), tuple(output.linear))
350
+
351
+ def gravity(
352
+ self, q: Sequence[float], base: Pose = Pose(),
353
+ loads: Iterable[Load] = (),
354
+ ) -> tuple[float, ...]:
355
+ q_array, base_c, loads_c = _array(q, "q"), _pose(base), _loads(loads)
356
+ output = (ct.c_double * self.joint_count)()
357
+ _check(_lib.dynibo_gravity(
358
+ self._robot, self._workspace, q_array, len(q), ct.byref(base_c),
359
+ loads_c, len(loads_c), output, len(output),
360
+ ))
361
+ return tuple(output)
362
+
363
+ def inverse_dynamics(
364
+ self, q: Sequence[float], qd: Sequence[float], qdd: Sequence[float],
365
+ base: Pose = Pose(), base_velocity: Twist = Twist(),
366
+ base_acceleration: Twist = Twist(), loads: Iterable[Load] = (),
367
+ ) -> tuple[float, ...]:
368
+ _require_same_length(q, qd=qd, qdd=qdd)
369
+ q_array = _array(q, "q")
370
+ qd_array = _array(qd, "qd")
371
+ qdd_array = _array(qdd, "qdd")
372
+ base_c, loads_c = _pose(base), _loads(loads)
373
+ output = (ct.c_double * self.joint_count)()
374
+ _check(_lib.dynibo_inverse_dynamics(
375
+ self._robot, self._workspace, q_array, qd_array, qdd_array, len(q),
376
+ ct.byref(base_c), _twist(base_velocity), _twist(base_acceleration),
377
+ loads_c, len(loads_c), output, len(output),
378
+ ))
379
+ return tuple(output)
dynibo/dynibo_c.dll ADDED
Binary file
@@ -0,0 +1,41 @@
1
+ Metadata-Version: 2.4
2
+ Name: dynibo
3
+ Version: 0.1.0
4
+ Summary: Python bindings for tree-structured robot kinematics and dynamics
5
+ Home-page: https://github.com/xiaojie-xue/dynibo
6
+ Author: Xiaojie Xue
7
+ License: MIT
8
+ Project-URL: Homepage, https://github.com/xiaojie-xue/dynibo
9
+ Project-URL: Repository, https://github.com/xiaojie-xue/dynibo
10
+ Keywords: robotics,kinematics,dynamics,urdf
11
+ Classifier: Development Status :: 3 - Alpha
12
+ Classifier: License :: OSI Approved :: MIT License
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Rust
15
+ Classifier: Topic :: Scientific/Engineering
16
+ Requires-Python: >=3.9
17
+ Description-Content-Type: text/markdown
18
+ License-File: LICENSE
19
+ Dynamic: home-page
20
+ Dynamic: license-file
21
+ Dynamic: requires-python
22
+
23
+ # dynibo
24
+
25
+ Python bindings for the `dynibo` tree-structured robot kinematics and dynamics
26
+ library. The wheel bundles the Rust native library and has no runtime Python
27
+ dependencies.
28
+
29
+ ```python
30
+ from dynibo import Robot
31
+
32
+ with Robot("robot.urdf") as robot:
33
+ tool = robot.link_id("tool")
34
+ q = [0.0] * robot.joint_count
35
+ pose = robot.forward_kinematics(q, tool)
36
+ jacobian = robot.jacobian(q, tool)
37
+ gravity = robot.gravity(q)
38
+ ```
39
+
40
+ The Jacobian is a flat column-major `6 x N` tuple. With NumPy, convert it using
41
+ `np.asarray(jacobian).reshape((6, robot.joint_count), order="F")`.
@@ -0,0 +1,8 @@
1
+ dynibo/__init__.py,sha256=ivcDsGx56N1tCdsoRrPrHKXD92an9OQ57chkyrdWSzc,429
2
+ dynibo/_native.py,sha256=Qrj_VpbFm4QlyxK9a1Vjvk0KpQ-SaDdgA5eVVTin_tU,13487
3
+ dynibo/dynibo_c.dll,sha256=eIPd-XTSY-FAjPweUNCFBHyOj69mQ5v2SmcB-FttcOM,733184
4
+ dynibo-0.1.0.dist-info/licenses/LICENSE,sha256=rwYXTB7H_1j0QnQKrx_MQBiHtfqJSWkWrIO8OpGyijo,1097
5
+ dynibo-0.1.0.dist-info/METADATA,sha256=7LN1mOz4m0tcc-yRbn-j_rzqP0xQ1wBiSNbM-PIifnA,1382
6
+ dynibo-0.1.0.dist-info/WHEEL,sha256=GjDPPQwEcripVP6P2r3RxLa-h5Lb9ifGB7FYYtbLDT0,98
7
+ dynibo-0.1.0.dist-info/top_level.txt,sha256=XlAaV8yUtT6rz79r6Y9uHVCwbIagI_RbLg58WHcmADc,7
8
+ dynibo-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (82.0.1)
3
+ Root-Is-Purelib: false
4
+ Tag: py3-none-win_amd64
5
+
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 dynibo contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ dynibo