2525
2626import numpy as np
2727from huggingface_hub import snapshot_download
28- from scipy .spatial .transform import Rotation as R
2928
3029import genesis as gs
31- from genesis .ext .pyrender .interaction .keybindings import KeyAction , Keybind
30+ import genesis .utils .geom as gu
31+ from genesis .vis .keybindings import Key , KeyAction , Keybind
3232
3333
3434def build_scene (use_ipc = False , show_viewer = False , enable_ipc_gui = False ):
@@ -152,9 +152,9 @@ def run_sim(scene, entities, mode="interactive", trajectory_file=None):
152152 target_entity = entities ["target" ]
153153
154154 robot_init_pos = np .array ([0.5 , 0 , 0.55 ])
155- robot_init_R = R . from_euler ( "y" , np .pi )
155+ robot_init_quat = gu . xyz_to_quat ( np . array ([ 0 , np .pi , 0 ])) # Rotation around Y axis
156156 target_pos = [robot_init_pos .copy ()] # Use list for mutability in closures
157- target_R = [robot_init_R ] # Use list for mutability in closures
157+ target_quat = [robot_init_quat . copy () ] # Use list for mutability in closures
158158
159159 n_dofs = robot .n_dofs
160160 motors_dof = np .arange (n_dofs - 2 )
@@ -174,10 +174,9 @@ def run_sim(scene, entities, mode="interactive", trajectory_file=None):
174174
175175 def reset_scene ():
176176 target_pos [0 ] = robot_init_pos .copy ()
177- target_R [0 ] = robot_init_R
178- target_quat = target_R [0 ].as_quat (scalar_first = True )
179- target_entity .set_qpos (np .concatenate ([target_pos [0 ], target_quat ]))
180- q = robot .inverse_kinematics (link = ee_link , pos = target_pos [0 ], quat = target_quat )
177+ target_quat [0 ] = robot_init_quat .copy ()
178+ target_entity .set_qpos (np .concatenate ([target_pos [0 ], target_quat [0 ]]))
179+ q = robot .inverse_kinematics (link = ee_link , pos = target_pos [0 ], quat = target_quat [0 ])
181180 robot .set_qpos (q [:- 2 ], motors_dof )
182181
183182 # entities["cube"].set_pos((random.uniform(0.2, 0.4), random.uniform(-0.2, 0.2), 0.05))
@@ -190,29 +189,32 @@ def move(dpos_delta: tuple[float, float, float]):
190189 target_pos [0 ] += np .array (dpos_delta , dtype = gs .np_float )
191190
192191 def rotate (axis : str , angle : float ):
193- target_R [0 ] = R .from_euler (axis , angle ) * target_R [0 ]
192+ # Create rotation quaternion for the specified axis
193+ euler = np .zeros (3 )
194+ axis_map = {"x" : 0 , "y" : 1 , "z" : 2 }
195+ euler [axis_map [axis ]] = angle
196+ drot_quat = gu .xyz_to_quat (euler )
197+ target_quat [0 ] = gu .transform_quat_by_quat (target_quat [0 ], drot_quat )
194198
195199 def toggle_gripper (close : bool = True ):
196200 gripper_closed [0 ] = close
197201
198- from pyglet .window import key
199-
200202 scene .viewer .register_keybinds (
201- Keybind (key .UP , KeyAction .HOLD , name = "move_forward" , callback = move , args = ((- dpos , 0 , 0 ),)),
202- Keybind (key .DOWN , KeyAction .HOLD , name = "move_backward" , callback = move , args = ((dpos , 0 , 0 ),)),
203- Keybind (key .LEFT , KeyAction .HOLD , name = "move_left" , callback = move , args = ((0 , - dpos , 0 ),)),
204- Keybind (key .RIGHT , KeyAction .HOLD , name = "move_right" , callback = move , args = ((0 , dpos , 0 ),)),
205- Keybind (key .N , KeyAction .HOLD , name = "move_up" , callback = move , args = ((0 , 0 , dpos ),)),
206- Keybind (key .M , KeyAction .HOLD , name = "move_down" , callback = move , args = ((0 , 0 , - dpos ),)),
207- Keybind (key .J , KeyAction .HOLD , name = "yaw_left" , callback = rotate , args = ("z" , drot )),
208- Keybind (key .K , KeyAction .HOLD , name = "yaw_right" , callback = rotate , args = ("z" , - drot )),
209- Keybind (key .I , KeyAction .HOLD , name = "pitch_up" , callback = rotate , args = ("y" , drot )),
210- Keybind (key .O , KeyAction .HOLD , name = "pitch_down" , callback = rotate , args = ("y" , - drot )),
211- Keybind (key .L , KeyAction .HOLD , name = "roll_left" , callback = rotate , args = ("x" , drot )),
212- Keybind (key .SEMICOLON , KeyAction .HOLD , name = "roll_right" , callback = rotate , args = ("x" , - drot )),
213- Keybind (key .U , KeyAction .HOLD , name = "reset_scene" , callback = reset_scene ),
214- Keybind (key .SPACE , KeyAction .PRESS , name = "close_gripper" , callback = toggle_gripper , args = (True ,)),
215- Keybind (key .SPACE , KeyAction .RELEASE , name = "open_gripper" , callback = toggle_gripper , args = (False ,)),
203+ Keybind (Key .UP , KeyAction .HOLD , name = "move_forward" , callback = move , args = ((- dpos , 0 , 0 ),)),
204+ Keybind (Key .DOWN , KeyAction .HOLD , name = "move_backward" , callback = move , args = ((dpos , 0 , 0 ),)),
205+ Keybind (Key .LEFT , KeyAction .HOLD , name = "move_left" , callback = move , args = ((0 , - dpos , 0 ),)),
206+ Keybind (Key .RIGHT , KeyAction .HOLD , name = "move_right" , callback = move , args = ((0 , dpos , 0 ),)),
207+ Keybind (Key .N , KeyAction .HOLD , name = "move_up" , callback = move , args = ((0 , 0 , dpos ),)),
208+ Keybind (Key .M , KeyAction .HOLD , name = "move_down" , callback = move , args = ((0 , 0 , - dpos ),)),
209+ Keybind (Key .J , KeyAction .HOLD , name = "yaw_left" , callback = rotate , args = ("z" , drot )),
210+ Keybind (Key .K , KeyAction .HOLD , name = "yaw_right" , callback = rotate , args = ("z" , - drot )),
211+ Keybind (Key .I , KeyAction .HOLD , name = "pitch_up" , callback = rotate , args = ("y" , drot )),
212+ Keybind (Key .O , KeyAction .HOLD , name = "pitch_down" , callback = rotate , args = ("y" , - drot )),
213+ Keybind (Key .L , KeyAction .HOLD , name = "roll_left" , callback = rotate , args = ("x" , drot )),
214+ Keybind (Key .SEMICOLON , KeyAction .HOLD , name = "roll_right" , callback = rotate , args = ("x" , - drot )),
215+ Keybind (Key .U , KeyAction .HOLD , name = "reset_scene" , callback = reset_scene ),
216+ Keybind (Key .SPACE , KeyAction .PRESS , name = "close_gripper" , callback = toggle_gripper , args = (True ,)),
217+ Keybind (Key .SPACE , KeyAction .RELEASE , name = "open_gripper" , callback = toggle_gripper , args = (False ,)),
216218 )
217219
218220 # Load trajectory if in playback mode
@@ -278,7 +280,7 @@ def toggle_gripper(close: bool = True):
278280 if step_count < len (trajectory ):
279281 step_data = trajectory [step_count ]
280282 target_pos [0 ] = step_data ["target_pos" ]
281- target_R [0 ] = R . from_quat ( step_data ["target_quat" ])
283+ target_quat [0 ] = step_data ["target_quat" ]
282284 gripper_closed [0 ] = step_data ["gripper_closed" ]
283285 step_count += 1
284286 print (f"\r Playback step: { step_count } /{ len (trajectory )} " , end = "" )
@@ -292,16 +294,15 @@ def toggle_gripper(close: bool = True):
292294 if recording :
293295 step_data = {
294296 "target_pos" : target_pos [0 ].copy (),
295- "target_quat" : target_R [0 ].as_quat (), # x,y,z,w format
297+ "target_quat" : target_quat [0 ].copy (),
296298 "gripper_closed" : gripper_closed [0 ],
297299 "step" : step_count ,
298300 }
299301 trajectory .append (step_data )
300302
301303 # control arm
302- target_quat = target_R [0 ].as_quat (scalar_first = True )
303- target_entity .set_qpos (np .concatenate ([target_pos [0 ], target_quat ]))
304- q , err = robot .inverse_kinematics (link = ee_link , pos = target_pos [0 ], quat = target_quat , return_error = True )
304+ target_entity .set_qpos (np .concatenate ([target_pos [0 ], target_quat [0 ]]))
305+ q , err = robot .inverse_kinematics (link = ee_link , pos = target_pos [0 ], quat = target_quat [0 ], return_error = True )
305306 robot .control_dofs_position (q [:- 2 ], motors_dof )
306307 # control gripper
307308 if gripper_closed [0 ]:
0 commit comments