visualize_obstacle_velocity_profile.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # This small script subscribes to the FeedbackMsg message of teb_local_planner
4 # and plots the current velocity.
5 # publish_feedback must be turned on such that the planner publishes this information.
6 # Author: christoph.roesmann@tu-dortmund.de
7 
8 import rospy, math
9 from teb_local_planner.msg import FeedbackMsg, TrajectoryMsg, TrajectoryPointMsg
10 from costmap_converter.msg import ObstacleArrayMsg
11 from geometry_msgs.msg import PolygonStamped, Point32, Twist
12 import numpy as np
13 import matplotlib.pyplot as plotter
14 
15 def twist_callback(data):
16  global trajectory_gt
17  global start_t
18 
19  if (start_t == 0):
20  start_t = rospy.Time.now() # cmd_vel doesn't provide any time information, therefore measure it
21 
22  point = TrajectoryPointMsg()
23  point.time_from_start = (rospy.Time.now()-start_t)
24  point.velocity.linear.x = data.linear.x
25  point.velocity.linear.y = data.linear.y
26  point.velocity.angular.z = data.angular.z
27 
28  trajectory_gt.append(point)
29 
30 
32  global trajectory_est
33  global start_t
34 
35  if (start_t == 0):
36  start_t = rospy.Time.now()
37 
38  point = TrajectoryPointMsg()
39  point.time_from_start = (rospy.Time.now()-start_t)
40  if(len(data.obstacles) > 0):
41  point.velocity.linear.x = data.obstacles[0].velocities.twist.linear.x
42  point.velocity.linear.y = data.obstacles[0].velocities.twist.linear.y
43  point.velocity.angular.z = data.obstacles[0].velocities.twist.angular.z
44 
45  trajectory_est.append(point)
46 
47 
48 
49 def plot_velocity_profiles(fig, ax_vx, ax_vy, t_gt, v_x_gt, v_y_gt, omega_gt, t_est, v_x_est, v_y_est, omega_est):
50  ax_vx.cla()
51  ax_vx.grid()
52  ax_vx.set_ylabel('Trans. velocity (x) [m/s]')
53  ax_vx.plot(t_gt, v_x_gt, '-bx', label='Ground Truth')
54  ax_vx.plot(t_est, v_x_est, '-rx', label='Estimate')
55  ax_vx.legend(loc='upper left')
56  ax_vy.cla()
57  ax_vy.grid()
58  ax_vy.set_ylabel('Trans. velocity (y) [m/s]')
59  ax_vy.plot(t_gt, v_y_gt, '-bx', label='Ground Truth')
60  ax_vy.plot(t_est, v_y_est, '-rx', label='Estimate')
61  ax_vy.set_xlabel('Time [s]')
62  fig.canvas.draw()
63 
64 
66  global trajectory_gt
67  global trajectory_est
68  global start_t
69 
70  rospy.init_node("visualize_obstacle_velocity_profile", anonymous=True)
71 
72  topic_name_ground_truth_vel = "cmd_vel"
73  rospy.Subscriber(topic_name_ground_truth_vel, Twist, twist_callback, queue_size = 1)
74  topic_name_estimated_vel = "/standalone_converter/costmap_obstacles"
75  rospy.Subscriber(topic_name_estimated_vel, ObstacleArrayMsg, obstacleArrayMsg_callback, queue_size = 1)
76 
77  rospy.loginfo("Visualizing ground truth velocity profile published on '%s'.", topic_name_ground_truth_vel)
78  rospy.loginfo("Visualizing estimated velocity profile published on '%s'.", topic_name_estimated_vel)
79 
80  # two subplots sharing the same t axis
81  fig, (ax_vx, ax_vy) = plotter.subplots(2, sharex=True)
82  plotter.ion()
83  plotter.show()
84 
85 
86  r = rospy.Rate(2) # define rate here
87  while not rospy.is_shutdown():
88 
89  t_gt = []
90  v_x_gt = []
91  v_y_gt = []
92  omega_gt = []
93 
94  t_est = []
95  v_x_est = []
96  v_y_est = []
97  omega_est = []
98 
99  for point in trajectory_gt:
100  t_gt.append(point.time_from_start.to_sec())
101  v_x_gt.append(point.velocity.linear.x)
102  v_y_gt.append(point.velocity.linear.y)
103  omega_gt.append(point.velocity.angular.z)
104 
105  for point in trajectory_est:
106  t_est.append(point.time_from_start.to_sec())
107  v_x_est.append(point.velocity.linear.x)
108  v_y_est.append(point.velocity.linear.y)
109  omega_est.append(point.velocity.angular.z)
110 
111 
112  plot_velocity_profiles(fig, ax_vx, ax_vy, np.asarray(t_gt), np.asarray(v_x_gt), np.asarray(v_y_gt), np.asarray(omega_gt),
113  np.asarray(t_est), np.asarray(v_x_est), np.asarray(v_y_est), np.asarray(omega_est))
114 
115  r.sleep()
116 
117 if __name__ == '__main__':
118  try:
119  trajectory_gt = []
120  trajectory_est = []
121  start_t = 0
123  except rospy.ROSInterruptException:
124  pass
125 # finally:
126 # plotter.savefig('/home/albers/Desktop/velocity_plot.pdf', bbox_inches='tight')
127 
def plot_velocity_profiles(fig, ax_vx, ax_vy, t_gt, v_x_gt, v_y_gt, omega_gt, t_est, v_x_est, v_y_est, omega_est)


teb_local_planner_tutorials
Author(s): Christoph Rösmann
autogenerated on Thu Jun 6 2019 19:31:31