25 from math
import pi, radians
30 from actionlib_msgs.msg
import GoalStatus
31 from geometry_msgs.msg
import Point, Pose, Quaternion, Twist
32 from move_base_msgs.msg
import MoveBaseAction, MoveBaseGoal
33 from tf.transformations
import quaternion_from_euler
34 from visualization_msgs.msg
import Marker
40 rospy.init_node(
'xiaoqiang_navigation', anonymous=
False)
46 square_size = rospy.get_param(
"~square_size", 1.0)
54 euler_angles = (pi/2, pi, 3*pi/2, 0)
58 for angle
in euler_angles:
59 q_angle = quaternion_from_euler(0, 0, angle, axes=
'sxyz')
60 q = Quaternion(*q_angle)
70 waypoints.append(Pose(Point(square_size, 0.0, 0.0), quaternions[0]))
72 Pose(Point(square_size, square_size, 0.0), quaternions[1]))
73 waypoints.append(Pose(Point(0.0, square_size, 0.0), quaternions[2]))
74 waypoints.append(Pose(Point(0.0, 0.0, 0.0), quaternions[3]))
82 for waypoint
in waypoints:
85 self.markers.points.append(p)
89 self.
cmd_vel_pub = rospy.Publisher(
'cmd_vel', Twist, queue_size=0)
94 "move_base", MoveBaseAction)
96 rospy.loginfo(
"Waiting for move_base action server...")
100 self.move_base.wait_for_server(rospy.Duration(60))
102 rospy.loginfo(
"Connected to move base server")
103 rospy.loginfo(
"Starting navigation test")
111 while i < 4
and not rospy.is_shutdown():
114 self.marker_pub.publish(self.
markers)
118 goal = MoveBaseGoal()
122 goal.target_pose.header.frame_id =
'map' 126 goal.target_pose.header.stamp = rospy.Time.now()
130 goal.target_pose.pose = waypoints[i]
141 self.move_base.send_goal(goal)
145 finished_within_time = self.move_base.wait_for_result(
150 if not finished_within_time:
151 self.move_base.cancel_goal()
152 rospy.loginfo(
"Timed out achieving goal")
155 state = self.move_base.get_state()
156 if state == GoalStatus.SUCCEEDED:
157 rospy.loginfo(
"Goal succeeded!")
164 marker_ns =
'waypoints' 166 marker_color = {
'r': 1.0, 'g': 0.7, 'b': 1.0, 'a': 1.0}
171 'waypoint_markers', Marker, queue_size=0)
176 self.markers.ns = marker_ns
177 self.markers.id = marker_id
178 self.markers.type = Marker.CUBE_LIST
179 self.markers.action = Marker.ADD
180 self.markers.lifetime = rospy.Duration(marker_lifetime)
181 self.markers.scale.x = marker_scale
182 self.markers.scale.y = marker_scale
183 self.markers.color.r = marker_color[
'r'] 184 self.markers.color.g = marker_color['g']
185 self.markers.color.b = marker_color[
'b']
186 self.markers.color.a = marker_color[
'a']
188 self.markers.header.frame_id =
'map' 189 self.markers.header.stamp = rospy.Time.now()
190 self.markers.points = list()
193 rospy.loginfo(
"Stopping the robot...")
195 self.move_base.cancel_goal()
198 self.cmd_vel_pub.publish(Twist())
202 if __name__ ==
'__main__':
205 except rospy.ROSInterruptException:
206 rospy.loginfo(
"Navigation test finished.")