CARLA
TrafficManagerServer.h
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 #pragma once
8 
9 #include <vector>
10 
11 #include "carla/Exception.h"
12 #include "carla/client/Actor.h"
14 #include "carla/rpc/Server.h"
17 
18 namespace carla {
19 namespace traffic_manager {
20 
22 using Path = std::vector<cg::Location>;
23 using Route = std::vector<uint8_t>;
24 
25 using namespace constants::Networking;
26 
28 public:
29 
30  TrafficManagerServer(const TrafficManagerServer &) = default;
32 
33  TrafficManagerServer &operator=(const TrafficManagerServer &) = default;
34  TrafficManagerServer &operator=(TrafficManagerServer &&) = default;
35 
36  /// Here RPCPort is the traffic manager local instance RPC server port where
37  /// it can listen to remote traffic managers and apply the changes to
38  /// local instance through a TrafficManagerBase pointer.
40  uint16_t &RPCPort,
42  : _RPCPort(RPCPort) {
43 
44  uint16_t counter = 0;
45  while(counter < MIN_TRY_COUNT) {
46  try {
47 
48  /// Create server instance.
49  server = new ::rpc::server(RPCPort);
50 
51  } catch(std::exception) {
52  using namespace std::chrono_literals;
53  /// Update port number and try again.
54  std::this_thread::sleep_for(500ms);
55  }
56 
57  /// If server created.
58  if(server != nullptr) {
59  break;
60  }
61  counter ++;
62  }
63 
64  /// If server still not created throw a runtime exception.
65  if(server == nullptr) {
66 
67  carla::throw_exception(std::runtime_error(
68  "trying to create rpc server for traffic manager; "
69  "but the system failed to create because of bind error."));
70  } else {
71  /// If the server creation was successful we are
72  /// binding a lambda function to the name "register_vehicle".
73  server->bind("register_vehicle", [=](std :: vector <carla::rpc::Actor> _actor_list) {
74  std::vector<ActorPtr> actor_list;
75  for (auto &&actor : _actor_list) {
76  actor_list.emplace_back(carla::client::detail::ActorVariant(actor).Get(tm->GetEpisodeProxy()));
77  }
78  tm->RegisterVehicles(actor_list);
79  });
80 
81 
82  /// Binding a lambda function to the name "unregister_vehicle".
83  server->bind("unregister_vehicle", [=](std :: vector <carla::rpc::Actor> _actor_list) {
84  std::vector<ActorPtr> actor_list;
85  for (auto &&actor : _actor_list) {
86  actor_list.emplace_back(carla::client::detail::ActorVariant(actor).Get(tm->GetEpisodeProxy()));
87  }
88  tm->UnregisterVehicles(actor_list);
89  });
90 
91  /// Method to set a vehicle's % decrease in velocity with respect to the speed limit.
92  /// If less than 0, it's a % increase.
93  server->bind("set_percentage_speed_difference", [=](carla::rpc::Actor actor, const float percentage) {
95  });
96 
97  /// Method to set a lane offset displacement from the center line.
98  /// Positive values imply a right offset while negative ones mean a left one.
99  server->bind("set_lane_offset", [=](carla::rpc::Actor actor, const float offset) {
101  });
102 
103  /// Set a vehicle's exact desired velocity.
104  server->bind("set_desired_speed", [=](carla::rpc::Actor actor, const float value) {
106  });
107 
108  /// Method to set the automatic management of the vehicle lights
109  server->bind("update_vehicle_lights", [=](carla::rpc::Actor actor, const bool do_update) {
111  });
112 
113  /// Method to set a global % decrease in velocity with respect to the speed limit.
114  /// If less than 0, it's a % increase.
115  server->bind("set_global_percentage_speed_difference", [=](const float percentage) {
116  tm->SetGlobalPercentageSpeedDifference(percentage);
117  });
118 
119  /// Method to set a global lane offset displacement from the center line.
120  /// Positive values imply a right offset while negative ones mean a left one.
121  server->bind("set_global_lane_offset", [=](const float offset) {
122  tm->SetGlobalLaneOffset(offset);
123  });
124 
125 
126  /// Method to set collision detection rules between vehicles.
127  server->bind("set_collision_detection", [=](const carla::rpc::Actor &reference_actor, const carla::rpc::Actor &other_actor, const bool detect_collision) {
128  const auto reference = carla::client::detail::ActorVariant(reference_actor).Get(tm->GetEpisodeProxy());
129  const auto other = carla::client::detail::ActorVariant(other_actor).Get(tm->GetEpisodeProxy());
130  tm->SetCollisionDetection(reference, other, detect_collision);
131  });
132 
133  /// Method to force lane change on a vehicle.
134  /// Direction flag can be set to true for left and false for right.
135  server->bind("set_force_lane_change", [=](carla::rpc::Actor actor, const bool direction) {
137  });
138 
139  /// Enable/disable automatic lane change on a vehicle.
140  server->bind("set_auto_lane_change", [=](carla::rpc::Actor actor, const bool enable) {
142  });
143 
144  /// Method to specify how much distance a vehicle should maintain to
145  /// the leading vehicle.
146  server->bind("set_distance_to_leading_vehicle", [=](carla::rpc::Actor actor, const float distance) {
148  });
149 
150  /// Method to the Global Distance to Leading vehicle
151 
152  server->bind("set_global_distance_to_leading_vehicle", [=]( const float distance) {
153  tm->SetGlobalDistanceToLeadingVehicle(distance);
154  });
155 
156  /// Method to specify the % chance of running any traffic light.
157  server->bind("set_percentage_running_light", [=](carla::rpc::Actor actor, const float percentage) {
159  });
160 
161  /// Method to specify the % chance of running any traffic sign.
162  server->bind("set_percentage_running_sign", [=](carla::rpc::Actor actor, const float percentage) {
164  });
165 
166  /// Method to specify the % chance of ignoring collisions with any walker.
167  server->bind("set_percentage_ignore_walkers", [=](carla::rpc::Actor actor, const float percentage) {
169  });
170 
171  /// Method to specify the % chance of ignoring collisions with any vehicle.
172  server->bind("set_percentage_ignore_vehicles", [=](carla::rpc::Actor actor, const float percentage) {
174  });
175 
176  /// Method to set % to keep on the right lane.
177  server->bind("set_percentage_keep_right_rule", [=](carla::rpc::Actor actor, const float percentage) {
179  });
180 
181  /// Method to set % to randomly do a left lane change.
182  server->bind("set_percentage_random_left_lanechange", [=](carla::rpc::Actor actor, const float percentage) {
184  });
185 
186  /// Method to set % to randomly do a right lane change.
187  server->bind("set_percentage_random_right_lanechange", [=](carla::rpc::Actor actor, const float percentage) {
189  });
190 
191  /// Method to set hybrid physics mode.
192  server->bind("set_hybrid_physics_mode", [=](const bool mode_switch) {
193  tm->SetHybridPhysicsMode(mode_switch);
194  });
195 
196  /// Method to set hybrid physics radius.
197  server->bind("set_hybrid_physics_radius", [=](const float radius) {
198  tm->SetHybridPhysicsRadius(radius);
199  });
200 
201  /// Method to set hybrid physics radius.
202  server->bind("set_osm_mode", [=](const bool mode_switch) {
203  tm->SetOSMMode(mode_switch);
204  });
205 
206  /// Method to set our own imported path.
207  server->bind("set_path", [=](carla::rpc::Actor actor, const Path path, const bool empty_buffer) {
208  tm->SetCustomPath(carla::client::detail::ActorVariant(actor).Get(tm->GetEpisodeProxy()), path, empty_buffer);
209  });
210 
211  /// Method to remove a list of points.
212  server->bind("remove_custom_path", [=](const ActorId actor_id, const bool remove_path) {
213  tm->RemoveUploadPath(actor_id, remove_path);
214  });
215 
216  /// Method to update an already set list of points.
217  server->bind("update_custom_path", [=](const ActorId actor_id, const Path path) {
218  tm->UpdateUploadPath(actor_id, path);
219  });
220 
221  /// Method to set our own imported route.
222  server->bind("set_imported_route", [=](carla::rpc::Actor actor, const Route route, const bool empty_buffer) {
223  tm->SetImportedRoute(carla::client::detail::ActorVariant(actor).Get(tm->GetEpisodeProxy()), route, empty_buffer);
224  });
225 
226  /// Method to remove a route.
227  server->bind("remove_imported_route", [=](const ActorId actor_id, const bool remove_path) {
228  tm->RemoveImportedRoute(actor_id, remove_path);
229  });
230 
231  /// Method to update an already set list of points.
232  server->bind("update_imported_route", [=](const ActorId actor_id, const Route route) {
233  tm->UpdateImportedRoute(actor_id, route);
234  });
235 
236  /// Method to set respawn dormant vehicles mode.
237  server->bind("set_respawn_dormant_vehicles", [=](const bool mode_switch) {
238  tm->SetRespawnDormantVehicles(mode_switch);
239  });
240 
241  /// Method to set respawn dormant vehicles mode.
242  server->bind("set_boundaries_respawn_dormant_vehicles", [=](const float lower_bound, const float upper_bound) {
243  tm->SetBoundariesRespawnDormantVehicles(lower_bound, upper_bound);
244  });
245 
246  /// Method to get the vehicle's next action.
247  server->bind("get_next_action", [=](const ActorId actor_id) {
248  tm->GetNextAction(actor_id);
249  });
250 
251  /// Method to get the vehicle's action buffer.
252  server->bind("get_all_actions", [=](const ActorId actor_id) {
253  tm->GetActionBuffer(actor_id);
254  });
255 
256  server->bind("shut_down", [=]() {
257  tm->Release();
258  });
259 
260  /// Method to set synchronous mode.
261  server->bind("set_synchronous_mode", [=](const bool mode) {
262  tm->SetSynchronousMode(mode);
263  });
264 
265  /// Method to set tick timeout for synchronous execution.
266  server->bind("set_synchronous_mode_timeout_in_milisecond", [=](const double time) {
268  });
269 
270  /// Method to set randomization seed.
271  server->bind("set_random_device_seed", [=](const uint64_t seed) {
272  tm->SetRandomDeviceSeed(seed);
273  });
274 
275  /// Method to provide synchronous tick.
276  server->bind("synchronous_tick", [=]() -> bool {
277  return tm->SynchronousTick();
278  });
279 
280  /// Method to check server is alive or not.
281  server->bind("health_check_remote_TM", [=](){});
282 
283  /// Run traffic manager server to respond of any
284  /// user client in asynchronous mode.
285  server->async_run();
286  }
287 
288  }
289 
291  if(server) {
292  server->stop();
293  delete server;
294  server = nullptr;
295  }
296  }
297 
298  uint16_t port() const {
299  return _RPCPort;
300  }
301 
302 private:
303 
304  /// Traffic manager server RPC port
305  uint16_t _RPCPort;
306 
307  /// Server instance
308  ::rpc::server *server = nullptr;
309 
310 };
311 
312 } // namespace traffic_manager
313 } // namespace carla
virtual void SetHybridPhysicsMode(const bool mode_switch)=0
Method to set hybrid physics mode.
virtual void SetAutoLaneChange(const ActorPtr &actor, const bool enable)=0
Enable/disable automatic lane change on a vehicle.
virtual void SetForceLaneChange(const ActorPtr &actor, const bool direction)=0
Method to force lane change on a vehicle.
virtual bool SynchronousTick()=0
Method to provide synchronous tick.
carla::SharedPtr< cc::Actor > ActorPtr
virtual void SetGlobalDistanceToLeadingVehicle(const float dist)=0
Method to set Global Distance to Leading Vehicle.
void throw_exception(const std::exception &e)
Definition: Carla.cpp:135
Holds an Actor, but only instantiates it when needed.
Definition: ActorVariant.h:30
virtual void SetPercentageIgnoreWalkers(const ActorPtr &actor, const float perc)=0
Method to specify the % chance of ignoring collisions with any walker.
virtual void Release()=0
To release the traffic manager.
std::vector< cg::Location > Path
boost::shared_ptr< T > SharedPtr
Use this SharedPtr (boost::shared_ptr) to keep compatibility with boost::python, but it would be nice...
Definition: Memory.h:20
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
virtual void SetPercentageSpeedDifference(const ActorPtr &actor, const float percentage)=0
Set a vehicle&#39;s % decrease in velocity with respect to the speed limit.
uint16_t _RPCPort
Traffic manager server RPC port.
virtual void UnregisterVehicles(const std::vector< ActorPtr > &actor_list)=0
This method unregisters a vehicle from traffic manager.
static T Get(carla::rpc::Response< T > &response)
TrafficManagerServer(uint16_t &RPCPort, carla::traffic_manager::TrafficManagerBase *tm)
Here RPCPort is the traffic manager local instance RPC server port where it can listen to remote traf...
virtual Action GetNextAction(const ActorId &actor_id)=0
Method to get the vehicle&#39;s next action.
virtual void SetRandomLeftLaneChangePercentage(const ActorPtr &actor, const float percentage)=0
Method to set % to randomly do a left lane change.
virtual void RemoveImportedRoute(const ActorId &actor_id, const bool remove_path)=0
Method to remove a route.
virtual void SetImportedRoute(const ActorPtr &actor, const Route route, const bool empty_buffer)=0
Method to set our own imported route.
virtual void SetOSMMode(const bool mode_switch)=0
Method to set Open Street Map mode.
virtual void RemoveUploadPath(const ActorId &actor_id, const bool remove_path)=0
Method to remove a path.
virtual void SetGlobalPercentageSpeedDifference(float const percentage)=0
Set a global % decrease in velocity with respect to the speed limit.
virtual void SetKeepRightPercentage(const ActorPtr &actor, const float percentage)=0
Method to set % to keep on the right lane.
carla::ActorId ActorId
The function of this class is to integrate all the various stages of the traffic manager appropriatel...
virtual void SetDistanceToLeadingVehicle(const ActorPtr &actor, const float distance)=0
Method to specify how much distance a vehicle should maintain to the leading vehicle.
virtual void SetUpdateVehicleLights(const ActorPtr &actor, const bool do_update)=0
Method to set the automatic management of the vehicle lights.
virtual void SetPercentageIgnoreVehicles(const ActorPtr &actor, const float perc)=0
Method to specify the % chance of ignoring collisions with any vehicle.
virtual void SetCustomPath(const ActorPtr &actor, const Path path, const bool empty_buffer)=0
Method to set our own imported path.
virtual void SetGlobalLaneOffset(float const offset)=0
Method to set a global lane offset displacement from the center line.
std::vector< uint8_t > Route
virtual void SetRespawnDormantVehicles(const bool mode_switch)=0
Method to set automatic respawn of dormant vehicles.
virtual void UpdateUploadPath(const ActorId &actor_id, const Path path)=0
Method to update an already set path.
virtual void SetCollisionDetection(const ActorPtr &reference_actor, const ActorPtr &other_actor, const bool detect_collision)=0
Method to set collision detection rules between vehicles.
virtual void SetPercentageRunningLight(const ActorPtr &actor, const float perc)=0
Method to specify the % chance of running any traffic light.
SharedPtr< client::Actor > Get(EpisodeProxy episode) const
Definition: ActorVariant.h:49
virtual void UpdateImportedRoute(const ActorId &actor_id, const Route route)=0
Method to update an already set route.
virtual void SetHybridPhysicsRadius(const float radius)=0
Method to set hybrid physics radius.
virtual void SetLaneOffset(const ActorPtr &actor, const float offset)=0
Method to set a lane offset displacement from the center line.
virtual void SetRandomDeviceSeed(const uint64_t seed)=0
Method to set randomization seed.
virtual void SetDesiredSpeed(const ActorPtr &actor, const float value)=0
Set a vehicle&#39;s exact desired velocity.
virtual void SetSynchronousModeTimeOutInMiliSecond(double time)=0
Method to set Tick timeout for synchronous execution.
virtual void SetBoundariesRespawnDormantVehicles(const float lower_bound, const float upper_bound)=0
Method to set boundaries for respawning vehicles.
virtual void SetPercentageRunningSign(const ActorPtr &actor, const float perc)=0
Method to specify the % chance of running any traffic sign.
virtual ActionBuffer GetActionBuffer(const ActorId &actor_id)=0
Method to get the vehicle&#39;s action buffer.
virtual void SetSynchronousMode(bool mode)=0
Method to switch traffic manager into synchronous execution.
virtual void RegisterVehicles(const std::vector< ActorPtr > &actor_list)=0
This method registers a vehicle with the traffic manager.
virtual void SetRandomRightLaneChangePercentage(const ActorPtr &actor, const float percentage)=0
Method to set % to randomly do a right lane change.
virtual carla::client::detail::EpisodeProxy & GetEpisodeProxy()=0
Get carla episode information.