Skip to content

Commit

Permalink
correciones en env
Browse files Browse the repository at this point in the history
  • Loading branch information
larapoves committed Oct 3, 2024
1 parent f71ed6d commit 37f30fc
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 230 deletions.
Binary file modified docs/images/follow_lane_deepRL/map.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 8 additions & 12 deletions src/configcarla.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def __init__(self, size:tuple[int, int], init:tuple[int, int], sensor:carla.Sens
self._road_percentage = 0
self._cm = np.zeros((2,), dtype=np.int32)
self._area = np.int32(0)
self._error_lane = False
self.error_lane = False
self._lane_left = []
self._lane_right = []
self._extra_surface = None
Expand Down Expand Up @@ -193,9 +193,7 @@ def _detect_lane(self, img:np.ndarray, mask:np.ndarray):

self._lane_left = self._points_lane(left_boundary, trafo_matrix_global_to_camera, LEFT_LANE)
self._lane_right = self._points_lane(right_boundary, trafo_matrix_global_to_camera, RIGHT_LANE)
if self._lane_left == None or self._lane_right == None:
self._error_lane = True
assert False, "Lane lost"
assert self._lane_left != None or self._lane_right != None, "Lane lost"

# Start in same height
size_left = len(self._lane_left)
Expand Down Expand Up @@ -235,9 +233,7 @@ def _detect_lane(self, img:np.ndarray, mask:np.ndarray):
# Calculate road porcentage
if self._seg:
self._road_percentage = count_road / count_total * 100
if self._road_percentage < self._threshold_road_per:
self._error_lane = True
assert False, "Low percentage of lane"
assert self._road_percentage >= self._threshold_road_per, "Low percentage of lane"

# Draw center of mass and vehicle
cv2.line(img, (x_cm, 0), (x_cm, SIZE_CAMERA - 1), (0, 255, 0), 2)
Expand All @@ -246,7 +242,7 @@ def _detect_lane(self, img:np.ndarray, mask:np.ndarray):
else:
self._deviation = SIZE_CAMERA / 2
self._road_percentage = 0
self._error_lane = True
self.error_lane = True
assert False, "Area zero"

return img
Expand All @@ -258,7 +254,7 @@ def get_road_percentage(self):
return self._road_percentage

def get_lane_cm(self):
if self._error_lane:
if self.error_lane:
# Move cm to the nearest corner
if self._cm[0] < SIZE_CAMERA / 2:
x_cm = 0
Expand All @@ -274,7 +270,7 @@ def get_lane_cm(self):
return self._cm

def get_lane_area(self):
if self._error_lane:
if self.error_lane:
self._area = 0

return self._area
Expand All @@ -289,7 +285,7 @@ def show_surface(self, surface:pygame.Surface, pos:tuple[int, int], text:str):

def process_data(self):
image = self.data
self._error_lane = False
self.error_lane = False
if image == None:
return

Expand Down Expand Up @@ -340,7 +336,7 @@ def process_data(self):
self.show_surface(surface=self._extra_surface, pos=self.init_extra, text=text_extra)

def get_lane_points(self, num_points:int=5, show:bool=False):
if not self._lane or self._error_lane or len(self._lane_left) == 0 or len(self._lane_right) == 0:
if not self._lane or self.error_lane or len(self._lane_left) == 0 or len(self._lane_right) == 0:
return [np.full((num_points, 2), SIZE_CAMERA / 2, dtype=np.int32)] * 2

lane_points = []
Expand Down
4 changes: 2 additions & 2 deletions src/deepRL/config/CarlaLaneDiscrete.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ DQN:
target_update_interval: 200
train_freq: 50
gradient_steps: 2
exploration_fraction: 0.725
exploration_fraction: 0.73
exploration_final_eps: 0.0
n_timesteps: 4_200_000
n_timesteps: 4_000_000
policy: "MultiInputPolicy"
35 changes: 20 additions & 15 deletions src/deepRL/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,12 @@ def __init__(self, human:bool, train:bool, alg:str=None, port:int=2000, num_poin
self._town = 'Town04'
self._init_locations = [
carla.Transform(carla.Location(x=352.65, y=-350.7, z=0.1), carla.Rotation(yaw=-137)),
carla.Transform(carla.Location(x=-25.0, y=-252, z=0.1), carla.Rotation(yaw=125.0)),
carla.Transform(carla.Location(x=-8.76, y=60.8, z=0.1), carla.Rotation(yaw=89.7))
carla.Transform(carla.Location(x=-8.76, y=60.8, z=0.1), carla.Rotation(yaw=89.7)),
carla.Transform(carla.Location(x=-25.0, y=-252, z=0.1), carla.Rotation(yaw=125.0))
]
else: # cambiar
self._town = 'Town04'
self._init_locations = [
carla.Transform(carla.Location(x=-25.0, y=-252, z=z), carla.Rotation(yaw=125.0))
else:
self._init_locations = [ # Merge routes 1 and 2 of circuit 0
carla.Transform(carla.Location(x=352.65, y=-350.7, z=0.1), carla.Rotation(yaw=-137))
]

# Pygame window
Expand Down Expand Up @@ -259,21 +258,24 @@ def step(self, action:np.ndarray):
# Reward function
reward = self._calculate_reward()

# Check if the episode has finished
t = self.ego_vehicle.get_transform()
if self._num_cir == 0:
finish_ep = self._index_loc <= 1 and(t.location.x + 442) <= 3 and (t.location.y - 30) <= 3
finish_ep = finish_ep or self._index_loc == 2 and t.location.y > -24.5
terminated = finish_ep

else: # cambiar
if t.location.y > -24.5:
terminated = True
finish_ep = True
if self._index_loc == 0:
finish_ep = abs(t.location.x + 7) <= 3 and abs(t.location.y - 55) <= 3
elif self._index_loc == 1:
finish_ep = abs(t.location.x + 442) <= 3 and abs(t.location.y - 30) <= 3
else:
finish_ep = self._index_loc == 2 and t.location.y > -24.5
else:
finish_ep = abs(t.location.x + 442) <= 3 and abs(t.location.y - 30) <= 3
terminated = finish_ep

except AssertionError:
terminated = True
reward = self._penalty_lane
print("No termino", self._count_ep, "finish:", finish_ep)
self._camera.error_lane = True
print("No termino", self._count_ep, "steps:", self._count, "index:", self._index_loc, "t:", self.ego_vehicle.get_transform())

# Check if a key has been pressed
if self._human:
Expand All @@ -282,6 +284,9 @@ def step(self, action:np.ndarray):
self._total_reward += reward
self._count += 1

if finish_ep:
print("Termino:", self._count_ep, "steps:", self._count, "index:", self._index_loc, "t:", t)

if terminated and self._train:
if self.model != None:
exploration_rate = self.model.exploration_rate
Expand Down
2 changes: 1 addition & 1 deletion src/deepRL/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def main(args):
port=args.port, alg=args.alg, normalize=True, seed=SEED),
n_envs=1)
else:
env = env_class(train=True, fixed_delta_seconds=args.delta, human=True, port=args.port,
env = env_class(train=True, fixed_delta_seconds=args.delta, human=False, port=args.port,
alg=args.alg, normalize=True, seed=SEED)

model = alg_class(policy, env, verbose=args.verbose, seed=SEED, tensorboard_log=log_dir, **model_params)
Expand Down
5 changes: 2 additions & 3 deletions src/follow_lane_pid.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,16 @@ def main(args):
if event.type == pygame.QUIT:
return

sensors.update_data(flip=False)
sensors.update_data()
t = ego_vehicle.get_transform()
print(t)

if t.location.y > -24.5:
print("Finish route")
return

# Control vehicle
error_road = camera.get_deviation()
camera.get_lane_points(show=True, num_points=10)
pygame.display.flip()
pid.controll_vehicle(error_road)

except KeyboardInterrupt:
Expand Down
77 changes: 0 additions & 77 deletions src/prueba.py

This file was deleted.

120 changes: 0 additions & 120 deletions src/teleoperator_copy.py

This file was deleted.

0 comments on commit 37f30fc

Please sign in to comment.