1+ from dataclasses import dataclass , field
12import typing
3+ from typing import List
24
35import numpy as np
46from xarm .wrapper import XArmAPI
57
68from rcs import common
79
10+ from scipy .spatial .transform import Rotation as R
811
9- class XArm7 (common .Robot ):
10- def __init__ (self , ip : str , urdf_path : str ):
11- self .ik = common .RL (urdf_path = urdf_path )
12- self ._xarm = XArmAPI (ip = ip )
12+
13+ @dataclass (kw_only = True )
14+ class XArm7Config (common .RobotConfig ):
15+ # some_custom_config: str = "default_value"
16+ payload_weight : float = 0.624
17+ payload_tcp : List [float ] = field (default_factory = lambda : [- 4.15 , 5.24 , 76.38 ])
18+ def __post_init__ (self ):
19+ super ().__init__ ()
20+
21+
22+ class XArm7 ():
23+ def __init__ (self , ip : str ):
24+ self .ik = None #common.RL(urdf_path=urdf_path)
25+ self ._config = XArm7Config ()
26+ self ._config .robot_platform = common .RobotPlatform .HARDWARE
27+ self ._config .robot_type = common .RobotType .XArm7
28+
29+ self ._xarm = XArmAPI (ip )
30+ self ._xarm .set_mode (0 )
31+ self ._xarm .clean_error ()
32+ self ._xarm .clean_warn ()
33+ self ._xarm .motion_enable (enable = True )
34+ self ._xarm .set_state (state = 0 )
35+ self ._xarm .set_tcp_load (
36+ weight = self ._config .payload_weight ,
37+ center_of_gravity = self ._config .payload_tcp ,
38+ wait = True ,
39+ )
1340
1441 # def get_base_pose_in_world_coordinates(self) -> Pose: ...
42+
1543 def get_cartesian_position (self ) -> common .Pose :
16- return self .ik .forward (self .get_joint_position ())
44+ code , xyzrpy = self ._xarm .get_position (is_radian = True )
45+ if code != 0 :
46+ raise RuntimeError ("couldn't get cartesian position from xarm" )
47+
48+ translation = np .array (xyzrpy [:3 ], dtype = np .float64 ).reshape ((3 , 1 )) * 0.001
49+ rpy = xyzrpy [3 :]
50+ quat = R .from_euler ('xyz' , rpy ).as_quat () # [x, y, z, w]
51+ quat = np .array (quat , dtype = np .float64 ).reshape ((4 , 1 ))
52+
53+ # return common.Pose(quaternion=quat, translation=translation)
54+ return common .Pose (rpy_vector = rpy , translation = translation )
1755
1856 def get_ik (self ) -> common .IK | None :
1957 return self .ik
2058
2159 def get_joint_position (self ) -> np .ndarray [tuple [typing .Literal [7 ]], np .dtype [np .float64 ]]: # type: ignore
22- obs = self ._hf_robot .get_servo_angle (is_radian = True )[1 ]
60+ obs = self ._xarm .get_servo_angle (is_radian = True )[1 ]
2361 return np .array (obs , dtype = np .float64 )
2462
25- def get_parameters (self ):
26- a = common . RobotConfig ()
27- a . robot_platform = common . RobotPlatform . HARDWARE
28- a . robot_type = common . RobotType . XArm7
29- return a
63+ def get_parameters (self ) -> XArm7Config :
64+ return self . _config
65+
66+ def set_parameters ( self , robot_cfg : XArm7Config ) -> None :
67+ self . _config = robot_cfg
3068
3169 def get_state (self ) -> common .RobotState :
3270 return common .RobotState ()
@@ -42,12 +80,19 @@ def reset(self) -> None:
4280 pass
4381
4482 def set_cartesian_position (self , pose : common .Pose ) -> None :
45- joints = self . ik . ik ( pose , q0 = self . get_joint_position () )
46- if joints is not None :
47- self . set_joint_position ( joints )
83+ x , y , z , roll , pitch , yaw = pose . xyzrpy ( )
84+ x_mm , y_mm , z_mm = 1000 * x , 1000 * y , 1000 * z
85+ self . _xarm . set_position ( x_mm , y_mm , z_mm , roll , pitch , yaw , is_radian = True , wait = True )
4886
4987 def set_joint_position (self , q : np .ndarray [tuple [typing .Literal [7 ]], np .dtype [np .float64 ]]) -> None : # type: ignore
50- self ._hf_robot .set_servo_angle (angle = q , is_radian = True , wait = True )
88+ self ._xarm .set_servo_angle (angle = q , is_radian = True , wait = True )
89+
90+ def __enter__ (self ):
91+ return self
92+
93+ def __exit__ (self , exc_type , exc_value , traceback ):
94+ self ._xarm .disconnect ()
95+
5196
5297 # def to_pose_in_robot_coordinates(self, pose_in_world_coordinates: Pose) -> Pose: ...
5398 # def to_pose_in_world_coordinates(self, pose_in_robot_coordinates: Pose) -> Pose: ...
0 commit comments