$search
00001 import roslib 00002 roslib.load_manifest('srs_states') 00003 import rospy 00004 import smach 00005 import smach_ros 00006 00007 from math import * 00008 import copy 00009 from struct import * 00010 00011 from simple_script_server import * 00012 sss = simple_script_server() 00013 00014 from cob_object_detection_msgs.msg import * 00015 from cob_object_detection_msgs.srv import * 00016 from shared_state_information import * 00017 from eval_objects import * 00018 from cob_3d_mapping_msgs.msg import * 00019 00020 from srs_symbolic_grounding.srv import * 00021 #from srs_symbolic_grounding.msg import * 00022 00023 #from srs_grasping.srv import * 00024 from geometry_msgs.msg import * 00025 00026 from sensor_msgs.msg import * 00027 from srs_assisted_detection.srv import * 00028 from geometry_msgs.msg import * 00029 ## Detect state 00030 # 00031 # outcomes save the outcome from the sm from the service function 00032 outcome_detectObjectSrv = '' 00033 outcome_user_intervention = '' 00034 #flag will be set if the service was called 00035 assisted_detection_service_called=False 00036 user_intervention_service_called=0 00037 00038 00039 class detect_object_assited(smach.State): 00040 def __init__(self,object_name = ""): 00041 smach.State.__init__( 00042 self, 00043 outcomes=['succeeded','failed','preempted'], 00044 input_keys=['object_name'], 00045 output_keys=['object_list']) 00046 00047 self.object_list = DetectionArray() 00048 self.object_name = object_name 00049 self.srv_name_object_detection = '/object_detection/detect_object' 00050 00051 self.torso_poses = [] 00052 self.torso_poses.append("back_right") 00053 self.torso_poses.append("back") 00054 self.torso_poses.append("back_left") 00055 self.torso_poses.append("back_right_extreme") 00056 self.torso_poses.append("back_extreme") 00057 self.torso_poses.append("back_left_extreme") 00058 #self.listener = tf.TransformListener() 00059 self.the_object = '' 00060 self.the_object_pose = '' 00061 00062 00063 def execute(self, userdata): 00064 00065 global s 00066 rospy.loginfo("Assisted Detection ready.") 00067 self.object_name=userdata.object_name 00068 s = rospy.Service('assisted_detection', UiDetector, self.detectObjectSrv) 00069 s.spin() 00070 if (assisted_detection_service_called): 00071 userdata.object_list=self.object_list 00072 return outcome_detectObjectSrv 00073 00074 def detectObjectSrv(self,req): 00075 00076 00077 00078 global outcome_detectObjectSrv 00079 00080 if self.preempt_requested(): 00081 self.service_preempt() 00082 outcome_detectObjectSrv = 'preempted' 00083 return detector_response 00084 00085 # move sdh as feedback 00086 sss.move("sdh","cylclosed",False) 00087 00088 # make the robot ready to inspect the scene 00089 00090 sss.say([current_task_info.speaking_language['Search'] + self.object_name + "."],False) 00091 handle_arm = sss.move("arm","folded-to-look_at_table",False) 00092 handle_torso = sss.move("torso","shake",False) 00093 handle_head = sss.move("head","back",False) 00094 00095 if self.preempt_requested(): 00096 self.service_preempt() 00097 #handle_base.set_failed(4) 00098 handle_arm.client.cancel_goal() 00099 handle_torso.client.cancel_goal() 00100 handle_head.client.cancel_goal() 00101 00102 outcome_detectObjectSrv = 'preempted' 00103 else: 00104 handle_arm.wait() 00105 handle_head.wait() 00106 handle_torso.wait() 00107 00108 00109 00110 # move sdh as feedback 00111 sss.move("sdh","home",False) 00112 00113 # wait for image to become stable 00114 sss.sleep(2) 00115 00116 00117 # check if object detection service is available 00118 try: 00119 rospy.wait_for_service(self.srv_name_object_detection,10) 00120 except rospy.ROSException, e: 00121 print "Service not available: %s"%e 00122 outcome_detectObjectSrv = 'failed' 00123 00124 # call object detection service 00125 try: 00126 detector_service = rospy.ServiceProxy(self.srv_name_object_detection, DetectObjects) 00127 req = DetectObjectsRequest() 00128 req.object_name.data = self.object_name 00129 res = detector_service(req) 00130 self.object_list=res.object_list 00131 00132 00133 00134 outcome_detectObjectSrv = 'succeeded' 00135 except rospy.ServiceException, e: 00136 print "Service call failed: %s"%e 00137 00138 outcome_detectObjectSrv = 'failed' 00139 00140 detector_response=UiDetectorResponse() 00141 if len(res.object_list.detections) > 0: 00142 detector_response.object_list.header=res.object_list.header 00143 for x in range(len(res.object_list.detections)): 00144 detector_response.object_list.detections.insert(x,res.object_list.detections[x]) 00145 00146 #shutdown server and set flag true 00147 global assisted_detection_service_called 00148 assisted_detection_service_called=True 00149 s.shutdown() 00150 00151 00152 self.object_list=detector_response.object_list 00153 #return service answer 00154 return detector_response 00155 00156 00157 00158 00159 00160 00161 00162 00163 00164 00165 class user_intervention_on_detection(smach.State): 00166 def __init__(self): 00167 smach.State.__init__(self, 00168 outcomes=['succeeded', 'bb_move', 'give_up', 'failed', 'preempted'], 00169 input_keys = ['target_object_name', 'target_object_list'], 00170 output_keys=['object','object_pose','bb_pose']) 00171 00172 self.target_object_name='' 00173 self.object_list=UiDetectorResponse() 00174 00175 self.object=Detection() 00176 self.object_pose=Pose() 00177 self.bb_pose=Pose2D() 00178 00179 00180 def execute(self, userdata): 00181 if self.preempt_requested(): 00182 self.service_preempt() 00183 return 'preempted' 00184 00185 self.target_object_name=userdata.target_object_name 00186 00187 self.object_list=userdata.target_object_list 00188 global s 00189 global s2 00190 00191 s = rospy.Service('assisted_answer', UiAnswer, self.answerObjectSrv) 00192 s2 = rospy.Service('assisted_BBmove', BBMove, self.moveBBSrv) 00193 00194 rospy.loginfo("Assisted Answer ready.") 00195 s.spin() 00196 s2.spin() 00197 if(user_intervention_service_called==1): 00198 #userdata.object=self.object 00199 #userdata.object_pose=self.object_pose 00200 #userdata.bb_pose=self.bbpose 00201 global listener 00202 try: 00203 #transform object_pose into base_link 00204 object_pose_in = self.object.pose 00205 object_pose_in.header.stamp = listener.getLatestCommonTime("/map",object_pose_in.header.frame_id) 00206 object_pose_map = listener.transformPose("/map", object_pose_in) 00207 except rospy.ROSException, e: 00208 print "Transformation not possible: %s"%e 00209 return 'failed' 00210 00211 00212 userdata.object_pose=object_pose_map 00213 userdata.object=self.object 00214 00215 return outcome_user_intervention 00216 if(user_intervention_service_called==2): 00217 print self.bb_pose 00218 userdata.bb_pose=[self.bb_pose.x,self.bb_pose.y,self.bb_pose.theta] 00219 return outcome_user_intervention 00220 00221 00222 00223 def answerObjectSrv(self,req): 00224 global user_intervention_service_called 00225 global outcome_user_intervention 00226 00227 user_intervention_service_called=1 00228 00229 rospy.loginfo("Get Object information") 00230 answer=UiAnswerResponse() 00231 00232 if(req.action=='give up'): 00233 outcome_user_intervention = 'give up' 00234 answer.message.data='process stopped' 00235 return answer 00236 00237 00238 00239 #save 00240 00241 00242 00243 #get position from good object 00244 pose=Pose() 00245 pose.position.x=self.object_list.detections[req.id].pose.pose.position.x 00246 pose.position.y=self.object_list.detections[req.id].pose.pose.position.y 00247 pose.position.z=self.object_list.detections[req.id].pose.pose.position.z 00248 pose.orientation.x=self.object_list.detections[req.id].pose.pose.orientation.x 00249 pose.orientation.y=self.object_list.detections[req.id].pose.pose.orientation.y 00250 pose.orientation.z=self.object_list.detections[req.id].pose.pose.orientation.z 00251 pose.orientation.w=self.object_list.detections[req.id].pose.pose.orientation.w 00252 00253 print pose 00254 self.object_pose=pose 00255 self.object=self.object_list.detections[req.id] 00256 00257 00258 #global action 00259 #default for user 00260 #action='grasp' 00261 00262 00263 #shutdown both service 00264 s.shutdown() 00265 s2.shutdown() 00266 00267 outcome_user_intervention = 'succeeded' 00268 answer.message.data='Action is running' 00269 return answer 00270 00271 00272 00273 00274 def moveBBSrv(self,req): 00275 rospy.loginfo("Get BB information") 00276 global user_intervention_service_called 00277 global outcome_user_intervention 00278 00279 user_intervention_service_called=2 00280 #BBmove service base and then movement 00281 moveBB=BBMoveResponse() 00282 00283 try: 00284 rospy.wait_for_service('scan_base_pose',10) 00285 except rospy.ROSException, e: 00286 print "Service not available: %s"%e 00287 s.shutdown() 00288 s2.shutdown() 00289 moveBB.message.data='service failed try again' 00290 00291 outcome_detectObjectSrv = 'failed' 00292 00293 try: 00294 base_pose_service = rospy.ServiceProxy('scan_base_pose', ScanBasePose) 00295 req_scan = ScanBasePoseRequest() 00296 srs_info=SRSSpatialInfo() 00297 srs_info.l=req.l 00298 srs_info.w=req.w 00299 srs_info.h=req.h 00300 srs_info.pose=req.pose 00301 req_scan.parent_obj_geometry=srs_info 00302 res = base_pose_service(req_scan) 00303 self.bb_pose=res.scan_base_pose_list[0] 00304 #print res 00305 s.shutdown() 00306 s2.shutdown() 00307 moveBB.message.data='moving to better position' 00308 00309 00310 00311 outcome_user_intervention = 'bb_move' 00312 except rospy.ServiceException, e: 00313 print "Service call failed: %s"%e 00314 s.shutdown() 00315 s2.shutdown() 00316 moveBB.message.data='service failed try again' 00317 00318 outcome_user_intervention = 'failed' 00319 00320 #service call to get better position 00321 #pose=Pose() 00322 # pose.position.x=1 00323 #pose.position.y=2 00324 #pose.position.z=3 00325 #self.bb_pose=pose 00326 00327 # outcome_user_intervention='bb_move' 00328 00329 #shutdown both service 00330 00331 return moveBB