File size: 2,904 Bytes
0a3f650
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Nominal endpoint of dense delta commands under perfect per-step tracking.

This is a geometric diagnostic, not a dynamics rollout: the trained deltas use
future measured TCP poses, which are unavailable when skipping intermediate steps.
"""

import torch
from mani_skill.utils.geometry.rotation_conversions import (
    euler_angles_to_matrix,
    quaternion_to_matrix,
)

from lim.action_encoding import ROTATION_SCALE, matrix_to_sixd, sixd_to_controller


def check_endpoint_controller(config):
    """Fail if the source controller differs from the audited CPU Panda contract."""
    if not (
        config.frame == "root_translation:root_aligned_body_rotation"
        and config.use_delta
        and not config.use_target
        and config.normalize_action
        and config.root_link_name is None
        and config.pos_lower == -0.1
        and config.pos_upper == 0.1
        and config.rot_lower == ROTATION_SCALE
    ):
        raise ValueError(
            "Dense endpoint requires the audited Panda delta-pose controller"
        )


def dense_chunk_endpoint(chunk, current_tcp, robot_root):
    """Return world target10 and decoded commands; keep ONLY the last gripper.

    Commands are composed in robot-root coordinates with left-multiplied
    rotations, matching ManiSkill CPU's target construction. Rotations are
    decoded through the same 6D adapter and controller clipping as native dense
    execution. No simulation, future feedback, object poses or IK are used.
    """
    chunk = torch.as_tensor(chunk, dtype=torch.float32)
    current = torch.as_tensor(current_tcp, dtype=chunk.dtype, device=chunk.device)
    root = torch.as_tensor(robot_root, dtype=chunk.dtype, device=chunk.device)
    if chunk.ndim != 2 or chunk.shape[1] != 10 or len(chunk) == 0:
        raise ValueError("Expected nonempty H x 10 dense delta predictions")
    for pose in (current, root):
        if pose.shape != (7,) or not torch.isfinite(pose).all():
            raise ValueError("Expected finite world xyz,wxyz poses")
        if not torch.isclose(pose[3:].norm(), pose.new_tensor(1.0), atol=1e-4):
            raise ValueError("Expected unit pose quaternion")
    commands = sixd_to_controller(chunk).clone()
    commands[:, :3] = commands[:, :3].clamp(-1, 1)
    commands[:, -1] = commands[:, -1].clamp(-1, 1)
    root_rotation = quaternion_to_matrix(root[3:])
    position = root_rotation.T @ (current[:3] - root[:3])
    rotation = root_rotation.T @ quaternion_to_matrix(current[3:])
    for command in commands:
        position = position + 0.1 * command[:3]
        rotation = (
            euler_angles_to_matrix(command[3:6] * ROTATION_SCALE, "XYZ") @ rotation
        )
    target = torch.cat(
        [
            root[:3] + root_rotation @ position,
            matrix_to_sixd(root_rotation @ rotation),
            commands[-1, -1:],
        ]
    )
    return target, commands