trajectory_marker_publisher.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Copyright (c) 2016 The UUV Simulator Authors.
3 # All rights reserved.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 from __future__ import print_function
17 import rospy
18 import os
19 import yaml
20 from datetime import datetime
21 from std_msgs.msg import Bool
22 from nav_msgs.msg import Path
23 from visualization_msgs.msg import MarkerArray, Marker
24 from geometry_msgs.msg import PoseStamped, Point, Quaternion
25 from uuv_control_msgs.msg import Trajectory, TrajectoryPoint, WaypointSet
26 import uuv_trajectory_generator
27 import uuv_waypoints
28 
29 
31  def __init__(self):
32  self._trajectory_sub = rospy.Subscriber(
33  'trajectory', Trajectory, self._update_trajectory)
34 
35  self._waypoints_sub = rospy.Subscriber(
36  'waypoints', WaypointSet, self._update_waypoints)
37 
38  # Vehicle state flags
39  self._is_auto_on = False
40  self._is_traj_tracking_on = False
42 
43  self._automatic_mode_sub = rospy.Subscriber(
44  'automatic_on', Bool, self._update_auto_mode)
45 
46  self._traj_tracking_mode_sub = rospy.Subscriber(
47  'trajectory_tracking_on', Bool, self._update_traj_tracking_mode)
48 
49  self._station_keeping_mode_sub = rospy.Subscriber(
50  'station_keeping_on', Bool, self._update_station_keeping_mode)
51 
52  self._reference_sub = rospy.Subscriber(
53  'reference', TrajectoryPoint, self._reference_callback)
54 
55  # Waypoint set received
56  self._waypoints = None
57  # Trajectory received
58  self._trajectory = None
59 
60  self._output_dir = None
61  if rospy.has_param('~output_dir'):
62  self._output_dir = rospy.get_param('~output_dir')
63  if not os.path.isdir(self._output_dir):
64  print('Invalid output directory, not saving the files, dir=', self._output_dir)
65  self._output_dir = None
66  else:
67  self._output_dir = os.path.join(self._output_dir, rospy.get_namespace().replace('/', ''))
68  if not os.path.isdir(self._output_dir):
69  os.makedirs(self._output_dir)
70 
71  # Visual marker publishers
72  self._trajectory_path_pub = rospy.Publisher(
73  'trajectory_marker', Path, queue_size=1)
74 
75  self._waypoint_markers_pub = rospy.Publisher(
76  'waypoint_markers', MarkerArray, queue_size=1)
77 
78  self._waypoint_path_pub = rospy.Publisher(
79  'waypoint_path_marker', Path, queue_size=1)
80 
81  self._reference_marker_pub = rospy.Publisher(
82  'reference_marker', Marker, queue_size=1)
83 
84  self._update_markers_timer = rospy.Timer(
85  rospy.Duration(0.5), self._update_markers)
86 
87  def _update_markers(self, event):
88  if self._waypoints is None:
89  waypoint_path_marker = Path()
90  t = rospy.Time.now()
91  waypoint_path_marker.header.stamp = t
92  waypoint_path_marker.header.frame_id = 'world'
93 
94  waypoint_marker = MarkerArray()
95  marker = Marker()
96  marker.header.stamp = t
97  marker.header.frame_id = 'world'
98  marker.id = 0
99  marker.type = Marker.SPHERE
100  marker.action = 3
101  else:
102  waypoint_path_marker = self._waypoints.to_path_marker()
103  waypoint_path_marker.header.frame_id = self._waypoints.inertial_frame_id
104  waypoint_marker = self._waypoints.to_marker_list()
105 
106  self._waypoint_path_pub.publish(waypoint_path_marker)
107  self._waypoint_markers_pub.publish(waypoint_marker)
108 
109  traj_marker = Path()
110  traj_marker.header.stamp = rospy.Time.now()
111  traj_marker.header.frame_id = 'world'
112 
113  if self._trajectory is not None:
114  traj_marker.header.frame_id = self._trajectory.header.frame_id
115  for pnt in self._trajectory.points:
116  p_msg = PoseStamped()
117  p_msg.header.stamp = pnt.header.stamp
118  p_msg.header.frame_id = self._trajectory.header.frame_id
119  p_msg.pose = pnt.pose
120  traj_marker.poses.append(p_msg)
121 
122  self._trajectory_path_pub.publish(traj_marker)
123  return True
124 
125  def _update_trajectory(self, msg):
126  self._trajectory = msg
127 
128  def _update_waypoints(self, msg):
129  self._waypoints = uuv_waypoints.WaypointSet()
130  self._waypoints.from_message(msg)
131 
132  def _update_auto_mode(self, msg):
133  self._is_auto_on = msg.data
134 
136  self._is_station_keeping_on = msg.data
137 
139  self._is_traj_tracking_on = msg.data
140 
141  def _reference_callback(self, msg):
142  marker = Marker()
143  marker.header.stamp = rospy.Time.now()
144  marker.header.frame_id = msg.header.frame_id
145  marker.id = 0
146  marker.type = Marker.SPHERE
147  marker.action = Marker.MODIFY
148 
149  marker.pose.position = msg.pose.position
150  marker.scale.x = 0.3
151  marker.scale.y = 0.3
152  marker.scale.z = 0.3
153 
154  marker.color.a = 1.0
155  marker.color.r = 0.0
156  marker.color.g = 1.0
157  marker.color.b = 0.0
158 
159  self._reference_marker_pub.publish(marker)
160 
161 if __name__ == '__main__':
162  print('Starting trajectory and waypoint marker publisher')
163  rospy.init_node('trajectory_marker_publisher')
164 
165  try:
167  rospy.spin()
168  except rospy.ROSInterruptException:
169  print('caught exception')
170  print('exiting')


uuv_control_utils
Author(s): Musa Morena Marcusso Manhaes , Sebastian Scherer , Luiz Ricardo Douat
autogenerated on Thu Jun 18 2020 03:28:47