agents package

Subpackages

Submodules

agents.leaderboard_agent module

Leaderboard 2.0 compatible version of the Lunatic Agent

Attention

Command line overrides are currently not supported for this agent, therefore this module allows to define some global constants to that can adjust settings the settings if they are not set to None (default).

These global settings are only used if LunaticChallenger.setup() is called with a string pointing to a configuration file. Passing a LaunchConfig directly will skip the Hydra setup and the global values will not be used.

agents.leaderboard_agent.ENABLE_RSS = None

attr:

Type:

If not None, overwrites .LunaticAgentSettings.rss.enabled

Type:

py

agents.leaderboard_agent.ENABLE_DETECTION_MATRIX = None

attr:

Type:

If not None, overwrites .LunaticAgentSettings.detection_matrix.enabled

Type:

py

agents.leaderboard_agent.DATA_MATRIX_ASYNC = None

attr:.

Type:

Run the DetectionMatrix update in a separate thread; overwrites .LunaticAgentSettings.detection_matrix.sync

Type:

py

agents.leaderboard_agent.DATA_MATRIX_INTERVAL = None

attr:

Type:

When running synchronously how many ticks should be between two updates. Overwrites .LunaticAgentSettings.detection_matrix.sync_interval

Type:

py

agents.leaderboard_agent.CAMERA_SPECTATOR = None

attr:

Type:

Whether to use the camera spectator; overwrites camera.spectator<.CameraConfig.spectator>

Type:

py

agents.leaderboard_agent.DOWNSAMPLING_FACTOR_OF_ROUTE_COORDINATES = 5

The smaller the the value the more exact will the agent stick to the original route, BUT ONLY IF the route is provided as a fine-grained route.

NOTE: We should NOT rely on the route to be available in a fine grained manner -> Should work with larger values

Larger values will make the agent cut corners and drive more straight lines. Needs extra tools to stick to the road.

agents.leaderboard_agent.args: LaunchConfig

Global access to the launch config; set in LunaticChallenger.setup()

class agents.leaderboard_agent.LunaticChallenger(*args: Any, **kwargs: Any)[source]

Bases: AutonomousAgent, LunaticAgent

Variant of the LunaticAgent that is compatible with the Leaderboard 2.0 interface.

Attention

If the LunaticChallenger is used without the Leaderboard 2.0 framework the __call__() method should be used instead of run_step() to acquire the next control.

setup(path_to_conf_file: str | LaunchConfig)[source]

Initializes the underlying LunaticAgent as well as instances of GameFramework and WorldModel.

To some extends initializes the Hydra framework and load the configuration.

Parameters:

path_to_conf_file (str | LaunchConfig) –

Can either be a string pointing to a configuration file to load a LaunchConfig or a LaunchConfig to be used directly.

Note

If a LaunchConfig is passed directly the Hydra setup will be skipped.

sensors() list[dict][source]

Define the sensor suite required by the agent

Returns:

A list containing the required sensors in the following format

[
{'type': 'sensor.camera.rgb', 'x': 0.7, 'y': -0.4, 'z': 1.60, 'roll': 0.0, 'pitch': 0.0, 'yaw': 0.0,
'width': 300, 'height': 200, 'fov': 100, 'id': 'Left'},

{'type': 'sensor.camera.rgb', 'x': 0.7, 'y': 0.4, 'z': 1.60, 'roll': 0.0, 'pitch': 0.0, 'yaw': 0.0,
'width': 300, 'height': 200, 'fov': 100, 'id': 'Right'},

{'type': 'sensor.lidar.ray_cast', 'x': 0.7, 'y': 0.0, 'z': 1.60, 'yaw': 0.0, 'pitch': 0.0, 'roll': 0.0,
'id': 'LIDAR'}
]

Note

The LunaticChallenger does not use any sensors; the usage of ‘sensor.opendrive_map’ is experimental, however there is yet no parsing done for the data.

Return type:

list[dict]

run_step(debug: bool = False, second_pass=False) carla.VehicleControl[source]
run_step(input_data: Dict[str, Tuple[int, Any]], timestamp: float = -1) carla.VehicleControl

Attention

Use __call__() instead of this method!

Parameters:

debug (bool)

Return type:

