From 48fb019be663740476af65d41bcf616528749d7e Mon Sep 17 00:00:00 2001 From: Praveen Kumar Date: Fri, 6 Mar 2020 19:25:51 +0530 Subject: [PATCH] Added parameter set_percentage_keep_right_rule --- .../carla/trafficmanager/Parameters.cpp | 20 +++++++++++++++++++ .../source/carla/trafficmanager/Parameters.h | 8 ++++++++ .../carla/trafficmanager/TrafficManager.h | 10 +++++++++- .../carla/trafficmanager/TrafficManagerBase.h | 3 +++ .../trafficmanager/TrafficManagerClient.h | 5 +++++ .../trafficmanager/TrafficManagerLocal.cpp | 4 ++++ .../trafficmanager/TrafficManagerLocal.h | 3 +++ .../trafficmanager/TrafficManagerRemote.cpp | 6 ++++++ .../trafficmanager/TrafficManagerRemote.h | 3 +++ .../trafficmanager/TrafficManagerServer.h | 7 ++++++- .../carla/source/libcarla/TrafficManager.cpp | 3 ++- 11 files changed, 69 insertions(+), 3 deletions(-) diff --git a/LibCarla/source/carla/trafficmanager/Parameters.cpp b/LibCarla/source/carla/trafficmanager/Parameters.cpp index b4e97995d..91c848043 100644 --- a/LibCarla/source/carla/trafficmanager/Parameters.cpp +++ b/LibCarla/source/carla/trafficmanager/Parameters.cpp @@ -66,6 +66,12 @@ void Parameters::SetForceLaneChange(const ActorPtr &actor, const bool direction) force_lane_change.AddEntry(entry); } +void Parameters::SetKeepRightPercentage(const ActorPtr &actor, const float percentage) { + + const auto entry = std::make_pair(actor->GetId(), percentage); + perc_keep_right.AddEntry(entry); +} + void Parameters::SetAutoLaneChange(const ActorPtr &actor, const bool enable) { const auto entry = std::make_pair(actor->GetId(), enable); @@ -137,6 +143,20 @@ ChangeLaneInfo Parameters::GetForceLaneChange(const ActorPtr &actor) { return change_lane_info; } +float Parameters::GetKeepRightPercentage(const ActorPtr &actor) { + + const ActorId actor_id = actor->GetId(); + float percentage = -1.0f; + + if (perc_keep_right.Contains(actor_id)) { + percentage = perc_keep_right.GetValue(actor_id); + } + + perc_keep_right.RemoveEntry(actor_id); + + return percentage; +} + bool Parameters::GetAutoLaneChange(const ActorPtr &actor) { const ActorId actor_id = actor->GetId(); diff --git a/LibCarla/source/carla/trafficmanager/Parameters.h b/LibCarla/source/carla/trafficmanager/Parameters.h index 6fd37b691..d2d9d8e58 100644 --- a/LibCarla/source/carla/trafficmanager/Parameters.h +++ b/LibCarla/source/carla/trafficmanager/Parameters.h @@ -54,6 +54,8 @@ namespace traffic_manager { AtomicMap perc_ignore_walkers; /// Map containing % of ignoring vehicles. AtomicMap perc_ignore_vehicles; + /// Map containing % of keep right rule. + AtomicMap perc_keep_right; /// Synchronous mode switch. std::atomic synchronous_mode; /// Distance Margin @@ -97,6 +99,9 @@ namespace traffic_manager { /// Method to query lane change command for a vehicle. ChangeLaneInfo GetForceLaneChange(const ActorPtr &actor); + /// Method to query percentage probability of keep right rule for a vehicle. + float GetKeepRightPercentage(const ActorPtr &actor); + /// Method to query auto lane change rule for a vehicle. bool GetAutoLaneChange(const ActorPtr &actor); @@ -130,6 +135,9 @@ namespace traffic_manager { /// Method to set % to ignore any vehicle. void SetPercentageIgnoreWalkers(const ActorPtr &actor, const float perc); + /// Method to set probabilistic preference to keep on the right lane. + void SetKeepRightPercentage(const ActorPtr &actor,const float percentage); + /// Method to get synchronous mode. bool GetSynchronousMode(); diff --git a/LibCarla/source/carla/trafficmanager/TrafficManager.h b/LibCarla/source/carla/trafficmanager/TrafficManager.h index 77ed89dd6..f40ee3f9a 100644 --- a/LibCarla/source/carla/trafficmanager/TrafficManager.h +++ b/LibCarla/source/carla/trafficmanager/TrafficManager.h @@ -204,7 +204,7 @@ public: return false; } - /// Method to Set Global distance to Leading vehicle + /// Method to Set Global distance to Leading vehicle void SetGlobalDistanceToLeadingVehicle(const float distance) { TrafficManagerBase* tm_ptr = GetTM(_port); if(tm_ptr != nullptr){ @@ -212,6 +212,14 @@ public: } } + /// Method to set probabilistic preference to keep on the right lane. + void SetKeepRightPercentage(const ActorPtr &actor, const float percentage) { + TrafficManagerBase* tm_ptr = GetTM(_port); + if(tm_ptr != nullptr){ + tm_ptr->SetKeepRightPercentage(actor, percentage); + } + } + private: void CreateTrafficManagerServer( diff --git a/LibCarla/source/carla/trafficmanager/TrafficManagerBase.h b/LibCarla/source/carla/trafficmanager/TrafficManagerBase.h index 4d9e85fdd..5e2968ad6 100644 --- a/LibCarla/source/carla/trafficmanager/TrafficManagerBase.h +++ b/LibCarla/source/carla/trafficmanager/TrafficManagerBase.h @@ -100,6 +100,9 @@ public: /// Method to set Global Distance to Leading Vehicle. virtual void SetGlobalDistanceToLeadingVehicle(const float dist) = 0; + /// Method to set probabilistic preference to keep on the right lane. + virtual void SetKeepRightPercentage(const ActorPtr &actor,const float percentage) = 0; + protected: }; diff --git a/LibCarla/source/carla/trafficmanager/TrafficManagerClient.h b/LibCarla/source/carla/trafficmanager/TrafficManagerClient.h index 7c2f43730..c10a1e69c 100644 --- a/LibCarla/source/carla/trafficmanager/TrafficManagerClient.h +++ b/LibCarla/source/carla/trafficmanager/TrafficManagerClient.h @@ -173,6 +173,11 @@ public: _client->call("set_global_distance_to_leading_vehicle",distance); } + /// Method to set probabilistic preference to keep on the right lane. + void SetKeepRightPercentage(const carla::rpc::Actor &actor, const float percentage) { + DEBUG_ASSERT(_client != nullptr); + _client->call("set_percentage_keep_right_rule", actor, percentage); + } private: diff --git a/LibCarla/source/carla/trafficmanager/TrafficManagerLocal.cpp b/LibCarla/source/carla/trafficmanager/TrafficManagerLocal.cpp index 21cc1ff04..d27c784f9 100644 --- a/LibCarla/source/carla/trafficmanager/TrafficManagerLocal.cpp +++ b/LibCarla/source/carla/trafficmanager/TrafficManagerLocal.cpp @@ -197,6 +197,10 @@ void TrafficManagerLocal::SetPercentageRunningSign(const ActorPtr &actor, const parameters.SetPercentageRunningSign(actor, perc); } +void TrafficManagerLocal::SetKeepRightPercentage(const ActorPtr &actor, const float percentage) { + parameters.SetKeepRightPercentage(actor, percentage); +} + bool TrafficManagerLocal::CheckAllFrozen(TLGroup tl_to_freeze) { for (auto& elem : tl_to_freeze) { if (!elem->IsFrozen() || elem->GetState() != TLS::Red) { diff --git a/LibCarla/source/carla/trafficmanager/TrafficManagerLocal.h b/LibCarla/source/carla/trafficmanager/TrafficManagerLocal.h index cfa34cf31..adc843b79 100644 --- a/LibCarla/source/carla/trafficmanager/TrafficManagerLocal.h +++ b/LibCarla/source/carla/trafficmanager/TrafficManagerLocal.h @@ -181,6 +181,9 @@ namespace traffic_manager { /// Method to specify how much distance a vehicle should maintain to /// the Global leading vehicle. void SetGlobalDistanceToLeadingVehicle(const float distance); + + /// Method to set probabilistic preference to keep on the right lane. + void SetKeepRightPercentage(const ActorPtr &actor, const float percentage); }; } // namespace traffic_manager diff --git a/LibCarla/source/carla/trafficmanager/TrafficManagerRemote.cpp b/LibCarla/source/carla/trafficmanager/TrafficManagerRemote.cpp index 44606182a..5c5702158 100644 --- a/LibCarla/source/carla/trafficmanager/TrafficManagerRemote.cpp +++ b/LibCarla/source/carla/trafficmanager/TrafficManagerRemote.cpp @@ -170,6 +170,12 @@ void TrafficManagerRemote::SetPercentageRunningSign(const ActorPtr &_actor, cons client.SetPercentageRunningSign(actor, percentage); } +void TrafficManagerRemote::SetKeepRightPercentage(const ActorPtr &_actor, const float percentage) { + carla::rpc::Actor actor(_actor->Serialize()); + + client.SetKeepRightPercentage(actor, percentage); +} + void TrafficManagerRemote::ResetAllTrafficLights() { client.ResetAllTrafficLights(); } diff --git a/LibCarla/source/carla/trafficmanager/TrafficManagerRemote.h b/LibCarla/source/carla/trafficmanager/TrafficManagerRemote.h index 3fe71b3b6..0a943143a 100644 --- a/LibCarla/source/carla/trafficmanager/TrafficManagerRemote.h +++ b/LibCarla/source/carla/trafficmanager/TrafficManagerRemote.h @@ -97,6 +97,9 @@ public: /// Method to set Tick timeout for synchronous execution. void SetSynchronousModeTimeOutInMiliSecond(double time); + /// Method to set probabilistic preference to keep on the right lane. + void SetKeepRightPercentage(const ActorPtr &actor, const float percentage); + /// Method to provide synchronous tick bool SynchronousTick(); diff --git a/LibCarla/source/carla/trafficmanager/TrafficManagerServer.h b/LibCarla/source/carla/trafficmanager/TrafficManagerServer.h index c5e55c9c2..e2604bc5a 100644 --- a/LibCarla/source/carla/trafficmanager/TrafficManagerServer.h +++ b/LibCarla/source/carla/trafficmanager/TrafficManagerServer.h @@ -120,7 +120,7 @@ public: }); /// Method to the Global Distance to Leading vehicle - + server->bind("set_global_distance_to_leading_vehicle", [=]( const float distance) { tm->SetGlobalDistanceToLeadingVehicle(distance); }); @@ -145,6 +145,11 @@ public: tm->SetPercentageIgnoreVehicles(carla::client::detail::ActorVariant(actor).Get(tm->GetEpisodeProxy()), percentage); }); + /// Method to specify the % chance of ignoring collisions with any vehicle. + server->bind("set_percentage_keep_right_rule", [=](carla::rpc::Actor actor, const float percentage) { + tm->SetKeepRightPercentage(carla::client::detail::ActorVariant(actor).Get(tm->GetEpisodeProxy()), percentage); + }); + /// Method to set synchronous mode. server->bind("set_synchronous_mode", [=](const bool mode) { tm->SetSynchronousMode(mode); diff --git a/PythonAPI/carla/source/libcarla/TrafficManager.cpp b/PythonAPI/carla/source/libcarla/TrafficManager.cpp index 9fcb686dd..a0b54a840 100644 --- a/PythonAPI/carla/source/libcarla/TrafficManager.cpp +++ b/PythonAPI/carla/source/libcarla/TrafficManager.cpp @@ -28,5 +28,6 @@ void export_trafficmanager() { .def("ignore_vehicles_percentage", &carla::traffic_manager::TrafficManager::SetPercentageIgnoreVehicles) .def("ignore_lights_percentage", &carla::traffic_manager::TrafficManager::SetPercentageRunningLight) .def("ignore_signs_percentage", &carla::traffic_manager::TrafficManager::SetPercentageRunningSign) - .def("set_global_distance_to_leading_vehicle", &carla::traffic_manager::TrafficManager::SetGlobalDistanceToLeadingVehicle); + .def("set_global_distance_to_leading_vehicle", &carla::traffic_manager::TrafficManager::SetGlobalDistanceToLeadingVehicle) + .def("set_percentage_keep_right_rule", &carla::traffic_manager::TrafficManager::SetKeepRightPercentage); }