Skip to content

Commit 371b23c

Browse files
committed
Adjust speed limits, add lidar data parameters, and refine obstacle detection logic
1 parent 11f529b commit 371b23c

3 files changed

Lines changed: 33 additions & 13 deletions

File tree

src/HL/Autotech_constant.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
import os
22
import numpy as np
33

4-
MAX_SOFT_SPEED = 1
4+
MAX_SOFT_SPEED = 0.5
55
MIN_SOFT_SPEED = 0.1
66
MAX_ANGLE = 18
77
CRASH_DIST = 110
88
REAR_BACKUP_DIST = 100 #mm Distance at which the car will NOT reverse due to the obstacle behind it
9+
LIDAR_DATA_AMPLITUDE = 1
10+
LIDAR_DATA_SIGMA = 45
11+
LIDAR_DATA_OFFSET = 0.5
912

1013
script_dir = os.path.dirname(os.path.abspath(__file__))
1114
MODEL_PATH = os.path.join(script_dir, "model_CNN1D.onnx") # Allows the model to be loaded from the same directory as the script regardless of the current working directory (aka where the script is run from)
1215

16+
1317
PWM_PROP = {
1418
"direction_prop": 1,
1519
"pwm_stop_prop": 7.37,
@@ -32,6 +36,6 @@
3236
ANGLE_LOOKUP = np.linspace(-MAX_ANGLE, MAX_ANGLE, 16)
3337
SPEED_LOOKUP = np.linspace(MIN_SOFT_SPEED, MAX_SOFT_SPEED, 16)
3438

35-
Temperature = 1 # Temperature parameter for softmax function, used to control the sharpness of the distribution resols around 1
39+
Temperature = 0.6 # Temperature parameter for softmax function, used to control the sharpness of the distribution resols around 1
3640
# the higher the temperature the more unprobalbe actions become probable, the lower the temperature the more probable actions become probable.
3741
# In our case Higher temperature means less agressive driving and lower temperature means more aggressive driving.

src/HL/Camera.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import logging as log
66
import threading
77
import shutil
8+
import scipy as sp
89

910
N_IMAGES = 100 # Number of images to capture
1011
SAVE_DIR = "Captured_Frames" # Directory to save frames
@@ -152,16 +153,18 @@ def recreate_image_from_matrix(self, image, matrix, adjusted_width, vector_size=
152153
self.debug_counter += 1
153154
return Image.fromarray(combined_image).convert("RGB")
154155

155-
def is_green_or_red(self):
156+
def is_green_or_red(self,lidar):
156157
"""
157158
Check if the car is facing a green or red wall by analyzing the bottom half of the image.
158159
"""
159160
image = self.get_last_image()
160161
height, _, _ = image.shape
161162
bottom_half = image[height // 2:, :, :] # Slice the bottom half of the image
162-
163-
red_intensity = np.mean(bottom_half[:, :, 0]) # Red channel in RGB
164-
green_intensity = np.mean(bottom_half[:, :, 1]) # Green channel in RGB
163+
lidar= np.max(sp.ndimage.zoom(lidar[595:855], image.shape[1]/len(lidar[595:855]),mode="nearest")[None,:],0) # Resize lidar data to match the image size
164+
print((lidar < 0.5).sum())
165+
print(f"min lidar: {lidar.min()}, max lidar: {lidar.max()}")
166+
red_intensity = np.mean(bottom_half[:, :, 0]*(lidar < 0.5)) # Red channel in RGB
167+
green_intensity = np.mean(bottom_half[:, :, 1]*(lidar < 0.5)) # Green channel in RGB
165168

166169
if green_intensity > red_intensity + COLOR_THRESHOLD:
167170
return COLOUR_KEY["green"]

src/HL/Car.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
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
1212
from Driver import Driver
1313
from Lidar import Lidar
1414
from 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

Comments
 (0)