26constexpr float kKmhToMs = 1.0f / 3.6f;
31 SharedPtr<client::Waypoint> current,
32 SharedPtr<client::Waypoint> next_wp,
33 float threshold = 35.0f) {
36 auto wrap = [](
float v,
float mod) {
37 float r = std::fmod(v, mod);
38 if (r < 0.0f) r += mod;
42 const float n = wrap(next_wp->GetTransform().rotation.yaw, 360.0f);
43 const float c = wrap(current->GetTransform().rotation.yaw, 360.0f);
44 const float diff = wrap(n - c, 180.0f);
46 if (diff < threshold || diff > (180.0f - threshold)) {
56std::vector<RoadOption> RetrieveOptions(
57 const std::vector<SharedPtr<client::Waypoint>> &candidates,
58 SharedPtr<client::Waypoint> current) {
59 std::vector<RoadOption> options;
60 options.reserve(candidates.size());
61 for (
const auto &next_wp : candidates) {
64 auto next_next = next_wp->GetNext(3.0);
65 if (next_next.empty()) {
66 options.push_back(ComputeConnection(current, next_wp));
68 options.push_back(ComputeConnection(current, next_next.front()));
84 : _vehicle(
std::move(vehicle)),
87 _target_speed(opt.target_speed),
88 _sampling_radius(opt.sampling_radius),
89 _lateral_args(opt.lateral),
90 _longitudinal_args(opt.longitudinal),
91 _max_throttle(opt.max_throttle),
92 _max_brake(opt.max_brake),
93 _max_steering(opt.max_steering),
95 _base_min_distance(opt.base_min_distance),
96 _distance_ratio(opt.distance_ratio),
97 _follow_speed_limits(opt.follow_speed_limits),
98 _rng(
std::random_device{}()) {
118 auto current_waypoint =
_map->GetWaypoint(
_vehicle->GetLocation());
131 std::cout <<
"WARNING: The max speed is currently set to follow the speed"
132 " limits. Use 'follow_speed_limits' to deactivate this\n";
147 k = std::min(k, available);
149 for (std::size_t i = 0; i < k; ++i) {
152 if (!last_waypoint)
break;
155 if (next_waypoints.empty()) {
161 if (next_waypoints.size() == 1) {
162 next_waypoint = next_waypoints.front();
166 auto road_options_list = RetrieveOptions(next_waypoints, last_waypoint);
167 std::uniform_int_distribution<std::size_t> dist(
168 0, road_options_list.size() - 1);
169 const std::size_t idx = dist(
_rng);
170 road_option = road_options_list[idx];
176 next_waypoint = next_waypoints[idx];
184 const std::vector<PlanItem> &plan,
185 bool stop_waypoint_creation,
193 const std::size_t new_plan_length =
199 for (
const auto &elem : plan) {
224 const auto veh_location =
_vehicle->GetLocation();
226 const float min_distance =
229 std::size_t num_removed = 0;
233 const float threshold =
236 const auto &wp_loc = item.first->GetTransform().location;
237 if (veh_location.Distance(wp_loc) < threshold) {
243 for (std::size_t i = 0; i < num_removed; ++i) {
250 control.
steer = 0.0f;
252 control.
brake = 1.0f;
262 auto world = std::make_shared<client::World>(
_vehicle->GetWorld());
269std::pair<SharedPtr<client::Waypoint>,
RoadOption>
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
std::size_t _min_queue_length
SharedPtr< client::Waypoint > _target_waypoint
SharedPtr< client::Map > _map
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).
bool _follow_speed_limits
bool _stop_waypoint_creation
std::size_t _max_queue_size
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.
VehiclePIDController::LateralArgs _lateral_args
bool Done() const
True when the queue is empty.
LocalPlanner(SharedPtr< client::Vehicle > vehicle, const Options &opt=DefaultOptions(), SharedPtr< client::Map > map=nullptr)
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.
RoadOption _target_road_option
void ResetVehicle()
Drop the vehicle reference (mirrors reset_vehicle).
RoadOption
Topological direction emitted by the planners.
void DrawWaypoints(SharedPtr< client::World > world, const std::vector< SharedPtr< client::Waypoint > > &waypoints, float z)
Draw the supplied waypoints in the world for a fixed lifetime.
float GetSpeed(SharedPtr< client::Actor > actor)
Compute speed of an actor in km/h.
This file contains definitions of common data structures used in traffic manager.
std::shared_ptr< T > SharedPtr
Aggregated configuration. Mirrors the opt_dict in local_planner.py.