File size: 4,629 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
137
138
139
140
r"""Feedforward + feedback controller composition.

A common dissertation experiment is "show how much an analytic
feedforward improves a pure-PD baseline". Rather than writing the
addition by hand in every script, this module composes two
:class:`Controller` instances: a feedforward (typically an inverse-
dynamics term that depends on the reference acceleration) and a
feedback term (PD, MPC, learned residual) that closes the loop on the
state error.

The composed controller returns

.. math::

    \mathbf{w}_\text{cable} \;=\; \mathbf{w}_\text{cable}^\text{ff}
        \;+\; \mathbf{w}_\text{cable}^\text{fb},

where each contribution is whatever its controller decides. Gravity
compensation lives in whichever controller wants to claim it --- pass
``gravity_compensation=False`` to the feedback controller to avoid
double-counting it when the feedforward already includes it.
"""

from __future__ import annotations

from dataclasses import dataclass
from typing import TYPE_CHECKING

import numpy as np
from numpy.typing import NDArray

if TYPE_CHECKING:                                           # pragma: no cover
    from cdpr.control.base import Controller
    from cdpr.core.frames import Pose, Twist, Wrench
    from cdpr.dynamics.rigid_body import PlatformState
    from cdpr.geometry.robot import Robot


@dataclass(slots=True)
class FeedforwardPlusFeedback:
    """Wrench-additive composition of two controllers."""

    feedforward: "Controller"
    feedback: "Controller"

    def __call__(
        self,
        *,
        state: "PlatformState",
        reference_pose: "Pose",
        reference_twist: "Twist",
        reference_accel: "tuple[NDArray[np.float64], NDArray[np.float64]] | None",
        t: float,
        robot: "Robot",
        gravity: NDArray[np.float64],
        external: "Wrench",
    ) -> "Wrench":
        from cdpr.core.frames import Wrench

        # Feedforward sees no external disturbance --- it doesn't have a
        # state-error signal to fight it with. The feedback term handles
        # disturbance rejection through the error.
        zero_ext = Wrench(np.zeros(6))
        w_ff = self.feedforward(
            state=state,
            reference_pose=reference_pose,
            reference_twist=reference_twist,
            reference_accel=reference_accel,
            t=t,
            robot=robot,
            gravity=gravity,
            external=zero_ext,
        )
        w_fb = self.feedback(
            state=state,
            reference_pose=reference_pose,
            reference_twist=reference_twist,
            reference_accel=reference_accel,
            t=t,
            robot=robot,
            gravity=gravity,
            external=external,
        )
        return w_ff + w_fb


# ---------------------------------------------------------------------------
# Pure feedforward inverse-dynamics term (analytic, no state feedback)
# ---------------------------------------------------------------------------

@dataclass(slots=True)
class InverseDynamicsFeedforward:
    """Analytic computed-torque term using *only* the reference acceleration.

    Returns the cable wrench needed to drive the rigid body along the
    reference trajectory in the absence of disturbance. Pair with a
    feedback controller via :class:`FeedforwardPlusFeedback` to get a
    standard "FF + FB" tracking law without the feedback term having to
    re-derive the inertia coupling.

    Mathematically::

        F  = m * a_ref_lin - m * g
        Tau = I_W * a_ref_ang + omega x I_W omega
    """

    include_coriolis: bool = True
    cancel_gravity: bool = True

    def __call__(
        self,
        *,
        state: "PlatformState",
        reference_pose: "Pose",
        reference_twist: "Twist",
        reference_accel: "tuple[NDArray[np.float64], NDArray[np.float64]] | None",
        t: float,
        robot: "Robot",
        gravity: NDArray[np.float64],
        external: "Wrench",
    ) -> "Wrench":
        from cdpr.core.frames import Wrench

        inertia = robot.require_inertia()
        a_lin = reference_accel[0] if reference_accel is not None else np.zeros(3)
        a_ang = reference_accel[1] if reference_accel is not None else np.zeros(3)

        F = inertia.mass * a_lin
        if self.cancel_gravity:
            F = F - inertia.mass * gravity

        R = state.pose.rotation.as_matrix()
        I_world = R @ inertia.inertia @ R.T
        Tau = I_world @ a_ang
        if self.include_coriolis:
            omega = state.velocity.angular
            Tau = Tau + np.cross(omega, I_world @ omega)

        return Wrench.from_parts(F, Tau)