CARLA
Toggle main menu visibility
Loading...
Searching...
No Matches
LibCarla
source
carla
agents
navigation
LocalPlanner.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
"
9
#include "
carla/agents/navigation/PIDController.h
"
10
#include "
carla/agents/navigation/Types.h
"
11
#include "
carla/rpc/VehicleControl.h
"
12
13
#include <deque>
14
#include <memory>
15
#include <random>
16
#include <utility>
17
#include <vector>
18
19
namespace
carla
{
20
21
namespace
client
{
22
class
Map
;
23
class
Vehicle
;
24
class
Waypoint
;
25
}
// namespace client
26
27
namespace
agents
{
28
namespace
navigation
{
29
30
/// LocalPlanner reproduces `agents.navigation.local_planner.LocalPlanner`.
31
/// It maintains a queue of (waypoint, RoadOption) pairs, refills it as
32
/// needed, and drives the vehicle along it via a VehiclePIDController.
33
class
LocalPlanner
{
34
public
:
35
using
PlanItem
= std::pair<SharedPtr<client::Waypoint>,
RoadOption
>;
36
37
/// Aggregated configuration. Mirrors the `opt_dict` in local_planner.py.
38
struct
Options
{
39
float
dt
= 1.0f / 20.0f;
40
float
target_speed
= 20.0f;
// km/h
41
float
sampling_radius
= 2.0f;
42
// LateralArgs/LongitudinalArgs supply their own defaults (1.95/0.05/0.20
43
// and 1.50/0.05/0.20 with dt=0.05) — leaving these value-initialized.
44
// Earlier brace-init member defaults here triggered a GCC aggregate-init
45
// quirk when Options was used as `= {}` default arg downstream.
46
VehiclePIDController::LateralArgs
lateral
{};
47
VehiclePIDController::LongitudinalArgs
longitudinal
{};
48
float
max_throttle
= 0.75f;
49
float
max_brake
= 0.30f;
50
float
max_steering
= 0.80f;
51
float
offset
= 0.0f;
52
float
base_min_distance
= 3.0f;
53
float
distance_ratio
= 0.5f;
54
bool
follow_speed_limits
=
false
;
55
};
56
57
/// Construct a LocalPlanner. If @a map is null we pull the map from the
58
/// vehicle's world (matches the Python `map_inst` fallback).
59
/// Returns a value-initialized Options. Defined out-of-line because GCC's
60
/// default-arg parser refuses `= {}` / `= Options()` when the struct
61
/// inherits from another aggregate with default member initializers.
62
static
Options
DefaultOptions
();
63
64
LocalPlanner
(
65
SharedPtr<client::Vehicle>
vehicle,
66
const
Options
&opt =
DefaultOptions
(),
67
SharedPtr<client::Map>
map =
nullptr
);
68
69
/// Drop the vehicle reference (mirrors `reset_vehicle`).
70
void
ResetVehicle
();
71
72
/// Override the target speed in km/h.
73
void
SetSpeed
(
float
speed_kmh);
74
75
/// Toggle automatic following of road speed limits.
76
void
FollowSpeedLimits
(
bool
value =
true
);
77
78
/// Replace (or append to) the waypoint queue.
79
void
SetGlobalPlan
(
80
const
std::vector<PlanItem> &plan,
81
bool
stop_waypoint_creation =
true
,
82
bool
clean_queue =
true
);
83
84
/// Apply a lateral offset to the underlying PID controller.
85
void
SetOffset
(
float
offset);
86
87
/// Drive one tick. Returns the VehicleControl to apply.
88
rpc::VehicleControl
RunStep
(
bool
debug =
false
);
89
90
/// Peek `steps` ahead in the queue, falling back to the last entry if
91
/// the queue is too short. Returns (nullptr, RoadOption::Void) when
92
/// the queue is empty.
93
std::pair<SharedPtr<client::Waypoint>,
RoadOption
>
94
GetIncomingWaypointAndDirection
(
int
steps = 3)
const
;
95
96
/// Read-only view of the queued plan.
97
const
std::deque<PlanItem> &
GetPlan
()
const
;
98
99
/// True when the queue is empty.
100
bool
Done
()
const
;
101
102
/// Currently active target waypoint (mirrors `target_waypoint`).
103
SharedPtr<client::Waypoint>
TargetWaypoint
()
const
;
104
105
/// Currently active target RoadOption (mirrors `target_road_option`).
106
RoadOption
TargetRoadOption
()
const
;
107
108
private
:
109
/// Build the VehiclePIDController and seed the queue with the vehicle's
110
/// current waypoint. Called from the constructor.
111
void
InitController
();
112
113
/// Append up to @a k waypoints to the back of the queue, branching with
114
/// random choice on intersections (mirrors `_compute_next_waypoints`).
115
void
ComputeNextWaypoints
(std::size_t k);
116
117
SharedPtr<client::Vehicle>
_vehicle
;
118
SharedPtr<client::Map>
_map
;
119
120
std::unique_ptr<VehiclePIDController>
_vehicle_controller
;
121
122
SharedPtr<client::Waypoint>
_target_waypoint
;
123
RoadOption
_target_road_option
=
RoadOption::Void
;
124
125
std::deque<PlanItem>
_waypoints_queue
;
126
std::size_t
_max_queue_size
= 10000;
127
std::size_t
_min_queue_length
= 100;
128
bool
_stop_waypoint_creation
=
false
;
129
130
// Tunables (mirror the Python instance fields).
131
float
_dt
;
132
float
_target_speed
;
133
float
_sampling_radius
;
134
VehiclePIDController::LateralArgs
_lateral_args
;
135
VehiclePIDController::LongitudinalArgs
_longitudinal_args
;
136
float
_max_throttle
;
137
float
_max_brake
;
138
float
_max_steering
;
139
float
_offset
;
140
float
_base_min_distance
;
141
float
_distance_ratio
;
142
bool
_follow_speed_limits
;
143
144
// Random branching. Seeded once per planner.
145
mutable
std::mt19937
_rng
;
146
};
147
148
inline
LocalPlanner::Options
LocalPlanner::DefaultOptions
() {
149
return
Options
{};
150
}
151
152
}
// namespace navigation
153
}
// namespace agents
154
}
// namespace carla
VehicleControl.h
Memory.h
PIDController.h
Types.h
carla::agents::navigation::LocalPlanner::ComputeNextWaypoints
void ComputeNextWaypoints(std::size_t k)
Append up to k waypoints to the back of the queue, branching with random choice on intersections (mir...
Definition
LocalPlanner.cpp:141
carla::agents::navigation::LocalPlanner::_longitudinal_args
VehiclePIDController::LongitudinalArgs _longitudinal_args
Definition
LocalPlanner.h:135
carla::agents::navigation::LocalPlanner::_waypoints_queue
std::deque< PlanItem > _waypoints_queue
Definition
LocalPlanner.h:125
carla::agents::navigation::LocalPlanner::_vehicle
SharedPtr< client::Vehicle > _vehicle
Definition
LocalPlanner.h:117
carla::agents::navigation::LocalPlanner::_min_queue_length
std::size_t _min_queue_length
Definition
LocalPlanner.h:127
carla::agents::navigation::LocalPlanner::_target_waypoint
SharedPtr< client::Waypoint > _target_waypoint
Definition
LocalPlanner.h:122
carla::agents::navigation::LocalPlanner::_map
SharedPtr< client::Map > _map
Definition
LocalPlanner.h:118
carla::agents::navigation::LocalPlanner::RunStep
rpc::VehicleControl RunStep(bool debug=false)
Drive one tick. Returns the VehicleControl to apply.
Definition
LocalPlanner.cpp:212
carla::agents::navigation::LocalPlanner::TargetRoadOption
RoadOption TargetRoadOption() const
Currently active target RoadOption (mirrors target_road_option).
Definition
LocalPlanner.cpp:293
carla::agents::navigation::LocalPlanner::_follow_speed_limits
bool _follow_speed_limits
Definition
LocalPlanner.h:142
carla::agents::navigation::LocalPlanner::_stop_waypoint_creation
bool _stop_waypoint_creation
Definition
LocalPlanner.h:128
carla::agents::navigation::LocalPlanner::_max_queue_size
std::size_t _max_queue_size
Definition
LocalPlanner.h:126
carla::agents::navigation::LocalPlanner::InitController
void InitController()
Build the VehiclePIDController and seed the queue with the vehicle's current waypoint.
Definition
LocalPlanner.cpp:107
carla::agents::navigation::LocalPlanner::FollowSpeedLimits
void FollowSpeedLimits(bool value=true)
Toggle automatic following of road speed limits.
Definition
LocalPlanner.cpp:137
carla::agents::navigation::LocalPlanner::TargetWaypoint
SharedPtr< client::Waypoint > TargetWaypoint() const
Currently active target waypoint (mirrors target_waypoint).
Definition
LocalPlanner.cpp:289
carla::agents::navigation::LocalPlanner::SetSpeed
void SetSpeed(float speed_kmh)
Override the target speed in km/h.
Definition
LocalPlanner.cpp:129
carla::agents::navigation::LocalPlanner::DefaultOptions
static Options DefaultOptions()
Construct a LocalPlanner.
Definition
LocalPlanner.h:148
carla::agents::navigation::LocalPlanner::_lateral_args
VehiclePIDController::LateralArgs _lateral_args
Definition
LocalPlanner.h:134
carla::agents::navigation::LocalPlanner::Done
bool Done() const
True when the queue is empty.
Definition
LocalPlanner.cpp:285
carla::agents::navigation::LocalPlanner::LocalPlanner
LocalPlanner(SharedPtr< client::Vehicle > vehicle, const Options &opt=DefaultOptions(), SharedPtr< client::Map > map=nullptr)
Definition
LocalPlanner.cpp:80
carla::agents::navigation::LocalPlanner::SetGlobalPlan
void SetGlobalPlan(const std::vector< PlanItem > &plan, bool stop_waypoint_creation=true, bool clean_queue=true)
Replace (or append to) the waypoint queue.
Definition
LocalPlanner.cpp:183
carla::agents::navigation::LocalPlanner::_rng
std::mt19937 _rng
Definition
LocalPlanner.h:145
carla::agents::navigation::LocalPlanner::_max_throttle
float _max_throttle
Definition
LocalPlanner.h:136
carla::agents::navigation::LocalPlanner::GetPlan
const std::deque< PlanItem > & GetPlan() const
Read-only view of the queued plan.
Definition
LocalPlanner.cpp:281
carla::agents::navigation::LocalPlanner::GetIncomingWaypointAndDirection
std::pair< SharedPtr< client::Waypoint >, RoadOption > GetIncomingWaypointAndDirection(int steps=3) const
Peek steps ahead in the queue, falling back to the last entry if the queue is too short.
Definition
LocalPlanner.cpp:270
carla::agents::navigation::LocalPlanner::_max_brake
float _max_brake
Definition
LocalPlanner.h:137
carla::agents::navigation::LocalPlanner::_vehicle_controller
std::unique_ptr< VehiclePIDController > _vehicle_controller
Definition
LocalPlanner.h:120
carla::agents::navigation::LocalPlanner::SetOffset
void SetOffset(float offset)
Apply a lateral offset to the underlying PID controller.
Definition
LocalPlanner.cpp:205
carla::agents::navigation::LocalPlanner::_target_road_option
RoadOption _target_road_option
Definition
LocalPlanner.h:123
carla::agents::navigation::LocalPlanner::_max_steering
float _max_steering
Definition
LocalPlanner.h:138
carla::agents::navigation::LocalPlanner::_dt
float _dt
Definition
LocalPlanner.h:131
carla::agents::navigation::LocalPlanner::_base_min_distance
float _base_min_distance
Definition
LocalPlanner.h:140
carla::agents::navigation::LocalPlanner::ResetVehicle
void ResetVehicle()
Drop the vehicle reference (mirrors reset_vehicle).
Definition
LocalPlanner.cpp:125
carla::agents::navigation::LocalPlanner::_target_speed
float _target_speed
Definition
LocalPlanner.h:132
carla::agents::navigation::LocalPlanner::_sampling_radius
float _sampling_radius
Definition
LocalPlanner.h:133
carla::agents::navigation::LocalPlanner::_offset
float _offset
Definition
LocalPlanner.h:139
carla::agents::navigation::LocalPlanner::PlanItem
std::pair< SharedPtr< client::Waypoint >, RoadOption > PlanItem
Definition
LocalPlanner.h:35
carla::agents::navigation::LocalPlanner::_distance_ratio
float _distance_ratio
Definition
LocalPlanner.h:141
carla::client::Map
Definition
client/Map.h:32
carla::client::Vehicle
Definition
Vehicle.h:33
carla::client::Waypoint
Definition
client/Waypoint.h:31
carla::rpc::VehicleControl
Definition
LibCarla/source/carla/rpc/VehicleControl.h:20
carla::agents::navigation
Definition
BasicAgent.cpp:40
carla::agents::navigation::RoadOption
RoadOption
Topological direction emitted by the planners.
Definition
agents/navigation/Types.h:16
carla::agents::navigation::RoadOption::Void
@ Void
Definition
agents/navigation/Types.h:17
carla::agents
Definition
BasicAgent.cpp:39
carla::client
Definition
BasicAgent.h:24
carla
This file contains definitions of common data structures used in traffic manager.
Definition
SignComponent.h:21
carla::SharedPtr
std::shared_ptr< T > SharedPtr
Definition
Memory.h:15
carla::agents::navigation::LocalPlanner::Options
Aggregated configuration. Mirrors the opt_dict in local_planner.py.
Definition
LocalPlanner.h:38
carla::agents::navigation::LocalPlanner::Options::longitudinal
VehiclePIDController::LongitudinalArgs longitudinal
Definition
LocalPlanner.h:47
carla::agents::navigation::LocalPlanner::Options::dt
float dt
Definition
LocalPlanner.h:39
carla::agents::navigation::LocalPlanner::Options::lateral
VehiclePIDController::LateralArgs lateral
Definition
LocalPlanner.h:46
carla::agents::navigation::LocalPlanner::Options::base_min_distance
float base_min_distance
Definition
LocalPlanner.h:52
carla::agents::navigation::LocalPlanner::Options::max_steering
float max_steering
Definition
LocalPlanner.h:50
carla::agents::navigation::LocalPlanner::Options::sampling_radius
float sampling_radius
Definition
LocalPlanner.h:41
carla::agents::navigation::LocalPlanner::Options::distance_ratio
float distance_ratio
Definition
LocalPlanner.h:53
carla::agents::navigation::LocalPlanner::Options::max_throttle
float max_throttle
Definition
LocalPlanner.h:48
carla::agents::navigation::LocalPlanner::Options::max_brake
float max_brake
Definition
LocalPlanner.h:49
carla::agents::navigation::LocalPlanner::Options::target_speed
float target_speed
Definition
LocalPlanner.h:40
carla::agents::navigation::LocalPlanner::Options::follow_speed_limits
bool follow_speed_limits
Definition
LocalPlanner.h:54
carla::agents::navigation::LocalPlanner::Options::offset
float offset
Definition
LocalPlanner.h:51
carla::agents::navigation::VehiclePIDController::LateralArgs
Definition
agents/navigation/PIDController.h:82
carla::agents::navigation::VehiclePIDController::LongitudinalArgs
Definition
agents/navigation/PIDController.h:89
Generated by
1.18.0