take_human_input.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 
00003 import time
00004 import rospy
00005 
00006 import roslib; roslib.load_manifest('bwi_tasks')
00007 
00008 import actionlib
00009 from bwi_kr_execution.msg import *
00010 import segbot_gui.srv
00011 
00012 from multiprocessing import Process, Value
00013 
00014 human_waiting = False
00015 next_room = None
00016 
00017 def get_human_waiting():
00018     global human_waiting
00019     return human_waiting
00020 
00021 def set_human_waiting(value):
00022     global human_waiting
00023     human_waiting = value
00024 
00025 # option 
00026 # 1: click me if you need help
00027 # 2: please let me know your goal place
00028 def gui_thread(human_waiting):
00029   
00030     rospy.init_node('human_input_gui_thread')
00031 
00032     print("gui_thread started")
00033 
00034     rospy.wait_for_service('question_dialog')
00035     
00036     while not rospy.is_shutdown():
00037 
00038         if human_waiting.value == True:
00039             rospy.sleep(2)
00040             continue
00041 
00042         try:
00043             handle = rospy.ServiceProxy('question_dialog', segbot_gui.srv.QuestionDialog)
00044             res = handle(1, "Please click the button, if you need my help.", ["Button"], 0)
00045             human_waiting.value = True
00046             res = handle(0, "Follow me please, I am moving fast.", ["Button"], 0)
00047 
00048         except rospy.ServiceException, e:
00049             print ("Service call failed")
00050     
00051     return True
00052 
00053 def platform_thread(human_waiting):
00054 
00055     rospy.init_node('human_input_platform_thread')
00056     rospy.wait_for_service('question_dialog')
00057 
00058     print("platform_thread started")
00059 
00060     rooms = ["414a", "414b", "416", "418", "420"]
00061     doors = ["d3_414a1", "d3_414b1", "d3_416", "d3_418", "d3_420"]
00062     
00063     client = actionlib.SimpleActionClient('/action_executor/execute_plan',\
00064             ExecutePlanAction)
00065     client.wait_for_server()
00066 
00067 
00068     while not rospy.is_shutdown():
00069         print("human_waiting: " + str(human_waiting))
00070 
00071         if human_waiting.value == True:
00072             print("Human is waiting. Let me see where the goal is.")
00073 
00074             try:
00075                 handle = rospy.ServiceProxy('question_dialog', segbot_gui.srv.QuestionDialog)
00076                 res = handle(1, "Where's your goal? \n\nPlease select the room.", rooms, 60)
00077             except rospy.ServiceException, e:
00078                 print ("Service call failed: %s"%e)
00079 
00080             if res.index == None:
00081 
00082                 try:
00083                     handle = rospy.ServiceProxy('question_dialog', segbot_gui.srv.QuestionDialog)
00084                     handle(0, "It seems you do not need me anymore. \nI am leaving.", doors, 0)
00085                 except rospy.ServiceException, e:
00086                     print ("Service call failed: %s"%e)
00087 
00088                 loc = doors[0]
00089 
00090             else: 
00091 
00092                 try:
00093                     handle = rospy.ServiceProxy('question_dialog', segbot_gui.srv.QuestionDialog)
00094                     handle(0, "Follow me please. We are arriving soon. ", doors, 0)
00095                 except rospy.ServiceException, e:
00096                     print ("Service call failed: %s"%e)
00097 
00098                 loc = doors[res.index]
00099 
00100             goal = ExecutePlanGoal()
00101             rule = AspRule()
00102             fluent = AspFluent()
00103             
00104             fluent.name = "not beside"
00105             fluent.variables = [loc]
00106             rule.body = [fluent]
00107             goal.aspGoal = [rule]
00108             
00109             print("sending goal: " + loc)
00110             client.send_goal(goal)
00111     
00112             client.wait_for_result()
00113 
00114             try:
00115                 handle = rospy.ServiceProxy('question_dialog', segbot_gui.srv.QuestionDialog)
00116                 res = handle(0, "You have arrived. I am leaving. \n\nThank you!", doors, 0)
00117                 rospy.sleep(10)
00118 
00119             except rospy.ServiceException, e:
00120                 print ("Service call failed: %s"%e)
00121 
00122             human_waiting.value = False
00123 
00124         else:
00125             print("No one needs my help. Let me take a random walk.")
00126 
00127             loc = doors[int(time.time()) % len(rooms)]
00128             
00129             goal = ExecutePlanGoal()
00130             rule = AspRule()
00131             fluent = AspFluent()
00132             
00133             fluent.name = "not beside"
00134             fluent.variables = [loc]
00135             rule.body = [fluent]
00136             goal.aspGoal = [rule]
00137             
00138             print("sending goal: " + loc)
00139             client.send_goal(goal)
00140     
00141             client.wait_for_result()
00142 
00143 
00144     return 1
00145 
00146 if __name__ == '__main__':
00147 
00148   try:
00149 
00150     human_waiting = Value('b', False)
00151 
00152     p1 = Process(target = gui_thread, args = (human_waiting, ))
00153     p2 = Process(target = platform_thread, args = (human_waiting, ))
00154 
00155     p1.start()
00156     p2.start()
00157     p1.join()
00158     p2.join()
00159 
00160   except:
00161 
00162     rospy.loginfo("Error: unable to start thread")
00163 
00164 


bwi_tasks
Author(s): Matteo Leonetti, Shiqi Zhang
autogenerated on Fri Aug 28 2015 10:14:58