ui_pri_topics.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 # ROS imports
00003 
00004 
00005 import roslib; roslib.load_manifest('srs_decision_making')
00006 import rospy
00007 import srs_decision_making.srv as xsrv
00008 import srs_msgs.msg as srs_msg
00009 
00010 class UI_PRI_TOPICS:
00011     
00012     #### Initialization and declaration of the variables ####
00013     def __init__(self):
00014         rospy.loginfo("Public topics for UI_PRI ...")
00015         self.pubUIerr = rospy.Publisher('DM_UI/interface_cmd',srs_msg.DM_UIcom)
00016         self.user_respsol =""
00017         self.user_resppar =""
00018         self.user_com_id = 0
00019         rospy.Subscriber ("DM_UI/interface_cmd_response",srs_msg.UI_DMresp, self.callbackUI)
00020 
00021     ### Declaration of  callback function for the commands from the user interface
00022     def callbackUI (self,data):
00023         rospy.loginfo ("I heard %s %s %i from the UI_PRI",data.solution,data.parameter, data.responseID)
00024         if (data.responseID == self.user_com_id):
00025             self.user_respsol = data.solution 
00026             self.user_resppar = data.parameter
00027             rospy.loginfo ("Match between responseId and requestID. Now user_respsol is:%s and user_resppar is:%s",self.user_respsol,self.user_resppar)  
00028         else:
00029             print ("but the id:%s does not correspond to requestId:%s",self.user_com_id)
00030 
00031     
00032     def handle_message_errors(self,req):
00033         print "Returning [%s]"%(req.current_state)
00034         print req.exceptional_case_id
00035         rospy.loginfo("The user will be invited to enter a new position to solve problem :")
00036         self.solfromUser = xsrv.errorsResponse()
00037         satisfaction_flag = False
00038         
00039         self.time_out_max = 30  
00040         try:
00041             self.time_out_max = rospy.get_param("srs/common/max_time_out")
00042         except Exception, e:
00043             rospy.loginfo("Parameter Server not ready, use default value for max time_out") 
00044         
00045         while (not satisfaction_flag):
00046             rospy.loginfo("Message sent to user to ask if he wants to continue:")
00047             self.user_respsol = ""
00048             message1 = srs_msg.DM_UIcom ("continue?","There is an obstacle on the robot's path. Do you want to try to move the robot manually? ",5)
00049             self.user_com_id = 5
00050             self.pubUIerr.publish(message1)
00051             
00052             timeout=0
00053             while (self.user_respsol =="" and timeout < self.time_out_max):
00054                 print "waiting for response",timeout," seconds from the user"
00055                 timeout = timeout + 1
00056                 print "user_respsol is :",self.user_respsol
00057                 rospy.sleep(1)
00058 
00059                       
00060             print "user_respsol is.:",self.user_respsol
00061 
00062 
00063             if (self.user_respsol.strip()=="continue"):
00064                 rospy.loginfo("Just received continue from the user and will ask for a new position to solve the problem :")
00065                 self.user_respsol =""
00066                 self.user_resppar = ""
00067                 message1 = srs_msg.DM_UIcom ("position?","Plese specify a new intermedeate position for the robot to try reaching the target from there",6)
00068                 rospy.loginfo("Just sent a request to the user to give me a new position to solve the problem :")
00069                 self.user_com_id = 6
00070                 self.pubUIerr.publish(message1)
00071                 #t2= raw_input()              
00072                 
00073                 timeout=0
00074                 while (self.user_respsol =="" and timeout < 3*self.time_out_max ):
00075                   print "waiting for response for",timeout," seconds from the user"
00076                   print "user_respsol is :",self.user_respsol
00077                   timeout = timeout + 1
00078                   rospy.sleep(1)
00079                   
00080                 if (self.user_respsol.strip() =="move"):
00081                     satisfaction_flag = True
00082                     self.solfromUser.giveup = 0
00083                     self.solfromUser.solution = self.user_resppar.strip()
00084                     print "We got a move response from the user"
00085                     print(self.solfromUser)
00086                 else:
00087                     satisfaction_flag = False
00088                     rospy.loginfo("The user response doesn't make sense or timed out")
00089                     self.solfromUser.giveup = 1
00090                     self.solfromUser.solution = ""
00091 
00092             else:
00093                 rospy.loginfo("The response from the user is: <<Do not to continue>>. Giving up")
00094                 satisfaction_flag = True
00095                 self.solfromUser.giveup = 1
00096                 self.solfromUser.solution = ""
00097 
00098                 print(self.solfromUser)        
00099                 
00100 
00101         return xsrv.errorsResponse(self.solfromUser.solution,self.solfromUser.giveup)
00102     
00103 if __name__ == '__main__':
00104     rospy.init_node('control_task_server')
00105     UI_PRI_TOPICs = UI_PRI_TOPICS()
00106     rospy.Service('message_errors', xsrv.errors, UI_PRI_TOPICs.handle_message_errors)
00107     rospy.spin()


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