ipa_examples_mod.py
Go to the documentation of this file.
00001 # ROS imports
00002 import roslib
00003 roslib.load_manifest('srs_decision_making')
00004 #import copy
00005 import rospy
00006 import smach
00007 #import smach_ros
00008 
00009 from std_msgs.msg import String, Bool, Int32
00010 from cob_srvs.srv import Trigger
00011 from math import *
00012 import time
00013 from kinematics_msgs.srv import *
00014 
00015 #import actionlib
00016 # ROS imports
00017 import roslib
00018 roslib.load_manifest('srs_states')
00019 import rospy
00020 import smach
00021 # include script server, to move the robot
00022 from simple_script_server import *
00023 sss = simple_script_server()
00024 
00025 # msg imports
00026 from geometry_msgs.msg import *
00027 from shared_state_information import *
00028 
00029 
00030 #from gazebo.srv import *
00031 #import gazebo.msg as gazebo
00032 
00033 
00034 
00035 
00036 
00037 
00038 
00039             
00040 
00041 """
00042 Below dummy generic states are copied and modified based on IPA examples for testing purpose
00043 They should be replaced by real states from other SRS components in the future  
00044 
00045 Basic states related to robot includes:
00046 
00047 approach_pose()
00048 approach_pose_without_retry()
00049 select_grasp()
00050 grasp_side()
00051 grasp_top()
00052 open_door()
00053 put_object_on_tray()
00054 detect_object()
00055 
00056 Only dummy outputs are given for testing purpose
00057 """
00058 
00059 
00060 
00061 
00062 
00063 class select_post_table_pose(smach.State):
00064     def __init__(self):
00065         smach.State.__init__(self, outcomes=['succeeded', 'failed','preempted'], input_keys=['post_table_pos'], output_keys=['post_table_pos'])
00066         #self.counter = 0
00067     
00068     def execute(self, userdata):
00069         global current_task_info
00070         
00071         if current_task_info.get_post_grasp_adjustment_state()==True:  # already adjusted
00072             return 'succeeded'            
00073         else:
00074             pos=current_task_info.get_robot_pos()
00075             
00076             if pos ==None:
00077                 userdata.post_table_pos=''
00078                 return 'failed'
00079             else:
00080                 current_task_info.set_post_grasp_adjustment_state(True)  
00081                 pos.x = pos.x + 0.15 * cos(pos.theta)
00082                 pos.y = pos.y + 0.15 * sin(pos.theta)
00083                 userdata.post_table_pos = list()
00084                 userdata.post_table_pos.append(pos.x)
00085                 userdata.post_table_pos.append(pos.y)
00086                 userdata.post_table_pos.append(pos.theta)
00087                 return 'succeeded'
00088         
00089 
00090 class select_pose(smach.State):
00091     def __init__(self):
00092         smach.State.__init__(self, outcomes=['got_to_next_pose', 'no_more_pose', 'failed','preempted'], 
00093                              input_keys=['target_base_pose_list'], output_keys=['current_target_base_pose'])
00094     def execute(self, userdata):
00095         userdata.current_target_base_pose='home'
00096         return 'got_to_next_pose'
00097 
00098 
00099 ## Put object on tray side state
00100 #
00101 # This state puts a side grasped object on the tray
00102 class put_object_on_tray(smach.State):
00103 
00104     def __init__(self):
00105         smach.State.__init__(
00106             self,
00107             outcomes=['succeeded', 'failed' ,'preempted', 'not_completed'],
00108             input_keys=['grasp_categorisation','surface_distance'])
00109         
00110         
00111     def execute(self, userdata):
00112         #TODO select position on tray depending on how many objects are on the tray already
00113         global current_task_info
00114         ipa_arm_navigation = 'false'
00115             
00116         try:
00117             ipa_arm_navigation = rospy.get_param("srs/ipa_arm_navigation")
00118         except Exception, e:
00119             print('can not read parameter of srs/ipa_arm_navigation, use the default value planned arm navigation disabled')         
00120         
00121         
00122         if True:#current_task_info.object_in_hand and not current_task_info.object_on_tray:
00123             
00124             print userdata.surface_distance
00125             
00126             target_pose = "grasp-to-tray"
00127             
00128             if userdata.surface_distance >= 0.1:
00129                 target_pose = "grasp-to-tray_top"
00130             
00131             # move object to frontside
00132             if ipa_arm_navigation.lower() == 'true':
00133                 handle_arm = sss.move("arm",target_pose ,False, 'Planned')
00134             else:
00135                 handle_arm = sss.move("arm",target_pose ,False)
00136             sss.sleep(2)
00137             sss.move("tray","up")
00138             handle_arm.wait()
00139             if self.preempt_requested():
00140                 self.service_preempt()
00141                 return 'preempted'
00142             
00143             # release object
00144             if userdata.grasp_categorisation == 'side' or userdata.grasp_categorisation == 'front':
00145                 sss.move("sdh","cylopen")
00146             elif userdata.grasp_categorisation == 'top':
00147                 sss.move("sdh","spheropen")
00148                 
00149             #object is transfered from hand to tray 
00150             current_task_info.object_in_hand = False
00151             current_task_info.object_on_tray = True
00152             
00153         if self.preempt_requested():
00154             self.service_preempt()
00155             return 'preempted'
00156         if ipa_arm_navigation.lower() == 'true':
00157             # move arm to backside again
00158             handle_arm = sss.move("arm","tray-to-folded",False, 'Planned')
00159         else:
00160             handle_arm = sss.move("arm","tray-to-folded",False)
00161         sss.sleep(3)
00162         sss.move("sdh","home")
00163         handle_arm.wait()
00164         
00165         if self.preempt_requested():
00166             self.service_preempt()
00167             return 'preempted'
00168         else:     
00169             try:
00170                 service_full_name = '/tray_monitor/occupied'
00171                 #rospy.wait_for_service(service_full_name,rospy.get_param('server_timeout',3))
00172                 rospy.wait_for_service(service_full_name,3)
00173                             
00174                 # to check if the tray is ocuppied
00175                 is_ocuppied = rospy.ServiceProxy(service_full_name,Trigger)
00176                 resp = is_ocuppied()
00177                 print "###Checking if there is any component on the tray..." 
00178                 print "###is_ocuppied? ", resp
00179                             
00180                 if(resp is not True):
00181                     print "The tray is not ocuppied."
00182                     return 'not_completed'
00183                 else:
00184                     return 'succeeded'
00185             except rospy.ROSException, e:
00186                     error_message = "%s"%e
00187                     rospy.logerr("<<%s>> service not available, error: %s",service_full_name, error_message)
00188                     print "the service /tray_monitor/occupied is not available"
00189                     return 'failed'
00190 
00191 
00192 #verify_object FROM PRO+IPA, the interface still need to be clarified 
00193 class verify_object(smach.State):
00194 
00195     def __init__(self):
00196         smach.State.__init__(self, outcomes=['object_verified','no_object_verified','failed','preempted'],
00197                                 input_keys=['reference_to_map','object_name_list'],
00198                                 output_keys=['updated_object_list'])
00199         
00200     def execute(self,userdata):
00201         # user specify key region on interface device for detection
00202         """
00203         Extract objects from current point map
00204         """
00205         updated_object = ""   #updated pose information about the object
00206         return 'failed'  
00207 
00208 #scan environment from IPA, the interface still need to be clarified    
00209 class update_env_model(smach.State):
00210     def __init__(self):
00211         smach.State.__init__(self, outcomes=['succeeded', 'not_completed', 'failed', 'preempted'])
00212         
00213     def execute(self,userdata):
00214         # user specify key region on interface device for detection
00215         """
00216         Get current point map
00217         """
00218         #map_reference = ""   
00219         return 'succeeded'      
00220 
00221 
00222 #verify_object FROM PRO+IPA, the interface still need to be clarified 
00223 #for integration meeting
00224 class object_verification_simple(smach.State):
00225 
00226     def __init__(self):
00227         smach.State.__init__(self, outcomes=['object_verified','no_object_verified','failed','preempted'],
00228                                 input_keys=['target_object_name','target_object_hh_id','target_object_pose'],
00229                                 output_keys=['target_object_pose'])
00230         #object verified: the expected object has been found, the revised pose is in the output_put key verified_target_object_pose
00231         #no_object_verified: no object found, given up
00232         
00233                 
00234     def execute(self,userdata):
00235         # user specify key region on interface device for detection
00236         
00237         
00238         
00239         #dummy code for testing
00240         #verified object is in the exact expected position
00241         #return object_verfied
00242         
00243         userdata.target_object_pose = userdata.target_object_pose
00244         return 'object_verified'  
00245 
00246     
00247 """
00248 ## Detect state
00249 #
00250 # This state will try to detect an object.
00251 class detect_object(smach.State):
00252     def __init__(self,object_name = "",max_retries = 1):
00253         smach.State.__init__(
00254             self,
00255             outcomes=['succeeded','retry','no_more_retries','failed'],
00256             input_keys=['object_name'],
00257             output_keys=['object'])
00258 
00259         self.object_name = object_name
00260         
00261 
00262     def execute(self, userdata):
00263         # determine object name
00264         if self.object_name != "":
00265             object_name = self.object_name
00266         elif type(userdata.object_name) is str:
00267             object_name = userdata.object_name
00268         else: # this should never happen
00269             rospy.logerr("Invalid userdata 'object_name'")
00270             self.retries = 0
00271             return 'failed'
00272 
00273         return 'succeeded'
00274 
00275 
00276 
00277 
00278 
00279 class move_head(smach.State):
00280 
00281     def __init__(self):
00282         smach.State.__init__(
00283             self,
00284             outcomes=['succeeded', 'failed'],
00285             input_keys=['torso_pose'])
00286         
00287         self.torso_poses = []
00288         self.torso_poses.append("home")
00289         self.torso_poses.append("left")
00290         self.torso_poses.append("right")
00291 
00292 
00293     def execute(self, userdata):
00294         sss.move("torso",userdata.torso_pose)
00295         return 'succeeded'
00296 
00297 
00298 
00299 
00300 ## Deliver object state
00301 #
00302 # This state will deliver an object which should be on the tray.
00303 class deliver_object(smach.State):
00304     def __init__(self):
00305         smach.State.__init__(self, 
00306             outcomes=['succeeded', 'retry', 'failed'])
00307 
00308     def execute(self, userdata):
00309         
00310         #sss.say(["Here is your " + userdata.object_name + ". Please help yourself."],False)
00311         sss.move("torso","nod",False)
00312         
00313         try:
00314             rospy.wait_for_service('/tray/check_occupied',10)
00315         except rospy.ROSException, e:
00316             rospy.loginfo("\n\nService not available: %s", e)
00317             rospy.loginfo('\n\nIf the tray has been emptied? Please enter Yes/No - Y/N')
00318             inp = raw_input()
00319             if inp == 'y' or inp == 'Y':
00320                 
00321                 sss.move("tray","down",False)
00322                 sss.move("torso","nod",False)
00323                 return  'succeeded'
00324             else:
00325                 return 'failed'
00326 
00327         time = rospy.Time.now().secs
00328         loop_rate = rospy.Rate(5) #hz
00329         while True:
00330             if rospy.Time.now().secs-time > 20:
00331                 return 'retry'
00332             try:
00333                 tray_service = rospy.ServiceProxy('/tray/check_occupied', CheckOccupied)            
00334                 req = CheckOccupiedRequest()
00335                 res = tray_service(req)
00336                 print "waiting for tray to be not occupied any more"
00337                 if(res.occupied.data == False):
00338                     break
00339             except rospy.ServiceException, e:
00340                 print "Service call failed: %s", e
00341                 return 'failed'
00342             sss.sleep(2)
00343         
00344         sss.move("tray","down",False)
00345         sss.move("torso","nod",False)
00346         
00347         return 'succeeded'
00348 """
00349 


srs_decision_making
Author(s): Renxi Qiu
autogenerated on Mon Oct 6 2014 08:38:32