File size: 7,337 Bytes
adf2e22 | 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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | # -*- coding: utf-8 -*-
#
# @File: termination_cfg.py
# @Author: Haozhe Xie
# @Date: 2025-09-26 10:24:59
# @Last Modified by: Haozhe Xie
# @Last Modified at: 2025-12-11 06:53:23
# @Email: root@haozhexie.com
from typing import Dict
import torch
from isaaclab.envs import ManagerBasedRLEnv
from isaaclab.managers import SceneEntityCfg, TerminationTermCfg
from isaaclab.utils import configclass
from isaaclab_tasks.manager_based.manipulation.lift import mdp
from simulations import helpers
def is_object_picked(
env: ManagerBasedRLEnv,
goal_position: torch.Tensor,
tolerance: float,
objects: list[str] = ["object"],
ee_frame_cfg: SceneEntityCfg = SceneEntityCfg("ee_frame"),
robot_cfg: SceneEntityCfg = SceneEntityCfg("robot"),
) -> torch.Tensor:
assert len(objects) == 1, "Only single object picking is supported."
object = env.scene[objects[0]]
ee_frame = env.scene[ee_frame_cfg.name]
robot = env.scene[robot_cfg.name]
object_position_w = object.data.root_pos_w
eef_position_w = ee_frame.data.target_pos_w[..., 0, :]
object_eef_dist = torch.norm(eef_position_w - object_position_w, dim=1)
goal_position_r = goal_position.to(device=robot.data.root_pos_w.device)
eef_position_r = helpers.get_robot_relative_position(
ee_frame.data.target_pos_w[..., 0, :] - robot.data.root_pos_w,
robot.data.root_quat_w,
)
eef_goal_dist = torch.norm(goal_position_r - eef_position_r, dim=1)
return object_eef_dist < tolerance and eef_goal_dist < tolerance
def are_objects_placed(
env: ManagerBasedRLEnv,
goal_position: torch.Tensor,
objects: list[str],
object_sizes: Dict[str, torch.Tensor],
container_size: torch.Tensor,
tolerance: float,
container_cfg: SceneEntityCfg = SceneEntityCfg("container"),
ee_frame_cfg: SceneEntityCfg = SceneEntityCfg("ee_frame"),
robot_cfg: SceneEntityCfg = SceneEntityCfg("robot"),
) -> torch.Tensor:
objects_placed = torch.ones(env.num_envs, dtype=torch.bool, device=env.device)
container = env.scene[container_cfg.name]
ee_frame = env.scene[ee_frame_cfg.name]
robot = env.scene[robot_cfg.name]
env_origins = robot.data.root_pos_w
robot_quat = robot.data.root_quat_w
container_position = helpers.get_robot_relative_position(
container.data.root_pos_w - env_origins, robot_quat
)
containier_size = helpers.get_object_relative_bbox(
container_size, container.data.root_quat_w, robot_quat
)
for obj in objects:
object = env.scene[obj]
object_position = helpers.get_robot_relative_position(
object.data.root_pos_w - env_origins, robot_quat
)
object_size = helpers.get_object_relative_bbox(
object_sizes[obj], object.data.root_quat_w, robot_quat
)
objects_placed = torch.logical_and(
objects_placed,
helpers.is_object_placed(
object_position,
object_size,
container_position,
containier_size,
),
)
goal_position_r = goal_position.to(device=env_origins.device)
eef_position_r = helpers.get_robot_relative_position(
ee_frame.data.target_pos_w[..., 0, :] - env_origins, robot_quat
)
eef_goal_dist = torch.norm(goal_position_r - eef_position_r, dim=1)
return torch.logical_and(objects_placed, eef_goal_dist < tolerance)
def are_objects_dropped(
env: ManagerBasedRLEnv,
minimum_height: float,
objects: list[str],
) -> torch.Tensor:
object_dropped = torch.zeros(env.num_envs, dtype=torch.bool, device=env.device)
for obj in objects:
_dropped = mdp.root_height_below_minimum(
env, minimum_height, SceneEntityCfg(obj)
)
object_dropped = torch.logical_or(object_dropped, _dropped)
return object_dropped
def are_objects_unreachable(
env: ManagerBasedRLEnv,
max_reach_dist: float,
objects: list[str],
robot_cfg: SceneEntityCfg = SceneEntityCfg("robot"),
) -> torch.Tensor:
object_unreachable = torch.zeros(env.num_envs, dtype=torch.bool, device=env.device)
robot = env.scene[robot_cfg.name]
env_origins = robot.data.root_pos_w
robot_quat = robot.data.root_quat_w
for obj in objects:
object = env.scene[obj]
object_position = helpers.get_robot_relative_position(
object.data.root_pos_w - env_origins, robot_quat
)
obj_dist = torch.norm(object_position)
object_unreachable = torch.logical_or(
object_unreachable, obj_dist > max_reach_dist
)
return object_unreachable
def get_done_term(terms: list[str]) -> str | None:
DONE_TERMS = ["object_picked", "objects_placed"]
for term in DONE_TERMS:
if term in terms:
return term
return None
@configclass
class TerminationsCfg:
"""Termination terms for the MDP."""
time_out = TerminationTermCfg(func=mdp.time_out, time_out=True)
object_dropping = TerminationTermCfg(
func=are_objects_dropped,
params={
"minimum_height": 0.1,
"objects": ["object"],
},
time_out=True,
)
# object_unreachable = TerminationTermCfg(
# func=are_objects_unreachable,
# params={
# "max_reach_dist": 0,
# "objects": ["object"],
# },
# time_out=True,
# )
@configclass
class PickTerminationsCfg(TerminationsCfg):
"""Termination terms for the Pick task."""
object_picked = TerminationTermCfg(
func=is_object_picked,
params={"goal_position": None, "tolerance": 0.015},
time_out=False,
)
@configclass
class PlaceTerminationsCfg(TerminationsCfg):
"""Termination terms for the Pick task."""
objects_placed = TerminationTermCfg(
func=are_objects_placed,
params={
"goal_position": None,
"objects": None,
"object_sizes": None,
"container_size": None,
"tolerance": 0.015,
},
time_out=False,
)
container_dropping = TerminationTermCfg(
func=mdp.root_height_below_minimum,
params={
"minimum_height": 0.1,
"asset_cfg": SceneEntityCfg("container"),
},
time_out=True,
)
def get_termination_cfg(task: str, args: dict = {}) -> TerminationsCfg:
done_term = None
if task == "pick":
cfg = PickTerminationsCfg()
done_term = cfg.object_picked
elif task in ["place", "long-horizon"]:
cfg = PlaceTerminationsCfg()
done_term = cfg.objects_placed
else:
cfg = TerminationsCfg()
for k, v in args.items():
if k in cfg.object_dropping.params:
cfg.object_dropping.params[k] = v
# if k in cfg.object_unreachable.params:
# cfg.object_unreachable.params[k] = v
# Update the parameters of the done term
if done_term is not None:
for k, v in args.items():
if k in done_term.params:
done_term.params[k] = v
return cfg
|