Skip to content

Commit 4fdea38

Browse files
glopezdiestglopezdiest
andauthored
Improved large vehicles at junctions (#9384)
* Added first wide turn implementation * Smoothed offset * Removed debug elements * Added CHANGELOG * Added maximum radius --------- Co-authored-by: glopezdiest <[email protected]>
1 parent 38c6880 commit 4fdea38

File tree

10 files changed

+257
-7
lines changed

10 files changed

+257
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
## Latest Changes
22

3+
* Improved the way the TrafficManager controls large vehicles at junctions, reducing the frequency of collisions with other elements in the simulation.
34
* Added a hybrid solid-state LiDAR with adjustable parameters (blueprint attributes)
5+
46
## CARLA 0.9.16
57

68
* Added NVIDIA Cosmos Transfer1 integration

LibCarla/source/carla/trafficmanager/ALSM.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ ALSM::ALSM(
2323
const cc::World &world,
2424
const LocalMapPtr &local_map,
2525
SimulationState &simulation_state,
26+
std::unordered_map<ActorId, std::pair<float, bool>> &large_vehicles,
2627
LocalizationStage &localization_stage,
2728
CollisionStage &collision_stage,
2829
TrafficLightStage &traffic_light_stage,
@@ -36,6 +37,7 @@ ALSM::ALSM(
3637
world(world),
3738
local_map(local_map),
3839
simulation_state(simulation_state),
40+
large_vehicles(large_vehicles),
3941
localization_stage(localization_stage),
4042
collision_stage(collision_stage),
4143
traffic_light_stage(traffic_light_stage),
@@ -364,6 +366,20 @@ bool ALSM::IsVehicleStuck(const ActorId& actor_id) {
364366
return false;
365367
}
366368

369+
void ALSM::AddActor(const Actor actor){
370+
ActorId actor_id = actor->GetId();
371+
for (auto&& attribute: actor->GetAttributes()) {
372+
if (attribute.GetId() == "base_type"){
373+
std::string value = attribute.GetValue();
374+
std::transform(value.begin(), value.end(), value.begin(),[](unsigned char c){ return std::tolower(c);});
375+
if (std::find(large_vehicle_types.begin(), large_vehicle_types.end(), attribute.GetValue()) != large_vehicle_types.end()) {
376+
large_vehicles[actor_id] = std::make_pair(0.0f, 0.0f);
377+
} else {
378+
}
379+
}
380+
}
381+
}
382+
367383
void ALSM::RemoveActor(const ActorId actor_id, const bool registered_actor) {
368384
if (registered_actor) {
369385
registered_vehicles.Remove({actor_id});
@@ -374,6 +390,10 @@ void ALSM::RemoveActor(const ActorId actor_id, const bool registered_actor) {
374390
traffic_light_stage.RemoveActor(actor_id);
375391
motion_plan_stage.RemoveActor(actor_id);
376392
vehicle_light_stage.RemoveActor(actor_id);
393+
394+
if (large_vehicles.find(actor_id) != large_vehicles.end()){
395+
large_vehicles.erase(actor_id);
396+
}
377397
}
378398
else {
379399
unregistered_actors.erase(actor_id);

LibCarla/source/carla/trafficmanager/ALSM.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ class ALSM {
6565
double elapsed_last_actor_destruction {0.0};
6666
cc::Timestamp current_timestamp;
6767
std::unordered_map<ActorId, bool> has_physics_enabled;
68+
std::unordered_map<ActorId, std::pair<float, bool>> &large_vehicles;
69+
// Base types of the vehicles considered as large ones
70+
std::vector<std::string> large_vehicle_types = {"bus", "truck"};
6871

6972
// Updates the duration for which a registered vehicle is stuck at a location.
7073
void UpdateIdleTime(std::pair<ActorId, double>& max_idle_time, const ActorId& actor_id);
@@ -97,6 +100,7 @@ class ALSM {
97100
const cc::World &world,
98101
const LocalMapPtr &local_map,
99102
SimulationState &simulation_state,
103+
std::unordered_map<ActorId, std::pair<float, bool>> &large_vehicles,
100104
LocalizationStage &localization_stage,
101105
CollisionStage &collision_stage,
102106
TrafficLightStage &traffic_light_stage,
@@ -105,6 +109,9 @@ class ALSM {
105109

106110
void Update();
107111

112+
// Initialize an actor to the traffic manager
113+
void AddActor(const Actor actor);
114+
108115
// Removes an actor from traffic manager and performs clean up of associated data
109116
// from various stages tracking the said vehicle.
110117
void RemoveActor(const ActorId actor_id, const bool registered_actor);

LibCarla/source/carla/trafficmanager/Constants.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ static const float JUNCTION_LOOK_AHEAD = 5.0f;
5757
static const float SAFE_DISTANCE_AFTER_JUNCTION = 4.0f;
5858
static const float MIN_JUNCTION_LENGTH = 8.0f;
5959
static const float MIN_SAFE_INTERVAL_LENGTH = 0.5f * SAFE_DISTANCE_AFTER_JUNCTION;
60+
static const float LARGE_VEHICLES_JUNCTION_OFFSET = 1.5f;
61+
static const float LARGE_VEHICLES_JUNCTION_POINT = 0.3f;
62+
static const float LARGE_VEHICLES_JUNCTION_MAX_RADIUS = 20.0f;
6063
} // namespace WaypointSelection
6164

6265
namespace LaneChange {

LibCarla/source/carla/trafficmanager/LocalizationStage.cpp

Lines changed: 132 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ LocalizationStage::LocalizationStage(
1919
const SimulationState &simulation_state,
2020
TrackTraffic &track_traffic,
2121
const LocalMapPtr &local_map,
22+
std::unordered_map<ActorId, std::pair<float, bool>> &large_vehicles,
2223
Parameters &parameters,
2324
std::vector<ActorId>& marked_for_removal,
2425
LocalizationFrame &output_array,
@@ -28,6 +29,7 @@ LocalizationStage::LocalizationStage(
2829
simulation_state(simulation_state),
2930
track_traffic(track_traffic),
3031
local_map(local_map),
32+
large_vehicles(large_vehicles),
3133
parameters(parameters),
3234
marked_for_removal(marked_for_removal),
3335
output_array(output_array),
@@ -215,6 +217,7 @@ void LocalizationStage::Update(const unsigned long index) {
215217
}
216218
}
217219
ExtendAndFindSafeSpace(actor_id, is_at_junction_entrance, waypoint_buffer);
220+
HandleLargeVehicleJunction(actor_id, is_at_junction_entrance, waypoint_buffer);
218221

219222
// Editing output array
220223
LocalizationData &output = output_array.at(index);
@@ -239,7 +242,7 @@ void LocalizationStage::ExtendAndFindSafeSpace(const ActorId actor_id,
239242

240243
SimpleWaypointPtr junction_end_point = nullptr;
241244
SimpleWaypointPtr safe_point_after_junction = nullptr;
242-
245+
243246
if (is_at_junction_entrance
244247
&& vehicles_at_junction_entrance.find(actor_id) == vehicles_at_junction_entrance.end()) {
245248

@@ -321,6 +324,134 @@ void LocalizationStage::ExtendAndFindSafeSpace(const ActorId actor_id,
321324
}
322325
}
323326

327+
void LocalizationStage::HandleLargeVehicleJunction(const ActorId actor_id,
328+
const bool is_at_junction_entrance,
329+
Buffer &waypoint_buffer) {
330+
331+
if (large_vehicles.find(actor_id) == large_vehicles.end()){
332+
return;
333+
}
334+
335+
SimpleWaypointPtr current_waypoint = nullptr;
336+
bool is_at_junction = waypoint_buffer.front()->CheckJunction();
337+
338+
if (is_at_junction_entrance
339+
&& large_vehicles_at_junction_entrance.find(actor_id) == large_vehicles_at_junction_entrance.end()
340+
&& large_vehicles_at_junction.find(actor_id) == large_vehicles_at_junction.end()) {
341+
342+
large_vehicles_at_junction_entrance.insert(actor_id);
343+
344+
const SimpleWaypointPtr first_waypoint = waypoint_buffer.front();
345+
const SimpleWaypointPtr last_waypoint = waypoint_buffer.back();
346+
const SimpleWaypointPtr middle_waypoint = waypoint_buffer.at(static_cast<uint16_t>(waypoint_buffer.size() / 2));
347+
348+
float radius = GetThreePointCircleRadius(first_waypoint->GetLocation(),
349+
middle_waypoint->GetLocation(),
350+
last_waypoint->GetLocation());
351+
352+
std::cout << "Radius: " << radius << std::endl;
353+
if (radius > LARGE_VEHICLES_JUNCTION_MAX_RADIUS){
354+
return; // Straight path
355+
}
356+
357+
bool entered_junction = false;
358+
float junction_length = 0.0f;
359+
bool is_straight_path = true;
360+
361+
for (unsigned long i = 0u; i < waypoint_buffer.size(); ++i) {
362+
current_waypoint = waypoint_buffer.at(i);
363+
364+
if (!entered_junction && current_waypoint->CheckJunction()) {
365+
entered_junction = true;
366+
}
367+
368+
if (i > 0 && entered_junction) {
369+
SimpleWaypointPtr prev_waypoint = waypoint_buffer.at(i-1);
370+
float new_distance = current_waypoint->Distance(prev_waypoint->GetLocation());
371+
junction_length = junction_length + new_distance;
372+
373+
if (is_straight_path){
374+
RoadOption junction_type = current_waypoint->GetRoadOption();
375+
if (junction_type == RoadOption::Right){
376+
large_vehicles[actor_id].second = true;
377+
is_straight_path = false;
378+
} else if (junction_type == RoadOption::Left) {
379+
large_vehicles[actor_id].second = false;
380+
is_straight_path = false;
381+
} else {
382+
}
383+
}
384+
}
385+
386+
if (entered_junction && !current_waypoint->CheckJunction()){
387+
break;
388+
}
389+
}
390+
if (!is_straight_path){
391+
large_vehicles[actor_id].first = junction_length;
392+
}
393+
394+
}
395+
else if (is_at_junction
396+
&& large_vehicles_at_junction_entrance.find(actor_id) != large_vehicles_at_junction_entrance.end()) {
397+
398+
large_vehicles_at_junction_entrance.erase(actor_id);
399+
large_vehicles_at_junction.insert(actor_id);
400+
}
401+
else if (!is_at_junction
402+
&& large_vehicles_at_junction.find(actor_id) != large_vehicles_at_junction.end()) {
403+
404+
large_vehicles_at_junction.erase(actor_id);
405+
if (large_vehicles.find(actor_id) != large_vehicles.end()){
406+
large_vehicles[actor_id].first = 0.0f;
407+
}
408+
}
409+
}
410+
411+
float LocalizationStage::GetThreePointCircleRadius(cg::Location first_location,
412+
cg::Location middle_location,
413+
cg::Location last_location) {
414+
415+
float x1 = first_location.x;
416+
float y1 = first_location.y;
417+
float x2 = middle_location.x;
418+
float y2 = middle_location.y;
419+
float x3 = last_location.x;
420+
float y3 = last_location.y;
421+
422+
float x12 = x1 - x2;
423+
float x13 = x1 - x3;
424+
float y12 = y1 - y2;
425+
float y13 = y1 - y3;
426+
float y31 = y3 - y1;
427+
float y21 = y2 - y1;
428+
float x31 = x3 - x1;
429+
float x21 = x2 - x1;
430+
431+
float sx13 = x1 * x1 - x3 * x3;
432+
float sy13 = y1 * y1 - y3 * y3;
433+
float sx21 = x2 * x2 - x1 * x1;
434+
float sy21 = y2 * y2 - y1 * y1;
435+
436+
float f_denom = 2 * (y31 * x12 - y21 * x13);
437+
if (f_denom == 0) {
438+
return std::numeric_limits<float>::max();
439+
}
440+
float f = (sx13 * x12 + sy13 * x12 + sx21 * x13 + sy21 * x13) / f_denom;
441+
442+
float g_denom = 2 * (x31 * y12 - x21 * y13);
443+
if (g_denom == 0) {
444+
return std::numeric_limits<float>::max();
445+
}
446+
float g = (sx13 * y12 + sy13 * y12 + sx21 * y13 + sy21 * y13) / g_denom;
447+
448+
float c = - (x1 * x1 + y1 * y1) - 2 * g * x1 - 2 * f * y1;
449+
float h = -g;
450+
float k = -f;
451+
452+
return std::sqrt(h * h + k * k - c);
453+
}
454+
324455
void LocalizationStage::RemoveActor(ActorId actor_id) {
325456
last_lane_change_swpt.erase(actor_id);
326457
vehicles_at_junction.erase(actor_id);

LibCarla/source/carla/trafficmanager/LocalizationStage.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ class LocalizationStage : Stage {
4444
ActorIdSet vehicles_at_junction;
4545
using SimpleWaypointPair = std::pair<SimpleWaypointPtr, SimpleWaypointPtr>;
4646
std::unordered_map<ActorId, SimpleWaypointPair> vehicles_at_junction_entrance;
47+
std::unordered_set<ActorId> large_vehicles_at_junction_entrance;
48+
std::unordered_set<ActorId> large_vehicles_at_junction;
4749
RandomGenerator &random_device;
50+
std::unordered_map<ActorId, std::pair<float, bool>> &large_vehicles;
4851

4952
SimpleWaypointPtr AssignLaneChange(const ActorId actor_id,
5053
const cg::Location vehicle_location,
@@ -55,6 +58,14 @@ class LocalizationStage : Stage {
5558
const bool is_at_junction_entrance,
5659
Buffer &waypoint_buffer);
5760

61+
void HandleLargeVehicleJunction(const ActorId actor_id,
62+
const bool is_at_junction_entrance,
63+
Buffer &waypoint_buffer);
64+
65+
float GetThreePointCircleRadius(cg::Location first_location,
66+
cg::Location middle_location,
67+
cg::Location last_location);
68+
5869
void ImportPath(Path &imported_path,
5970
Buffer &waypoint_buffer,
6071
const ActorId actor_id,
@@ -71,6 +82,7 @@ class LocalizationStage : Stage {
7182
const SimulationState &simulation_state,
7283
TrackTraffic &track_traffic,
7384
const LocalMapPtr &local_map,
85+
std::unordered_map<ActorId, std::pair<float, bool>> &large_vehicles,
7486
Parameters &parameters,
7587
std::vector<ActorId>& marked_for_removal,
7688
LocalizationFrame &output_array,

0 commit comments

Comments
 (0)