|
14 | 14 | common.RobotType("Yam"), |
15 | 15 | ] |
16 | 16 |
|
| 17 | +# Robots with a genuine redundant DOF (7-DoF arms), where null-space biasing has room to act. |
| 18 | +REDUNDANT_ROBOTS = [ |
| 19 | + common.RobotType.FR3, |
| 20 | + common.RobotType("XArm7"), |
| 21 | +] |
| 22 | + |
| 23 | +# Identity pose / no TCP offset, reused across tests. |
| 24 | +NO_TCP_OFFSET = common.Pose() |
| 25 | + |
17 | 26 |
|
18 | 27 | @pytest.mark.parametrize("robot_name", PIN_SUPPORTED_ROBOTS) |
19 | 28 | def test_kinematics_identity(robot_name): |
20 | 29 | robot = rcs.ROBOTS[robot_name] |
21 | | - |
22 | | - # Determine model path and type |
23 | 30 | model_path = robot.mjcf_model_path |
24 | | - |
25 | 31 | frame_id = robot.attachment_site |
| 32 | + q_home = robot.q_home |
26 | 33 |
|
27 | | - # Initialize Pinocchio interface |
| 34 | + # Default Pin: limit-clamping on, no null-space bias. |
28 | 35 | try: |
29 | 36 | pin = common.Pin(model_path, frame_id, False) |
30 | 37 | except Exception as e: |
31 | 38 | pytest.fail(f"Failed to initialize Pin for {robot_name}: {e}") |
32 | 39 |
|
33 | | - q_home = robot.q_home |
34 | | - |
35 | | - # Test 1: FK at home |
36 | | - # Identity pose (no TCP offset) |
37 | | - tcp_offset = common.Pose() |
38 | | - |
39 | | - pose_home = pin.forward(q_home, tcp_offset) |
| 40 | + # Test 1: FK at home. |
| 41 | + pose_home = pin.forward(q_home, NO_TCP_OFFSET) |
40 | 42 | assert isinstance(pose_home, common.Pose) |
41 | 43 |
|
42 | | - # Test 2: IK at home pose should return a solution (ideally close to q_home, but IK is redundant) |
43 | | - # We use q_home as initial guess |
44 | | - q_sol: np.ndarray | None = pin.inverse(pose_home, q_home, tcp_offset) |
45 | | - |
| 44 | + # Test 2: IK at the home pose returns a solution reaching that pose. The home |
| 45 | + # configuration is within the joint limits, so clamping does not interfere. |
| 46 | + q_sol: np.ndarray | None = pin.inverse(pose_home, q_home, NO_TCP_OFFSET) |
46 | 47 | assert q_sol is not None, "IK failed for home pose" |
47 | 48 |
|
48 | | - # Verify the solution with FK |
49 | | - pose_sol = pin.forward(q_sol, tcp_offset) |
50 | | - |
51 | | - # Check if pose_sol is close to pose_home |
| 49 | + pose_sol = pin.forward(q_sol, NO_TCP_OFFSET) |
52 | 50 | assert pose_sol.is_close( |
53 | 51 | pose_home, eps_r=1e-4, eps_t=1e-4 |
54 | 52 | ), f"FK(IK(pose)) does not match pose.\nOriginal: {pose_home}\nResult: {pose_sol}" |
55 | 53 |
|
56 | | - # Test 3: Perturbed configuration |
57 | | - # Add small noise to q_home to test non-trivial pose |
58 | | - # Ensure we stay within limits if possible, but for small noise it should be fine |
| 54 | + # Test 3: Perturbed configuration. We disable limit clamping here so that |
| 55 | + # reachability of FK(q_perturbed) does not depend on how close q_home sits to |
| 56 | + # a joint limit (e.g. SO101), keeping this a pure IK-convergence check. |
| 57 | + pin_free = common.Pin(model_path, frame_id, False, np.array([]), 0.0, False) |
| 58 | + |
59 | 59 | np.random.seed(42) |
60 | 60 | q_perturbed = q_home + np.random.uniform(-0.1, 0.1, size=q_home.shape) |
61 | 61 |
|
62 | | - pose_perturbed = pin.forward(q_perturbed, tcp_offset) # type: ignore |
63 | | - q_sol_perturbed: np.ndarray | None = pin.inverse(pose_perturbed, q_home, tcp_offset) # Use q_home as seed |
64 | | - |
| 62 | + pose_perturbed = pin_free.forward(q_perturbed, NO_TCP_OFFSET) |
| 63 | + q_sol_perturbed: np.ndarray | None = pin_free.inverse(pose_perturbed, q_home, NO_TCP_OFFSET) |
65 | 64 | assert q_sol_perturbed is not None, "IK failed for perturbed pose" |
66 | 65 |
|
67 | | - pose_sol_perturbed = pin.forward(q_sol_perturbed, tcp_offset) |
| 66 | + pose_sol_perturbed = pin_free.forward(q_sol_perturbed, NO_TCP_OFFSET) |
68 | 67 | assert pose_sol_perturbed.is_close( |
69 | 68 | pose_perturbed, eps_r=1e-3, eps_t=1e-3 |
70 | 69 | ), f"FK(IK(perturbed_pose)) does not match.\nOriginal: {pose_perturbed}\nResult: {pose_sol_perturbed}" |
| 70 | + |
| 71 | + |
| 72 | +@pytest.mark.parametrize("robot_name", REDUNDANT_ROBOTS) |
| 73 | +def test_kinematics_nullspace_bias(robot_name): |
| 74 | + """A null-space target biases the redundant DOF toward the preferred posture |
| 75 | + without changing the achieved end-effector pose.""" |
| 76 | + robot = rcs.ROBOTS[robot_name] |
| 77 | + model_path = robot.mjcf_model_path |
| 78 | + frame_id = robot.attachment_site |
| 79 | + q_home = robot.q_home |
| 80 | + |
| 81 | + # Clamping off on both so the comparison isolates the null-space term. |
| 82 | + pin_plain = common.Pin(model_path, frame_id, False, np.array([]), 0.0, False) |
| 83 | + pin_ns = common.Pin(model_path, frame_id, False, q_home, 2.0, False) # bias toward home |
| 84 | + |
| 85 | + # Target the home pose; a seed away from home exercises the redundancy so the |
| 86 | + # two solvers can settle on different configurations for the same pose. |
| 87 | + pose_home = pin_plain.forward(q_home, NO_TCP_OFFSET) |
| 88 | + q_seed = q_home.copy() |
| 89 | + q_seed[0] += 0.5 |
| 90 | + q_seed[2] += 0.5 |
| 91 | + q_seed[3] += 0.3 |
| 92 | + |
| 93 | + q_plain = pin_plain.inverse(pose_home, q_seed, NO_TCP_OFFSET) |
| 94 | + q_ns = pin_ns.inverse(pose_home, q_seed, NO_TCP_OFFSET) |
| 95 | + |
| 96 | + assert q_plain is not None, "plain IK failed" |
| 97 | + assert q_ns is not None, "null-space IK failed" |
| 98 | + |
| 99 | + # Both solutions must reach the same end-effector pose. |
| 100 | + assert pin_plain.forward(q_plain, NO_TCP_OFFSET).is_close(pose_home, eps_r=1e-3, eps_t=1e-3) |
| 101 | + assert pin_ns.forward(q_ns, NO_TCP_OFFSET).is_close(pose_home, eps_r=1e-3, eps_t=1e-3) |
| 102 | + |
| 103 | + # The null-space-biased solution sits closer to the preferred (home) posture. |
| 104 | + d_plain = float(np.linalg.norm(q_plain - q_home)) |
| 105 | + d_ns = float(np.linalg.norm(q_ns - q_home)) |
| 106 | + assert d_ns < d_plain, f"null-space bias did not pull toward home: d_ns={d_ns} vs d_plain={d_plain}" |
0 commit comments