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  /// Set a vehicle's exact desired velocity.
98  server->bind("set_desired_speed", [=](carla::rpc::Actor actor, const float value) {
100  });
101 
102  /// Method to set the automatic management of the vehicle lights
103  server->bind("update_vehicle_lights", [=](carla::rpc::Actor actor, const bool do_update) {
105  });
106 
107  /// Method to set a global % decrease in velocity with respect to the speed limit.
108  /// If less than 0, it's a % increase.
109  server->bind("set_global_percentage_speed_difference", [=](const float percentage) {
110  tm->SetGlobalPercentageSpeedDifference(percentage);
111  });
112 
113  /// Method to set collision detection rules between vehicles.
114  server->bind("set_collision_detection", [=](const carla::rpc::Actor &reference_actor, const carla::rpc::Actor &other_actor, const bool detect_collision) {
115  const auto reference = carla::client::detail::ActorVariant(reference_actor).Get(tm->GetEpisodeProxy());
116  const auto other = carla::client::detail::ActorVariant(other_actor).Get(tm->GetEpisodeProxy());
117  tm->SetCollisionDetection(reference, other, detect_collision);
118  });
119 
120  /// Method to force lane change on a vehicle.
121  /// Direction flag can be set to true for left and false for right.
122  server->bind("set_force_lane_change", [=](carla::rpc::Actor actor, const bool direction) {
124  });
125 
126  /// Enable/disable automatic lane change on a vehicle.
127  server->bind("set_auto_lane_change", [=](carla::rpc::Actor actor, const bool enable) {
129  });
130 
131  /// Method to specify how much distance a vehicle should maintain to
132  /// the leading vehicle.
133  server->bind("set_distance_to_leading_vehicle", [=](carla::rpc::Actor actor, const float distance) {
135  });
136 
137  /// Method to the Global Distance to Leading vehicle
138 
139  server->bind("set_global_distance_to_leading_vehicle", [=]( const float distance) {
140  tm->SetGlobalDistanceToLeadingVehicle(distance);
141  });
142 
143  /// Method to specify the % chance of running any traffic light.
144  server->bind("set_percentage_running_light", [=](carla::rpc::Actor actor, const float percentage) {
146  });
147 
148  /// Method to specify the % chance of running any traffic sign.
149  server->bind("set_percentage_running_sign", [=](carla::rpc::Actor actor, const float percentage) {
151  });
152 
153  /// Method to specify the % chance of ignoring collisions with any walker.
154  server->bind("set_percentage_ignore_walkers", [=](carla::rpc::Actor actor, const float percentage) {
156  });
157 
158  /// Method to specify the % chance of ignoring collisions with any vehicle.
159  server->bind("set_percentage_ignore_vehicles", [=](carla::rpc::Actor actor, const float percentage) {
161  });
162 
163  /// Method to set % to keep on the right lane.
164  server->bind("set_percentage_keep_right_rule", [=](carla::rpc::Actor actor, const float percentage) {
166  });
167 
168  /// Method to set % to randomly do a left lane change.
169  server->bind("set_percentage_random_left_lanechange", [=](carla::rpc::Actor actor, const float percentage) {
171  });
172 
173  /// Method to set % to randomly do a right lane change.
174  server->bind("set_percentage_random_right_lanechange", [=](carla::rpc::Actor actor, const float percentage) {
176  });
177 
178  /// Method to set hybrid physics mode.
179  server->bind("set_hybrid_physics_mode", [=](const bool mode_switch) {
180  tm->SetHybridPhysicsMode(mode_switch);
181  });
182 
183  /// Method to set hybrid physics radius.
184  server->bind("set_hybrid_physics_radius", [=](const float radius) {
185  tm->SetHybridPhysicsRadius(radius);
186  });
187 
188  /// Method to set hybrid physics radius.
189  server->bind("set_osm_mode", [=](const bool mode_switch) {
190  tm->SetOSMMode(mode_switch);
191  });
192 
193  /// Method to set our own imported path.
194  server->bind("set_path", [=](carla::rpc::Actor actor, const Path path, const bool empty_buffer) {
195  tm->SetCustomPath(carla::client::detail::ActorVariant(actor).Get(tm->GetEpisodeProxy()), path, empty_buffer);
196  });
197 
198  /// Method to remove a list of points.
199  server->bind("remove_custom_path", [=](const ActorId actor_id, const bool remove_path) {
200  tm->RemoveUploadPath(actor_id, remove_path);
201  });
202 
203  /// Method to update an already set list of points.
204  server->bind("update_custom_path", [=](const ActorId actor_id, const Path path) {
205  tm->UpdateUploadPath(actor_id, path);
206  });
207 
208  /// Method to set our own imported route.
209  server->bind("set_imported_route", [=](carla::rpc::Actor actor, const Route route, const bool empty_buffer) {
210  tm->SetImportedRoute(carla::client::detail::ActorVariant(actor).Get(tm->GetEpisodeProxy()), route, empty_buffer);
211  });
212 
213  /// Method to remove a route.
214  server->bind("remove_imported_route", [=](const ActorId actor_id, const bool remove_path) {
215  tm->RemoveImportedRoute(actor_id, remove_path);
216  });
217 
218  /// Method to update an already set list of points.
219  server->bind("update_imported_route", [=](const ActorId actor_id, const Route route) {
220  tm->UpdateImportedRoute(actor_id, route);
221  });
222 
223  /// Method to set respawn dormant vehicles mode.
224  server->bind("set_respawn_dormant_vehicles", [=](const bool mode_switch) {
225  tm->SetRespawnDormantVehicles(mode_switch);
226  });
227 
228  /// Method to set respawn dormant vehicles mode.
229  server->bind("set_boundaries_respawn_dormant_vehicles", [=](const float lower_bound, const float upper_bound) {
230  tm->SetBoundariesRespawnDormantVehicles(lower_bound, upper_bound);
231  });
232 
233  /// Method to get the vehicle's next action.
234  server->bind("get_next_action", [=](const ActorId actor_id) {
235  tm->GetNextAction(actor_id);
236  });
237 
238  /// Method to get the vehicle's action buffer.
239  server->bind("get_all_actions", [=](const ActorId actor_id) {
240  tm->GetActionBuffer(actor_id);
241  });
242 
243  server->bind("shut_down", [=]() {
244  tm->Release();
245  });
246 
247  /// Method to set synchronous mode.
248  server->bind("set_synchronous_mode", [=](const bool mode) {
249  tm->SetSynchronousMode(mode);
250  });
251 
252  /// Method to set tick timeout for synchronous execution.
253  server->bind("set_synchronous_mode_timeout_in_milisecond", [=](const double time) {
255  });
256 
257  /// Method to set randomization seed.
258  server->bind("set_random_device_seed", [=](const uint64_t seed) {
259  tm->SetRandomDeviceSeed(seed);
260  });
261 
262  /// Method to provide synchronous tick.
263  server->bind("synchronous_tick", [=]() -> bool {
264  return tm->SynchronousTick();
265  });
266 
267  /// Method to check server is alive or not.
268  server->bind("health_check_remote_TM", [=](){});
269 
270  /// Run traffic manager server to respond of any
271  /// user client in asynchronous mode.
272  server->async_run();
273  }
274 
275  }
276 
278  if(server) {
279  server->stop();
280  delete server;
281  server = nullptr;
282  }
283  }
284 
285  uint16_t port() const {
286  return _RPCPort;
287  }
288 
289 private:
290 
291  /// Traffic manager server RPC port
292  uint16_t _RPCPort;
293 
294  /// Server instance
295  ::rpc::server *server = nullptr;
296 
297 };
298 
299 } // namespace traffic_manager
300 } // 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.
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 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.