Skip to content

Commit 0c72322

Browse files
committed
update unit test to use joint_pos_out_of_limit
1 parent 45c2c90 commit 0c72322

File tree

1 file changed

+28
-25
lines changed

1 file changed

+28
-25
lines changed

source/isaaclab/test/assets/test_articulation.py

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
from isaaclab.assets import Articulation, ArticulationCfg
3232
from isaaclab.sim import build_simulation_context
3333
from isaaclab.utils.assets import ISAAC_NUCLEUS_DIR
34+
from isaaclab.managers import SceneEntityCfg
35+
from isaaclab.envs.mdp.terminations import joint_pos_out_of_limit
3436

3537
##
3638
# Pre-defined configs
@@ -657,25 +659,21 @@ def test_out_of_range_default_joint_vel(sim, device):
657659
@pytest.mark.parametrize("device", ["cuda:0", "cpu"])
658660
@pytest.mark.parametrize("add_ground_plane", [True])
659661
def test_joint_pos_limits(sim, num_articulations, device, add_ground_plane):
660-
"""Test write_joint_limits_to_sim API and when default pos falls outside of the new limits.
661-
662-
This test verifies that:
663-
1. Joint limits can be set correctly
664-
2. Default positions are preserved when setting new limits
665-
3. Joint limits can be set with indexing
666-
4. Invalid joint positions are properly handled
667-
668-
Args:
669-
sim: The simulation fixture
670-
num_articulations: Number of articulations to test
671-
"""
662+
"""Test write_joint_limits_to_sim API and validate limits via joint_pos_out_of_limit()."""
672663
# Create articulation
673664
articulation_cfg = generate_articulation_cfg(articulation_type="panda")
674665
articulation, _ = generate_articulation(articulation_cfg, num_articulations, device)
675666

667+
# Minimal fake env that exposes scene["robot"] -> articulation
668+
class _Env:
669+
def __init__(self, art):
670+
self.scene = {"robot": art}
671+
672+
env = _Env(articulation)
673+
robot_all = SceneEntityCfg(name="robot") # all joints
674+
676675
# Play sim
677676
sim.reset()
678-
# Check if articulation is initialized
679677
assert articulation.is_initialized
680678

681679
# Get current default joint pos
@@ -691,6 +689,10 @@ def test_joint_pos_limits(sim, num_articulations, device, add_ground_plane):
691689
torch.testing.assert_close(articulation._data.joint_pos_limits, limits)
692690
torch.testing.assert_close(articulation._data.default_joint_pos, default_joint_pos)
693691

692+
# Validate via function: no joint should be out of limits
693+
out = joint_pos_out_of_limit(env, robot_all) # [N]
694+
assert torch.all(~out)
695+
694696
# Set new joint limits with indexing
695697
env_ids = torch.arange(1, device=device)
696698
joint_ids = torch.arange(2, device=device)
@@ -703,29 +705,30 @@ def test_joint_pos_limits(sim, num_articulations, device, add_ground_plane):
703705
torch.testing.assert_close(articulation._data.joint_pos_limits[env_ids][:, joint_ids], limits)
704706
torch.testing.assert_close(articulation._data.default_joint_pos, default_joint_pos)
705707

706-
# Set new joint limits that invalidate default joint pos
708+
# Validate via function on selected joints
709+
robot_subset = SceneEntityCfg(name="robot", joint_ids=joint_ids.tolist())
710+
out = joint_pos_out_of_limit(env, robot_subset) # [N]
711+
assert torch.all(~out)
712+
713+
# Set new joint limits that (narrowly) constrain default joint pos
707714
limits = torch.zeros(num_articulations, articulation.num_joints, 2, device=device)
708715
limits[..., 0] = torch.rand(num_articulations, articulation.num_joints, device=device) * -0.1
709716
limits[..., 1] = torch.rand(num_articulations, articulation.num_joints, device=device) * 0.1
710717
articulation.write_joint_position_limit_to_sim(limits)
711718

712-
# Check if all values are within the bounds
713-
within_bounds = (articulation._data.default_joint_pos >= limits[..., 0]) & (
714-
articulation._data.default_joint_pos <= limits[..., 1]
715-
)
716-
assert torch.all(within_bounds)
719+
# Validate via function: defaults should still be within new limits
720+
out = joint_pos_out_of_limit(env, robot_all) # [N]
721+
assert torch.all(~out)
717722

718-
# Set new joint limits that invalidate default joint pos with indexing
723+
# Set new joint limits that constrain default joint pos with indexing
719724
limits = torch.zeros(env_ids.shape[0], joint_ids.shape[0], 2, device=device)
720725
limits[..., 0] = torch.rand(env_ids.shape[0], joint_ids.shape[0], device=device) * -0.1
721726
limits[..., 1] = torch.rand(env_ids.shape[0], joint_ids.shape[0], device=device) * 0.1
722727
articulation.write_joint_position_limit_to_sim(limits, env_ids=env_ids, joint_ids=joint_ids)
723728

724-
# Check if all values are within the bounds
725-
within_bounds = (articulation._data.default_joint_pos[env_ids][:, joint_ids] >= limits[..., 0]) & (
726-
articulation._data.default_joint_pos[env_ids][:, joint_ids] <= limits[..., 1]
727-
)
728-
assert torch.all(within_bounds)
729+
# Validate via function on selected joints
730+
out = joint_pos_out_of_limit(env, robot_subset) # [N]
731+
assert torch.all(~out)
729732

730733

731734
@pytest.mark.parametrize("num_articulations", [1, 2])

0 commit comments

Comments
 (0)