88from controller import Controller
99from geometry_msgs .msg import PoseStamped
1010from dynamixel_workbench_msgs .srv import SetPID
11+ from object_detection .srv import ObjectDetection
1112
1213# Initialize controller
1314ctrl = Controller ()
@@ -18,13 +19,14 @@ def __init__(self):
1819
1920 def execute (self , userdata ):
2021 userdata .tool_id = - 1
22+ ctrl .open_gripper ()
2123 ctrl .set_camera_angles (ctrl .HOME_POS_CAMERA_01 )
2224 ctrl .set_arm_joint_angles (ctrl .HOME_POS_MANIPULATOR_01 )
2325 rospy .loginfo ('Executing state IDLE' )
2426 while (True ):
2527 # TODO: Ask for Input
2628 userdata .tool_id = input ("Enter Tool ID you want:" )
27- if (isinstance (userdata .tool_id , int ) and userdata .tool_id > 0 and userdata .tool_id < 4 ):
29+ if (isinstance (userdata .tool_id , int ) and userdata .tool_id > 0 and userdata .tool_id < 5 ):
2830 break
2931
3032 # Return success
@@ -35,24 +37,35 @@ def execute(self, userdata):
3537# define state FindTool
3638class FindTool (smach .State ):
3739 def __init__ (self ):
38- smach .State .__init__ (self , outcomes = ['foundTool' ], input_keys = ['tool_id' ], output_keys = ['frame_name' ])
40+ smach .State .__init__ (self , outcomes = ['foundTool' , 'notfoundTool' ], input_keys = ['tool_id' ], output_keys = ['frame_name' ])
3941
4042 def execute (self , userdata ):
4143 rospy .loginfo ('Executing state FINDTOOL' )
42- print (userdata .tool_id )
43- ## TODO: change the name of the variable frame_name
44- userdata .frame_name = "random"
45- return 'foundTool'
44+ rospy .wait_for_service ('detect_object' )
45+ try :
46+ get_success = rospy .ServiceProxy ('detect_object' , ObjectDetection )
47+ success = get_success (tool_id )
48+ if success :
49+ userdata .frame_name = "/icp_cuboid_frame"
50+ return 'foundTool'
51+ else :
52+ rospy .logwarn ("Service returned false" )
53+ return 'notfoundTool'
54+ except : rospy .ServiceException as e :
55+ rospy .logwarn ("Service called failed:{0}" .format (e ))
56+ return 'notfoundTool'
4657
4758# define state IK1
4859class IK1 (smach .State ):
4960 def __init__ (self ):
5061 smach .State .__init__ (self , outcomes = ['noIK' ,'foundIK' ], input_keys = ['frame_name' ])
5162
5263 def execute (self , userdata ):
53- success = ctrl .go_to_pose ( userdata . frame_name )
64+ ctrl .set_arm_joint_angles ( ctrl . HOME_POS_MANIPULATOR_00 )
5465 rospy .loginfo ('Executing state IK1' )
66+ success = ctrl .go_to_grasp_pose (userdata .frame_name )
5567 return 'foundIK' if success else 'noIK'
68+ # return 'foundIK'
5669
5770# define state Move
5871# class Move(smach.State):
@@ -66,7 +79,7 @@ def execute(self, userdata):
6679# define state Grab
6780class Grab (smach .State ):
6881 def __init__ (self ):
69- smach .State .__init__ (self , outcomes = ['grabSuccess' ,'grabFailure' ])
82+ smach .State .__init__ (self , outcomes = ['grabSuccess' , 'grabFailure' ])
7083
7184 def execute (self , userdata ):
7285 ctrl .close_gripper ()
@@ -104,7 +117,7 @@ def __init__(self):
104117
105118 def execute (self , userdata ):
106119 ## Call service to get the frame id of the hand centroid
107- posestamped = rospy .wait_for_message ("/openface2/head_pose" , PoseStamped )
120+ userdata . head_pose = rospy .wait_for_message ("/openface2/head_pose" , PoseStamped )
108121 rospy .loginfo ('Executing state FindAttention' )
109122 return 'giveTool'
110123
@@ -117,7 +130,7 @@ def execute(self, userdata):
117130 ## Wait till IK not found. Change tolerance and call compute IK again
118131 ## Break go to pose into find pose and compute IK functions
119132 ## Make a different function with gripper pointing upwards and call it from here
120- success = ctrl .go_to_pose (userdata .head_pose )
133+ success = ctrl .go_to_handover_pose (userdata .head_pose )
121134 # if not success
122135 rospy .loginfo ('Executing state IK2' )
123136 return 'foundIK'
@@ -134,7 +147,7 @@ def execute(self, userdata):
134147# define state ChangePID
135148class ChangePID (smach .State ):
136149 def __init__ (self ):
137- smach .State .__init__ (self , outcomes = ['changed' , 'not_changed ' ], input_keys = ['joint_nums' ,'PID' ])
150+ smach .State .__init__ (self , outcomes = ['changed' , 'notchanged ' ], input_keys = ['joint_nums' ,'PID' ])
138151
139152 def execute (self , userdata ):
140153 rospy .wait_for_service ('SetPID' )
@@ -146,7 +159,7 @@ def execute(self, userdata):
146159 return 'changed'
147160 except rospy .ServiceException as e :
148161 rospy .logwarn ("Service call failed:{0}" .format (e ))
149- return 'not_changed '
162+ return 'notchanged '
150163
151164# define state OpenGripper
152165class OpenGripper (smach .State ):
@@ -155,7 +168,7 @@ def __init__(self):
155168
156169 def execute (self , userdata ):
157170 ## Detect when to Open gripper
158- self .open_gripper ()
171+ ctrl .open_gripper ()
159172 rospy .loginfo ('Executing state OpenGripper' )
160173 return 'opened'
161174
@@ -166,17 +179,18 @@ def main():
166179 # Create a SMACH state machine
167180 sm = smach .StateMachine (outcomes = ['stop' ])
168181 sm .userdata .tool_id = - 1
169- sm .userdata .joint_nums = [1 ,2 , 3 ]
182+ sm .userdata .joint_nums = [1 ,6 ]
170183 sm .userdata .PID = [0 ,0 ,0 ]
171184 sis = smach_ros .IntrospectionServer ('server_name' , sm , '/SM_ROOT' )
172185 sis .start ()
186+ rospy .sleep (5 )
173187 # Open the container
174188 with sm :
175189 # Add states to the container
176190 smach .StateMachine .add ('IDLE' , Idle (),
177191 transitions = {'gotToolInput' :'FINDTOOL' })
178192 smach .StateMachine .add ('FINDTOOL' , FindTool (),
179- transitions = {'foundTool' :'IK1' })
193+ transitions = {'foundTool' :'IK1' , 'notfoundTool' : 'IDLE' })
180194 smach .StateMachine .add ('IK1' , IK1 (),
181195 transitions = {'noIK' :'stop' ,'foundIK' :'GRAB' })
182196 # smach.StateMachine.add('MOVE', Move(),
0 commit comments