carla.VehicleControl

set_global_plan(global_plan_gps: Sequence[tuple[_GPSDataDict, RoadOption]], global_plan_world_coord: Sequence[tuple[carla.Transform, RoadOption]])[source]

Set the plan (route) for the agent

Parameters:
__call__() carla.VehicleControl[source]

Executes the next step and returns the control for the vehicle.

Attention

Use this function instead of run_step()!

Return type:

carla.VehicleControl

destroy()[source]

Resets attributes and destroys helpers like the DetectionMatrix.

class agents.leaderboard_agent._GPSDataDict[source]

Bases: object

Bases: typing.TypedDict

Type hint for the global plan in GPS coordinates, used in LunaticChallenger.set_global_plan().

lat: float
lon: float
z: float

agents.lunatic_agent module

This module implements an agent that roams around a track following random waypoints and avoiding other vehicles. The agent also responds to traffic lights, traffic signs, and has different possible configurations.

class agents.lunatic_agent.LunaticAgent(settings: str | LunaticAgentSettings, world_model: WorldModel | None = None, *, vehicle: carla.Vehicle | None = None, overwrite_options: dict[str, Any] | None = None, debug: bool = True)[source]

Bases: BehaviorAgent

BasicAgent implements an agent that navigates the scene. This agent respects traffic lights and other vehicles, but ignores stop signs. It has several functions available to specify the route that the agent must follow, as well as to change its parameters in case a different driving mode is desired.

Parameters:
BASE_SETTINGS

Base AgentConfig class for this agent. This is used to create the default settings for the agent if none are provided.

alias of LunaticAgentSettings

DEFAULT_RULES: ClassVar[Dict[Phase, List[Rule]]]

Default rules of this agent class when initialized.

walkers_nearby: List[carla.Walker]
vehicles_nearby: List[carla.Vehicle]
static_obstacles_nearby: List[carla.Actor]

Static obstacles detected by the InformationManager

obstacles_nearby: List[carla.Actor]

Combination of vehicles_nearby, walkers_nearby and static_obstacles_nearby.

traffic_lights_nearby: List[carla.TrafficLight]
classmethod create_world_and_agent(args: LaunchConfig, *, vehicle: carla.Vehicle | None = None, sim_world: carla.World, settings_archetype: type[AgentConfig] | None = None, agent_config: 'LunaticAgentSettings' | None = None, overwrites: Dict[str, Any] | None = None) tuple[Self, WorldModel, GlobalRoutePlanner][source]

Setup function to create the agent from the LaunchConfig settings.

Note

Parameters:
Return type:

tuple[Self, WorldModel, GlobalRoutePlanner]

current_phase: Phase

current phase of the agent inside the loop

ctx: Context

The context object of the current step

rules: Dict[Phase, List[Rule]]

The rules of the this agent. When initialized the rules are deep copied from DEFAULT_RULES.

current_states: Dict[AgentState, int]

The current states of the agent. The count of the steps being each state is stored as value.

add_rule(rule: Rule, position: int | None = None)[source]

Add a rule to the agent. The rule will be inserted at the given position.

Parameters:
  • rule (Rule) – The rule to add

  • position (int | None) – The position to insert the rule at. If None the rule list will be sorted by priority. Defaults to None.

add_rules(rules: Rule | Iterable[Rule])[source]

Add a list of rules and sort the agents rules by priority.

Parameters:

rules (Rule | Iterable[Rule])

add_config_rules(config: LunaticAgentSettings | List[RuleCreatingParameters] | None = None)[source]

Adds rules

Parameters:

config (Optional[Union[LunaticAgentSettings, List[RuleCreatingParameters]]])

property live_info: LiveInfo
property detection_matrix

Returns DetectionMatrix.getMatrix if the matrix is set.

property detected_hazards_info: Dict[Hazard, Any]

Information about the detected hazards, e.g. severity.

property detected_hazards: Set[Hazard]
discard_hazard(hazard: Hazard, match: Literal['exact', 'subset', 'intersection'] = 'subset')

Discards a hazard from the detected hazards.

