Go to the documentation of this file.00001
00002 """
00003 Command Navigator
00004 """
00005 import argparse
00006 import sys
00007
00008 import actionlib
00009 import rospy
00010 import yocs_msgs.msg as yocs_msgs
00011
00012 def parse_arguments():
00013 parser = argparse.ArgumentParser(description="Robot Commander.")
00014 parser.add_argument('-r', '--robot', type=str, help='Robot name')
00015 parser.add_argument('-t', '--target', type=str, help='Target Location')
00016
00017 args = parser.parse_args(sys.argv[1:])
00018 return args.robot, args.target
00019
00020 class NavigateCommander(object):
00021
00022 ACTION_NAVIGATOR = 'navigator'
00023
00024 def __init__(self, robot_name):
00025 '''
00026 Initilise NavigatorCommander with given robot name
00027 '''
00028 self._robot_name = robot_name
00029
00030 ac_name = '/%s/%s'%(robot_name, NavigateCommander.ACTION_NAVIGATOR)
00031 self._ac_navigator = actionlib.SimpleActionClient(ac_name, yocs_msgs.NavigateToAction)
00032 self.loginfo('Setting navigator of %s'%robot_name)
00033 rospy.on_shutdown(self.shutdown)
00034
00035 if not self._ac_navigator.wait_for_server(rospy.Duration(3.0)):
00036 self.logerr('Navigator action server not found. Please check if you have captured %s'%robot_name)
00037 self._ready = False
00038 else:
00039 self._ready = True
00040
00041 def command(self, target):
00042 '''
00043 Send navigate to action
00044 '''
00045 if not self._ready:
00046 self.logerr('Navigator is not ready exiting..')
00047 return
00048
00049 goal = yocs_msgs.NavigateToGoal(location=target, approach_type=yocs_msgs.NavigateToGoal.APPROACH_ON, num_retry=1, timeout=300.0)
00050 self._done = False
00051 self._ac_navigator.send_goal(goal, feedback_cb=self._navigator_feedback)
00052 self._ac_navigator.wait_for_result()
00053 result = self._ac_navigator.get_result()
00054 self.loginfo("%s, Message : %s"%(result.success,result.message))
00055
00056 def _navigator_feedback(self, feedback):
00057 navigator_feed = str("Distance : %s, Remain Time : %s, Message : %s"%(str(feedback.distance),str(feedback.remain_time),str(feedback.message)))
00058 self.loginfo(navigator_feed)
00059
00060 def shutdown(self):
00061 self._ac_navigator.cancel_goal()
00062
00063 def loginfo(self, msg):
00064 rospy.loginfo('%s : %s'%(rospy.get_name(), str(msg)))
00065
00066 def logerr(self, msg):
00067 rospy.logerr('%s : %s'%(rospy.get_name(), str(msg)))
00068
00069
00070 if __name__ == '__main__':
00071 rospy.init_node('navigate_commander')
00072
00073 robot_name, target = parse_arguments()
00074
00075 nc = NavigateCommander(robot_name)
00076 nc.loginfo("initialised")
00077 nc.command(target)
00078 nc.loginfo("Bye Bye")