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 
23 using namespace constants::Networking;
24 
26 public:
27 
28  TrafficManagerServer(const TrafficManagerServer &) = default;
30 
31  TrafficManagerServer &operator=(const TrafficManagerServer &) = default;
32  TrafficManagerServer &operator=(TrafficManagerServer &&) = default;
33 
34  /// Here RPCPort is the traffic manager local instance RPC server port where
35  /// it can listen to remote traffic managers and apply the changes to
36  /// local instance through a TrafficManagerBase pointer.
38  uint16_t &RPCPort,
40  : _RPCPort(RPCPort) {
41 
42  uint16_t counter = 0;
43  while(counter < MIN_TRY_COUNT) {
44  try {
45 
46  /// Create server instance.
47  server = new ::rpc::server(RPCPort);
48 
49  } catch(std::exception) {
50  using namespace std::chrono_literals;
51  /// Update port number and try again.
52  std::this_thread::sleep_for(500ms);
53  }
54 
55  /// If server created.
56  if(server != nullptr) {
57  break;
58  }
59  counter ++;
60  }
61 
62  /// If server still not created throw a runtime exception.
63  if(server == nullptr) {
64 
65  carla::throw_exception(std::runtime_error(
66  "trying to create rpc server for traffic manager; "
67  "but the system failed to create because of bind error."));
68  } else {
69  /// If the server creation was successful we are
70  /// binding a lambda function to the name "register_vehicle".
71  server->bind("register_vehicle", [=](std :: vector <carla::rpc::Actor> _actor_list) {
72  std::vector<ActorPtr> actor_list;
73  for (auto &&actor : _actor_list) {
74  actor_list.emplace_back(carla::client::detail::ActorVariant(actor).Get(tm->GetEpisodeProxy()));
75  }
76  tm->RegisterVehicles(actor_list);
77  });
78 
79 
80  /// Binding a lambda function to the name "unregister_vehicle".
81  server->bind("unregister_vehicle", [=](std :: vector <carla::rpc::Actor> _actor_list) {
82  std::vector<ActorPtr> actor_list;
83  for (auto &&actor : _actor_list) {
84  actor_list.emplace_back(carla::client::detail::ActorVariant(actor).Get(tm->GetEpisodeProxy()));
85  }
86  tm->UnregisterVehicles(actor_list);
87  });
88 
89  /// Method to set a vehicle's % decrease in velocity with respect to the speed limit.
90  /// If less than 0, it's a % increase.
91  server->bind("set_percentage_speed_difference", [=](carla::rpc::Actor actor, const float percentage) {
93  });
94 
95  /// Method to set a global % decrease in velocity with respect to the speed limit.
96  /// If less than 0, it's a % increase.
97  server->bind("set_global_percentage_speed_difference", [=](const float percentage) {
98  tm->SetGlobalPercentageSpeedDifference(percentage);
99  });
100 
101  /// Method to set collision detection rules between vehicles.
102  server->bind("set_collision_detection", [=](const carla::rpc::Actor &reference_actor, const carla::rpc::Actor &other_actor, const bool detect_collision) {
103  const auto reference = carla::client::detail::ActorVariant(reference_actor).Get(tm->GetEpisodeProxy());
104  const auto other = carla::client::detail::ActorVariant(other_actor).Get(tm->GetEpisodeProxy());
105  tm->SetCollisionDetection(reference, other, detect_collision);
106  });
107 
108  /// Method to force lane change on a vehicle.
109  /// Direction flag can be set to true for left and false for right.
110  server->bind("set_force_lane_change", [=](carla::rpc::Actor actor, const bool direction) {
112  });
113 
114  /// Enable/disable automatic lane change on a vehicle.
115  server->bind("set_auto_lane_change", [=](carla::rpc::Actor actor, const bool enable) {
117  });
118 
119  /// Method to specify how much distance a vehicle should maintain to
120  /// the leading vehicle.
121  server->bind("set_distance_to_leading_vehicle", [=](carla::rpc::Actor actor, const float distance) {
123  });
124 
125  /// Method to the Global Distance to Leading vehicle
126 
127  server->bind("set_global_distance_to_leading_vehicle", [=]( const float distance) {
128  tm->SetGlobalDistanceToLeadingVehicle(distance);
129  });
130 
131  /// Method to specify the % chance of running any traffic light.
132  server->bind("set_percentage_running_light", [=](carla::rpc::Actor actor, const float percentage) {
134  });
135 
136  /// Method to specify the % chance of running any traffic sign.
137  server->bind("set_percentage_running_sign", [=](carla::rpc::Actor actor, const float percentage) {
139  });
140 
141  /// Method to specify the % chance of ignoring collisions with any walker.
142  server->bind("set_percentage_ignore_walkers", [=](carla::rpc::Actor actor, const float percentage) {
144  });
145 
146  /// Method to specify the % chance of ignoring collisions with any vehicle.
147  server->bind("set_percentage_ignore_vehicles", [=](carla::rpc::Actor actor, const float percentage) {
149  });
150 
151  /// Method to specify the % chance of ignoring collisions with any vehicle.
152  server->bind("set_percentage_keep_right_rule", [=](carla::rpc::Actor actor, const float percentage) {
154  });
155 
156  /// Method to set hybrid physics mode.
157  server->bind("set_hybrid_physics_mode", [=](const bool mode_switch) {
158  tm->SetHybridPhysicsMode(mode_switch);
159  });
160 
161  /// Method to set hybrid physics radius.
162  server->bind("set_hybrid_physics_radius", [=](const float radius) {
163  tm->SetHybridPhysicsRadius(radius);
164  });
165 
166  /// Method to set hybrid physics radius.
167  server->bind("set_osm_mode", [=](const bool mode_switch) {
168  tm->SetHybridPhysicsRadius(mode_switch);
169  });
170 
171  /// Method to set respawn dormant vehicles mode.
172  server->bind("set_respawn_dormant_vehicles", [=](const bool mode_switch) {
173  tm->SetRespawnDormantVehicles(mode_switch);
174  });
175 
176  /// Method to set respawn dormant vehicles mode.
177  server->bind("set_boundaries_respawn_dormant_vehicles", [=](const float lower_bound, const float upper_bound) {
178  tm->SetBoundariesRespawnDormantVehicles(lower_bound, upper_bound);
179  });
180 
181  server->bind("shut_down", [=]() {
182  tm->Release();
183  });
184 
185  /// Method to set synchronous mode.
186  server->bind("set_synchronous_mode", [=](const bool mode) {
187  tm->SetSynchronousMode(mode);
188  });
189 
190  /// Method to set tick timeout for synchronous execution.
191  server->bind("set_synchronous_mode_timeout_in_milisecond", [=](const double time) {
193  });
194 
195  /// Method to set randomization seed.
196  server->bind("set_random_device_seed", [=](const uint64_t seed) {
197  tm->SetRandomDeviceSeed(seed);
198  });
199 
200  /// Method to provide synchronous tick.
201  server->bind("synchronous_tick", [=]() -> bool {
202  return tm->SynchronousTick();
203  });
204 
205  /// Method to check server is alive or not.
206  server->bind("health_check_remote_TM", [=](){});
207 
208  /// Run traffic manager server to respond of any
209  /// user client in asynchronous mode.
210  server->async_run();
211  }
212 
213  }
214 
216  if(server) {
217  server->stop();
218  delete server;
219  server = nullptr;
220  }
221  }
222 
223  uint16_t port() const {
224  return _RPCPort;
225  }
226 
227 private:
228 
229  /// Traffic manager server RPC port
230  uint16_t _RPCPort;
231 
232  /// Server instance
233  ::rpc::server *server = nullptr;
234 
235 };
236 
237 } // namespace traffic_manager
238 } // 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:22
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.
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 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 probabilistic preference to keep on the right lane.
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 SetPercentageIgnoreVehicles(const ActorPtr &actor, const float perc)=0
Method to specify the % chance of ignoring collisions with any vehicle.
virtual void SetRespawnDormantVehicles(const bool mode_switch)=0
Method to set automatic respawn of dormant vehicles.
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:41
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 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 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 carla::client::detail::EpisodeProxy & GetEpisodeProxy()=0
Get carla episode information.