Parameters:
  • hazard (Hazard) – Hazard to remove from detected_hazards.

  • match (Literal['exact', 'subset', 'intersection']) –

    How to match the hazard to remove.

    • ”exact” removes if the exact Hazard flag is present.

    • ”subset” removes if the hazard is a subset of the detected hazard, e.g.:

      • discard_hazard(Hazard.VEHICLE, match="subset") would remove Hazard.OBSTACLE = Hazard.VEHICLE | PEDESTRIAN | STATIC_OBSTACLE.

      • discard_hazard(Hazard.TRAFFIC_LIGHT, match="subset") would not remove Hazard.TRAFFIC_LIGHT_RED.

add_hazard(hazard: Hazard, hazard_level: HazardSeverity = HazardSeverity.EMERGENCY)

Add the specified hazard to the detected hazards, in parallel the hazard_level can be set which which is stored in detected_hazards_info.

Parameters:
has_hazard(hazard: Hazard, match: Literal['exact', 'subset', 'intersection'] = 'intersection') bool

Checks if the hazard intersects with any of the detected hazards.

See discard_hazard() for the different matching options.

Parameters:
  • hazard (Hazard)

  • match (Literal['exact', 'subset', 'intersection'])

Return type:

bool

property current_traffic_light: carla.TrafficLight | None

Alias to self._last_traffic_light.

property phase_results: Dict[Phase, Any]

Retrieves agent.ctx.phase_results

Stores the results of the phases the agent has been in. By default the keys are set to Context.PHASE_NOT_EXECUTED.

property active_blocking_rules: Set[BlockingRule]

Blocking rules that are currently active and have taken over the agents loop.

update_information(second_pass: bool = False)[source]

Updates the information regarding the ego vehicle based on the surrounding world.

Parameters:

second_pass (bool) – Internal usage set to True if this function is called a second time in the same tick, e.g. after a route update.

See also

  • information_manager

Executes the phases:
is_taking_turn() bool[source]

Checks if the agent is taking a turn in a few steps

Return type:

bool

is_changing_lane() bool[source]

Checks if the agent is changing lanes in a few steps

Return type:

bool

execute_phase(phase: Phase, *, prior_results: Any, update_controls: carla.VehicleControl | None = None) Context[source]

Sets the current phase of the agent and executes all rules that are associated with it.

Parameters:
  • phase (Phase) – The phase to execute.

  • prior_results (Any) – The results of the previous phase, e.g. detected_hazards.

  • update_controls (Optional[carla.VehicleControl]) – Optionally controls that should be used from now onward.

Return type:

Context

__call__(debug: bool = False) carla.VehicleControl[source]

Calculates the next vehicle control object.

Parameters:

debug (bool)

Return type:

carla.VehicleControl

run_step(debug: bool = False, second_pass: bool = False) carla.VehicleControl[source]

Calculates the next vehicle control object.

Parameters:
  • debug (bool) – Whether to enable debug mode. This prints some more information and debug drawings.

  • second_pass (bool) – Internal usage set to True if this function is called a second time, e.g. after a route update.

Return type:

carla.VehicleControl

Warning

To be compatible with the LunaticChallenger, always pass debug as a positional argument, or use the __call__() method.

_inner_step(debug: bool = False) carla.VehicleControl[source]

This is is the internal function to provide the next control object for the agent; it should run every tick.

Raises:

EmergencyStopException – If detected_hazards is not empty when the function returns.

Parameters:

debug (bool)

Return type:

carla.VehicleControl

parse_keyboard_input(allow_user_updates: bool = True, *, control: carla.VehicleControl | None = None) None[source]

Parse the current user input and allow manual updates of the controls.

Parameters:
  • allow_user_updates (bool) – If True, the user can update the controls manually. Otherwise only the normal hotkeys do work.

  • control (Optional[carla.VehicleControl])

Return type:

None

Executes Phases:
apply_control(control: carla.VehicleControl | None = None)[source]

Applies the control to the agent’s actor. Will execute the Phase.EXECUTION | Phase.BEGIN and Phase.EXECUTION | Phase.END phases.

Note

The final control object that is applied to the agent’s actor is stored in the ctx.control attribute.

Raises ValueError:

If the control object is not set, i.e. get_control() returns None.

Parameters:

control (Optional[carla.VehicleControl])

detect_traffic_light(traffic_lights: ActorList[carla.TrafficLight] | None = None) TrafficLightDetectionResult

