"""Fail-closed, fixed-waist G1 wrist-position preflight using the native URDF. An outer bound only rejects impossible positions. Passing requires separate, input-matched native IK/FK evidence; it never qualifies a grasp or a scene. """ import hashlib import json import math from pathlib import Path import sys def sha256(path): return hashlib.sha256(Path(path).read_bytes()).hexdigest() def to_pelvis(point, approach, height): if len(point) != 3: raise ValueError("World position must have exactly three coordinates") values = list(point) + [approach[k] for k in ("x_m", "y_m", "yaw_deg")] + [height] if not all(isinstance(v, (int, float)) and math.isfinite(v) for v in values): raise ValueError("World pose and pelvis height must be finite numbers") yaw = math.radians(approach["yaw_deg"]) dx, dy = point[0]-approach["x_m"], point[1]-approach["y_m"] return [math.cos(yaw)*dx+math.sin(yaw)*dy, -math.sin(yaw)*dx+math.cos(yaw)*dy, point[2]-height] def position_gate(geometry, evidence, tolerance_m=.01): if not math.isfinite(tolerance_m) or not 0 < tolerance_m <= .01: raise ValueError("Position tolerance must be positive and no greater than 1 cm") reasons = [] if geometry.get("inside_outer_bound") is not True: reasons.append("outside_native_chain_outer_bound") if evidence is None: reasons.append("missing_native_ik_fk_evidence") else: if evidence.get("request_sha256") != geometry.get("request_sha256") or not geometry.get("request_sha256"): reasons.append("evidence_input_mismatch") for key, limit in [("position_error_m", tolerance_m), ("joint_limit_violation_rad", 1e-6), ("fixed_chain_max_abs_rad", 1e-9), ("returned_full_vs_internal_fk_max_abs", 1e-8)]: value = evidence.get(key) if not isinstance(value, (int, float)) or not math.isfinite(value) or not 0 <= value <= limit: reasons.append("invalid_or_excessive_"+key) return {"position_reachable": not reasons, "rejection_reasons": reasons, "position_tolerance_m": tolerance_m, "grasp_qualified": False, "grasp_qualification_reason": "Object-to-wrist grasp transform and orientation have not been calibrated", "scene_support_checked": False, "navigation_checked": False} class NativeReachPreflight: @classmethod def from_sdk(cls, urdf, sdk_root, side="left"): if side not in {"left", "right"}: raise ValueError("Unknown arm side") import numpy as np sys.path.insert(0, str(sdk_root)) from isaaclab_arena_g1.g1_env.g1_supplemental_info import G1SupplementalInfo from isaaclab_arena_g1.g1_env.robot_model import RobotModel self = cls() self.urdf = Path(urdf) self.urdf_sha256 = sha256(urdf) self.side = side self.frame = side+"_wrist_yaw_link" self.robot = RobotModel(str(urdf), str(Path(urdf).parent), supplemental_info=G1SupplementalInfo()) robot = self.robot model = robot.pinocchio_wrapper.model if model.nq != len(robot.q_default): raise ValueError("Native q0 does not match model") for name, index in robot.joint_to_dof_index.items(): if model.joints[model.getJointId(name)].idx_q != index: raise ValueError("Native joint ordering mismatch: "+name) robot.cache_forward_kinematics(robot.q_default, auto_clip=False) shoulder = model.getJointId(side+"_shoulder_pitch_joint") if shoulder == 0 or model.getFrameId(self.frame) >= model.nframes: raise ValueError("Required native shoulder or wrist is missing") anchor = robot.pinocchio_wrapper.data.oMi[shoulder].translation.copy() frame = model.frames[model.getFrameId(self.frame)] joint_id = frame.parentJoint segments = [] while joint_id != shoulder: if joint_id == 0: raise ValueError("Native wrist ancestry does not include shoulder") joint = model.joints[joint_id] if joint.nq != 1 or joint.nv != 1 or not joint.shortname().startswith("JointModelR"): raise ValueError("Outer bound requires a rigid, revolute native arm chain") xyz = model.jointPlacements[joint_id].translation segments.append({"joint": model.names[joint_id], "translation_xyz_m": xyz.tolist(), "length_m": float(np.linalg.norm(xyz)), "lower_limit_rad": float(robot.lower_joint_limits[joint.idx_q]), "upper_limit_rad": float(robot.upper_joint_limits[joint.idx_q])}) joint_id = model.parents[joint_id] fixed = [] joint_id = model.parents[shoulder] while joint_id: name = model.names[joint_id] index = robot.joint_to_dof_index[name] fixed.append({"joint": name, "index": index, "q0_rad": float(robot.q_default[index])}) joint_id = model.parents[joint_id] self.fixed_ancestors = fixed self.chain = {"anchor_joint": model.names[shoulder], "anchor_pelvis_xyz_m": anchor.tolist(), "segments": segments, "wrist_frame_offset_m": float(np.linalg.norm(frame.placement.translation)), "fixed_ancestors": fixed} self.chain["outer_radius_m"] = sum(x["length_m"] for x in segments)+self.chain["wrist_frame_offset_m"] return self def geometry(self, target_world_xyz_m, approach_pose, pelvis_height_m): local = to_pelvis(target_world_xyz_m, approach_pose, pelvis_height_m) request = {"target_world_xyz_m": list(target_world_xyz_m), "approach_pose": approach_pose, "pelvis_height_m": pelvis_height_m, "root_frame": "pelvis", "frame": self.frame, "urdf_sha256": self.urdf_sha256, "fixed_ancestors": self.fixed_ancestors} distance = math.dist(local, self.chain["anchor_pelvis_xyz_m"]) radius = self.chain["outer_radius_m"] return {**request, "request_sha256": hashlib.sha256(json.dumps(request, sort_keys=True, allow_nan=False).encode()).hexdigest(), "target_pelvis_xyz_m": local, "target_to_anchor_m": distance, "chain_outer_radius_m": radius, "minimum_position_error_m": max(0, distance-radius), "inside_outer_bound": distance <= radius+1e-12, "geometry_status": "requires_native_ik" if distance <= radius+1e-12 else "impossible_with_fixed_pelvis_and_waist"}