Added how to create a custom agent and some other minor fixes.

This commit is contained in:
corkyw10 2021-07-07 17:22:43 +02:00 committed by bernat
parent dd9442b47f
commit 144252915f
1 changed files with 65 additions and 10 deletions

View File

@ -1,6 +1,6 @@
# CARLA Agents
CARLA Agent scripts allow a vehicle to follow either a random, endless route or to take the shortest route to a given destination. Agents obey traffic lights and react to other obstacles in the road. There are three agent types available. Parameters such as target speed, braking distance, overtaking behavior, tailgating behavior, and more can be modified.
CARLA Agent scripts allow a vehicle to either follow a random, endless route or take the shortest route to a given destination. Agents obey traffic lights and react to other obstacles in the road. There are three agent types available. Parameters such as target speed, braking distance, overtaking behavior, tailgating behavior, and more can be modified. Actor classes can be modified or used as a base class to create custom agents according to the user's needs.
- [__Overview of agent scripts__](#overview-of-agent-scripts)
- [Planning and control](#planning-and-control)
@ -8,38 +8,40 @@ CARLA Agent scripts allow a vehicle to follow either a random, endless route or
- [__Implement an agent__](#implement-an-agent)
- [__Behavior types__](#behavior-types)
- [Create your own behavior type](#create-your-own-behavior-type)
- [__Creating an agent__](#creating-an-agent)
---
## Overview of agent scripts
All scripts involved in the CARLA Agents are found in `PythonAPI/carla/agents/navigation`. They fall into two categories; __planning and control__ and __agent behaviors__.
The main scripts involved in the CARLA Agents are found in `PythonAPI/carla/agents/navigation`. They fall into two categories; __planning and control__ and __agent behaviors__. There are also some calculus helper scripts found in `PythonAPI/carla/agents/tools`.
### Planning and control
- __`controller.py`:__ Combines longitudinal and lateral PID controllers into a single class, __VehiclePIDController__, used for low-level control of vehicles from the client side of CARLA.
- __`global_route_planner.py`:__ Gets detailed topology from the CARLA server to build a graph representation of the world map, providing waypoint and road option information for the __Local Planner__.
- __`global_route_planner_dao.py`:__ Accesses the information from the server-side instance of the __Global Route Planner__.
- __`local_planner.py`:__ Follows waypoints provided by the __Global Route Planner__ based on control inputs from the __VehiclePIDController__.
- __`local_planner.py`:__ Follows waypoints based on control inputs from the __VehiclePIDController__. Waypoints can either be provided by the __Global Route Planner__ or be calculated dynamically, choosing random paths at junctions, similar to the [Traffic Manager](adv_traffic_manager.md).
### Agent behaviors
- __`agent.py`:__ Contains the base class needed to define agents in CARLA. This agent roams around the map, avoids other vehicles, and responds to traffic lights.
- __`basic_agent.py`:__ Contains a class that implements a __Basic Agent__ that roams around the map or reaches a target destination in the shortest distance possible, avoiding other vehicles, responding to traffic lights but ignoring stop signs.
- __`behavior_agent.py`:__ Contains a class that implements a more complex __Behavior Agent__ that can reach a target destination in the shortest distance possible, following traffic lights, signs, and speed limits, while making overtaking or tailgating decisions. There are three predefined types that condition how the agent behaves.
- __`basic_agent.py`:__ Contains an agent base class that implements a __Basic Agent__ that roams around the map or reaches a target destination in the shortest distance possible, avoiding other vehicles, responding to traffic lights but ignoring stop signs.
- __`behavior_agent.py`:__ Contains a class that implements a more complex __Behavior Agent__ that can reach a target destination in the shortest distance possible, following traffic lights, signs, and speed limits while making overtaking or tailgating decisions. There are three predefined types that condition how the agent behaves.
- __`behavior_types.py`:__ Contains the parameters for the behavior types that condition the __Behavior Agent__; Cautious, Normal, and Aggressive.
---
## Implement an agent
This section will explain how to use the CARLA Agent classes in your own scripts. At the end of the section, you will find out how to run an example script that shows the different agents in action.
This section will explain how to use the example CARLA Agent classes in your own scripts. At the end of the section, you will find out how to run an example script that shows the different agents in action.
__1.__ Import the agent classes you want to use:
__1.__ Import the agent class you want to use:
```py
# To import a basic agent
from agents.navigation.basic_agent import BasicAgent
# To import a behavior agent
from agents.navigation.behavior_agent import BehaviorAgent
```
@ -80,6 +82,28 @@ while True:
vehicle.apply_control(agent.run_step())
```
__7.__ Instead of finishing the simulation when an agent has reached its target destination, a new, random route can be generated for the agent to follow:
```py
while True:
if agent.done():
agent.set_destination(random.choice(spawn_points).location)
print("The target has been reached, searching for another target")
vehicle.apply_control(agent.run_step())
```
The __Basic Agent__ provides a few methods to manipulate agent behavior or program routes to follow:
- __`set_target_speed(speed)`:__ Set the target speed in km/h
- __`follow_speed_limits(value=True)`:__ Sets the agent to ignore or follow speed limits.
- __`set_destination(end_location, start_location=None)`:__ The agent will travel from a specific start location to an end location via the shortest route possible. If no start location is provided, it will use the current agent location.
- __`set_global_plan(plan, stop_waypoint_creation=True, clean_queue=True)`:__ Adds a specific plan for the agent to follow. The plan argument should consist of a list of `[carla.Waypoint, RoadOption]` that will be the path the agent needs to take. `stop_waypoint_creation` will prevent waypoints from being automatically created once the path has run its course. `clean_queue` will reset the agent's current plan.
- __`trace_route(start_waypoint, end_waypoint)`:__ Gets the shortest distance between two waypoints from the Global Route Planner and returns the path as a list of `[carla.Waypoint, RoadOption]` for the agent to follow.
- __`ignore_traffic_lights(active=True)`:__ Set the agent to ignore or obey traffic lights.
- __`ignore_stop_signs(active=True)`:__ Set the agent to ignore or obey stop signs.
- __`ignore_vehicles(active=True)`:__ Set the agent to ignore or react to other vehicles.
The `automatic_control.py` script, found in `PythonAPI/examples`, is an example of the Basic and Behavior Agents in action. To try the script, navigate to the example directory and run the following command:
```sh
@ -97,7 +121,7 @@ python3 automatic_control.py --agent=Behavior --behavior=aggressive
Behavior types for the behavior agent are defined in `behavior_types.py`. The three pre-configured profiles are __'cautious'__, __'normal'__, and __'aggressive'__. You can use the set profiles, modify them or create your own. The following variables can be adjusted:
- __`max_speed`__: The maximum speed in km/h your vehicle will be able to reach.
- __`speed_lim_dist`__: Value in km/h that defines how far your vehicle's target speed will be from the current speed limit (eg. if the speed limit is 30km/h and `speed_lim_dist` is 10km/h, then the target speed will be 20km/h)
- __`speed_lim_dist`__: Value in km/h that defines how far your vehicle's target speed will be from the current speed limit (e.g., if the speed limit is 30km/h and `speed_lim_dist` is 10km/h, then the target speed will be 20km/h)
- __`speed_decrease`__: How quickly in km/h your vehicle will slow down when approaching a slower vehicle ahead.
- __`safety_time`__: Time-to-collision; an approximation of the time it will take for your vehicle to collide with one in front if it brakes suddenly.
- __`min_proximity_threshold`__: The minimum distance in meters from another vehicle or pedestrian before your vehicle performs a maneuver such as overtaking, avoidance, or tailgating.
@ -135,6 +159,37 @@ elif behavior == '<type_name>':
---
## Creating an agent
The CARLA Agents are just examples of the kind of agents users can run. Users can build upon the __Basic Agent__ to create their own agents. The possibilities are endless. There are only two elements that are necessary for every agent, __the initialization__ and the __run step__.
Find an example of a minimal layout of a custom agent below:
```py
import carla
from agents.navigation.basic_agent import BasicAgent
class CustomAgent(BasicAgent):
def __init__(self, vehicle, target_speed=20, debug=False):
"""
:param vehicle: actor to apply to local planner logic onto
:param target_speed: speed (in Km/h) at which the vehicle will move
"""
super(BasicAgent)
def run_step(self, debug=False):
"""
Execute one step of navigation.
:return: carla.VehicleControl
"""
# Actions to take during each simulation step
```
Check out the `basic_agent.py` and `behavior_agent.py` scripts to explore their structure and functions for more ideas on how to create your own.
---
You can explore the provided agent scripts, expand upon them or use them as a baseline to create your own. If you have any questions about the agents, feel free to post in the [forum](https://github.com/carla-simulator/carla/discussions/).