This method is in charge of behaviors for red lights.

Parameters:
Return type:

TrafficLightDetectionResult

traffic_light_manager(traffic_lights: ActorList[carla.TrafficLight] | None = None) TrafficLightDetectionResult

Alias of detect_traffic_light()

Parameters:
Return type:

TrafficLightDetectionResult

detect_hazard() Set[Hazard][source]

Checks for red traffic lights and pedestrians in the agents path.

If LunaticAgentSettings.obstacles.detect_yellow_tlights is set to True, then yellow traffic lights will also be regarded as a hazard that can trigger an EmergencyStopException in react_to_hazard() that is executed after this function.

Return type:

Set[Hazard]

react_to_hazard(hazard_detected: Hazard | Iterable[Hazard] | None) NoReturn | None[source]

Called when a hazard was detected-

Will store the detected hazards in the Context: ctx.prior_result If no rule clears this variable, the agent will throw a EmergencyStopException

Raises:

EmergencyStopException – If a hazard was detected and no rule cleared it.

Parameters:

hazard_detected (Union[Hazard, Iterable[Hazard], None])

Return type:

Optional[NoReturn]

pedestrian_avoidance_behavior() tuple[bool, ObstacleDetectionResult][source]

Detects pedestrians in the agents path.

Returns:

A tuple containing a boolean indicating if the detected pedestrian is dangerous and the detection result.

Return type:

tuple[bool, ObstacleDetectionResult]

car_following_behavior(vehicle_detected: bool, vehicle: carla.Actor, distance: float) carla.VehicleControl[source]

:param Must match ObstacleDetectionResult:

Assumes:
  • That an obstacle was detected:

    vehicle_detected is the detected vehicle.

Parameters:
Return type:

carla.VehicleControl

car_following_manager(vehicle: carla.Vehicle, distance: float, debug: bool = False) carla.VehicleControl

Module in charge of car-following behaviors when there’s someone in front of us.

Parameters:
Returns:

Calculates the control for the vehicle

Assumes:

No control has been calculated in this tick.

Return type:

carla.VehicleControl

emergency_manager(*, reasons: set[Hazard], control: carla.VehicleControl | None = None, force=False) carla.VehicleControl

Modifies the control values to perform an emergency stop. The steering remains unchanged to avoid going out of the lane during turns.

Parameters:
  • reasons (set[Hazard]) – set of Hazard that triggered the emergency stop. If empty this function will do nothing. Normally detected_hazards.

  • control (carla.VehicleControl | None) – control to be modified in place. If None the control for the current step will be calculated.

  • force – if True, the emergency stop will be performed even if the reasons are empty.

  • self (LunaticAgent)

Return type:

carla.VehicleControl

affected_by_traffic_light(lights_list: ActorList[carla.TrafficLight] | None = None, max_distance: float | None = None) TrafficLightDetectionResult

Method to check if there is a red light affecting the vehicle.

Parameters:
  • lights_list (Optional[ActorList[carla.TrafficLight]]) – list containing traffic light objects. If None, all traffic lights in the scene are used.

  • max_distance (Optional[float]) – max distance for a traffic lights to be considered relevant. If None, the base threshold value is used.

  • self (CanDetectNearbyTrafficLights)

Return type:

TrafficLightDetectionResult

detect_obstacles_in_path(obstacle_list: Sequence[carla.Actor] | carla.ActorList | Literal['all'] | None) ObstacleDetectionResult

This module is in charge of warning in case of a collision and managing possible tailgating chances.

Parameters:
Return type:

ObstacleDetectionResult

Note

  • Distance to detect vehicles that hinder a lance change are calculated with the max_detection_distance() function.

  • Former BehaviorAgent.collision_and_car_avoid_manager, which evaded cars via the tailgating function; this is now rule based.

Tip

As the first argument is the agent, this function can be used as a method, i.e it can be added / imported directly into the agent class’ body.

add_emergency_stop(control: carla.VehicleControl, reasons: set[str] | None = None) carla.VehicleControl[source]

Modifies the control values to perform an emergency stop. The steering remains unchanged to avoid going out of the lane during turns.

