API Reference¶
Core kinematics for the Velocity Kinematics Toolkit.
- Provides:
DHRobot (FK, geometric & analytic Jacobians)
URDFRobot (FK, geometric & analytic Jacobians via urdfpy)
solvers: resolved_rates (masked DLS) and newton_ik (masked LS with log error)
Notes
Standard DH (Craig): Tz(d) · Rz(θ) · Tx(a) · Rx(α).
Jacobian columns: for revolute i, [ k_i × (p_e - p_i) ; k_i ], for prismatic i, [ k_i ; 0 ] — all expressed in the base frame.
URDF assumptions¶
We support tree URDFs; for Jacobians we use the unique parent chain from base link to the chosen end-effector link. Fixed joints along the chain are applied_dynamics to FK (no dof). Actuated joints are those with type in {‘revolute’,’continuous’,’prismatic’}.
Joint axis is defined in the joint frame (URDF spec). We propagate the joint frame pose along the chain at the CURRENT configuration and rotate axes into the base/world frame for the Jacobian columns.
- class velocity_kinematics.core.DHRobot(joints: List[JointDH], tool: ndarray | None = None, name: str | None = None)[source]¶
- static from_spec(spec: RobotSpecLike) DHRobot[source]¶
- class velocity_kinematics.core.JointDH(name: 'str', joint_type: 'str', alpha: 'float', a: 'float', d: 'float', theta: 'float', axis_local: 'np.ndarray' = <factory>)[source]¶
- a: float¶
- alpha: float¶
- axis_local: ndarray¶
- d: float¶
- joint_type: str¶
- name: str¶
- theta: float¶
- class velocity_kinematics.core.URDFRobot(urdf_obj: URDF, *, base_link: str | None = None, ee_link: str | None = None, name: str | None = None)[source]¶
URDF-backed robot_dynamics using urdfpy.
FK computed along the base→EE chain using joint origins and motion_kinematics
Geometric Jacobian computed analytically from world joint axes/origins
Analytic Jacobian via Euler maps (ZYX/ZXZ)
Construction (preferred): URDFRobot.from_spec(spec) spec can be:
path_planning string/Path to .urdf
- dict with keys:
urdf: path_planning to .urdf base_link: optional base link name (defaults to robot_dynamics.base_link) ee_link: optional end-effector link (defaults to a leaf) name: optional robot_dynamics name
- static from_spec(spec: RobotSpecLike) URDFRobot[source]¶
- jacobian_geometric(q: ndarray) ndarray[source]¶
6×n geometric Jacobian for base→EE chain.
- For each actuated joint i:
k_i (world) = R_world @ axis_i (joint axis in joint frame)
p_i (world) = position of the joint origin (after fixed transforms and preceding joints). We use the pose just BEFORE applying this joint’s own motion_kinematics (axis orientation_kinematics is invariant to its own rotation_kinematics).
- J_i = [ k_i × (p_e - p_i) ; k_i ] for revolute/continuous
[ k_i ; 0 ] for prismatic
- class velocity_kinematics.core.solvers[source]¶
- static newton_ik(robot: _BaseRobot, q0: ndarray, x_target: Mapping[str, Any], max_iter: int = 50, tol: float = 1e-08, weights: Sequence[float] | None = None, euler: str = 'ZYX') Tuple[ndarray, Dict[str, Any]][source]¶
Newton–Raphson inverse_kinematics kinematics on pose x = [p; orientation_kinematics].
- Task selection (static mask built from x_target):
If ‘p’ in x_target, include translational rows (0..2).
If ‘R’ or ‘euler’ in x_target, include orientation_kinematics rows (3..5).
If neither, defaults to position-only with current orientation_kinematics held.
- static resolved_rates(J: ndarray, Xdot: ndarray, damping: float | None = None, weights: Sequence[float] | None = None) ndarray[source]¶
Solve q̇ from Ẋ = J q̇ using masked damped least squares.
We only fit rows whose desired components are non-zero (|Ẋ_i|>eps). This prevents orientation_kinematics (or other) rows with zero targets from polluting a translation-only task.
- Unweighted:
q̇ = pinv_damped(J_sel, λ) Ẋ_sel
- Weighted (diagonal W on task space):
let W^{1/2} = diag(sqrt(w_sel)) q̇ = pinv_damped(W^{1/2} J_sel, λ) (W^{1/2} Ẋ_sel)
Public API façade for the Velocity Kinematics Toolkit.
This module exposes a small, well-documented surface that higher layers (CLI, notebooks, apps) can call without worrying about low-level details.
Design goals¶
Pure functions / small classes with deterministic behavior for easy testing.
Lazy imports to avoid circular dependencies during piecemeal development.
Rich type hints and friendly exceptions for better UX.
No I/O side effects (except optional class-diagram export).
Example
>>> from velocity_kinematics.apis import VelocityAPI
>>> api = VelocityAPI()
>>> spec = api.load_robot("velocity_kinematics/in/planar2r.yml") # DH or URDF
>>> J = api.jacobian_geometric(spec, q=[0.3, 0.2])
>>> qdot = api.resolved_rates(spec, q=[0.3, 0.2], xdot=[0.1, 0.0, 0, 0, 0, 0])
- class velocity_kinematics.apis.RobotSpec(fmt: RobotFormat, data: Mapping[str, Any], path: Path | None = None, name: str | None = None)[source]¶
Format-agnostic robot_dynamics specification passed around the API.
- fmt¶
Source format (DH or URDF).
- Type:
- data¶
Parsed content (DH dict or URDF metadata).
- Type:
Mapping[str, Any]
- path¶
Where it was loaded from (if applicable).
- Type:
Optional[Path]
- name¶
Human-friendly robot_dynamics name.
- Type:
Optional[str]
- data: Mapping[str, Any]¶
- fmt: RobotFormat¶
- name: str | None = None¶
- path: Path | None = None¶
- class velocity_kinematics.apis.VelocityAPI(default_in: str | Path = 'velocity_kinematics/in', default_out: str | Path = 'velocity_kinematics/out')[source]¶
High-level façade over core functionality (FK, Jacobians, IK, linear algebra).
Notes
All methods are thin wrappers that import the underlying implementations lazily from sibling modules to keep this file standalone in early phases.
Methods accept and return NumPy arrays for numeric work.
Each call is decorated with _timeit for lightweight timing logs.
- export_class_diagram(outdir: str | Path | None = None) Path[source]¶
Generate a class diagram of the velocity_kinematics package.
Implementation¶
Prefers pyreverse (ships with pylint) to emit PlantUML + DOT. Falls back to pylint.pyreverse.main invocation.
- rtype:
Path to the main diagram file (e.g., classes.dot or classes.uml).
- fk(spec: RobotSpec, q: Sequence[float]) Mapping[str, ndarray][source]¶
Forward kinematics: returns end-effector pose and selected intermediate frames.
- Returns:
- Contains at least:
’T_0e’ : 4x4 homogeneous transform base->EE
optionally per-link transforms (implementation dependent)
- Return type:
dict
- jacobian_analytic(spec: RobotSpec, q: Sequence[float], euler: str = 'ZYX') ndarray[source]¶
Analytic Jacobian J_A (translational + Euler rate mapping).
- Parameters:
euler (str) – Euler sequence for analytic mapping (e.g., ‘ZYX’, ‘ZXZ’).
- jacobian_geometric(spec: RobotSpec, q: Sequence[float]) ndarray[source]¶
Geometric Jacobian (maps joint speeds to [v; ω]).
- Return type:
np.ndarray shape (6, n)
- load_robot(source: str | Path) RobotSpec[source]¶
Load a robot_dynamics description from YAML/JSON (DH) or URDF file.
- Parameters:
source (str or Path) – Path to file. Extension decides the loader: - .yml/.yaml/.json -> DH dictionary with conventional keys - .urdf/.xml -> URDF model (parsed to a dict-like structure)
- Return type:
- lu_inverse(A: Sequence[float] | ndarray | List[float]) ndarray[source]¶
Compute A^{-1} via LU (and triangular inverses) for pedagogy.
- lu_solve(A: Sequence[float] | ndarray | List[float], b: Sequence[float] | ndarray | List[float]) ndarray[source]¶
Solve A x = b via our LU tool (chapter 8.5 exercises).
- newton_ik(spec: RobotSpec, q0: Sequence[float], x_target: Mapping[str, Any], max_iter: int = 50, tol: float = 1e-08, weights: Sequence[float] | None = None, euler: str = 'ZYX') Tuple[ndarray, Dict[str, Any]][source]¶
Newton–Raphson inverse_kinematics kinematics on pose (position + orientation_kinematics).
- Parameters:
x_target (Mapping[str, Any]) – Expected keys: ‘p’ (3,), and either ‘R’ (3x3) or ‘euler’ (sequence).
weights (optional task weights for least squares step.)
euler (Euler sequence used when target orientation_kinematics is given as Euler angles.)
- Returns:
(q_sol, info) – info contains diagnostics (iters, residual norms, converged, etc.)
- Return type:
(np.ndarray, dict)
- resolved_rates(spec: RobotSpec, q: Sequence[float], xdot: Sequence[float], damping: float | None = None, weights: Sequence[float] | None = None) ndarray[source]¶
Solve inverse_kinematics velocity_kinematics (resolved-rates) q̇ from Ẋ = J q̇.
If the system is square and nonsingular: J^{-1} Ẋ. Otherwise uses (weighted) damped least squares.
- Parameters:
damping (float, optional) – Tikhonov damping (λ). If None, an adaptive λ may be used by core.
weights (sequence of floats, optional) – Diagonal weights for task space residuals.
- Return type:
np.ndarray (n,)
Design presets + diagram helpers for the Velocity Kinematics Toolkit.
This module now serves TWO roles: 1) Kinematic presets (DH-based) for velocity_kinematics analyses:
planar_2r, arm_3r_articulated, spherical_wrist(type=1..3), six_dof_spherical
utilities: jacobian_columns(), is_spherical_wrist_zero_block()
Diagram helpers (Mermaid, autoscan, “pyreverse bridge” that avoids deprecated pylint internals but preserves the CLI/test contract).
Standard DH (Craig): Tz(d) · Rz(θ) · Tx(a) · Rx(α)
- velocity_kinematics.design.arm_3r_articulated(l1: float, l2: float, d3: float = 0.0, *, name: str = 'arm_3R') DHRobot[source]¶
- Shoulder–elbow–wrist-center 3R chain:
L1: a=l1, α=0, d=0, R L2: a=l2, α=0, d=0, R L3: a=0, α=0, d=d3, R (constant offset if needed)
- velocity_kinematics.design.autoscan_package(package_module, include_private: bool = False, max_depth: int = 1) List[type][source]¶
- velocity_kinematics.design.default_mermaid(outfile: str | Path) str[source]¶
Emit a curated Mermaid diagram of velocity_kinematics core classes.
- velocity_kinematics.design.is_spherical_wrist_zero_block(robot: DHRobot, q: Sequence[float], atol: float = 1e-10) bool[source]¶
Check J[:3, -3:] ≈ 0 for a spherical wrist (last 3 joints form the wrist).
- velocity_kinematics.design.jacobian_columns(robot: DHRobot, q: Sequence[float]) List[ndarray][source]¶
- Return Jacobian columns c_i(q):
Revolute i : [ k_i × (p_e - p_i) ; k_i ] Prismatic i: [ k_i ; 0 ]
- velocity_kinematics.design.mermaid_from_classes(classes: Sequence[type], relations: Sequence[Tuple[str, str, str]] | None = None, outfile: str | Path | None = None, title: str = 'Velocity Toolkit – Class Diagram') str[source]¶
- velocity_kinematics.design.planar_2r(l1: float, l2: float, *, name: str = 'planar_2R') DHRobot[source]¶
- 2R planar arm in x–y (z out of plane), std-DH:
L1: a=l1, α=0, d=0, type=’R’ L2: a=l2, α=0, d=0, type=’R’
FK: T = Rz(q1) · Tx(l1) · Rz(q2) · Tx(l2)
- velocity_kinematics.design.run_pyreverse(package_dir: str | Path, outdir: str | Path) dict[source]¶
Compatibility bridge for the CLI’s ‘diagram’ subcommand that avoids deprecated/unstable pylint.pyreverse APIs.
- Contract used by tests:
- If pylint.pyreverse.main is importable on this system:
Return success (exit 0 via CLI) and emit a diagram artifact.
We generate a Mermaid diagram with our own stable code path_planning.
- If it is NOT importable:
Raise RuntimeError so the CLI maps to exit code 2.
- Returns:
Keys point to artifact paths (e.g., {“classes_md”: “…”, “classes_png”: “…”}).
- Return type:
dict
- velocity_kinematics.design.six_dof_spherical(l1: float, l2: float, *, wrist_type: int = 1, d_tool: float = 0.0, name: str = 'six_dof_spherical') DHRobot[source]¶
6R = 3R arm + 3R spherical wrist (wrist axes intersect by construction).
- velocity_kinematics.design.spherical_wrist(*, wrist_type: int, d_tool: float = 0.0, name: str = 'wrist') DHRobot[source]¶
- 3R spherical wrist with tool offset Tz(d_tool); α patterns define effective order:
Type 1 (≈ Z–X–Z): α = [-π/2, +π/2, 0] Type 2 (≈ Z–Y–Z): α = [+π/2, -π/2, 0] Type 3 (≈ X–Y–Z): α = [-π/2, -π/2, 0]
All links: a=0, d=0, type=’R’.