forked from JMU-Robotics-Club/example_code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
action_nav.py
97 lines (71 loc) · 3.16 KB
/
action_nav.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python
"""Demo of sending navigation goals using actionlib.
Moves the robot to a designated point relative to the its current
position.
Usage:
action_nav.py TARGET_X TARGET_Y [THETA]
Author: Nathan Sprague
Version: 10/2015
"""
import sys
import rospy
import actionlib
import tf
from move_base_msgs.msg import MoveBaseGoal, MoveBaseAction
from actionlib_msgs.msg import GoalStatus
class NavNode(object):
def __init__(self):
""" Set up the node. """
rospy.init_node('nav_node')
self.ac = actionlib.SimpleActionClient("move_base", MoveBaseAction)
self.state_names = {}
self.state_names[GoalStatus.PENDING] = "PENDING"
self.state_names[GoalStatus.ACTIVE] = "ACTIVE"
self.state_names[GoalStatus.PREEMPTED] = "PREEMPTED"
self.state_names[GoalStatus.SUCCEEDED] = "SUCCEEDED"
self.state_names[GoalStatus.ABORTED] = "ABORTED"
self.state_names[GoalStatus.REJECTED] = "REJECTED"
self.state_names[GoalStatus.RECALLED] = "RECALLED"
self.state_names[GoalStatus.LOST] = "LOST"
def goal_message(self, x_target, y_target, theta_target):
""" Create a goal message in the base_link coordinate frame"""
quat = tf.transformations.quaternion_from_euler(0, 0, theta_target)
# Create a goal message ...
goal = MoveBaseGoal()
goal.target_pose.header.frame_id = "base_link"
goal.target_pose.header.stamp = rospy.get_rostime()
goal.target_pose.pose.position.x = x_target
goal.target_pose.pose.position.y = y_target
goal.target_pose.pose.orientation.x = quat[0]
goal.target_pose.pose.orientation.y = quat[1]
goal.target_pose.pose.orientation.z = quat[2]
goal.target_pose.pose.orientation.w = quat[3]
return goal
def goto_point(self, x_target, y_target, theta_target=0):
""" Move to a location relative to the robot's current position """
rospy.loginfo("navigating to: ({},{})".format(x_target, y_target))
goal = self.goal_message(x_target, y_target, theta_target)
rospy.loginfo("Waiting for server.")
self.ac.wait_for_server()
rospy.loginfo("Sending goal.")
self.ac.send_goal(goal)
rospy.loginfo("Goal Sent.")
# Check in after a while to see how things are going.
rospy.sleep(1.0)
rospy.loginfo("Status Text: {}".format(self.ac.get_goal_status_text()))
# Should be either "ACTIVE"
state_name = self.state_names[self.ac.get_state()]
rospy.loginfo("State : {}".format(state_name))
# Wait until the server reports a result.
self.ac.wait_for_result()
rospy.loginfo("Result Text: {}".format(self.ac.get_goal_status_text()))
# Should be either "SUCCEEDED" or "ABORTED"
state_name = self.state_names[self.ac.get_state()]
rospy.loginfo("State : {}".format(state_name))
if __name__ == "__main__":
nav_node = NavNode()
if len(sys.argv) == 3:
nav_node.goto_point(float(sys.argv[1]), float(sys.argv[2]))
else:
nav_node.goto_point(float(sys.argv[1]), float(sys.argv[2]),
float(sys.argv[3]))