Parameters:
  • control (carla.VehicleControl) – (carla.VehicleControl) control to be modified

  • enable_random_steer – (bool, optional) Flag to enable random steering

  • reasons (Optional[set[str]])

Return type:

carla.VehicleControl

lane_change(direction: Literal['left', 'right'], same_lane_time: float = 0, other_lane_time: float = 0, lane_change_time: float = 2, *, check: bool = False)[source]

Changes the path so that the vehicle performs a lane change. Use ‘direction’ to specify either a ‘left’ or ‘right’ lane change, and the time parameters to fine tune the maneuver.

Steps for the lane change:
  1. same_lane_time seconds in the same lane.

  2. lane_change_time seconds to reach the other lane.

  3. other_lane_time seconds to stay in the other lane.

Parameters:
  • waypoint – The starting waypoint.

  • direction (Literal['left', 'right']) – The direction of the lane change, either ‘left’ or ‘right’. Defaults to ‘left’.

  • same_lane_time (float) – The time to follow the same lane before the lane change.

  • other_lane_time (float) – The time to follow the other lane after the lane change.

  • lane_change_time (float) – The time to reach the center of the last lane. A low value will make a fast lane change, while a high value will make slow lane change.

  • check (bool) – If True, the function will check if the lane change is possible, i.e. if there is a lane of carla.LaneType.Driving in the desired direction. Otherwise it can change to other lane types as well. Defaults to False.

done()

Check whether the agent has reached its destination.

get_global_planner()

Get method for protected member local planner

get_local_planner()

Get method for protected member local planner

make_lane_change(order: Iterable[Literal['left', 'right']] = ['left', 'right'], up_angle_th: int = 180, low_angle_th: int = 0) Literal[True] | None[source]

Move to the left/right lane if possible

Parameters:
  • order (Iterable[Literal['left', 'right']]) – The order in which the agent should try to change lanes. If a single string is given, the agent will try to change to that lane. Default is ["left", "right"].

  • up_angle_th (int) – The angle threshold for the upper limit of obstacle detection in the other lane. Default is 180 degrees, meaning that the agent will detect obstacles ahead.

  • low_angle_th (int) – The angle threshold for the lower limit of obstacle detection in the other lane. Default is 0 degrees, meaning that the agent will detect obstacles behind.

Return type:

Literal[True] | None

Assumes:
  • (self.config.live_info.incoming_direction == RoadOption.LANEFOLLOW and not waypoint.is_junction and self.config.live_info.current_speed > 10)

  • check_behind.obstacle_was_found and self.config.live_info.current_speed < get_speed(check_behind.obstacle)

set_global_plan(plan: list[tuple[carla.Waypoint, RoadOption]], stop_waypoint_creation: bool = True, clean_queue: bool = True) None

Adds a specific plan to the agent.

param plan:

list of [carla.Waypoint, RoadOption] representing the route to be followed

param stop_waypoint_creation:

stops the automatic random creation of waypoints

param clean_queue:

resets the current agent’s plan

Parameters:
Return type:

None

set_offset(offset)

Sets an offset for the vehicle

trace_route(start_waypoint, end_waypoint)

Calculates the shortest route between a starting and ending waypoint.

param start_waypoint (carla.Waypoint):

initial waypoint

param end_waypoint (carla.Waypoint):

final waypoint

verify_settings(config: LunaticAgentSettings | None = None, *, verify_dataclass: 'type[AgentConfig]' | bool = True, strictness: Literal[-1, 0, 1, 2, 3, 4] = 0)[source]

Verifies the settings of the LunaticAgent. Foremost this checks if the planner.dt value has been set to the speed of the world ticks in synchronous mode. Secondly if verify_dataclass=True or a different AgentConfig class is provided, it will check for correct type usage.

Parameters:
  • config (Optional[LunaticAgentSettings]) – The configuration to verify. If not provided, the agent’s default configuration will be used. Defaults to None.

  • verify_dataclass (Union['type[AgentConfig]', bool]) – Determines the dataclass to use for verification. If True, the BASE_SETTINGS dataclass will be used. See AgentConfig.check_config() for more details. Defaults to True.

  • strictness (Literal[-1, 0, 1, 2, 3, 4]) – The strictness level for AgentConfig.check_config(). Defaults to 3.

