16
16
17
17
from autoware_planning_msgs .msg import Trajectory
18
18
from autoware_planning_msgs .msg import TrajectoryPoint
19
+ from geometry_msgs .msg import AccelWithCovarianceStamped
19
20
from geometry_msgs .msg import Point
20
21
from geometry_msgs .msg import PolygonStamped
22
+ import matplotlib .pyplot as plt
21
23
from nav_msgs .msg import Odometry
22
24
import numpy as np
23
25
from numpy import arctan
28
30
import rclpy
29
31
from rclpy .node import Node
30
32
from scipy .spatial .transform import Rotation as R
33
+ import seaborn as sns
31
34
from visualization_msgs .msg import Marker
32
35
from visualization_msgs .msg import MarkerArray
33
36
34
37
debug_matplotlib_plot_flag = False
35
38
Differential_Smoothing_Flag = True
36
39
USE_CURVATURE_RADIUS_FLAG = False
37
- if debug_matplotlib_plot_flag :
38
- import matplotlib .pyplot as plt
39
40
40
41
41
42
def smooth_bounding (upper : np .ndarray , threshold : np .ndarray , x : np .ndarray ):
@@ -133,6 +134,20 @@ class DataCollectingTrajectoryPublisher(Node):
133
134
def __init__ (self ):
134
135
super ().__init__ ("data_collecting_trajectory_publisher" )
135
136
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
+
136
151
self .declare_parameter (
137
152
"max_lateral_accel" ,
138
153
0.5 , # 0.3G
@@ -217,7 +232,13 @@ def __init__(self):
217
232
self .onOdometry ,
218
233
1 ,
219
234
)
220
- self .sub_odometry_
235
+
236
+ self .sub_acceleration_ = self .create_subscription (
237
+ AccelWithCovarianceStamped ,
238
+ "/localization/acceleration" ,
239
+ self .onAcceleration ,
240
+ 1 ,
241
+ )
221
242
222
243
self .sub_data_collecting_area_ = self .create_subscription (
223
244
PolygonStamped ,
@@ -233,6 +254,7 @@ def __init__(self):
233
254
self .timer = self .create_timer (self .timer_period_callback , self .timer_callback )
234
255
235
256
self ._present_kinematic_state = None
257
+ self ._present_acceleration = None
236
258
237
259
self .trajectory_position_data = None
238
260
self .trajectory_yaw_data = None
@@ -252,6 +274,9 @@ def __init__(self):
252
274
def onOdometry (self , msg ):
253
275
self ._present_kinematic_state = msg
254
276
277
+ def onAcceleration (self , msg ):
278
+ self ._present_acceleration = msg
279
+
255
280
def onDataCollectingArea (self , msg ):
256
281
self ._data_collecting_area_polygon = msg
257
282
self .updateNominalTargetTrajectory ()
@@ -424,8 +449,52 @@ def updateNominalTargetTrajectory(self):
424
449
425
450
self .get_logger ().info ("update nominal target trajectory" )
426
451
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
+
427
485
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
+
429
498
# [0] update nominal target trajectory if changing related ros2 params
430
499
target_longitudinal_velocity = (
431
500
self .get_parameter ("target_longitudinal_velocity" )
@@ -708,9 +777,8 @@ def timer_callback(self):
708
777
709
778
self .data_collecting_trajectory_marker_array_pub_ .publish (marker_array )
710
779
711
- # [99] debug plot
712
780
if debug_matplotlib_plot_flag :
713
- plt .cla ()
781
+ self . axs [ 0 ] .cla ()
714
782
step_size_array = np .sqrt (
715
783
((trajectory_position_data [1 :] - trajectory_position_data [:- 1 ]) ** 2 ).sum (
716
784
axis = 1
@@ -726,46 +794,42 @@ def timer_callback(self):
726
794
timestamp [i ] = timestamp [i - 1 ] + time_width_array [i - 1 ]
727
795
timestamp -= timestamp [nearestIndex ]
728
796
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" )
730
798
731
- plt .plot (
732
- # distance[nearestIndex : nearestIndex + pub_traj_len],
799
+ self .axs [0 ].plot (
733
800
timestamp [nearestIndex : nearestIndex + pub_traj_len ],
734
801
trajectory_longitudinal_velocity_data_without_limit [
735
802
nearestIndex : nearestIndex + pub_traj_len
736
803
],
737
804
"--" ,
738
805
label = "target vel before applying limit" ,
739
806
)
740
- plt .plot (
741
- # distance[nearestIndex : nearestIndex + pub_traj_len],
807
+ self .axs [0 ].plot (
742
808
timestamp [nearestIndex : nearestIndex + pub_traj_len ],
743
809
lateral_acc_limit [nearestIndex : nearestIndex + pub_traj_len ],
744
810
"--" ,
745
811
label = "lateral acc limit (always)" ,
746
812
)
747
- plt .plot (
748
- # distance[nearestIndex : nearestIndex + pub_traj_len],
813
+ self .axs [0 ].plot (
749
814
timestamp [nearestIndex : nearestIndex + pub_traj_len ],
750
815
velocity_limit_by_tracking_error * np .ones (pub_traj_len ),
751
816
"--" ,
752
817
label = "vel limit by tracking error (only when exceeding threshold)" ,
753
818
)
754
- plt .plot (
755
- # distance[nearestIndex : nearestIndex + pub_traj_len],
819
+ self .axs [0 ].plot (
756
820
timestamp [nearestIndex : nearestIndex + pub_traj_len ],
757
821
trajectory_longitudinal_velocity_data [
758
822
nearestIndex : nearestIndex + pub_traj_len
759
823
],
760
824
label = "actual target vel" ,
761
825
)
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 ])
764
828
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 ( )
769
833
plt .pause (0.01 )
770
834
771
835
0 commit comments