File size: 4,450 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
"""Tension distribution: feasibility, bound satisfaction, objective behaviour."""

from __future__ import annotations

import numpy as np
import pytest

from cdpr.core.exceptions import InfeasibleTensionError
from cdpr.kinematics.jacobian import structure_matrix
from cdpr.statics.tension import (
    TensionObjective,
    is_wrench_feasible,
    min_norm_tension,
    tension_distribution,
)


def _gravity_wrench(mass: float, dof: int) -> np.ndarray:
    w = np.zeros(dof)
    w[2] = -mass * 9.81  # Fz: gravity pulls down
    return w


# ---------------------------------------------------------------------------
# Point-mass robot: 4 cables, 3 DOF, redundancy r = 1
# ---------------------------------------------------------------------------

def test_min_norm_tension_solves_equality(point_mass_robot, home_pose):
    W = structure_matrix(home_pose, point_mass_robot)
    w_ext = _gravity_wrench(point_mass_robot.inertia.mass, 3)
    t = min_norm_tension(W, w_ext)
    assert np.allclose(W @ t + w_ext, 0.0, atol=1e-9)


def test_tension_distribution_feasible_at_home(point_mass_robot, home_pose):
    W = structure_matrix(home_pose, point_mass_robot)
    w_ext = _gravity_wrench(point_mass_robot.inertia.mass, 3)
    limits = point_mass_robot.limits
    t = tension_distribution(W, w_ext, limits.t_min, limits.t_max)
    assert (t >= limits.t_min - 1e-8).all()
    assert (t <= limits.t_max + 1e-8).all()
    assert np.allclose(W @ t + w_ext, 0.0, atol=1e-6)


def test_centered_objective_stays_inside_bounds_and_solves_equilibrium(point_mass_robot, home_pose):
    """Centered objective keeps the tensions inside the bounds and on the equilibrium line."""
    W = structure_matrix(home_pose, point_mass_robot)
    w_ext = _gravity_wrench(point_mass_robot.inertia.mass, 3)
    limits = point_mass_robot.limits
    t = tension_distribution(W, w_ext, limits.t_min, limits.t_max,
                             objective=TensionObjective.CENTERED)
    assert (t >= limits.t_min - 1e-8).all()
    assert (t <= limits.t_max + 1e-8).all()
    assert np.allclose(W @ t + w_ext, 0.0, atol=1e-6)


def test_feasibility_check_matches_solver(ipanema, home_pose):
    W = structure_matrix(home_pose, ipanema)
    w_ext = np.zeros(6)
    w_ext[2] = -ipanema.inertia.mass * 9.81
    limits = ipanema.limits
    assert is_wrench_feasible(W, w_ext, limits.t_min, limits.t_max)
    # Solver should now succeed.
    t = tension_distribution(W, w_ext, limits.t_min, limits.t_max)
    assert np.allclose(W @ t + w_ext, 0.0, atol=1e-6)


def test_excessive_load_is_infeasible(point_mass_robot, home_pose):
    W = structure_matrix(home_pose, point_mass_robot)
    # Pull straight down with a force that exceeds 4 * t_max * sin(angle).
    excess = np.array([0.0, 0.0, -1e9])
    limits = point_mass_robot.limits
    assert not is_wrench_feasible(W, excess, limits.t_min, limits.t_max)
    with pytest.raises(InfeasibleTensionError):
        tension_distribution(W, excess, limits.t_min, limits.t_max)


def test_preferred_objective_tracks_warm_start(point_mass_robot, home_pose):
    """Using the previous-step tension as t_pref should bias the new solution toward it."""
    W = structure_matrix(home_pose, point_mass_robot)
    w_ext = _gravity_wrench(point_mass_robot.inertia.mass, 3)
    limits = point_mass_robot.limits

    t_default = tension_distribution(W, w_ext, limits.t_min, limits.t_max,
                                     objective=TensionObjective.CENTERED)
    # Bias t_pref toward the upper bound; t should shift upward (within the line).
    pref = np.full(4, limits.t_max[0] * 0.8)
    t_pref_obj = tension_distribution(W, w_ext, limits.t_min, limits.t_max,
                                      objective=TensionObjective.PREFERRED, t_pref=pref)
    assert (t_pref_obj > t_default - 1e-9).all()


# ---------------------------------------------------------------------------
# IPAnema-class: 8 cables, 6 DOF, redundancy r = 2 -- general SLSQP path
# ---------------------------------------------------------------------------

def test_general_path_succeeds_on_ipanema(ipanema, home_pose):
    W = structure_matrix(home_pose, ipanema)
    w_ext = np.zeros(6)
    w_ext[2] = -ipanema.inertia.mass * 9.81
    limits = ipanema.limits
    t = tension_distribution(W, w_ext, limits.t_min, limits.t_max)
    assert (t >= limits.t_min - 1e-6).all()
    assert (t <= limits.t_max + 1e-6).all()
    assert np.allclose(W @ t + w_ext, 0.0, atol=1e-5)