11import actionlib
22import control_msgs .msg
3+ import rospy
4+ import trajectory_msgs .msg
35
46from .base import ROSRobotInterfaceBase
57
68
79class NextageROSRobotInterface (ROSRobotInterfaceBase ):
8-
910 def __init__ (self , * args , ** kwargs ):
11+ self .on_gazebo = rospy .get_param ('/gazebo/time_step' , None ) is not None \
12+ and rospy .get_param ('/torso_controller/type' , None ) is not None
13+
14+ if self .on_gazebo :
15+ rospy .loginfo ("Gazebo environment detected" )
16+
17+ self .lhand = None
18+ self .rhand = None
19+
1020 super (NextageROSRobotInterface , self ).__init__ (* args , ** kwargs )
1121
12- self .rarm_move = actionlib .SimpleActionClient (
13- '/rarm_controller/follow_joint_trajectory_action' ,
14- control_msgs .msg .FollowJointTrajectoryAction
15- )
16- self .rarm_move .wait_for_server ()
22+ def _init_lhand (self ):
23+ if self .lhand is None :
24+ self .lhand = LHandInterface ()
25+ return self .lhand
1726
18- self .larm_move = actionlib .SimpleActionClient (
19- '/larm_controller/follow_joint_trajectory_action' ,
20- control_msgs .msg .FollowJointTrajectoryAction
27+ def _init_rhand (self ):
28+ if self .rhand is None :
29+ self .rhand = RHandInterface ()
30+ return self .rhand
31+
32+ @property
33+ def fullbody_controller (self ):
34+ return dict (
35+ controller_type = 'fullbody_controller' ,
36+ controller_action = '/fullbody_controller/follow_joint_trajectory_action' ,
37+ controller_state = '/fullbody_controller/state' ,
38+ action_type = control_msgs .msg .FollowJointTrajectoryAction ,
39+ joint_names = [j .name for j in self .robot .joint_list ],
2140 )
22- self .larm_move .wait_for_server ()
2341
2442 @property
2543 def rarm_controller (self ):
@@ -41,19 +59,137 @@ def larm_controller(self):
4159 joint_names = [j .name for j in self .robot .larm .joint_list ],
4260 )
4361
62+ @property
63+ def torso_controller (self ):
64+ return dict (
65+ controller_type = 'torso_controller' ,
66+ controller_action = '/torso_controller/follow_joint_trajectory_action' ,
67+ controller_state = '/torso_controller/state' ,
68+ action_type = control_msgs .msg .FollowJointTrajectoryAction ,
69+ joint_names = [j .name for j in self .robot .torso .joint_list ],
70+ )
71+
72+ @property
73+ def head_controller (self ):
74+ return dict (
75+ controller_type = 'head_controller' ,
76+ controller_action = '/head_controller/follow_joint_trajectory_action' ,
77+ controller_state = '/head_controller/state' ,
78+ action_type = control_msgs .msg .FollowJointTrajectoryAction ,
79+ joint_names = [j .name for j in self .robot .head .joint_list ],
80+ )
81+
4482 def default_controller (self ):
45- return [self .rarm_controller , self .larm_controller ]
83+ if self .on_gazebo :
84+ return [self .larm_controller , self .rarm_controller ,
85+ self .torso_controller , self .head_controller ]
86+ else :
87+ return [self .fullbody_controller ]
88+
89+ def start_grasp (self , arm = 'arms' , ** kwargs ):
90+ if arm == 'larm' :
91+ self ._init_lhand ().start_grasp (** kwargs )
92+ elif arm == 'rarm' :
93+ self ._init_rhand ().start_grasp (** kwargs )
94+ elif arm == 'arms' :
95+ self ._init_lhand ().start_grasp (** kwargs )
96+ self ._init_rhand ().start_grasp (** kwargs )
97+
98+ def stop_grasp (self , arm = 'arms' , ** kwargs ):
99+ if arm == 'larm' :
100+ self ._init_lhand ().stop_grasp (** kwargs )
101+ elif arm == 'rarm' :
102+ self ._init_rhand ().stop_grasp (** kwargs )
103+ elif arm == 'arms' :
104+ self ._init_lhand ().stop_grasp (** kwargs )
105+ self ._init_rhand ().stop_grasp (** kwargs )
46106
47- def move_arm (self , trajectory , arm = 'rarm' , wait = True ):
107+ def open_forceps (self , arm = 'arms' , ** kwargs ):
108+ if arm == 'larm' :
109+ self ._init_lhand ().open_forceps (** kwargs )
110+ elif arm == 'arms' :
111+ self ._init_lhand ().open_forceps (** kwargs )
112+
113+ def close_forceps (self , arm = 'arms' , ** kwargs ):
114+ if arm == 'larm' :
115+ self ._init_lhand ().close_forceps (** kwargs )
116+ elif arm == 'arms' :
117+ self ._init_lhand ().close_forceps (** kwargs )
118+
119+ def open_holder (self , arm = 'arms' , ** kwargs ):
120+ if arm == 'rarm' :
121+ self ._init_rhand ().open_holder (** kwargs )
122+ elif arm == 'arms' :
123+ self ._init_rhand ().open_holder (** kwargs )
124+
125+ def close_holder (self , arm = 'arms' , ** kwargs ):
48126 if arm == 'rarm' :
49- self .send_trajectory (self .rarm_move , trajectory , wait )
50- elif arm == 'larm' :
51- self .send_trajectory (self .larm_move , trajectory , wait )
127+ self ._init_rhand ().close_holder (** kwargs )
128+ elif arm == 'arms' :
129+ self ._init_rhand ().close_holder (** kwargs )
130+
131+
132+ class LHandInterface :
133+ def __init__ (self ):
134+ self .action_client = actionlib .SimpleActionClient (
135+ "/lhand/position_joint_trajectory_controller/follow_joint_trajectory" ,
136+ control_msgs .msg .FollowJointTrajectoryAction
137+ )
138+ if not self .action_client .wait_for_server (rospy .Duration (5 )):
139+ rospy .logwarn ("LHand action server not available" )
52140
53- def send_trajectory (self , client , trajectory , wait = True ):
141+ def move_hand (self , grasp_angle , wait = True , tm = 1.0 ):
54142 goal = control_msgs .msg .FollowJointTrajectoryGoal ()
55- goal .trajectory = trajectory
143+ goal .trajectory .joint_names = ["lhand_joint" ]
144+ point = trajectory_msgs .msg .JointTrajectoryPoint ()
145+ point .positions = [grasp_angle ]
146+ point .time_from_start = rospy .Duration (tm )
147+ goal .trajectory .points = [point ]
148+ self .action_client .send_goal (goal )
56149 if wait :
57- client .send_goal_and_wait (goal )
58- else :
59- client .send_goal (goal )
150+ self .action_client .wait_for_result (rospy .Duration (tm + 5.0 ))
151+
152+ def start_grasp (self , ** kwargs ):
153+ return self .move_hand (- 2.7 , ** kwargs )
154+
155+ def stop_grasp (self , ** kwargs ):
156+ return self .move_hand (0.0 , ** kwargs )
157+
158+ def open_forceps (self , wait = False , tm = 0.2 ):
159+ return self .move_hand (- 2.2 , wait , tm )
160+
161+ def close_forceps (self , wait = False , tm = 0.2 ):
162+ return self .move_hand (- 3.2 , wait , tm )
163+
164+
165+ class RHandInterface :
166+ def __init__ (self ):
167+ self .action_client = actionlib .SimpleActionClient (
168+ "/rhand/position_joint_trajectory_controller/follow_joint_trajectory" ,
169+ control_msgs .msg .FollowJointTrajectoryAction
170+ )
171+ if not self .action_client .wait_for_server (rospy .Duration (5 )):
172+ rospy .logwarn ("RHand action server not available" )
173+
174+ def move_hand (self , grasp_angle , wait = True , tm = 1.0 ):
175+ goal = control_msgs .msg .FollowJointTrajectoryGoal ()
176+ goal .trajectory .joint_names = ["rhand_joint" ]
177+ point = trajectory_msgs .msg .JointTrajectoryPoint ()
178+ point .positions = [grasp_angle ]
179+ point .time_from_start = rospy .Duration (tm )
180+ goal .trajectory .points = [point ]
181+ self .action_client .send_goal (goal )
182+ if wait :
183+ self .action_client .wait_for_result (rospy .Duration (tm + 5.0 ))
184+
185+ def start_grasp (self , ** kwargs ):
186+ return self .move_hand (- 2.7 , ** kwargs )
187+
188+ def stop_grasp (self , ** kwargs ):
189+ return self .move_hand (0.0 , ** kwargs )
190+
191+ def open_holder (self , wait = True , tm = 0.2 ):
192+ return self .move_hand (- 0.20 , wait , tm )
193+
194+ def close_holder (self , wait = True , tm = 0.2 ):
195+ return self .move_hand (0.08 , wait , tm )
0 commit comments