CARLA
Loading...
Searching...
No Matches
TrafficManagerRemote.cpp
Go to the documentation of this file.
1// Copyright (c) 2026 Computer Vision Center (CVC) at the Universitat Autonoma
2// de Barcelona (UAB).
3//
4// This work is licensed under the terms of the MIT license.
5// For a copy, see <https://opensource.org/licenses/MIT>.
6
7#include <thread>
8
10
12
13namespace carla {
14namespace traffic_manager {
15
17 const std::pair<std::string, uint16_t> &_serverTM,
19 : client(_serverTM.first, _serverTM.second),
20 episodeProxyTM(episodeProxy) {
21
22 Start();
23
24}
25
26/// Destructor.
30
32 _keep_alive = true;
33
34 std::thread _thread = std::thread([this] () {
35 std::chrono::milliseconds wait_time(TM_TIMEOUT);
36 try {
37 do {
38 std::this_thread::sleep_for(wait_time);
39
41
42 /// Until connection active
43 } while (_keep_alive);
44 } catch (...) {
45
46 std::string rhost("");
47 uint16_t rport = 0;
48
49 client.getServerDetails(rhost, rport);
50
51 std::string strtmserver(rhost + ":" + std::to_string(rport));
52
53 /// Create error msg
54 std::string errmsg(
55 "Trying to connect rpc server of traffic manager; "
56 "but the system failed to connect at " + strtmserver);
57
58 /// TSet the error message
59 if(_keep_alive) {
60 this->episodeProxyTM.Lock()->AddPendingException(errmsg);
61 }
62 }
63 _keep_alive = false;
64 _cv.notify_one();
65 });
66
67 _thread.detach();
68}
69
71 if(_keep_alive) {
72 _keep_alive = false;
73 std::unique_lock<std::mutex> lock(_mutex);
74 std::chrono::milliseconds wait_time(TM_TIMEOUT + 1000);
75 _cv.wait_for(lock, wait_time);
76 }
77}
78
82
83void TrafficManagerRemote::Reset(uint64_t episode_id) {
84 // Every client alive in the process reports the change; reset once.
85 if (episodeProxyTM.GetId() == episode_id) {
86 return;
87 }
88 Stop();
89
90 carla::client::detail::EpisodeProxy episode_proxy = episodeProxyTM.Lock()->GetCurrentEpisode();
91 episodeProxyTM = episode_proxy;
92
93 Start();
94}
95
96void TrafficManagerRemote::RegisterVehicles(const std::vector<ActorPtr> &_actor_list) {
97 std::vector<carla::rpc::Actor> actor_list;
98 for (auto &&actor : _actor_list) {
99 actor_list.emplace_back(actor->Serialize());
100 }
101 client.RegisterVehicle(actor_list);
102}
103
104void TrafficManagerRemote::UnregisterVehicles(const std::vector<ActorPtr> &_actor_list) {
105 std::vector<carla::rpc::Actor> actor_list;
106 for (auto &&actor : _actor_list) {
107 actor_list.emplace_back(actor->Serialize());
108 }
109 client.UnregisterVehicle(actor_list);
110}
111
112void TrafficManagerRemote::SetPercentageSpeedDifference(const ActorPtr &_actor, const float percentage) {
113 carla::rpc::Actor actor(_actor->Serialize());
114
115 client.SetPercentageSpeedDifference(actor, percentage);
116}
117
118void TrafficManagerRemote::SetDesiredSpeed(const ActorPtr &_actor, const float value) {
119 carla::rpc::Actor actor(_actor->Serialize());
120
121 client.SetDesiredSpeed(actor, value);
122}
123
127
128void TrafficManagerRemote::SetLaneOffset(const ActorPtr &_actor, const float offset) {
129 carla::rpc::Actor actor(_actor->Serialize());
130
131 client.SetLaneOffset(actor, offset);
132}
133
136}
137
138void TrafficManagerRemote::SetLargeVehicleWideTurn(const ActorPtr &_actor, const bool enable) {
139 carla::rpc::Actor actor(_actor->Serialize());
140
141 client.SetLargeVehicleWideTurn(actor, enable);
142}
143
147
148void TrafficManagerRemote::SetUpdateVehicleLights(const ActorPtr &_actor, const bool do_update) {
149 carla::rpc::Actor actor(_actor->Serialize());
150
151 client.SetUpdateVehicleLights(actor, do_update);
152}
153
154void TrafficManagerRemote::SetCollisionDetection(const ActorPtr &_reference_actor, const ActorPtr &_other_actor, const bool detect_collision) {
155 carla::rpc::Actor reference_actor(_reference_actor->Serialize());
156 carla::rpc::Actor other_actor(_other_actor->Serialize());
157
158 client.SetCollisionDetection(reference_actor, other_actor, detect_collision);
159}
160
161void TrafficManagerRemote::SetForceLaneChange(const ActorPtr &_actor, const bool direction) {
162 carla::rpc::Actor actor(_actor->Serialize());
163
164 client.SetForceLaneChange(actor, direction);
165}
166
167void TrafficManagerRemote::SetAutoLaneChange(const ActorPtr &_actor, const bool enable) {
168 carla::rpc::Actor actor(_actor->Serialize());
169
170 client.SetAutoLaneChange(actor, enable);
171}
172
173void TrafficManagerRemote::SetDistanceToLeadingVehicle(const ActorPtr &_actor, const float distance) {
174 carla::rpc::Actor actor(_actor->Serialize());
175
176 client.SetDistanceToLeadingVehicle(actor, distance);
177}
178
182
183
184void TrafficManagerRemote::SetPercentageIgnoreWalkers(const ActorPtr &_actor, const float percentage) {
185 carla::rpc::Actor actor(_actor->Serialize());
186
187 client.SetPercentageIgnoreWalkers(actor, percentage);
188}
189
190void TrafficManagerRemote::SetPercentageIgnoreVehicles(const ActorPtr &_actor, const float percentage) {
191 carla::rpc::Actor actor(_actor->Serialize());
192
193 client.SetPercentageIgnoreVehicles(actor, percentage);
194}
195
196void TrafficManagerRemote::SetPercentageRunningLight(const ActorPtr &_actor, const float percentage) {
197 carla::rpc::Actor actor(_actor->Serialize());
198
199 client.SetPercentageRunningLight(actor, percentage);
200}
201
202void TrafficManagerRemote::SetPercentageRunningSign(const ActorPtr &_actor, const float percentage) {
203 carla::rpc::Actor actor(_actor->Serialize());
204
205 client.SetPercentageRunningSign(actor, percentage);
206}
207
208void TrafficManagerRemote::SetKeepSlowLanePercentage(const ActorPtr &_actor, const float percentage) {
209 carla::rpc::Actor actor(_actor->Serialize());
210
211 client.SetKeepSlowLanePercentage(actor, percentage);
212}
213
214void TrafficManagerRemote::SetRandomLeftLaneChangePercentage(const ActorPtr &_actor, const float percentage) {
215 carla::rpc::Actor actor(_actor->Serialize());
216
217 client.SetRandomLeftLaneChangePercentage(actor, percentage);
218}
219
220void TrafficManagerRemote::SetRandomRightLaneChangePercentage(const ActorPtr &_actor, const float percentage) {
221 carla::rpc::Actor actor(_actor->Serialize());
222
224}
225
226void TrafficManagerRemote::SetHybridPhysicsMode(const bool mode_switch) {
227 client.SetHybridPhysicsMode(mode_switch);
228}
229
233
234void TrafficManagerRemote::SetOSMMode(const bool mode_switch) {
235 client.SetOSMMode(mode_switch);
236}
237
238void TrafficManagerRemote::SetCustomPath(const ActorPtr &_actor, const Path path, const bool empty_buffer) {
239 carla::rpc::Actor actor(_actor->Serialize());
240
241 client.SetCustomPath(actor, path, empty_buffer);
242}
243
244void TrafficManagerRemote::RemoveUploadPath(const ActorId &actor_id, const bool remove_path) {
245 client.RemoveUploadPath(actor_id, remove_path);
246}
247
248void TrafficManagerRemote::UpdateUploadPath(const ActorId &actor_id, const Path path) {
249 client.UpdateUploadPath(actor_id, path);
250}
251
252void TrafficManagerRemote::SetImportedRoute(const ActorPtr &_actor, const Route route, const bool empty_buffer) {
253 carla::rpc::Actor actor(_actor->Serialize());
254
255 client.SetImportedRoute(actor, route, empty_buffer);
256}
257
258void TrafficManagerRemote::RemoveImportedRoute(const ActorId &actor_id, const bool remove_path) {
259 client.RemoveImportedRoute(actor_id, remove_path);
260}
261
262void TrafficManagerRemote::UpdateImportedRoute(const ActorId &actor_id, const Route route) {
263 client.UpdateImportedRoute(actor_id, route);
264}
265
269
273
277
279 return client.GetNextAction(actor_id);
280}
281
285
287 return false;
288}
289
293
297
300}
301
302} // namespace traffic_manager
303} // namespace carla
SharedPtrType Lock() const
Same as TryLock but never return nullptr.
void SetGlobalLaneOffset(const float offset)
Method to set a global lane offset displacement from the center line.
Action GetNextAction(const ActorId &actor_id)
Method to get the vehicle's next action.
void HealthCheckRemoteTM()
Check if remote traffic manager is alive.
void SetPercentageIgnoreWalkers(const carla::rpc::Actor &actor, const float percentage)
Method to specify the % chance of ignoring collisions with any walker.
void SetDesiredSpeed(const carla::rpc::Actor &_actor, const float value)
Set a vehicle's exact desired velocity.
void SetGlobalPercentageSpeedDifference(const float percentage)
Method to set a global % decrease in velocity with respect to the speed limit.
void SetGlobalLargeVehicleWideTurn(const bool enable)
Method to enable/disable the wide-turn manoeuvre globally for large vehicles.
void SetRandomDeviceSeed(const uint64_t seed)
Method to set randomization seed.
void SetSynchronousMode(const bool mode)
Method to switch traffic manager into synchronous execution.
ActionBuffer GetActionBuffer(const ActorId &actor_id)
Method to get the vehicle's action buffer.
void RemoveUploadPath(const ActorId &actor_id, const bool remove_path)
Method to remove a list of points.
void SetPercentageRunningLight(const carla::rpc::Actor &actor, const float percentage)
Method to specify the % chance of running a traffic sign.
void RegisterVehicle(const std::vector< carla::rpc::Actor > &actor_list)
Register vehicles to remote traffic manager server via RPC client.
void UnregisterVehicle(const std::vector< carla::rpc::Actor > &actor_list)
Unregister vehicles to remote traffic manager server via RPC client.
void SetOSMMode(const bool mode_switch)
Method to set Open Street Map mode.
void getServerDetails(std::string &_host, uint16_t &_port)
Get parameters.
void SetPercentageSpeedDifference(const carla::rpc::Actor &_actor, const float percentage)
Method to set a vehicle's % decrease in velocity with respect to the speed limit.
void SetCustomPath(const carla::rpc::Actor &actor, const Path path, const bool empty_buffer)
Method to set our own imported path.
void UpdateImportedRoute(const ActorId &actor_id, const Route route)
Method to update an already set list of points.
void SetUpdateVehicleLights(const carla::rpc::Actor &_actor, const bool do_update)
Method to set the automatic management of the vehicle lights.
void SetRandomRightLaneChangePercentage(const carla::rpc::Actor &actor, const float percentage)
Method to set % to randomly do a right lane change.
void SetLargeVehicleWideTurn(const carla::rpc::Actor &_actor, const bool enable)
Method to enable/disable the wide-turn manoeuvre for a single large vehicle.
void SetPercentageIgnoreVehicles(const carla::rpc::Actor &actor, const float percentage)
Method to specify the % chance of ignoring collisions with any vehicle.
void SetForceLaneChange(const carla::rpc::Actor &actor, const bool direction)
Method to force lane change on a vehicle.
void SetDistanceToLeadingVehicle(const carla::rpc::Actor &actor, const float distance)
Method to specify how much distance a vehicle should maintain to the leading vehicle.
void SetLaneOffset(const carla::rpc::Actor &_actor, const float offset)
Method to set a lane offset displacement from the center line.
void SetSynchronousModeTimeOutInMiliSecond(const double time)
Method to set tick timeout for synchronous execution.
void SetAutoLaneChange(const carla::rpc::Actor &actor, const bool enable)
Enable/disable automatic lane change on a vehicle.
void SetKeepSlowLanePercentage(const carla::rpc::Actor &actor, const float percentage)
Method to set % to keep on the slow lane.
void SetPercentageRunningSign(const carla::rpc::Actor &actor, const float percentage)
Method to specify the % chance of running any traffic sign.
void SetRandomLeftLaneChangePercentage(const carla::rpc::Actor &actor, const float percentage)
Method to set % to randomly do a left lane change.
void SetImportedRoute(const carla::rpc::Actor &actor, const Route route, const bool empty_buffer)
Method to set our own imported route.
void SetHybridPhysicsRadius(const float radius)
Method to set hybrid physics mode.
void SetGlobalDistanceToLeadingVehicle(const float distance)
Method to specify how much distance a vehicle should maintain to the Global leading vehicle.
void UpdateUploadPath(const ActorId &actor_id, const Path path)
Method to update an already set list of points.
void SetHybridPhysicsMode(const bool mode_switch)
Method to set hybrid physics mode.
void SetCollisionDetection(const carla::rpc::Actor &reference_actor, const carla::rpc::Actor &other_actor, const bool detect_collision)
Method to set collision detection rules between vehicles.
void RemoveImportedRoute(const ActorId &actor_id, const bool remove_path)
Method to remove a route.
void SetPercentageIgnoreVehicles(const ActorPtr &actor, const float perc)
Method to specify the % chance of ignoring collisions with any vehicle.
carla::client::detail::EpisodeProxy & GetEpisodeProxy()
Get CARLA episode information.
void SetUpdateVehicleLights(const ActorPtr &actor, const bool do_update)
Method to set the automatic management of the vehicle lights.
void SetGlobalPercentageSpeedDifference(float const percentage)
Method to set a global % decrease in velocity with respect to the speed limit.
void RemoveUploadPath(const ActorId &actor_id, const bool remove_path)
Method to remove a path.
carla::client::detail::EpisodeProxy episodeProxyTM
CARLA client connection object.
void SetCollisionDetection(const ActorPtr &reference_actor, const ActorPtr &other_actor, const bool detect_collision)
Method to set collision detection rules between vehicles.
bool SynchronousTick()
Method to provide synchronous tick.
void UpdateUploadPath(const ActorId &actor_id, const Path path)
Method to update an already set path.
void SetGlobalLaneOffset(float const offset)
Method to set a global lane offset displacement from the center line.
void UnregisterVehicles(const std::vector< ActorPtr > &actor_list)
This method unregisters a vehicle from traffic manager.
void SetLargeVehicleWideTurn(const ActorPtr &actor, const bool enable)
Method to enable/disable the wide-turn manoeuvre for a single large vehicle.
void SetHybridPhysicsMode(const bool mode_switch)
Method to set hybrid physics mode.
void RemoveImportedRoute(const ActorId &actor_id, const bool remove_path)
Method to remove a route.
void RegisterVehicles(const std::vector< ActorPtr > &actor_list)
This method registers a vehicle with the traffic manager.
void SetCustomPath(const ActorPtr &actor, const Path path, const bool empty_buffer)
Method to set our own imported path.
void SetDesiredSpeed(const ActorPtr &actor, const float value)
Set a vehicle's exact desired velocity.
void SetLaneOffset(const ActorPtr &actor, const float offset)
Method to set a lane offset displacement from the center line.
void SetOSMMode(const bool mode_switch)
Method to set Open Street Map mode.
void SetRandomDeviceSeed(const uint64_t seed)
Method to set randomization seed.
void UpdateImportedRoute(const ActorId &actor_id, const Route route)
Method to update an already set route.
void SetGlobalDistanceToLeadingVehicle(const float distance)
Method to specify Global Distance.
void SetRandomRightLaneChangePercentage(const ActorPtr &actor, const float percentage)
Method to set % to randomly do a right lane change.
void SetGlobalLargeVehicleWideTurn(const bool enable)
Method to enable/disable the wide-turn manoeuvre globally for large vehicles.
TrafficManagerClient client
Remote client using the IP and port information it connects to as remote RPC traffic manager server.
void SetAutoLaneChange(const ActorPtr &actor, const bool enable)
Enable/disable automatic lane change on a vehicle.
void Reset(uint64_t episode_id)
To reset the traffic manager for the episode episode_id.
void SetImportedRoute(const ActorPtr &actor, const Route route, const bool empty_buffer)
Method to set our own imported route.
void SetPercentageIgnoreWalkers(const ActorPtr &actor, const float perc)
Method to specify the % chance of ignoring collisions with any walker.
Action GetNextAction(const ActorId &actor_id)
Method to get the vehicle's next action.
void SetPercentageRunningLight(const ActorPtr &actor, const float perc)
Method to specify the % chance of running any traffic light.
void SetSynchronousModeTimeOutInMiliSecond(double time)
Method to set Tick timeout for synchronous execution.
ActionBuffer GetActionBuffer(const ActorId &actor_id)
Method to get the vehicle's action buffer.
void SetRandomLeftLaneChangePercentage(const ActorPtr &actor, const float percentage)
Method to set % to randomly do a left lane change.
void SetPercentageSpeedDifference(const ActorPtr &actor, const float percentage)
Method to set a vehicle's % decrease in velocity with respect to the speed limit.
TrafficManagerRemote(const std::pair< std::string, uint16_t > &_serverTM, carla::client::detail::EpisodeProxy &episodeProxy)
Constructor store remote location information.
void SetDistanceToLeadingVehicle(const ActorPtr &actor, const float distance)
Method to specify how much distance a vehicle should maintain to the leading vehicle.
void HealthCheckRemoteTM()
Method to check server is alive or not.
void SetPercentageRunningSign(const ActorPtr &actor, const float perc)
Method to specify the % chance of running any traffic sign.
void SetKeepSlowLanePercentage(const ActorPtr &actor, const float percentage)
Method to set % to keep on the slow lane.
void SetSynchronousMode(bool mode)
Method to switch traffic manager into synchronous execution.
void SetForceLaneChange(const ActorPtr &actor, const bool direction)
Method to force lane change on a vehicle.
void SetHybridPhysicsRadius(const float radius)
Method to set hybrid physics radius.
void Release()
To release the traffic manager.
EpisodeProxyImpl< EpisodeProxyPointerType::Strong > EpisodeProxy
carla::SharedPtr< cc::Actor > ActorPtr
std::vector< uint8_t > Route
std::vector< cg::Location > Path
std::vector< Action > ActionBuffer
std::pair< RoadOption, WaypointPtr > Action
This file contains definitions of common data structures used in traffic manager.