CARLA
Loading...
Searching...
No Matches
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"
12
13#include <deque>
14#include <memory>
15#include <random>
16#include <utility>
17#include <vector>
18
19namespace carla {
20
21namespace client {
22 class Map;
23 class Vehicle;
24 class Waypoint;
25} // namespace client
26
27namespace agents {
28namespace 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.
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.
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
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`).
104
105 /// Currently active target RoadOption (mirrors `target_road_option`).
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
119
120 std::unique_ptr<VehiclePIDController> _vehicle_controller;
121
124
125 std::deque<PlanItem> _waypoints_queue;
126 std::size_t _max_queue_size = 10000;
127 std::size_t _min_queue_length = 100;
129
130 // Tunables (mirror the Python instance fields).
131 float _dt;
139 float _offset;
143
144 // Random branching. Seeded once per planner.
145 mutable std::mt19937 _rng;
146 };
147
151
152} // namespace navigation
153} // namespace agents
154} // namespace carla
LocalPlanner reproduces agents.navigation.local_planner.LocalPlanner.
void ComputeNextWaypoints(std::size_t k)
Append up to k waypoints to the back of the queue, branching with random choice on intersections (mir...
VehiclePIDController::LongitudinalArgs _longitudinal_args
std::deque< PlanItem > _waypoints_queue
SharedPtr< client::Vehicle > _vehicle
SharedPtr< client::Waypoint > _target_waypoint
rpc::VehicleControl RunStep(bool debug=false)
Drive one tick. Returns the VehicleControl to apply.
RoadOption TargetRoadOption() const
Currently active target RoadOption (mirrors target_road_option).
void InitController()
Build the VehiclePIDController and seed the queue with the vehicle's current waypoint.
void FollowSpeedLimits(bool value=true)
Toggle automatic following of road speed limits.
SharedPtr< client::Waypoint > TargetWaypoint() const
Currently active target waypoint (mirrors target_waypoint).
void SetSpeed(float speed_kmh)
Override the target speed in km/h.
static Options DefaultOptions()
Construct a LocalPlanner.
VehiclePIDController::LateralArgs _lateral_args
bool Done() const
True when the queue is empty.
void SetGlobalPlan(const std::vector< PlanItem > &plan, bool stop_waypoint_creation=true, bool clean_queue=true)
Replace (or append to) the waypoint queue.
const std::deque< PlanItem > & GetPlan() const
Read-only view of the queued plan.
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.
std::unique_ptr< VehiclePIDController > _vehicle_controller
void SetOffset(float offset)
Apply a lateral offset to the underlying PID controller.
void ResetVehicle()
Drop the vehicle reference (mirrors reset_vehicle).
std::pair< SharedPtr< client::Waypoint >, RoadOption > PlanItem
RoadOption
Topological direction emitted by the planners.
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.
VehiclePIDController::LongitudinalArgs longitudinal
VehiclePIDController::LateralArgs lateral