Skip to content

Commit

Permalink
Fixes parsing of actuator parameters from dictionary (#265)
Browse files Browse the repository at this point in the history
# Description

Quick fix for actuator config parsing.

## Type of change

- Bug fix (non-breaking change which fixes an issue)

## Checklist

- [x] I have run the [`pre-commit` checks](https://pre-commit.com/) with
`./orbit.sh --format`
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [ ] I have updated the changelog and the corresponding version in the
extension's `config/extension.toml` file
- [x] I have added my name to the `CONTRIBUTORS.md` or my name already
exists there

---------

Signed-off-by: David Hoeller <[email protected]>
Co-authored-by: Mayank Mittal <[email protected]>
  • Loading branch information
David Hoeller and Mayankm96 authored Nov 30, 2023
1 parent 71b89c4 commit 0691eef
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,9 @@ def _parse_joint_parameter(
param[:] = float(cfg_value)
elif isinstance(cfg_value, dict):
# if dict, then parse the regular expression
indices, _, values = string_utils.resolve_matching_names_values(param, self.joint_names)
param[:, indices] = torch.tensor(values, device=self._device)
indices, _, values = string_utils.resolve_matching_names_values(cfg_value, self.joint_names)
# note: need to specify type to be safe (e.g. values are ints, but we want floats)
param[:, indices] = torch.tensor(values, dtype=torch.float, device=self._device)
else:
raise TypeError(f"Invalid type for parameter value: {type(cfg_value)}. Expected float or dict.")
elif default_value is not None:
Expand All @@ -226,7 +227,7 @@ def _parse_joint_parameter(
param[:] = float(default_value)
elif isinstance(default_value, torch.Tensor):
# if tensor, then use the same tensor for all joints
param[:] = default_value
param[:] = default_value.float()
else:
raise TypeError(f"Invalid type for default value: {type(default_value)}. Expected float or Tensor.")
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,13 +291,41 @@ def test_loading_gains_from_usd(self):
torch.testing.assert_close(robot.actuators["body"].damping, expected_damping)

def test_setting_gains_from_cfg(self):
"""Test that gains are loaded from the configuration correctly."""
"""Test that gains are loaded from the configuration correctly.
Note: We purposefully give one argument as int and other as float to check that it is handled correctly.
"""
# Create articulation
robot_cfg = ArticulationCfg(
prim_path="/World/Robot",
spawn=sim_utils.UsdFileCfg(usd_path=f"{ISAAC_NUCLEUS_DIR}/Robots/Humanoid/humanoid_instanceable.usd"),
init_state=ArticulationCfg.InitialStateCfg(pos=(0.0, 0.0, 1.34)),
actuators={"body": ImplicitActuatorCfg(joint_names_expr=[".*"], stiffness=10, damping=2.0)},
)
robot = Articulation(cfg=robot_cfg)

# Play sim
self.sim.reset()

# Expected gains
expected_stiffness = torch.full((robot.root_view.count, robot.num_joints), 10.0, device=robot.device)
expected_damping = torch.full_like(expected_stiffness, 2.0)

# Check that gains are loaded from USD file
torch.testing.assert_close(robot.actuators["body"].stiffness, expected_stiffness)
torch.testing.assert_close(robot.actuators["body"].damping, expected_damping)

def test_setting_gains_from_cfg_dict(self):
"""Test that gains are loaded from the configuration dictionary correctly.
Note: We purposefully give one argument as int and other as float to check that it is handled correctly.
"""
# Create articulation
robot_cfg = ArticulationCfg(
prim_path="/World/Robot",
spawn=sim_utils.UsdFileCfg(usd_path=f"{ISAAC_NUCLEUS_DIR}/Robots/Humanoid/humanoid_instanceable.usd"),
init_state=ArticulationCfg.InitialStateCfg(pos=(0.0, 0.0, 1.34)),
actuators={"body": ImplicitActuatorCfg(joint_names_expr=[".*"], stiffness=10.0, damping=2.0)},
actuators={"body": ImplicitActuatorCfg(joint_names_expr=[".*"], stiffness={".*": 10}, damping={".*": 2.0})},
)
robot = Articulation(cfg=robot_cfg)

Expand Down

0 comments on commit 0691eef

Please sign in to comment.