Go to the documentation of this file.00001
00002
00003
00004 import rospy
00005 import actionlib
00006
00007 from lama_interfaces.core_interface import MapAgentInterface
00008 from lama_interfaces.srv import ActOnMap
00009 from lama_interfaces.srv import ActOnMapRequest
00010 from lama_jockeys.msg import LearnAction
00011 from lama_jockeys.msg import LearnGoal
00012 from lama_jockeys.msg import LearnResult
00013 from lama_msgs.msg import LamaObject
00014
00015 g_has_result = False
00016
00017
00018 def add_edge_to_map(descriptor_link):
00019 """Add two vertices and an edge and assign the segment"""
00020 iface = MapAgentInterface(start=False)
00021 map_agent = rospy.ServiceProxy(iface.action_service_name,
00022 ActOnMap)
00023 rospy.loginfo('Waiting for service')
00024 map_agent.wait_for_service()
00025 rospy.loginfo('MapAgent ready')
00026
00027 edge = LamaObject()
00028 edge.type = edge.EDGE
00029 edge.references = [-1, -1]
00030 edge_response = map_agent(object=edge, action=ActOnMapRequest.PUSH_EDGE)
00031 if not edge_response.objects:
00032 rospy.logerr('Database error')
00033 return
00034 edge_id = edge_response.objects[0].id
00035 rospy.loginfo('edge {} added'.format(edge_id))
00036
00037
00038 map_action = ActOnMapRequest()
00039 map_action.action = map_action.ASSIGN_DESCRIPTOR_EDGE
00040 map_action.object.id = edge_id
00041 map_action.descriptor_id = descriptor_link.descriptor_id
00042 map_action.interface_name = descriptor_link.interface_name
00043 map_agent(map_action)
00044 rospy.loginfo('segment {} assigned to edge {}'.format(
00045 descriptor_link.descriptor_id, edge_id))
00046
00047
00048 def learn_done_cb(state, learn_result):
00049 global g_has_result
00050 if learn_result.final_state == LearnResult.DONE:
00051 add_edge_to_map(learn_result.descriptor_links[0])
00052 rospy.loginfo('Learning finished (jockey triggered)')
00053 g_has_result = True
00054
00055 if __name__ == '__main__':
00056 import sys
00057 rospy.init_node('test_anj_featurenav')
00058 try:
00059 learning_time = float(sys.argv[1])
00060 except (IndexError, ValueError):
00061 learning_time = 5
00062
00063 client = actionlib.SimpleActionClient('anj_featurenav_learner', LearnAction)
00064 client.wait_for_server()
00065
00066 learn_goal = LearnGoal()
00067 learn_goal.action = learn_goal.LEARN
00068 client.send_goal(learn_goal, done_cb=learn_done_cb)
00069
00070 rospy.loginfo('Learning started for at most {} s'.format(learning_time))
00071
00072
00073 rospy.sleep(learning_time)
00074
00075
00076 if not g_has_result:
00077 learn_goal = LearnGoal()
00078 learn_goal.action = learn_goal.STOP
00079 client.send_goal_and_wait(learn_goal)
00080 learn_result = client.get_result()
00081 if not learn_result:
00082 rospy.logerr('Did not receive segment descriptor')
00083 exit()
00084 add_edge_to_map(learn_result.descriptor_links[0])
00085 rospy.loginfo('Learning finished (high-level trigger)')
00086
00087 rospy.spin()