ui_pri_topics_yes_no.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 
00003 #### this program publish service /answer_yes_no (parameter is the question for the user) and deals with the clients through topics /DM_UI/interface_cmd_yes_no (for the question) and /DM_UI/interface_cmd_yes_no_response (for the answer)
00004 
00005 # ROS imports
00006 
00007 import roslib; roslib.load_manifest('srs_decision_making')
00008 import rospy
00009 import srs_decision_making.srv as xsrv
00010 import srs_msgs.msg as srs_msg
00011 from random import randrange
00012 
00013 class UI_PRI_TOPICS_YES_NO:
00014     
00015     #### Initialization and declaration of the variables ####
00016     def __init__(self):
00017         rospy.loginfo("Public topics for UI_PRI_OBJ_SEL ...")
00018         self.pubUIobjsel = rospy.Publisher('DM_UI/interface_cmd_yes_no',srs_msg.DM_UIobjcom)
00019         self.user_answer =""
00020         self.user_comobj_id = 0
00021         rospy.Subscriber ("DM_UI/interface_cmd_yes_no_response",srs_msg.UI_DMobjresp, self.callbackUIobj)
00022 
00023     ### Declaration of callback function for the object selection commands from the user interface through the topic
00024     def callbackUIobj (self,data):
00025         rospy.loginfo ("I heard %s %i from the UI_PRI",data.solution, data.responseID)
00026         if (data.responseID == self.user_comobj_id):
00027             self.user_answer = data.solution     
00028             rospy.loginfo ("Match between responseID and requestID. Now user_answer is:%s ",self.user_answer)
00029         else:
00030             print ("unfortunately the responceID:%i does not correspond to requestId:%i" % (data.responseID,self.user_comobj_id))
00031             self.user_answer = ""
00032 
00033 ### the handler for the service call    
00034     def selectObjHandle(self,req):
00035 
00036         rospy.loginfo("The user will be invited to select a new object:")
00037 
00038         satisfaction_flag = False
00039         
00040         self.time_out_max = 20
00041         try:
00042             self.time_out_max = rospy.get_param("srs/common/max_time_out")
00043         except Exception, e:
00044             rospy.loginfo("Parameter Server not ready, use default value for max time_out")
00045         
00046         while (not satisfaction_flag):
00047             self.user_answer = ""
00048             self.user_comobj_id = randrange(1000)
00049             message1 = srs_msg.DM_UIobjcom ("ask",req.message,self.user_comobj_id)
00050             self.pubUIobjsel.publish(message1)
00051             rospy.loginfo("Message sent to user to ask Yes or No. RequestId is:", )
00052             
00053             timeout=0
00054             while (self.user_answer =="" and timeout < self.time_out_max):
00055                 print "waiting for response",timeout," seconds from the user"
00056                 timeout = timeout + 1
00057                 print "user_answer is :",self.user_answer
00058                 rospy.sleep(1)
00059 
00060                       
00061             print "the final self.user_answer is.:",self.user_answer
00062 
00063 
00064             if (self.user_answer.strip()=="y" or self.user_answer.strip()=="Y"or self.user_answer.strip()=="yes"or self.user_answer.strip()=="yes" or self.user_answer.strip()=="Yes" or self.user_answer.strip()=="YES"):
00065                 rospy.loginfo("The response from the user is: <<Yes>>.")
00066 
00067                 self.user_respobjsol =""
00068                 self.user_respobjpar = ""
00069                 self.giveup = 0
00070                 self.answer = "Yes"
00071                 satisfaction_flag = True
00072             
00073             else:    
00074 
00075                 if (self.user_answer.strip()=="n" or self.user_answer.strip()=="N"or self.user_answer.strip()=="no" or self.user_answer.strip()=="No" or self.user_answer.strip()=="NO"):
00076                    rospy.loginfo("The response from the user is: <<No>>.")
00077                    satisfaction_flag = True
00078                    self.giveup = 0
00079                    self.answer = "No"
00080 
00081                 
00082                 else: 
00083                    rospy.loginfo("There was no responce from the user or it didn't make sense... Assuming NO")
00084                    satisfaction_flag = True
00085                    self.giveup = 1
00086                    self.answer = "No"
00087 
00088 
00089 
00090                       
00091         return xsrv.answer_yes_noResponse(self.answer,self.giveup)
00092     
00093 if __name__ == '__main__':
00094     rospy.init_node('control_task_server')
00095     UI_PRI_OBJ_TOPIC_s = UI_PRI_TOPICS_YES_NO()
00096     rospy.Service('answer_yes_no', xsrv.answer_yes_no, UI_PRI_OBJ_TOPIC_s.selectObjHandle)
00097     rospy.spin()


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