File size: 5,094 Bytes
5f38e95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
r"""Forward kinematics: cable lengths :math:`\to` platform pose.

The spatial CDPR forward-kinematics problem has no closed form: given
:math:`m` measured cable lengths :math:`L_i^\star` we seek a pose
:math:`(\mathbf{p}, \mathbf{R})` satisfying

.. math::

    f_i(\mathbf{p}, \boldsymbol{\theta}) \;=\;
    \lVert \mathbf{a}_i - \mathbf{p} - \mathbf{R}(\boldsymbol{\theta})\,\mathbf{b}_i \rVert
      - L_i^\star
    \;=\; 0, \qquad i = 1, \dots, m .

For a fully constrained / redundantly actuated robot (:math:`m \geq n+1`) this
is an overdetermined system; we minimise the squared residual using a
trust-region Levenberg--Marquardt step. The orientation is parameterised by
its rotation vector :math:`\boldsymbol{\theta} \in \mathbb{R}^3` (axis times
angle), which is minimal and avoids the unit-quaternion constraint at the
cost of a coordinate singularity at :math:`\lVert \boldsymbol{\theta} \rVert = \pi`;
in the trust-region setting this is harmless provided the initial guess is
within a few radians of the solution, which is the regime forward kinematics
is typically called in (tracking, controller inner loops).

Because FK is locally but not globally unique for many CDPR geometries, an
initial guess is required. Callers tracking a trajectory should pass the
previous step's pose; callers solving cold should use a geometrically
sensible seed (e.g. the centroid of cable anchors with identity rotation).
"""

from __future__ import annotations

import numpy as np
from numpy.typing import ArrayLike, NDArray
from scipy.optimize import least_squares
from scipy.spatial.transform import Rotation

from cdpr.core.frames import Pose
from cdpr.geometry.robot import Robot, RobotGeometry
from cdpr.kinematics.inverse import _geometry, cable_lengths


def forward_kinematics(
    target_lengths: ArrayLike,
    robot: Robot | RobotGeometry,
    initial_guess: Pose,
    *,
    ftol: float = 1e-10,
    xtol: float = 1e-10,
    max_iter: int = 100,
    return_diagnostics: bool = False,
) -> Pose | tuple[Pose, dict[str, float]]:
    r"""Solve for the platform pose given measured cable lengths.

    Parameters
    ----------
    target_lengths:
        Measured cable lengths :math:`L_i^\star`, shape ``(m,)``.
    robot:
        Robot or geometry; ``robot.dof`` controls the parameter count
        (3 for translational, 6 for spatial).
    initial_guess:
        Seed pose. For tracking applications, the previous solved pose.
    ftol, xtol, max_iter:
        Forwarded to ``scipy.optimize.least_squares`` (Levenberg--Marquardt).
    return_diagnostics:
        If true, also return a dict with residual norm, iteration count,
        condition number of the final Jacobian, and convergence status.

    Returns
    -------
    pose:
        Solved :class:`Pose`. If diagnostics are requested, a ``(pose, dict)``
        tuple instead.

    Notes
    -----
    SciPy's ``least_squares`` with ``method='lm'`` uses MINPACK's LMDIF, which
    requires the residual count :math:`m` to be at least the parameter count
    (3 or 6). This is automatically satisfied by the
    :class:`RobotGeometry` invariant :math:`m \geq n + 1`.
    """
    g = _geometry(robot)
    L_star = np.asarray(target_lengths, dtype=np.float64)
    if L_star.shape != (g.n_cables,):
        raise ValueError(f"target_lengths must have shape ({g.n_cables},); got {L_star.shape}")

    if g.dof == 3:
        x0 = np.asarray(initial_guess.position, dtype=np.float64).copy()

        def residual(x: NDArray[np.float64]) -> NDArray[np.float64]:
            pose = Pose(position=x, rotation=Rotation.identity())
            return cable_lengths(pose, g) - L_star

    elif g.dof == 6:
        p0 = np.asarray(initial_guess.position, dtype=np.float64)
        theta0 = initial_guess.rotation.as_rotvec()
        x0 = np.concatenate([p0, theta0])

        def residual(x: NDArray[np.float64]) -> NDArray[np.float64]:
            pose = Pose(position=x[:3], rotation=Rotation.from_rotvec(x[3:]))
            return cable_lengths(pose, g) - L_star

    else:
        raise NotImplementedError(
            f"Forward kinematics for dof={g.dof} is not implemented; "
            "currently only 3-DOF translational and 6-DOF spatial CDPRs are supported."
        )

    result = least_squares(
        residual,
        x0,
        method="lm",
        ftol=ftol,
        xtol=xtol,
        max_nfev=max_iter * (len(x0) + 1),
    )

    if g.dof == 3:
        solved = Pose(position=result.x, rotation=Rotation.identity())
    else:
        solved = Pose(position=result.x[:3], rotation=Rotation.from_rotvec(result.x[3:]))

    if return_diagnostics:
        # Final Jacobian conditioning for diagnostics.
        J = result.jac
        s = np.linalg.svd(J, compute_uv=False)
        cond = float(s[0] / s[-1]) if s[-1] > 0 else float("inf")
        return solved, {
            "residual_norm": float(np.linalg.norm(result.fun)),
            "n_iter": int(result.nfev),
            "jacobian_condition": cond,
            "status": int(result.status),
            "message": result.message,
        }
    return solved