CARLA
Loading...
Searching...
No Matches
ConstantVelocityAgent.cpp
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
7
9
10#include "carla/client/Actor.h"
14#include "carla/client/Sensor.h"
17#include "carla/client/World.h"
21#include "carla/geom/Vector3D.h"
23
24#include <utility>
25
26namespace carla {
27namespace agents {
28namespace navigation {
29
31 float target_speed_kmh,
32 const ConstantOptions &opts,
35 : BasicAgent(std::move(vehicle), target_speed_kmh,
36 static_cast<const Options &>(opts), std::move(map), std::move(grp)),
37 _use_basic_behavior(opts.use_basic_behavior),
38 _target_speed_ms(target_speed_kmh / 3.6f),
39 _restart_time(opts.restart_time),
40 _is_constant_velocity_active(true),
41 _constant_velocity_stop_time(0.0),
42 _has_stop_timestamp(false),
43 _collision_sensor(nullptr) {
44
47}
48
52
54 _target_speed_ms = speed_kmh / 3.6f;
55 _local_planner->SetSpeed(speed_kmh);
56}
57
60 _vehicle->DisableConstantVelocity();
62 _world->GetSnapshot().GetTimestamp().elapsed_seconds;
64}
65
70
72 _vehicle->EnableConstantVelocity(geom::Vector3D(speed_ms, 0.0f, 0.0f));
73}
74
77 const double elapsed =
78 _world->GetSnapshot().GetTimestamp().elapsed_seconds;
81 static_cast<double>(_restart_time)) {
84 } else if (_use_basic_behavior) {
85 return BasicAgent::RunStep();
86 } else {
87 return rpc::VehicleControl{};
88 }
89 }
90
91 bool hazard_detected = false;
92 float hazard_speed = 0.0f;
93
94 auto actor_list = _world->GetActors();
95 auto vehicle_list = actor_list->Filter("*vehicle*");
96 auto lights_list = actor_list->Filter("*traffic_light*");
97
98 const float vehicle_speed = _vehicle->GetVelocity().Length();
99
100 // Vehicle obstacle check.
101 const float max_vehicle_distance = _base_vehicle_threshold + vehicle_speed;
102 auto v_obs = VehicleObstacleDetected(vehicle_list, max_vehicle_distance);
103 if (v_obs.affected && v_obs.actor) {
104 const auto vehicle_velocity = _vehicle->GetVelocity();
105 const float v_len = vehicle_velocity.Length();
106 if (v_len == 0.0f) {
107 hazard_speed = 0.0f;
108 } else {
109 const auto adv_velocity = v_obs.actor->GetVelocity();
110 const float dot = vehicle_velocity.x * adv_velocity.x +
111 vehicle_velocity.y * adv_velocity.y +
112 vehicle_velocity.z * adv_velocity.z;
113 hazard_speed = dot / v_len;
114 }
115 hazard_detected = true;
116 }
117
118 // Red-light check (uses 0.3 * speed factor instead of _speed_ratio — match
119 // the Python source verbatim).
120 const float max_tlight_distance = _base_tlight_threshold + 0.3f * vehicle_speed;
121 auto tl_pair = AffectedByTrafficLight(lights_list, max_tlight_distance);
122 if (tl_pair.first) {
123 hazard_speed = 0.0f;
124 hazard_detected = true;
125 }
126
127 // Run the local planner so the wheels keep spinning even though the actual
128 // forward motion comes from EnableConstantVelocity.
129 rpc::VehicleControl control = _local_planner->RunStep();
130 if (hazard_detected) {
131 SetConstantVelocity(hazard_speed);
132 } else {
134 }
135 return control;
136}
137
139 auto blueprint_lib = _world->GetBlueprintLibrary();
140 const auto *blueprint = blueprint_lib->Find("sensor.other.collision");
141 if (!blueprint) {
142 return;
143 }
144
145 auto actor = _world->SpawnActor(*blueprint, geom::Transform{}, _vehicle.get());
146 _collision_sensor = std::dynamic_pointer_cast<client::Sensor>(actor);
147 if (!_collision_sensor) {
148 return;
149 }
150
151 // Capture `this` raw — the agent owns the sensor and tears it down in the
152 // destructor, so the callback can never fire on a destroyed agent.
153 _collision_sensor->Listen(
155 this->StopConstantVelocity();
156 });
157}
158
160 if (_collision_sensor) {
161 if (_collision_sensor->IsListening()) {
162 _collision_sensor->Stop();
163 }
164 _collision_sensor->Destroy();
165 _collision_sensor.reset();
166 }
167}
168
169} // namespace navigation
170} // namespace agents
171} // namespace carla
C++ port of agents.navigation.basic_agent.BasicAgent.
Definition BasicAgent.h:44
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.
SharedPtr< client::World > _world
Definition BasicAgent.h:150
SharedPtr< client::Vehicle > _vehicle
Definition BasicAgent.h:149
virtual rpc::VehicleControl RunStep()
std::unique_ptr< LocalPlanner > _local_planner
Definition BasicAgent.h:153
void RestartConstantVelocity()
Re-enable constant velocity at the current _target_speed_ms.
void StopConstantVelocity()
Disables constant velocity mode and stamps the stop time.
void DestroySensor()
Tear down the collision sensor (called from the destructor too).
void SetTargetSpeed(float speed_kmh)
Hides BasicAgent::SetTargetSpeed; converts km/h to m/s for the internal _target_speed_ms and forwards...
ConstantVelocityAgent(SharedPtr< client::Vehicle > vehicle, float target_speed_kmh=20.0f, const ConstantOptions &opts=DefaultConstantOptions(), SharedPtr< client::Map > map=nullptr, SharedPtr< GlobalRoutePlanner > grp=nullptr)
A rigid transform in CARLA's / Unreal's left-handed frame: x forward, y right, z up,...
This file contains definitions of common data structures used in traffic manager.
std::shared_ptr< T > SharedPtr
Definition Memory.h:15