11import logging
2+ from time import sleep
23from typing import Any , cast
34
45import gymnasium as gym
56import mujoco
67import numpy as np
78from rcs ._core .common import Pose
89from rcs .envs .base import GripperWrapper , RobotEnv
10+ from rcs .envs .creators import FR3SimplePickUpSimEnvCreator
911
1012logger = logging .getLogger (__name__ )
1113logger .setLevel (logging .INFO )
@@ -25,54 +27,49 @@ def get_object_pose(self, geom_name) -> Pose:
2527 data = self .env .get_wrapper_attr ("sim" ).data
2628
2729 geom_id = mujoco .mj_name2id (model , mujoco .mjtObj .mjOBJ_GEOM , geom_name )
28- return Pose (translation = data .geom_xpos [geom_id ], rotation = data .geom_xmat [geom_id ].reshape (3 , 3 ))
30+ obj_pose_world_coordinates = Pose (
31+ translation = data .geom_xpos [geom_id ], rotation = data .geom_xmat [geom_id ].reshape (3 , 3 )
32+ ) * Pose (rpy_vector = np .array ([0 , 0 , np .pi ]), translation = [0 , 0 , 0 ]) # type: ignore
33+ return self .unwrapped .robot .to_pose_in_robot_coordinates (obj_pose_world_coordinates )
2934
3035 def generate_waypoints (self , start_pose : Pose , end_pose : Pose , num_waypoints : int ) -> list [Pose ]:
3136 waypoints = []
3237 for i in range (num_waypoints + 1 ):
33- t = i / (num_waypoints + 1 )
38+ t = i / (num_waypoints )
3439 waypoints .append (start_pose .interpolate (end_pose , t ))
35- waypoints .append (end_pose )
3640 return waypoints
3741
3842 def step (self , action : dict ) -> dict :
3943 return self .env .step (action )[0 ]
4044
4145 def plan_linear_motion (self , geom_name : str , delta_up : float , num_waypoints : int = 20 ) -> list [Pose ]:
4246 end_eff_pose = self .unwrapped .robot .get_cartesian_position ()
43-
4447 goal_pose = self .get_object_pose (geom_name = geom_name )
45- # goal pose is above the object and gripper coordinate must flip z-axis (end effector base rotation is [1, 0, 0, 0])
46- # be careful we define identity quaternion as as [0, 0, 0, 1]
47- # this does not work if the object is flipped
4848 goal_pose *= Pose (translation = np .array ([0 , 0 , delta_up ]), quaternion = np .array ([1 , 0 , 0 , 0 ])) # type: ignore
49-
5049 return self .generate_waypoints (end_eff_pose , goal_pose , num_waypoints = num_waypoints )
5150
5251 def execute_motion (self , waypoints : list [Pose ], gripper : float = GripperWrapper .BINARY_GRIPPER_OPEN ) -> dict :
5352 for i in range (len (waypoints )):
54- # calculate delta action
55- # delta_action = waypoints[i] * waypoints[i - 1].inverse()
5653 obs = self .step (self ._action (waypoints [i ], gripper ))
5754 return obs
5855
5956 def approach (self , geom_name : str ):
60- waypoints = self .plan_linear_motion (geom_name = geom_name , delta_up = 0.2 , num_waypoints = 5 )
57+ waypoints = self .plan_linear_motion (geom_name = geom_name , delta_up = 0.2 , num_waypoints = 60 )
6158 self .execute_motion (waypoints = waypoints , gripper = GripperWrapper .BINARY_GRIPPER_OPEN )
6259
6360 def grasp (self , geom_name : str ):
6461
65- waypoints = self .plan_linear_motion (geom_name = geom_name , delta_up = 0 , num_waypoints = 15 )
62+ waypoints = self .plan_linear_motion (geom_name = geom_name , delta_up = 0.01 , num_waypoints = 60 )
6663 self .execute_motion (waypoints = waypoints , gripper = GripperWrapper .BINARY_GRIPPER_OPEN )
6764
6865 self .step (self ._action (Pose (), GripperWrapper .BINARY_GRIPPER_CLOSED ))
6966
70- waypoints = self .plan_linear_motion (geom_name = geom_name , delta_up = 0.2 , num_waypoints = 20 )
67+ waypoints = self .plan_linear_motion (geom_name = geom_name , delta_up = 0.2 , num_waypoints = 60 )
7168 self .execute_motion (waypoints = waypoints , gripper = GripperWrapper .BINARY_GRIPPER_CLOSED )
7269
7370 def move_home (self ):
7471 end_eff_pose = self .unwrapped .robot .get_cartesian_position ()
75- waypoints = self .generate_waypoints (end_eff_pose , self .home_pose , num_waypoints = 10 )
72+ waypoints = self .generate_waypoints (end_eff_pose , self .home_pose , num_waypoints = 60 )
7673 self .execute_motion (waypoints = waypoints , gripper = GripperWrapper .BINARY_GRIPPER_CLOSED )
7774
7875 def pickup (self , geom_name : str ):
@@ -82,15 +79,17 @@ def pickup(self, geom_name: str):
8279
8380
8481def main ():
85- env = gym .make (
86- "rcs/FR3SimplePickUpSim-v0" ,
82+ env = FR3SimplePickUpSimEnvCreator ()(
8783 render_mode = "human" ,
8884 delta_actions = False ,
8985 )
90- env .reset ()
91- print (env .unwrapped .robot .get_cartesian_position ().translation ()) # type: ignore
92- controller = PickUpDemo (env )
93- controller .pickup ("box_geom" )
86+ # wait for gui to open
87+ sleep (3 )
88+ for _ in range (100 ):
89+ env .reset ()
90+ print (env .unwrapped .robot .get_cartesian_position ().translation ()) # type: ignore
91+ controller = PickUpDemo (env )
92+ controller .pickup ("box_geom" )
9493
9594
9695if __name__ == "__main__" :
0 commit comments