88
99
1010# Import constants from HL.Autotech_constant to share them between files and ease of use
11- from Autotech_constant import MAX_SOFT_SPEED , MAX_ANGLE , CRASH_DIST , MODEL_PATH , PWM_DIR , PWM_PROP , SOCKET_ADRESS , REAR_BACKUP_DIST
11+ from Autotech_constant import MAX_SOFT_SPEED , MAX_ANGLE , CRASH_DIST , MODEL_PATH , PWM_DIR , PWM_PROP , SOCKET_ADRESS , REAR_BACKUP_DIST , LIDAR_DATA_SIGMA , LIDAR_DATA_AMPLITUDE , LIDAR_DATA_OFFSET
1212from Driver import Driver
1313from Lidar import Lidar
1414from Camera import Camera
@@ -149,7 +149,7 @@ def recule(self,angle,duration=0.5):
149149 time .sleep (0.2 )
150150 self .set_vitesse_m_s (0 )
151151 time .sleep (0.2 )
152- self .set_vitesse_m_s (- 2 )
152+ self .set_vitesse_m_s (- 4 )
153153 time .sleep (duration + 0.3 )
154154 if angle != 0 :
155155 self .set_direction_degre (- angle )
@@ -169,7 +169,7 @@ def stop(self):
169169
170170 def has_Crashed (self ):
171171
172- small_distances = [d for d in self .lidar .rDistance [300 : 780 ] if 0 < d < CRASH_DIST ] # 360 to 720 is the front of the car. 1/3 of the fov of the lidar
172+ small_distances = [d for d in self .lidar .rDistance [200 : 880 ] if 0 < d < CRASH_DIST ] # 360 to 720 is the front of the car. 1/3 of the fov of the lidar
173173 log .debug (f"Distances: { small_distances } " )
174174 if len (small_distances ) > 2 :
175175 # min_index = self.lidar.rDistance.index(min(small_distances))
@@ -196,20 +196,33 @@ def turn_around(self):
196196 def main (self ):
197197 # récupération des données du lidar. On ne prend que les 1080 premières valeurs et on ignore la dernière par facilit" pour l'ia
198198
199- lidar_data = self .lidar .rDistance [:1080 ]/ 1000
200- angle , vitesse = self .driving (lidar_data ) #l'ai prend des distance en mètre et non en mm
199+ lidar_data = (self .lidar .rDistance [:1080 ]/ 1000 )
200+ lidar_data_ai = (lidar_data - 0.5 )* (
201+ LIDAR_DATA_OFFSET + LIDAR_DATA_AMPLITUDE * np .exp (- 1 / 2 * ((np .arange (1080 ) - 135 ) / LIDAR_DATA_SIGMA ** 2 ))
202+ ) #convertir en mètre et ajouter un bruit gaussien
203+ angle , vitesse = self .driving (lidar_data_ai ) #l'ai prend des distance en mètre et non en mm
201204 log .debug (f"Min Lidar: { min (lidar_data )} , Max Lidar: { max (lidar_data )} " )
202205 self .set_direction_degre (angle )
203206 self .set_vitesse_m_s (vitesse )
204207 if self .camera .is_running_in_reversed ():
205208 self .reverse_count += 1
206209 else :
207210 self .reverse_count = 0
208- if self .reverse_count > 5 :
211+ if self .reverse_count > 2 :
209212 self .turn_around ()
210213 self .reverse_count = 0
211214 if self .has_Crashed ():
212- color = self .camera .is_green_or_red ()
215+ print ("Obstacle détecté" )
216+ color = self .camera .is_green_or_red (lidar_data )
217+ if color == 0 :
218+ small_distances = [d for d in self .lidar .rDistance if 0 < d < CRASH_DIST ]
219+ if len (small_distances ) == 0 :
220+ log .info ("Aucun obstacle détecté" )
221+ return
222+ min_index = np .argmin (small_distances )
223+ direction = MAX_ANGLE if min_index < 540 else - MAX_ANGLE #540 is the middle of the lidar
224+ color = direction / direction
225+ log .info ("Obstacle détecté, Lidar Fallback" )
213226 if color == - 1 :
214227 log .info ("Obstacle rouge détecté" )
215228 if color == 1 :
0 commit comments