Go to the documentation of this file.00001
00002
00003 from __future__ import print_function
00004
00005 import random
00006
00007 import rospy
00008 import actionlib
00009 from lama_jockeys.msg import NavigateAction
00010 from lama_jockeys.msg import NavigateGoal
00011 from lama_jockeys.msg import LocalizeAction
00012 from lama_jockeys.msg import LocalizeGoal
00013
00014
00015 def navigateFeedbackCallback(feedback):
00016
00017 states = ['', 'TRAVERSING', 'INTERRUPTED']
00018 rospy.loginfo('Navigation current state: ' + states[feedback.current_state])
00019 rospy.loginfo('Time elapsed: {:.3f} s'.format(feedback.time_elapsed.to_sec()))
00020 rospy.loginfo('Completion: {:.2f} %'.format(feedback.completion * 100))
00021
00022 if __name__ == '__main__':
00023 rospy.init_node('random_walker')
00024
00025 navigate = actionlib.SimpleActionClient('navigating_jockey', NavigateAction)
00026 navigate.wait_for_server()
00027 localize = actionlib.SimpleActionClient('localizing_jockey', LocalizeAction)
00028 localize.wait_for_server()
00029
00030 nav_goal = NavigateGoal()
00031 try:
00032 while True:
00033 loc_goal = LocalizeGoal()
00034 loc_goal.action = loc_goal.GET_VERTEX_DESCRIPTOR
00035 localize.send_goal_and_wait(loc_goal, rospy.Duration(0.1))
00036 loc_result = localize.get_result()
00037 if not loc_result:
00038 rospy.logerr('Did not receive vertex descriptor within 0.1 s exiting')
00039 break
00040 rospy.loginfo('Received vertex descriptor')
00041
00042 loc_goal.action = loc_goal.GET_EDGES_DESCRIPTORS
00043 localize.send_goal_and_wait(loc_goal, rospy.Duration(0.5))
00044 loc_result = localize.get_result()
00045 if not loc_result:
00046 rospy.logerr('Did not receive edge descriptors within 0.5 s exiting')
00047 break
00048 rospy.loginfo('Received {} descriptors in {:.3f} s'.format(
00049 len(loc_result.descriptor_links),
00050 loc_result.completion_time.to_sec()))
00051
00052 nav_goal.action = nav_goal.TRAVERSE
00053 random_descriptor = random.choice(loc_result.descriptor_links)
00054 rospy.loginfo('Setting new goal: {}'.format(
00055 random_descriptor.descriptor_id))
00056 nav_goal.descriptor_link = random_descriptor
00057 navigate.send_goal(nav_goal,
00058 feedback_cb=navigateFeedbackCallback)
00059 navigate.wait_for_result(rospy.Duration.from_sec(5.0))
00060 nav_result = navigate.get_result()
00061 if nav_result and nav_result.final_state == nav_result.DONE:
00062 print('Traversed egde {} in {:.2f} s'.format(
00063 nav_goal.descriptor_link.descriptor_id,
00064 nav_result.completion_time.to_sec()))
00065 else:
00066 if rospy.is_shutdown():
00067 break
00068 print('Waited 5 sec for action server without success, retrying')
00069 except rospy.ServiceException, e:
00070 print('Service call failed: {}'.format(e))