CARLA
TrafficManagerRemote.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020 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 
13 namespace carla {
14 namespace 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.
28  Release();
29 }
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 
80  Stop();
81 }
82 
84  Stop();
85 
86  carla::client::detail::EpisodeProxy episode_proxy = episodeProxyTM.Lock()->GetCurrentEpisode();
87  episodeProxyTM = episode_proxy;
88 
89  Start();
90 }
91 
92 void TrafficManagerRemote::RegisterVehicles(const std::vector<ActorPtr> &_actor_list) {
93  std::vector<carla::rpc::Actor> actor_list;
94  for (auto &&actor : _actor_list) {
95  actor_list.emplace_back(actor->Serialize());
96  }
97  client.RegisterVehicle(actor_list);
98 }
99 
100 void TrafficManagerRemote::UnregisterVehicles(const std::vector<ActorPtr> &_actor_list) {
101  std::vector<carla::rpc::Actor> actor_list;
102  for (auto &&actor : _actor_list) {
103  actor_list.emplace_back(actor->Serialize());
104  }
105  client.UnregisterVehicle(actor_list);
106 }
107 
108 void TrafficManagerRemote::SetPercentageSpeedDifference(const ActorPtr &_actor, const float percentage) {
109  carla::rpc::Actor actor(_actor->Serialize());
110 
111  client.SetPercentageSpeedDifference(actor, percentage);
112 }
113 
114 void TrafficManagerRemote::SetDesiredSpeed(const ActorPtr &_actor, const float value) {
115  carla::rpc::Actor actor(_actor->Serialize());
116 
117  client.SetDesiredSpeed(actor, value);
118 }
119 
122 }
123 
124 void TrafficManagerRemote::SetLaneOffset(const ActorPtr &_actor, const float offset) {
125  carla::rpc::Actor actor(_actor->Serialize());
126 
127  client.SetLaneOffset(actor, offset);
128 }
129 
131  client.SetGlobalLaneOffset(offset);
132 }
133 
134 void TrafficManagerRemote::SetUpdateVehicleLights(const ActorPtr &_actor, const bool do_update) {
135  carla::rpc::Actor actor(_actor->Serialize());
136 
137  client.SetUpdateVehicleLights(actor, do_update);
138 }
139 
140 void TrafficManagerRemote::SetCollisionDetection(const ActorPtr &_reference_actor, const ActorPtr &_other_actor, const bool detect_collision) {
141  carla::rpc::Actor reference_actor(_reference_actor->Serialize());
142  carla::rpc::Actor other_actor(_other_actor->Serialize());
143 
144  client.SetCollisionDetection(reference_actor, other_actor, detect_collision);
145 }
146 
147 void TrafficManagerRemote::SetForceLaneChange(const ActorPtr &_actor, const bool direction) {
148  carla::rpc::Actor actor(_actor->Serialize());
149 
150  client.SetForceLaneChange(actor, direction);
151 }
152 
153 void TrafficManagerRemote::SetAutoLaneChange(const ActorPtr &_actor, const bool enable) {
154  carla::rpc::Actor actor(_actor->Serialize());
155 
156  client.SetAutoLaneChange(actor, enable);
157 }
158 
159 void TrafficManagerRemote::SetDistanceToLeadingVehicle(const ActorPtr &_actor, const float distance) {
160  carla::rpc::Actor actor(_actor->Serialize());
161 
162  client.SetDistanceToLeadingVehicle(actor, distance);
163 }
164 
167 }
168 
169 
170 void TrafficManagerRemote::SetPercentageIgnoreWalkers(const ActorPtr &_actor, const float percentage) {
171  carla::rpc::Actor actor(_actor->Serialize());
172 
173  client.SetPercentageIgnoreWalkers(actor, percentage);
174 }
175 
176 void TrafficManagerRemote::SetPercentageIgnoreVehicles(const ActorPtr &_actor, const float percentage) {
177  carla::rpc::Actor actor(_actor->Serialize());
178 
179  client.SetPercentageIgnoreVehicles(actor, percentage);
180 }
181 
182 void TrafficManagerRemote::SetPercentageRunningLight(const ActorPtr &_actor, const float percentage) {
183  carla::rpc::Actor actor(_actor->Serialize());
184 
185  client.SetPercentageRunningLight(actor, percentage);
186 }
187 
188 void TrafficManagerRemote::SetPercentageRunningSign(const ActorPtr &_actor, const float percentage) {
189  carla::rpc::Actor actor(_actor->Serialize());
190 
191  client.SetPercentageRunningSign(actor, percentage);
192 }
193 
194 void TrafficManagerRemote::SetKeepRightPercentage(const ActorPtr &_actor, const float percentage) {
195  carla::rpc::Actor actor(_actor->Serialize());
196 
197  client.SetKeepRightPercentage(actor, percentage);
198 }
199 
200 void TrafficManagerRemote::SetRandomLeftLaneChangePercentage(const ActorPtr &_actor, const float percentage) {
201  carla::rpc::Actor actor(_actor->Serialize());
202 
203  client.SetRandomLeftLaneChangePercentage(actor, percentage);
204 }
205 
206 void TrafficManagerRemote::SetRandomRightLaneChangePercentage(const ActorPtr &_actor, const float percentage) {
207  carla::rpc::Actor actor(_actor->Serialize());
208 
209  client.SetRandomRightLaneChangePercentage(actor, percentage);
210 }
211 
212 void TrafficManagerRemote::SetHybridPhysicsMode(const bool mode_switch) {
213  client.SetHybridPhysicsMode(mode_switch);
214 }
215 
218 }
219 
220 void TrafficManagerRemote::SetOSMMode(const bool mode_switch) {
221  client.SetOSMMode(mode_switch);
222 }
223 
224 void TrafficManagerRemote::SetCustomPath(const ActorPtr &_actor, const Path path, const bool empty_buffer) {
225  carla::rpc::Actor actor(_actor->Serialize());
226 
227  client.SetCustomPath(actor, path, empty_buffer);
228 }
229 
230 void TrafficManagerRemote::RemoveUploadPath(const ActorId &actor_id, const bool remove_path) {
231  client.RemoveUploadPath(actor_id, remove_path);
232 }
233 
234 void TrafficManagerRemote::UpdateUploadPath(const ActorId &actor_id, const Path path) {
235  client.UpdateUploadPath(actor_id, path);
236 }
237 
238 void TrafficManagerRemote::SetImportedRoute(const ActorPtr &_actor, const Route route, const bool empty_buffer) {
239  carla::rpc::Actor actor(_actor->Serialize());
240 
241  client.SetImportedRoute(actor, route, empty_buffer);
242 }
243 
244 void TrafficManagerRemote::RemoveImportedRoute(const ActorId &actor_id, const bool remove_path) {
245  client.RemoveImportedRoute(actor_id, remove_path);
246 }
247 
248 void TrafficManagerRemote::UpdateImportedRoute(const ActorId &actor_id, const Route route) {
249  client.UpdateImportedRoute(actor_id, route);
250 }
251 
253  client.SetRespawnDormantVehicles(mode_switch);
254 }
255 
256 void TrafficManagerRemote::SetBoundariesRespawnDormantVehicles(const float lower_bound, const float upper_bound) {
257  client.SetBoundariesRespawnDormantVehicles(lower_bound, upper_bound);
258 }
259 
260 void TrafficManagerRemote::SetMaxBoundaries(const float lower, const float upper) {
261  client.SetMaxBoundaries(lower, upper);
262 }
263 
265  client.ShutDown();
266 }
267 
270 }
271 
274 }
275 
277  return client.GetNextAction(actor_id);
278 }
279 
281  return client.GetActionBuffer(actor_id);
282 }
283 
285  return false;
286 }
287 
290 }
291 
293  return episodeProxyTM;
294 }
295 
296 void TrafficManagerRemote::SetRandomDeviceSeed(const uint64_t seed) {
298 }
299 
300 } // namespace traffic_manager
301 } // namespace carla
TrafficManagerClient client
Remote client using the IP and port information it connects to as remote RPC traffic manager server...
void SetForceLaneChange(const ActorPtr &actor, const bool direction)
Method to force lane change on a vehicle.
void SetPercentageIgnoreVehicles(const ActorPtr &actor, const float perc)
Method to specify the % chance of ignoring collisions with any vehicle.
void SetAutoLaneChange(const ActorPtr &actor, const bool enable)
Enable/disable automatic lane change on a vehicle.
void SetPercentageRunningSign(const ActorPtr &actor, const float perc)
Method to specify the % chance of running any traffic sign.
void SetHybridPhysicsMode(const bool mode_switch)
Method to set hybrid physics mode.
carla::client::detail::EpisodeProxy episodeProxyTM
CARLA client connection object.
void SetSynchronousModeTimeOutInMiliSecond(double time)
Method to set Tick timeout for synchronous execution.
void UpdateImportedRoute(const ActorId &actor_id, const Route route)
Method to update an already set route.
void SetForceLaneChange(const carla::rpc::Actor &actor, const bool direction)
Method to force lane change on a vehicle.
void SetUpdateVehicleLights(const carla::rpc::Actor &_actor, const bool do_update)
Method to set the automatic management of the vehicle lights.
void SetGlobalDistanceToLeadingVehicle(const float distance)
Method to specify Global Distance.
void SetOSMMode(const bool mode_switch)
Method to set Open Street Map mode.
void Reset()
To reset the traffic manager.
carla::SharedPtr< cc::Actor > ActorPtr
void SetHybridPhysicsRadius(const float radius)
Method to set hybrid physics radius.
void SetSynchronousModeTimeOutInMiliSecond(const double time)
Method to set tick timeout for synchronous execution.
void UnregisterVehicles(const std::vector< ActorPtr > &actor_list)
This method unregisters a vehicle from traffic manager.
void SetAutoLaneChange(const carla::rpc::Actor &actor, const bool enable)
Enable/disable automatic lane change on a vehicle.
void SetRespawnDormantVehicles(const bool mode_switch)
Method to set automatic respawn of dormant vehicles.
void SetSynchronousMode(bool mode)
Method to switch traffic manager into synchronous execution.
void SetKeepRightPercentage(const carla::rpc::Actor &actor, const float percentage)
Method to set % to keep on the right lane.
bool SynchronousTick()
Method to provide synchronous tick.
ActionBuffer GetActionBuffer(const ActorId &actor_id)
Method to get the vehicle&#39;s action buffer.
std::vector< cg::Location > Path
void SetLaneOffset(const carla::rpc::Actor &_actor, const float offset)
Method to set a lane offset displacement from the center line.
void SetCollisionDetection(const ActorPtr &reference_actor, const ActorPtr &other_actor, const bool detect_collision)
Method to set collision detection rules between vehicles.
Action GetNextAction(const ActorId &actor_id)
Method to get the vehicle&#39;s next action.
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
void SetPercentageIgnoreWalkers(const carla::rpc::Actor &actor, const float percentage)
Method to specify the % chance of ignoring collisions with any walker.
void RegisterVehicles(const std::vector< ActorPtr > &actor_list)
This method registers a vehicle with the traffic manager.
void SetPercentageIgnoreWalkers(const ActorPtr &actor, const float perc)
Method to specify the % chance of ignoring collisions with any walker.
void SetPercentageRunningLight(const ActorPtr &actor, const float perc)
Method to specify the % chance of running any traffic light.
void SetSynchronousMode(const bool mode)
Method to switch traffic manager into synchronous execution.
void SetGlobalPercentageSpeedDifference(float const percentage)
Method to set a global % decrease in velocity with respect to the speed limit.
void SetRandomDeviceSeed(const uint64_t seed)
Method to set randomization seed.
void SetGlobalLaneOffset(const float offset)
Method to set a global lane offset displacement from the center line.
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&#39;s exact desired velocity.
void SetPercentageSpeedDifference(const ActorPtr &actor, const float percentage)
Method to set a vehicle&#39;s % decrease in velocity with respect to the speed limit. ...
void SetUpdateVehicleLights(const ActorPtr &actor, const bool do_update)
Method to set the automatic management of the vehicle lights.
void SetLaneOffset(const ActorPtr &actor, const float offset)
Method to set a lane offset displacement from the center line.
void RemoveUploadPath(const ActorId &actor_id, const bool remove_path)
Method to remove a path.
void SetBoundariesRespawnDormantVehicles(const float lower_bound, const float upper_bound)
Method to set boundaries for respawning vehicles.
Action GetNextAction(const ActorId &actor_id)
Method to get the vehicle&#39;s next action.
void SetImportedRoute(const ActorPtr &actor, const Route route, const bool empty_buffer)
Method to set our own imported route.
void SetHybridPhysicsMode(const bool mode_switch)
Method to set hybrid physics mode.
void Start()
To start the TrafficManager.
void SetRandomRightLaneChangePercentage(const ActorPtr &actor, const float percentage)
Method to set % to randomly do a right lane change.
void SetHybridPhysicsRadius(const float radius)
Method to set hybrid physics mode.
std::pair< RoadOption, WaypointPtr > Action
void HealthCheckRemoteTM()
Check if remote traffic manager is alive.
void SetDesiredSpeed(const carla::rpc::Actor &_actor, const float value)
Set a vehicle&#39;s exact desired velocity.
void SetOSMMode(const bool mode_switch)
Method to set Open Street Map mode.
void RemoveImportedRoute(const ActorId &actor_id, const bool remove_path)
Method to remove a route.
carla::ActorId ActorId
void UpdateUploadPath(const ActorId &actor_id, const Path path)
Method to update an already set list of points.
void SetPercentageRunningLight(const carla::rpc::Actor &actor, const float percentage)
Method to specify the % chance of running a traffic sign.
void UpdateUploadPath(const ActorId &actor_id, const Path path)
Method to update an already set path.
void SetPercentageIgnoreVehicles(const carla::rpc::Actor &actor, const float percentage)
Method to specify the % chance of ignoring collisions with any vehicle.
std::vector< uint8_t > Route
void getServerDetails(std::string &_host, uint16_t &_port)
Get parameters.
void SetMaxBoundaries(const float lower, const float upper)
Method to set limits for boundaries when respawning vehicles.
void RemoveUploadPath(const ActorId &actor_id, const bool remove_path)
Method to remove a list of points.
void UpdateImportedRoute(const ActorId &actor_id, const Route route)
Method to update an already set list of points.
void SetRespawnDormantVehicles(const bool mode_switch)
Method to set automatic respawn of dormant vehicles.
TrafficManagerRemote(const std::pair< std::string, uint16_t > &_serverTM, carla::client::detail::EpisodeProxy &episodeProxy)
Constructor store remote location information.
void SetGlobalDistanceToLeadingVehicle(const float distance)
Method to specify how much distance a vehicle should maintain to the Global leading vehicle...
void SetRandomLeftLaneChangePercentage(const carla::rpc::Actor &actor, const float percentage)
Method to set % to randomly do a left lane change.
void SetDistanceToLeadingVehicle(const ActorPtr &actor, const float distance)
Method to specify how much distance a vehicle should maintain to the leading vehicle.
void RegisterVehicle(const std::vector< carla::rpc::Actor > &actor_list)
Register vehicles to remote traffic manager server via RPC client.
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 Release()
To release the traffic manager.
void RemoveImportedRoute(const ActorId &actor_id, const bool remove_path)
Method to remove a route.
void SetPercentageRunningSign(const carla::rpc::Actor &actor, const float percentage)
Method to specify the % chance of running any traffic sign.
void SetCustomPath(const carla::rpc::Actor &actor, const Path path, const bool empty_buffer)
Method to set our own imported path.
void SetGlobalLaneOffset(float const offset)
Method to set a global lane offset displacement from the center line.
SharedPtrType Lock() const
Same as TryLock but never return nullptr.
void UnregisterVehicle(const std::vector< carla::rpc::Actor > &actor_list)
Unregister vehicles to remote traffic manager server via RPC client.
ActionBuffer GetActionBuffer(const ActorId &actor_id)
Method to get the vehicle&#39;s action buffer.
void SetKeepRightPercentage(const ActorPtr &actor, const float percentage)
Method to set % to keep on the right lane.
void SetGlobalPercentageSpeedDifference(const float percentage)
Method to set a global % decrease in velocity with respect to the speed limit.
void SetRandomRightLaneChangePercentage(const carla::rpc::Actor &actor, const float percentage)
Method to set % to randomly do a right lane change.
void HealthCheckRemoteTM()
Method to check server is alive or not.
std::vector< Action > ActionBuffer
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 SetPercentageSpeedDifference(const carla::rpc::Actor &_actor, const float percentage)
Method to set a vehicle&#39;s % decrease in velocity with respect to the speed limit. ...
void SetMaxBoundaries(const float lower, const float upper)
Method to set boundaries for respawning vehicles.
void SetRandomDeviceSeed(const uint64_t seed)
Method to set randomization seed.
carla::client::detail::EpisodeProxy & GetEpisodeProxy()
Get CARLA episode information.
void SetImportedRoute(const carla::rpc::Actor &actor, const Route route, const bool empty_buffer)
Method to set our own imported route.
void SetBoundariesRespawnDormantVehicles(const float lower_bound, const float upper_bound)
Method to set boundaries for respawning vehicles.
void SetRandomLeftLaneChangePercentage(const ActorPtr &actor, const float percentage)
Method to set % to randomly do a left lane change.