CARLA
Loading...
Searching...
No Matches
BasicAgent.h
Go to the documentation of this file.
1// Copyright (c) 2018-2020 CVC.
2//
3// This work is licensed under the terms of the MIT license.
4// For a copy, see <https://opensource.org/licenses/MIT>.
5
6#pragma once
7
8#include "carla/Memory.h"
11#include "carla/geom/Location.h"
13
14#include <cstdint>
15#include <memory>
16#include <optional>
17#include <string>
18#include <unordered_map>
19#include <utility>
20#include <vector>
21
22namespace carla {
23
24namespace client {
25 class ActorList;
26 class Map;
27 class TrafficLight;
28 class Vehicle;
29 class Waypoint;
30 class World;
31 class Actor;
32} // namespace client
33
34namespace agents {
35namespace navigation {
36
37 // Forward decls — heavy headers only pulled in BasicAgent.cpp.
38 class GlobalRoutePlanner;
39
40 /// C++ port of agents.navigation.basic_agent.BasicAgent.
41 ///
42 /// Roams the scene following random or scripted waypoints while obeying
43 /// traffic lights and avoiding other vehicles. Stop signs are ignored.
44 class BasicAgent {
45 public:
46
47 /// Tunable knobs. Inherits LocalPlanner::Options so the same dictionary
48 /// can drive the local planner.
51 bool ignore_stop_signs = false;
52 bool ignore_vehicles = false;
53 bool use_bbs_detection = false;
54 float sampling_resolution = 2.0f;
58 float max_brake = 0.6f; // emergency brake (overrides LocalPlanner.max_brake)
59 };
60
61 /// Out-of-line default factory; see LocalPlanner::DefaultOptions().
62 static Options DefaultOptions();
63
65 float target_speed_kmh = 20.0f,
66 const Options &options = DefaultOptions(),
67 SharedPtr<client::Map> map = nullptr,
68 SharedPtr<GlobalRoutePlanner> grp = nullptr);
69
70 virtual ~BasicAgent() = default;
71
72 /// Overwrites throttle/brake to perform an emergency stop. Steering is
73 /// left intact so the agent doesn't drift mid-turn.
75
76 void SetTargetSpeed(float speed_kmh);
77 void FollowSpeedLimits(bool value = true);
78
81
82 /// Computes a route from `start` (or the current target waypoint) to
83 /// `end` and forwards it to the local planner.
84 void SetDestination(const geom::Location &end,
85 std::optional<geom::Location> start = std::nullopt);
86
87 void SetGlobalPlan(const std::vector<LocalPlanner::PlanItem> &plan,
88 bool stop_waypoint_creation = true,
89 bool clean_queue = true);
90
91 std::vector<LocalPlanner::PlanItem>
94
96
97 bool Done() const;
98
99 void IgnoreTrafficLights(bool active = true);
100 void IgnoreStopSigns(bool active = true);
101 void IgnoreVehicles(bool active = true);
102 void SetOffset(float offset);
103
104 /// Replaces the current plan with one that performs a lane change.
105 /// `direction` must be "left" or "right".
106 void LaneChange(const std::string &direction,
107 float same_lane_time = 0.0f,
108 float other_lane_time = 0.0f,
109 float lane_change_time = 2.0f);
110
111 protected:
112
113 // ---- helpers exposed to BehaviorAgent ----------------------------------
114
115 /// Returns (affected, light) similar to Python `_affected_by_traffic_light`.
116 /// `lights` may be null — in which case all *traffic_light* actors are
117 /// pulled from the world. `max_distance` defaults to `_base_tlight_threshold`.
118 std::pair<bool, SharedPtr<client::TrafficLight>>
120 std::optional<float> max_distance = std::nullopt);
121
123 bool affected = false;
125 float distance = -1.0f;
126 };
127
128 /// Mirrors Python `_vehicle_obstacle_detected`.
130 SharedPtr<client::ActorList> vehicles = nullptr,
131 std::optional<float> max_distance = std::nullopt,
132 float up_angle_th = 90.0f,
133 float low_angle_th = 0.0f,
134 int lane_offset = 0);
135
136 /// Mirrors Python `_generate_lane_change_path`. Returns an empty vector
137 /// if no valid path exists.
138 std::vector<LocalPlanner::PlanItem> GenerateLaneChangePath(
140 const std::string &direction = "left",
141 float distance_same_lane = 10.0f,
142 float distance_other_lane = 25.0f,
143 float lane_change_distance = 25.0f,
144 bool check = true,
145 int lane_changes = 1,
146 float step_distance = 2.0f);
147
148 // ---- members BehaviorAgent reads/touches -------------------------------
153 std::unique_ptr<LocalPlanner> _local_planner;
156 std::unordered_map<uint32_t, SharedPtr<client::Waypoint>> _lights_map;
157
168 float _offset;
169 };
170
174
175} // namespace navigation
176} // namespace agents
177} // namespace carla
C++ port of agents.navigation.basic_agent.BasicAgent.
Definition BasicAgent.h:44
SharedPtr< client::Map > _map
Definition BasicAgent.h:151
std::pair< bool, SharedPtr< client::TrafficLight > > AffectedByTrafficLight(SharedPtr< client::ActorList > lights=nullptr, std::optional< float > max_distance=std::nullopt)
Returns (affected, light) similar to Python _affected_by_traffic_light.
VehicleObstacle VehicleObstacleDetected(SharedPtr< client::ActorList > vehicles=nullptr, std::optional< float > max_distance=std::nullopt, float up_angle_th=90.0f, float low_angle_th=0.0f, int lane_offset=0)
Mirrors Python _vehicle_obstacle_detected.
rpc::VehicleControl AddEmergencyStop(rpc::VehicleControl c) const
Overwrites throttle/brake to perform an emergency stop.
void IgnoreTrafficLights(bool active=true)
SharedPtr< client::World > _world
Definition BasicAgent.h:150
static Options DefaultOptions()
Out-of-line default factory; see LocalPlanner::DefaultOptions().
Definition BasicAgent.h:171
SharedPtr< client::ActorList > _lights_list
Definition BasicAgent.h:155
void IgnoreVehicles(bool active=true)
void SetGlobalPlan(const std::vector< LocalPlanner::PlanItem > &plan, bool stop_waypoint_creation=true, bool clean_queue=true)
void FollowSpeedLimits(bool value=true)
SharedPtr< client::TrafficLight > _last_traffic_light
Definition BasicAgent.h:152
std::vector< LocalPlanner::PlanItem > TraceRoute(SharedPtr< client::Waypoint > start, SharedPtr< client::Waypoint > end)
SharedPtr< client::Vehicle > _vehicle
Definition BasicAgent.h:149
std::vector< LocalPlanner::PlanItem > GenerateLaneChangePath(SharedPtr< client::Waypoint > waypoint, const std::string &direction="left", float distance_same_lane=10.0f, float distance_other_lane=25.0f, float lane_change_distance=25.0f, bool check=true, int lane_changes=1, float step_distance=2.0f)
Mirrors Python _generate_lane_change_path.
virtual rpc::VehicleControl RunStep()
std::unordered_map< uint32_t, SharedPtr< client::Waypoint > > _lights_map
Definition BasicAgent.h:156
SharedPtr< GlobalRoutePlanner > _global_planner
Definition BasicAgent.h:154
std::unique_ptr< LocalPlanner > _local_planner
Definition BasicAgent.h:153
void SetDestination(const geom::Location &end, std::optional< geom::Location > start=std::nullopt)
Computes a route from start (or the current target waypoint) to end and forwards it to the local plan...
void IgnoreStopSigns(bool active=true)
GlobalRoutePlanner * GetGlobalPlanner()
C++ port of agents.navigation.global_route_planner.GlobalRoutePlanner.
LocalPlanner reproduces agents.navigation.local_planner.LocalPlanner.
Represents an actor in the simulation.
road::element::LaneMarking::LaneChange LaneChange
This file contains definitions of common data structures used in traffic manager.
std::shared_ptr< T > SharedPtr
Definition Memory.h:15
Aggregated configuration. Mirrors the opt_dict in local_planner.py.