Skip to content

Commit a1aba06

Browse files
authored
feat(data_collecting_tool): visualize collected acc and vel grid (#83)
* feat(data_collecting_tool): visualize collected acc and vel grid Signed-off-by: kosuke55 <[email protected]> * fix spell Signed-off-by: kosuke55 <[email protected]> * fix import Signed-off-by: kosuke55 <[email protected]> * add matplotlib depend Signed-off-by: kosuke55 <[email protected]> * add dependency * update division Signed-off-by: kosuke55 <[email protected]> * fix blinking grid Signed-off-by: kosuke55 <[email protected]> --------- Signed-off-by: kosuke55 <[email protected]>
1 parent 97ed6ee commit a1aba06

File tree

2 files changed

+88
-21
lines changed

2 files changed

+88
-21
lines changed

control_data_collecting_tool/package.xml

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
<depend>rviz_common</depend>
2424
<depend>rviz_default_plugins</depend>
2525

26+
<exec_depend>python3-matplotlib</exec_depend>
27+
<exec_depend>python3-seaborn</exec_depend>
28+
2629
<test_depend>ament_index_python</test_depend>
2730
<test_depend>ament_lint_auto</test_depend>
2831
<test_depend>autoware_lint_common</test_depend>

control_data_collecting_tool/scripts/data_collecting_trajectory_publisher.py

+85-21
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616

1717
from autoware_planning_msgs.msg import Trajectory
1818
from autoware_planning_msgs.msg import TrajectoryPoint
19+
from geometry_msgs.msg import AccelWithCovarianceStamped
1920
from geometry_msgs.msg import Point
2021
from geometry_msgs.msg import PolygonStamped
22+
import matplotlib.pyplot as plt
2123
from nav_msgs.msg import Odometry
2224
import numpy as np
2325
from numpy import arctan
@@ -28,14 +30,13 @@
2830
import rclpy
2931
from rclpy.node import Node
3032
from scipy.spatial.transform import Rotation as R
33+
import seaborn as sns
3134
from visualization_msgs.msg import Marker
3235
from visualization_msgs.msg import MarkerArray
3336

3437
debug_matplotlib_plot_flag = False
3538
Differential_Smoothing_Flag = True
3639
USE_CURVATURE_RADIUS_FLAG = False
37-
if debug_matplotlib_plot_flag:
38-
import matplotlib.pyplot as plt
3940

4041

4142
def smooth_bounding(upper: np.ndarray, threshold: np.ndarray, x: np.ndarray):
@@ -133,6 +134,20 @@ class DataCollectingTrajectoryPublisher(Node):
133134
def __init__(self):
134135
super().__init__("data_collecting_trajectory_publisher")
135136

137+
# velocity and acceleration grid
138+
self.num_bins = 10
139+
self.v_min, self.v_max = 0, 13.8
140+
self.a_min, self.a_max = -1, 1
141+
self.collected_data_counts = np.zeros((self.num_bins, self.num_bins))
142+
self.v_bins = np.linspace(self.v_min, self.v_max, self.num_bins + 1)
143+
self.a_bins = np.linspace(self.a_min, self.a_max, self.num_bins + 1)
144+
self.v_bin_centers = (self.v_bins[:-1] + self.v_bins[1:]) / 2
145+
self.a_bin_centers = (self.a_bins[:-1] + self.a_bins[1:]) / 2
146+
self.fig, self.axs = plt.subplots(2, 1, figsize=(10, 12))
147+
self.grid_update_time_interval = 1
148+
self.last_grid_update_time = None
149+
plt.ion()
150+
136151
self.declare_parameter(
137152
"max_lateral_accel",
138153
0.5, # 0.3G
@@ -217,7 +232,13 @@ def __init__(self):
217232
self.onOdometry,
218233
1,
219234
)
220-
self.sub_odometry_
235+
236+
self.sub_acceleration_ = self.create_subscription(
237+
AccelWithCovarianceStamped,
238+
"/localization/acceleration",
239+
self.onAcceleration,
240+
1,
241+
)
221242

222243
self.sub_data_collecting_area_ = self.create_subscription(
223244
PolygonStamped,
@@ -233,6 +254,7 @@ def __init__(self):
233254
self.timer = self.create_timer(self.timer_period_callback, self.timer_callback)
234255

235256
self._present_kinematic_state = None
257+
self._present_acceleration = None
236258

237259
self.trajectory_position_data = None
238260
self.trajectory_yaw_data = None
@@ -252,6 +274,9 @@ def __init__(self):
252274
def onOdometry(self, msg):
253275
self._present_kinematic_state = msg
254276

277+
def onAcceleration(self, msg):
278+
self._present_acceleration = msg
279+
255280
def onDataCollectingArea(self, msg):
256281
self._data_collecting_area_polygon = msg
257282
self.updateNominalTargetTrajectory()
@@ -424,8 +449,52 @@ def updateNominalTargetTrajectory(self):
424449

425450
self.get_logger().info("update nominal target trajectory")
426451

452+
def count_observations(self, v, a):
453+
v_bin = np.digitize(v, self.v_bins) - 1
454+
a_bin = np.digitize(a, self.a_bins) - 1
455+
if 0 <= v_bin < self.num_bins and 0 <= a_bin < self.num_bins:
456+
self.collected_data_counts[v_bin, a_bin] += 1
457+
458+
def plot_data_collection_grid(self):
459+
# do not update if enough time has not passed
460+
if self.last_grid_update_time is not None:
461+
time_elapsed = self.get_clock().now() - self.last_grid_update_time
462+
if self.grid_update_time_interval > time_elapsed.nanoseconds / 1e9:
463+
return
464+
# update collected acceleration and velocity grid
465+
for collection in self.axs[1].collections:
466+
if collection.colorbar is not None:
467+
collection.colorbar.remove()
468+
self.axs[1].cla()
469+
self.heatmap = sns.heatmap(
470+
self.collected_data_counts,
471+
annot=True,
472+
cmap="coolwarm",
473+
xticklabels=np.round(self.a_bin_centers, 2),
474+
yticklabels=np.round(self.v_bin_centers, 2),
475+
ax=self.axs[1],
476+
linewidths=0.1,
477+
linecolor="gray",
478+
)
479+
self.axs[1].set_xlabel("Acceleration bins")
480+
self.axs[1].set_ylabel("Velocity bins")
481+
self.axs[1].set_title("Counts of Observations in Each Grid Cell")
482+
self.last_grid_update_time = self.get_clock().now()
483+
self.fig.canvas.draw()
484+
427485
def timer_callback(self):
428-
if self._present_kinematic_state is not None and self.trajectory_position_data is not None:
486+
if (
487+
self._present_kinematic_state is not None
488+
and self._present_acceleration is not None
489+
and self.trajectory_position_data is not None
490+
):
491+
# update velocity and acceleration bin if ego vehicle is moving
492+
if self._present_kinematic_state.twist.twist.linear.x > 1e-3:
493+
self.count_observations(
494+
self._present_kinematic_state.twist.twist.linear.x,
495+
self._present_acceleration.accel.accel.linear.x,
496+
)
497+
429498
# [0] update nominal target trajectory if changing related ros2 params
430499
target_longitudinal_velocity = (
431500
self.get_parameter("target_longitudinal_velocity")
@@ -708,9 +777,8 @@ def timer_callback(self):
708777

709778
self.data_collecting_trajectory_marker_array_pub_.publish(marker_array)
710779

711-
# [99] debug plot
712780
if debug_matplotlib_plot_flag:
713-
plt.cla()
781+
self.axs[0].cla()
714782
step_size_array = np.sqrt(
715783
((trajectory_position_data[1:] - trajectory_position_data[:-1]) ** 2).sum(
716784
axis=1
@@ -726,46 +794,42 @@ def timer_callback(self):
726794
timestamp[i] = timestamp[i - 1] + time_width_array[i - 1]
727795
timestamp -= timestamp[nearestIndex]
728796

729-
plt.plot(0, present_linear_velocity[0], "o", label="current vel")
797+
self.axs[0].plot(0, present_linear_velocity[0], "o", label="current vel")
730798

731-
plt.plot(
732-
# distance[nearestIndex : nearestIndex + pub_traj_len],
799+
self.axs[0].plot(
733800
timestamp[nearestIndex : nearestIndex + pub_traj_len],
734801
trajectory_longitudinal_velocity_data_without_limit[
735802
nearestIndex : nearestIndex + pub_traj_len
736803
],
737804
"--",
738805
label="target vel before applying limit",
739806
)
740-
plt.plot(
741-
# distance[nearestIndex : nearestIndex + pub_traj_len],
807+
self.axs[0].plot(
742808
timestamp[nearestIndex : nearestIndex + pub_traj_len],
743809
lateral_acc_limit[nearestIndex : nearestIndex + pub_traj_len],
744810
"--",
745811
label="lateral acc limit (always)",
746812
)
747-
plt.plot(
748-
# distance[nearestIndex : nearestIndex + pub_traj_len],
813+
self.axs[0].plot(
749814
timestamp[nearestIndex : nearestIndex + pub_traj_len],
750815
velocity_limit_by_tracking_error * np.ones(pub_traj_len),
751816
"--",
752817
label="vel limit by tracking error (only when exceeding threshold)",
753818
)
754-
plt.plot(
755-
# distance[nearestIndex : nearestIndex + pub_traj_len],
819+
self.axs[0].plot(
756820
timestamp[nearestIndex : nearestIndex + pub_traj_len],
757821
trajectory_longitudinal_velocity_data[
758822
nearestIndex : nearestIndex + pub_traj_len
759823
],
760824
label="actual target vel",
761825
)
762-
plt.xlim([-0.5, 10.5])
763-
plt.ylim([-0.5, 12.5])
826+
self.axs[0].set_xlim([-0.5, 10.5])
827+
self.axs[0].set_ylim([-0.5, 12.5])
764828

765-
# plt.xlabel("future driving distance [m]")
766-
plt.xlabel("future timestamp [s]")
767-
plt.ylabel("longitudinal_velocity [m/s]")
768-
plt.legend(fontsize=8)
829+
self.axs[0].set_xlabel("future timestamp [s]")
830+
self.axs[0].set_ylabel("longitudinal_velocity [m/s]")
831+
self.axs[0].legend(fontsize=8)
832+
self.plot_data_collection_grid()
769833
plt.pause(0.01)
770834

771835

0 commit comments

Comments
 (0)