Raises:
  • TypeError – If verify_dataclass is not a valid AgentConfig subclass or True.

  • MissingMandatoryValue – If config.planner.dt is not present or not a float

get_control()[source]

Returns the currently planned control of the agent.

If retrieved before the local planner has been run, it will return None.

set_control(control: carla.VehicleControl)[source]

Set new controls for the agent. Must be called before apply_control.

Raises:

ValueError – If the control is None.

Parameters:

control (carla.VehicleControl)

set_destination(end_location: carla.Location, start_location: carla.Location | None = None, clean_queue: bool = True) None[source]

This method creates a list of waypoints between a starting and ending location, based on the route returned by the global router, and adds it to the local planner. If no starting location is passed and clean_queue is True, the vehicle local planner’s target location is chosen, which corresponds (by default), to a location about 5 meters in front of the vehicle. If clean_queue is False the newly planned route will be appended to the current route.

param end_location (carla.Location):

final location of the route

param start_location (carla.Location):

starting location of the route

param clean_queue (bool):

Whether to clear or append to the currently planned route

Parameters:
Return type:

None

set_target_speed(speed: float) None[source]
Changes the target speed of the agent.
param speed (float):

target speed in Km/h

Parameters:

speed (float)

Return type:

None

follow_speed_limits(value: bool = True) None[source]

If active, the agent will dynamically change the target speed according to the speed limits.

Parameters:

value (bool) – Whether to activate this behavior

Return type:

None

ignore_traffic_lights(active: bool = True) None[source]

(De)activates the checks for traffic lights

Parameters:

active (bool)

Return type:

None

ignore_stop_signs(active: bool = True) None[source]

(De)activates the checks for stop signs

Parameters:

active (bool)

Return type:

None

ignore_vehicles(active: bool = True) None[source]

(De)activates the checks for stop signs

Parameters:

active (bool)

Return type:

None

rss_set_road_boundaries_mode(road_boundaries_mode: bool | RssRoadBoundariesModeAlias | None = None) None[source]
Parameters:

road_boundaries_mode (Optional[Union[bool, RssRoadBoundariesModeAlias]])

Return type:

None

detect_vehicles(vehicle_list: Sequence[carla.Actor] | carla.ActorList | None = None, max_distance: float | None = None, up_angle_th: float = 90, low_angle_th: float = 0, lane_offset: int = 0) ObstacleDetectionResult

Method to check if there is a vehicle in front or around the agent blocking its path.

Parameters:
  • self (CanDetectObstacles) – The agent

  • vehicle_list (Optional[Sequence[carla.Actor] | carla.ActorList]) – list containing vehicle objects. If None, all vehicle in the scene are used.

  • max_distance (Optional[float]) – max free-space to check for obstacles. If None, the LunaticAgentSettings.obstacles.base_vehicle_threshold value is used.

  • lane_offset (int) – check a different lane than the one the agent is currently in.

  • up_angle_th (float)

  • low_angle_th (float)

Return type:

ObstacleDetectionResult

The angle between the location and reference transform will also be taken into account. Being 0 a location in front and 180, one behind, i.e, the vector between has to satisfy: low_angle_th < angle < up_angle_th.

Tip

As the first argument is the agent, this function can be used as a method, i.e it can be added / imported directly into the agent class’ body.

Deprecated since version Use: detect_obstacles() instead.

max_detection_distance(lane: Literal['same_lane', 'other_lane', 'overtaking', 'tailgating']) float

Convenience function to be used with lunatic_agent_tools.detect_vehicles() and LunaticAgent.detect_obstacles_in_path.

The max distance to consider an obstacle is calculated as:

max(obstacles.min_proximity_threshold,
    live_info.current_speed_limit / obstacles.speed_detection_downscale.[same|other]_lane)
Parameters:
  • self (HasConfig['BehaviorAgentSettings | LunaticAgentSettings']) – An object that implements the config and live_info attributes

  • lane (Literal['same_lane', 'other_lane', 'overtaking', 'tailgating']) – The lane to consider.

Return type:

float

Note

lane must be a key in BehaviorAgentObstacleSettings.SpeedLimitDetectionDownscale.

destroy()[source]

Resets attributes and destroys helpers like the DetectionMatrix.