File size: 2,888 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
"""Validation against the dynamic model on a synthetic simulation."""

from __future__ import annotations

import numpy as np
import pandas as pd

from cdpr.core.frames import Pose
from cdpr.dynamics.rigid_body import PlatformState
from cdpr.dynamics.simulator import simulate
from cdpr.ingest import (
    ColumnMap,
    IngestedExperiment,
    Pipeline,
    load_dataframe,
    reconstruct_trajectory,
    validate_against_robot,
)


def _experiment_from_simulation(robot, home_pose) -> IngestedExperiment:
    """Run a hold simulation and pour it into an IngestedExperiment via the
    pipeline --- mirrors the path real lab data takes, just with a clean
    source."""
    state0 = PlatformState.at_rest(home_pose)
    result = simulate(robot=robot, state0=state0, duration=0.05, dt=1e-3)

    m = result.cable_tensions.shape[1]
    df = pd.DataFrame({"t": result.time})
    df["x"] = result.positions[:, 0]
    df["y"] = result.positions[:, 1]
    df["z"] = result.positions[:, 2]
    df["qx"] = result.quaternions_xyzw[:, 0]
    df["qy"] = result.quaternions_xyzw[:, 1]
    df["qz"] = result.quaternions_xyzw[:, 2]
    df["qw"] = result.quaternions_xyzw[:, 3]
    for i in range(m):
        df[f"T{i + 1}"] = result.cable_tensions[:, i]
        df[f"L{i + 1}"] = result.cable_lengths[:, i]

    raw = load_dataframe(df)
    cmap = ColumnMap(
        time="t",
        position=("x", "y", "z"),
        quaternion=("qx", "qy", "qz", "qw"),
        cable_tensions=tuple(f"T{i + 1}" for i in range(m)),
        cable_lengths=tuple(f"L{i + 1}" for i in range(m)),
    )
    return Pipeline(raw, columns=cmap).run()


def test_ik_residual_is_machine_zero_for_consistent_data(ipanema, home_pose):
    exp = _experiment_from_simulation(ipanema, home_pose)
    report = validate_against_robot(exp, ipanema)
    # The pipeline did no resampling / filtering, so the cable lengths and
    # poses are still internally consistent; IK residual should be near zero.
    assert report.ik_length_residual is not None
    assert report.ik_length_residual.peak < 1e-10


def test_tension_residual_near_zero_at_static_hold(ipanema, home_pose):
    exp = _experiment_from_simulation(ipanema, home_pose)
    report = validate_against_robot(exp, ipanema)
    # The simulator solved gravity-cancelling tensions; wrench residual
    # should be at the QP solver's tolerance.
    assert report.tension_wrench_residual is not None
    assert report.tension_wrench_residual.peak < 1e-6


def test_reconstruct_trajectory_matches_recorded_positions(ipanema, home_pose):
    exp = _experiment_from_simulation(ipanema, home_pose)
    positions, residual = reconstruct_trajectory(
        exp, ipanema, seed=Pose(position=exp.positions[0], rotation=None) if False else None,
    )
    assert residual is not None
    # FK + LMA recovers the position trajectory to micrometre level.
    assert residual.peak < 1e-5