Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use a method from a custom class as the func in ObservationTermCfg #38

Open
hao-tian-zhang opened this issue Oct 13, 2024 · 2 comments

Comments

@hao-tian-zhang
Copy link

hao-tian-zhang commented Oct 13, 2024

When I try to create a class to manage the observation functions by myself, I encounter an error.

Error executing job with overrides: []
Traceback (most recent call last):
  File "/home/robot/code/IsaacLab/source/extensions/omni.isaac.lab_tasks/omni/isaac/lab_tasks/utils/hydra.py", line 85, in hydra_main
    env_cfg.from_dict(hydra_env_cfg["env"])
  File "/home/robot/code/IsaacLab/source/extensions/omni.isaac.lab/omni/isaac/lab/utils/configclass.py", line 140, in _update_class_from_dict
    update_class_from_dict(obj, data, _ns="")
  File "/home/robot/code/IsaacLab/source/extensions/omni.isaac.lab/omni/isaac/lab/utils/dict.py", line 87, in update_class_from_dict
    update_class_from_dict(obj_mem, value, _ns=key_ns)
  File "/home/robot/code/IsaacLab/source/extensions/omni.isaac.lab/omni/isaac/lab/utils/dict.py", line 87, in update_class_from_dict
    update_class_from_dict(obj_mem, value, _ns=key_ns)
  File "/home/robot/code/IsaacLab/source/extensions/omni.isaac.lab/omni/isaac/lab/utils/dict.py", line 110, in update_class_from_dict
    value = string_to_callable(value)
  File "/home/robot/code/IsaacLab/source/extensions/omni.isaac.lab/omni/isaac/lab/utils/string.py", line 158, in string_to_callable
    callable_object = getattr(mod, attr_name)
AttributeError: module 'robot_lab.tasks.locomotion.walker.mdp.observations' has no attribute 'get_gait_phase'

Set the environment variable HYDRA_FULL_ERROR=1 for a complete stack trace.
@hao-tian-zhang
Copy link
Author

hao-tian-zhang commented Oct 13, 2024

These are part of the code for my custom class```
class ObsTermAppends:

def __init__(self):
    self.cycle_time = 0.64
    self.target_joint_pos_scale = 0.2
    self.num_envs = 4096
    self.N = 10

def get_gait_phase(self, env : ManagerBasedRLEnv, asset_cfg: SceneEntityCfg = SceneEntityCfg("robot")):
    # return float mask 1 is stance, 0 is swing
    asset: Articulation = env.scene[asset_cfg.name]
    self.ref_dof_pos = torch.zeros_like(asset.data.joint_pos[:, asset_cfg.joint_ids])
    self.phase = env.episode_length_buf * env.step_dt / self.cycle_time
    self.stance_mask = torch.zeros((env.num_envs, 2))
    self.compute_ref_state
    sin_pos = torch.sin(2 * torch.pi * self.phase)
    # Add double support phase
    self.stance_mask.zero_()
    # left foot stance
    self.stance_mask[:, 0] = sin_pos >= 0
    # right foot stance
    self.stance_mask[:, 1] = sin_pos < 0
    # Double support phase
    self.stance_mask[torch.abs(sin_pos) < 0.1] = 1
    return self.stance_mask

  def base_pos_z(self, env : ManagerBasedRLEnv, asset_cfg: SceneEntityCfg = SceneEntityCfg("robot")) -> torch.Tensor:
      """Root height in the simulation world frame."""
      # extract the used quantities (to enable type-hinting)
      asset: Articulation = env.scene[asset_cfg.name]
      self.base_pos_z = asset.data.root_pos_w[:, 2].unsqueeze(-1)
      return self.base_pos_z

  def base_lin_vel(self, env : ManagerBasedRLEnv, asset_cfg: SceneEntityCfg = SceneEntityCfg("robot")) -> torch.Tensor:
      """Root linear velocity in the asset's root frame."""
      # extract the used quantities (to enable type-hinting)
      asset: Articulation = env.scene[asset_cfg.name]
      self.base_lin_vel = asset.data.root_lin_vel_b
      return self.base_lin_vel
  
  def base_ang_vel(self, env : ManagerBasedRLEnv, asset_cfg: SceneEntityCfg = SceneEntityCfg("robot")) -> torch.Tensor:
      """Root angular velocity in the asset's root frame."""
      # extract the used quantities (to enable type-hinting)
      asset: Articulation = env.scene[asset_cfg.name]
      self.base_ang_vel = asset.data.root_ang_vel_b
      return self.base_ang_vel

@hao-tian-zhang
Copy link
Author

hao-tian-zhang commented Oct 13, 2024

and,I am using these functions like this:

class ObservationsCfg:
    """Observation specifications for the MDP."""
    obsAppends = ObsTermAppends()

    get_gait_phase = ObsTerm(
        func=obsAppends.get_gait_phase,
    )
    diff_ref_pos = ObsTerm(
        func=obsAppends.diff_ref_pos,
    )
    # observation terms (order preserved)
    base_pos_z = ObsTerm(
        func=obsAppends.base_lin_vel,
        noise=Unoise(n_min=-0.1, n_max=0.1),
    )
    base_lin_vel = ObsTerm(
        func=obsAppends.base_lin_vel,
        noise=Unoise(n_min=-0.1, n_max=0.1),
    )
    base_ang_vel = ObsTerm(
        func=obsAppends.base_ang_vel,
        noise=Unoise(n_min=-0.2, n_max=0.2),
    )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant