Skip to content

Commit

Permalink
Dynamic Traffic Light distance
Browse files Browse the repository at this point in the history
BasicAgent and the BehaviourAgent do differ here, this allows both options for both agents.
  • Loading branch information
Daraan committed Mar 5, 2024
1 parent 2f7ad9a commit 3f587a8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
9 changes: 9 additions & 0 deletions PythonAPI/carla/agents/conf/agent_settings_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,16 @@ class BasicAgentObstacleSettings(AgentConfig):
USAGE: max_tlight_distance = base_tlight_threshold + detection_speed_ratio * vehicle_speed
"""

dynamic_threshold_by_speed : bool = True
"""
Whether to add a dynamic threshold based on the vehicle speed to the base threshold.
Usage: base_threshold + dynamic_threshold_by_speed * vehicle_speed
"""

detection_angles : BasicAgentObstacleDetectionAngles = field(default_factory=BasicAgentObstacleDetectionAngles)
"""Defines detection angles used when checking for obstacles."""


@dataclass
class BehaviorAgentObstacleSettings(BasicAgentObstacleSettings):
Expand Down
8 changes: 7 additions & 1 deletion PythonAPI/carla/agents/navigation/basic_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,13 @@ def run_step(self):
hazard_detected = True

# Check if the vehicle is affected by a red traffic light
max_tlight_distance = self.config.obstacles.base_tlight_threshold + self.config.obstacles.detection_speed_ratio * vehicle_speed
if self.config.obstacles.dynamic_threshold_by_speed:
# Basic agent setting:
max_tlight_distance = self.config.obstacles.base_tlight_threshold + self.config.obstacles.detection_speed_ratio * self.config.live_info.current_speed
else:
# Behavior setting:
max_tlight_distance = self.config.obstacles.base_tlight_threshold

affected_by_tlight, _ = self._affected_by_traffic_light(self._lights_list, max_tlight_distance)
if affected_by_tlight:
hazard_detected = True
Expand Down
10 changes: 8 additions & 2 deletions PythonAPI/carla/agents/navigation/behavior_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,13 @@ def traffic_light_manager(self):
"""
actor_list = self._world.get_actors()
lights_list = actor_list.filter("*traffic_light*")
affected, _ = self._affected_by_traffic_light(lights_list)
if self.config.obstacles.dynamic_threshold_by_speed:
# Basic agent setting:
max_tlight_distance = self.config.obstacles.base_tlight_threshold + self.config.obstacles.detection_speed_ratio * self.config.live_info.current_speed
else:
# Behavior setting:
max_tlight_distance = self.config.obstacles.base_tlight_threshold
affected, _ = self._affected_by_traffic_light(lights_list, max_distance=max_tlight_distance)

return affected

Expand Down Expand Up @@ -301,7 +307,7 @@ def run_step(self, debug=False):
elif self._incoming_waypoint.is_junction and (self._incoming_direction in [RoadOption.LEFT, RoadOption.RIGHT]):
target_speed = min([
self.config.speed.max_speed,
self.config.live_info.current_speed_limit - 5])
self.config.live_info.current_speed_limit - self.config.speed.intersection_speed_decrease])
self._local_planner.set_speed(target_speed)
control = self._local_planner.run_step(debug=debug)

Expand Down
7 changes: 6 additions & 1 deletion PythonAPI/carla/agents/navigation/constant_velocity_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,12 @@ def run_step(self):
hazard_detected = True

# Check if the vehicle is affected by a red traffic light
max_tlight_distance = self.config.obstacles.base_tlight_threshold + self.config.distance.distance_ratio * vehicle_speed
if self.config.obstacles.dynamic_threshold_by_speed:
# Basic agent setting:
max_tlight_distance = self.config.obstacles.base_tlight_threshold + self.config.obstacles.detection_speed_ratio * self.config.live_info.current_speed
else:
# Behavior setting:
max_tlight_distance = self.config.obstacles.base_tlight_threshold
affected_by_tlight, _ = self._affected_by_traffic_light(lights_list, max_tlight_distance)
if affected_by_tlight:
hazard_speed = 0
Expand Down

0 comments on commit 3f587a8

Please sign in to comment.