11import math
2+ from matplotlib import pyplot as plt
23import scipy as sp
34from scipy .special import softmax
45import numpy as np
56import onnxruntime as ort
7+ import logging as log
68
79from Autotech_constant import SPEED_LOOKUP , ANGLE_LOOKUP , MODEL_PATH , Temperature
810
911
1012
1113class Driver :
1214 def __init__ (self , context_size = 0 , horizontal_size = 0 ):
15+ self .context_size = context_size
16+ self .horizontal_size = horizontal_size
1317 self .ai_session = ort .InferenceSession (MODEL_PATH )
1418 self .context = np .zeros ([2 , context_size , horizontal_size ], dtype = np .float32 )
1519
20+ if log .getLogger ().isEnabledFor (log .DEBUG ):
21+ self .fig , self .ax = plt .subplots (4 , 1 , figsize = (10 , 8 ))
22+ self .steering_bars = self .ax [0 ].bar (range (16 ), np .zeros (16 ), color = 'blue' )
23+ self .steering_avg = [
24+ self .ax [0 ].plot ([0 , 0 ], [0 , 1 ], color = (i / 3 , 1 - i / 3 , 0 ), label = 'Average' )[0 ]
25+ for i in range (4 )
26+ ]
27+ self .ax [0 ].set_ylim (0 , 1 ) # Probabilities range from 0 to 1
28+ self .ax [0 ].set_title ('Steering Action Probabilities' )
29+
30+ # Speed bars
31+ self .speed_bars = self .ax [1 ].bar (range (16 ), np .zeros (16 ), color = 'blue' )
32+ self .speed_avg = self .ax [1 ].plot ([0 , 0 ], [0 , 1 ], color = 'red' , label = 'Average' )[0 ]
33+ self .ax [1 ].set_ylim (0 , 1 ) # Probabilities range from 0 to 1
34+ self .ax [1 ].set_title ('Speed Action Probabilities' )
35+
36+ # LiDAR img
37+ self .lidar_img = self .ax [2 ].imshow (
38+ np .zeros ((128 , 128 )),
39+ cmap = 'gray' , vmin = 0 , vmax = np .log (31 )
40+ )
41+ self .ax [2 ].set_title ('LiDAR Image' )
42+
43+ # Camera img
44+ self .camera_img = self .ax [3 ].imshow (
45+ np .zeros ((128 , 128 , 3 )),
46+ cmap = 'RdYlGn' , vmin = - 1 , vmax = 1
47+ )
48+ self .ax [3 ].set_title ('Camera Image' )
49+
50+ def reset (self ):
51+ self .context = np .zeros ([2 , self .context_size , self .horizontal_size ], dtype = np .float32 )
52+
1653 def omniscent (self , lidar_data , camera_data ):
1754 return self .ai_update_lidar_camera (lidar_data , camera_data )
1855
@@ -23,10 +60,12 @@ def simple_minded(self, lidar_data):
2360 return self .farthest_distants (lidar_data )
2461
2562 def ai_update_lidar_camera (self , lidar_data , camera_data ):
63+ log .info (f"MIN MAX lidar_data: { (min (lidar_data ), max (lidar_data ))} " )
64+
2665 lidar_data = sp .ndimage .zoom (
2766 np .array (lidar_data , dtype = np .float32 ),
2867 128 / len (lidar_data )
29- )
68+ ) / 1000 * 0.8
3069 camera_data = sp .ndimage .zoom (
3170 np .array (camera_data , dtype = np .float32 ),
3271 128 / len (camera_data )
@@ -38,15 +77,40 @@ def ai_update_lidar_camera(self, lidar_data, camera_data):
3877 ], axis = 1 )
3978
4079 # 2 vectors direction and speed. direction is between hard left at index 0 and hard right at index 1. speed is between min speed at index 0 and max speed at index 1
41- vect = self .ai_session .run (None , {'input' : self .context })[0 ][0 ]
80+ vect = self .ai_session .run (None , {'input' : self .context [ None ] })[0 ][0 ]
4281
4382 vect_dir , vect_prop = vect [:16 ], vect [16 :] # split the vector in 2
4483 vect_dir = softmax (vect_dir ) # distribution de probabilité
4584 vect_prop = softmax (vect_prop )
4685
47- angle = sum (SPEED_LOOKUP * vect_dir ) # moyenne pondérée des angles
86+ if log .getLogger ().isEnabledFor (log .DEBUG ):
87+ log .info (f"MIN MAX lidar_data: { (min (lidar_data ), max (lidar_data ))} " )
88+ self .lidar_img .set_array (np .log (1 + self .context [0 ]))
89+ self .camera_img .set_array (self .context [1 ])
90+
91+ for i , bar in enumerate (self .steering_bars ):
92+ bar .set_height (vect_dir [i ].item ())
93+
94+ for i in range (4 ):
95+ steering_avg = (vect_dir / vect_dir .sum () * np .arange (16 )).sum ().item ()
96+ self .steering_avg [i ].set_xdata ([steering_avg , steering_avg ])
97+
98+ for i , bar in enumerate (self .speed_bars ):
99+ bar .set_height (vect_dir [i ].item ())
100+
101+ speed_avg = (vect_dir * np .arange (16 )).sum ().item ()
102+ self .speed_avg .set_xdata ([speed_avg , speed_avg ])
103+
104+ plt .draw ()
105+ plt .pause (1e-8 )
106+
107+
108+ print (" " .join ([f"{ x :.1f} " for x in vect_dir ]))
109+ print (" " .join ([f"{ x :.1f} " for x in vect_prop ]), flush = True )
110+
111+ angle = sum (ANGLE_LOOKUP * vect_dir ) # moyenne pondérée des angles
48112 # moyenne pondérée des vitesses
49- vitesse = sum (ANGLE_LOOKUP * vect_prop )
113+ vitesse = sum (SPEED_LOOKUP * vect_prop )
50114
51115 return angle , vitesse
52116
0 commit comments