CARLA
Toggle main menu visibility
Loading...
Searching...
No Matches
LibCarla
source
carla
agents
navigation
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
6
#include "
carla/agents/navigation/ConstantVelocityAgent.h
"
7
8
#include "
carla/agents/navigation/LocalPlanner.h
"
9
10
#include "
carla/client/Actor.h
"
11
#include "
carla/client/ActorBlueprint.h
"
12
#include "
carla/client/ActorList.h
"
13
#include "
carla/client/BlueprintLibrary.h
"
14
#include "
carla/client/Sensor.h
"
15
#include "
carla/client/TrafficLight.h
"
16
#include "
carla/client/Vehicle.h
"
17
#include "
carla/client/World.h
"
18
#include "
carla/client/WorldSnapshot.h
"
19
#include "
carla/client/Timestamp.h
"
20
#include "
carla/geom/Transform.h
"
21
#include "
carla/geom/Vector3D.h
"
22
#include "
carla/rpc/VehicleControl.h
"
23
24
#include <utility>
25
26
namespace
carla
{
27
namespace
agents
{
28
namespace
navigation
{
29
30
ConstantVelocityAgent::ConstantVelocityAgent
(
SharedPtr<client::Vehicle>
vehicle,
31
float
target_speed_kmh,
32
const
ConstantOptions
&opts,
33
SharedPtr<client::Map>
map,
34
SharedPtr<GlobalRoutePlanner>
grp)
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
45
SetCollisionSensor
();
46
SetConstantVelocity
(
_target_speed_ms
);
47
}
48
49
ConstantVelocityAgent::~ConstantVelocityAgent
() {
50
DestroySensor
();
51
}
52
53
void
ConstantVelocityAgent::SetTargetSpeed
(
float
speed_kmh) {
54
_target_speed_ms
= speed_kmh / 3.6f;
55
_local_planner
->SetSpeed(speed_kmh);
56
}
57
58
void
ConstantVelocityAgent::StopConstantVelocity
() {
59
_is_constant_velocity_active
=
false
;
60
_vehicle
->DisableConstantVelocity();
61
_constant_velocity_stop_time
=
62
_world
->GetSnapshot().GetTimestamp().elapsed_seconds;
63
_has_stop_timestamp
=
true
;
64
}
65
66
void
ConstantVelocityAgent::RestartConstantVelocity
() {
67
_is_constant_velocity_active
=
true
;
68
SetConstantVelocity
(
_target_speed_ms
);
69
}
70
71
void
ConstantVelocityAgent::SetConstantVelocity
(
float
speed_ms) {
72
_vehicle
->EnableConstantVelocity(
geom::Vector3D
(speed_ms, 0.0f, 0.0f));
73
}
74
75
rpc::VehicleControl
ConstantVelocityAgent::RunStep
() {
76
if
(!
_is_constant_velocity_active
) {
77
const
double
elapsed =
78
_world
->GetSnapshot().GetTimestamp().elapsed_seconds;
79
if
(
_has_stop_timestamp
&&
80
(elapsed -
_constant_velocity_stop_time
) >
81
static_cast<
double
>
(
_restart_time
)) {
82
RestartConstantVelocity
();
83
_is_constant_velocity_active
=
true
;
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
{
133
SetConstantVelocity
(
_target_speed_ms
);
134
}
135
return
control;
136
}
137
138
void
ConstantVelocityAgent::SetCollisionSensor
() {
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(
154
[
this
](
SharedPtr<sensor::SensorData>
) {
155
this->
StopConstantVelocity
();
156
});
157
}
158
159
void
ConstantVelocityAgent::DestroySensor
() {
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
ActorBlueprint.h
ActorList.h
BlueprintLibrary.h
ConstantVelocityAgent.h
Sensor.h
VehicleControl.h
LocalPlanner.h
Timestamp.h
TrafficLight.h
Vehicle.h
WorldSnapshot.h
World.h
carla::agents::navigation::BasicAgent::AffectedByTrafficLight
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.
Definition
BasicAgent.cpp:242
carla::agents::navigation::BasicAgent::VehicleObstacleDetected
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.
Definition
BasicAgent.cpp:331
carla::agents::navigation::BasicAgent::_world
SharedPtr< client::World > _world
Definition
BasicAgent.h:150
carla::agents::navigation::BasicAgent::BasicAgent
BasicAgent(SharedPtr< client::Vehicle > vehicle, float target_speed_kmh=20.0f, const Options &options=DefaultOptions(), SharedPtr< client::Map > map=nullptr, SharedPtr< GlobalRoutePlanner > grp=nullptr)
Definition
BasicAgent.cpp:63
carla::agents::navigation::BasicAgent::_base_vehicle_threshold
float _base_vehicle_threshold
Definition
BasicAgent.h:165
carla::agents::navigation::BasicAgent::_vehicle
SharedPtr< client::Vehicle > _vehicle
Definition
BasicAgent.h:149
carla::agents::navigation::BasicAgent::RunStep
virtual rpc::VehicleControl RunStep()
Definition
BasicAgent.cpp:175
carla::agents::navigation::BasicAgent::_base_tlight_threshold
float _base_tlight_threshold
Definition
BasicAgent.h:164
carla::agents::navigation::BasicAgent::_local_planner
std::unique_ptr< LocalPlanner > _local_planner
Definition
BasicAgent.h:153
carla::agents::navigation::ConstantVelocityAgent::SetCollisionSensor
void SetCollisionSensor()
Definition
ConstantVelocityAgent.cpp:138
carla::agents::navigation::ConstantVelocityAgent::_restart_time
float _restart_time
Definition
ConstantVelocityAgent.h:75
carla::agents::navigation::ConstantVelocityAgent::_use_basic_behavior
bool _use_basic_behavior
Definition
ConstantVelocityAgent.h:73
carla::agents::navigation::ConstantVelocityAgent::RestartConstantVelocity
void RestartConstantVelocity()
Re-enable constant velocity at the current _target_speed_ms.
Definition
ConstantVelocityAgent.cpp:66
carla::agents::navigation::ConstantVelocityAgent::StopConstantVelocity
void StopConstantVelocity()
Disables constant velocity mode and stamps the stop time.
Definition
ConstantVelocityAgent.cpp:58
carla::agents::navigation::ConstantVelocityAgent::DestroySensor
void DestroySensor()
Tear down the collision sensor (called from the destructor too).
Definition
ConstantVelocityAgent.cpp:159
carla::agents::navigation::ConstantVelocityAgent::SetTargetSpeed
void SetTargetSpeed(float speed_kmh)
Hides BasicAgent::SetTargetSpeed; converts km/h to m/s for the internal _target_speed_ms and forwards...
Definition
ConstantVelocityAgent.cpp:53
carla::agents::navigation::ConstantVelocityAgent::_target_speed_ms
float _target_speed_ms
Definition
ConstantVelocityAgent.h:74
carla::agents::navigation::ConstantVelocityAgent::_is_constant_velocity_active
bool _is_constant_velocity_active
Definition
ConstantVelocityAgent.h:76
carla::agents::navigation::ConstantVelocityAgent::_constant_velocity_stop_time
double _constant_velocity_stop_time
Definition
ConstantVelocityAgent.h:77
carla::agents::navigation::ConstantVelocityAgent::RunStep
rpc::VehicleControl RunStep() override
Definition
ConstantVelocityAgent.cpp:75
carla::agents::navigation::ConstantVelocityAgent::_has_stop_timestamp
bool _has_stop_timestamp
Definition
ConstantVelocityAgent.h:78
carla::agents::navigation::ConstantVelocityAgent::_collision_sensor
SharedPtr< client::Sensor > _collision_sensor
Definition
ConstantVelocityAgent.h:80
carla::agents::navigation::ConstantVelocityAgent::SetConstantVelocity
void SetConstantVelocity(float speed_ms)
Definition
ConstantVelocityAgent.cpp:71
carla::agents::navigation::ConstantVelocityAgent::~ConstantVelocityAgent
~ConstantVelocityAgent() override
Definition
ConstantVelocityAgent.cpp:49
carla::agents::navigation::ConstantVelocityAgent::ConstantVelocityAgent
ConstantVelocityAgent(SharedPtr< client::Vehicle > vehicle, float target_speed_kmh=20.0f, const ConstantOptions &opts=DefaultConstantOptions(), SharedPtr< client::Map > map=nullptr, SharedPtr< GlobalRoutePlanner > grp=nullptr)
Definition
ConstantVelocityAgent.cpp:30
carla::geom::Transform
A rigid transform in CARLA's / Unreal's left-handed frame: x forward, y right, z up,...
Definition
geom/Transform.h:32
carla::rpc::VehicleControl
Definition
LibCarla/source/carla/rpc/VehicleControl.h:20
Actor.h
Transform.h
Vector3D.h
carla::agents::navigation
Definition
BasicAgent.cpp:40
carla::agents
Definition
BasicAgent.cpp:39
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
std
Definition
CarlaRecorderCollision.h:31
carla::agents::navigation::BasicAgent::Options
Tunable knobs.
Definition
BasicAgent.h:49
carla::agents::navigation::ConstantVelocityAgent::ConstantOptions
Definition
ConstantVelocityAgent.h:35
carla::geom::Vector3D
Definition
geom/Vector3D.h:17
Generated by
1.18.0