8
8
from controller import Controller
9
9
from geometry_msgs .msg import PoseStamped
10
10
from dynamixel_workbench_msgs .srv import SetPID
11
+ from object_detection .srv import ObjectDetection
11
12
12
13
# Initialize controller
13
14
ctrl = Controller ()
@@ -18,13 +19,14 @@ def __init__(self):
18
19
19
20
def execute (self , userdata ):
20
21
userdata .tool_id = - 1
22
+ ctrl .open_gripper ()
21
23
ctrl .set_camera_angles (ctrl .HOME_POS_CAMERA_01 )
22
24
ctrl .set_arm_joint_angles (ctrl .HOME_POS_MANIPULATOR_01 )
23
25
rospy .loginfo ('Executing state IDLE' )
24
26
while (True ):
25
27
# TODO: Ask for Input
26
28
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 ):
28
30
break
29
31
30
32
# Return success
@@ -35,24 +37,35 @@ def execute(self, userdata):
35
37
# define state FindTool
36
38
class FindTool (smach .State ):
37
39
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' ])
39
41
40
42
def execute (self , userdata ):
41
43
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'
46
57
47
58
# define state IK1
48
59
class IK1 (smach .State ):
49
60
def __init__ (self ):
50
61
smach .State .__init__ (self , outcomes = ['noIK' ,'foundIK' ], input_keys = ['frame_name' ])
51
62
52
63
def execute (self , userdata ):
53
- success = ctrl .go_to_pose ( userdata . frame_name )
64
+ ctrl .set_arm_joint_angles ( ctrl . HOME_POS_MANIPULATOR_00 )
54
65
rospy .loginfo ('Executing state IK1' )
66
+ success = ctrl .go_to_grasp_pose (userdata .frame_name )
55
67
return 'foundIK' if success else 'noIK'
68
+ # return 'foundIK'
56
69
57
70
# define state Move
58
71
# class Move(smach.State):
@@ -66,7 +79,7 @@ def execute(self, userdata):
66
79
# define state Grab
67
80
class Grab (smach .State ):
68
81
def __init__ (self ):
69
- smach .State .__init__ (self , outcomes = ['grabSuccess' ,'grabFailure' ])
82
+ smach .State .__init__ (self , outcomes = ['grabSuccess' , 'grabFailure' ])
70
83
71
84
def execute (self , userdata ):
72
85
ctrl .close_gripper ()
@@ -104,7 +117,7 @@ def __init__(self):
104
117
105
118
def execute (self , userdata ):
106
119
## 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 )
108
121
rospy .loginfo ('Executing state FindAttention' )
109
122
return 'giveTool'
110
123
@@ -117,7 +130,7 @@ def execute(self, userdata):
117
130
## Wait till IK not found. Change tolerance and call compute IK again
118
131
## Break go to pose into find pose and compute IK functions
119
132
## 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 )
121
134
# if not success
122
135
rospy .loginfo ('Executing state IK2' )
123
136
return 'foundIK'
@@ -134,7 +147,7 @@ def execute(self, userdata):
134
147
# define state ChangePID
135
148
class ChangePID (smach .State ):
136
149
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' ])
138
151
139
152
def execute (self , userdata ):
140
153
rospy .wait_for_service ('SetPID' )
@@ -146,7 +159,7 @@ def execute(self, userdata):
146
159
return 'changed'
147
160
except rospy .ServiceException as e :
148
161
rospy .logwarn ("Service call failed:{0}" .format (e ))
149
- return 'not_changed '
162
+ return 'notchanged '
150
163
151
164
# define state OpenGripper
152
165
class OpenGripper (smach .State ):
@@ -155,7 +168,7 @@ def __init__(self):
155
168
156
169
def execute (self , userdata ):
157
170
## Detect when to Open gripper
158
- self .open_gripper ()
171
+ ctrl .open_gripper ()
159
172
rospy .loginfo ('Executing state OpenGripper' )
160
173
return 'opened'
161
174
@@ -166,17 +179,18 @@ def main():
166
179
# Create a SMACH state machine
167
180
sm = smach .StateMachine (outcomes = ['stop' ])
168
181
sm .userdata .tool_id = - 1
169
- sm .userdata .joint_nums = [1 ,2 , 3 ]
182
+ sm .userdata .joint_nums = [1 ,6 ]
170
183
sm .userdata .PID = [0 ,0 ,0 ]
171
184
sis = smach_ros .IntrospectionServer ('server_name' , sm , '/SM_ROOT' )
172
185
sis .start ()
186
+ rospy .sleep (5 )
173
187
# Open the container
174
188
with sm :
175
189
# Add states to the container
176
190
smach .StateMachine .add ('IDLE' , Idle (),
177
191
transitions = {'gotToolInput' :'FINDTOOL' })
178
192
smach .StateMachine .add ('FINDTOOL' , FindTool (),
179
- transitions = {'foundTool' :'IK1' })
193
+ transitions = {'foundTool' :'IK1' , 'notfoundTool' : 'IDLE' })
180
194
smach .StateMachine .add ('IK1' , IK1 (),
181
195
transitions = {'noIK' :'stop' ,'foundIK' :'GRAB' })
182
196
# smach.StateMachine.add('MOVE', Move(),
0 commit comments