CARLA
TrafficManagerClient.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 
10 #include "carla/rpc/Actor.h"
11 
12 #include <rpc/client.h>
13 
14 namespace carla {
15 namespace traffic_manager {
16 
19 
20 /// Provides communication with the rpc of TrafficManagerServer.
22 
23 public:
24 
25  TrafficManagerClient(const TrafficManagerClient &) = default;
27 
30 
31  /// Parametric constructor to initialize the parameters.
33  const std::string &_host,
34  const uint16_t &_port)
35  : tmhost(_host),
36  tmport(_port) {
37 
38  /// Create client instance.
39  if(!_client) {
40  _client = new ::rpc::client(tmhost, tmport);
41  _client->set_timeout(TM_TIMEOUT);
42  }
43  }
44 
45  /// Destructor method.
47  if(_client) {
48  delete _client;
49  _client = nullptr;
50  }
51  };
52 
53  /// Set parameters.
54  void setServerDetails(const std::string &_host, const uint16_t &_port) {
55  tmhost = _host;
56  tmport = _port;
57  }
58 
59  /// Get parameters.
60  void getServerDetails(std::string &_host, uint16_t &_port) {
61  _host = tmhost;
62  _port = tmport;
63  }
64 
65  /// Register vehicles to remote traffic manager server via RPC client.
66  void RegisterVehicle(const std::vector<carla::rpc::Actor> &actor_list) {
67  DEBUG_ASSERT(_client != nullptr);
68  _client->call("register_vehicle", std::move(actor_list));
69  }
70 
71  /// Unregister vehicles to remote traffic manager server via RPC client.
72  void UnregisterVehicle(const std::vector<carla::rpc::Actor> &actor_list) {
73  DEBUG_ASSERT(_client != nullptr);
74  _client->call("unregister_vehicle", std::move(actor_list));
75  }
76 
77  /// Method to set a vehicle's % decrease in velocity with respect to the speed limit.
78  /// If less than 0, it's a % increase.
79  void SetPercentageSpeedDifference(const carla::rpc::Actor &_actor, const float percentage) {
80  DEBUG_ASSERT(_client != nullptr);
81  _client->call("set_percentage_speed_difference", std::move(_actor), percentage);
82  }
83 
84  /// Method to set a lane offset displacement from the center line.
85  /// Positive values imply a right offset while negative ones mean a left one.
86  void SetLaneOffset(const carla::rpc::Actor &_actor, const float offset) {
87  DEBUG_ASSERT(_client != nullptr);
88  _client->call("set_lane_offset", std::move(_actor), offset);
89  }
90 
91  /// Set a vehicle's exact desired velocity.
92  void SetDesiredSpeed(const carla::rpc::Actor &_actor, const float value) {
93  DEBUG_ASSERT(_client != nullptr);
94  _client->call("set_desired_speed", std::move(_actor), value);
95  }
96 
97  /// Method to set a global % decrease in velocity with respect to the speed limit.
98  /// If less than 0, it's a % increase.
99  void SetGlobalPercentageSpeedDifference(const float percentage) {
100  DEBUG_ASSERT(_client != nullptr);
101  _client->call("set_global_percentage_speed_difference", percentage);
102  }
103 
104  /// Method to set a global lane offset displacement from the center line.
105  /// Positive values imply a right offset while negative ones mean a left one.
106  void SetGlobalLaneOffset(const float offset) {
107  DEBUG_ASSERT(_client != nullptr);
108  _client->call("set_global_lane_offset", offset);
109  }
110 
111  /// Method to set the automatic management of the vehicle lights
112  void SetUpdateVehicleLights(const carla::rpc::Actor &_actor, const bool do_update) {
113  DEBUG_ASSERT(_client != nullptr);
114  _client->call("update_vehicle_lights", std::move(_actor), do_update);
115  }
116 
117  /// Method to set collision detection rules between vehicles.
118  void SetCollisionDetection(const carla::rpc::Actor &reference_actor, const carla::rpc::Actor &other_actor, const bool detect_collision) {
119  DEBUG_ASSERT(_client != nullptr);
120  _client->call("set_collision_detection", reference_actor, other_actor, detect_collision);
121  }
122 
123  /// Method to force lane change on a vehicle.
124  /// Direction flag can be set to true for left and false for right.
125  void SetForceLaneChange(const carla::rpc::Actor &actor, const bool direction) {
126  DEBUG_ASSERT(_client != nullptr);
127  _client->call("set_force_lane_change", actor, direction);
128  }
129 
130  /// Enable/disable automatic lane change on a vehicle.
131  void SetAutoLaneChange(const carla::rpc::Actor &actor, const bool enable) {
132  DEBUG_ASSERT(_client != nullptr);
133  _client->call("set_auto_lane_change", actor, enable);
134  }
135 
136  /// Method to specify how much distance a vehicle should maintain to
137  /// the leading vehicle.
138  void SetDistanceToLeadingVehicle(const carla::rpc::Actor &actor, const float distance) {
139  DEBUG_ASSERT(_client != nullptr);
140  _client->call("set_distance_to_leading_vehicle", actor, distance);
141  }
142 
143  /// Method to specify the % chance of ignoring collisions with any walker.
144  void SetPercentageIgnoreWalkers(const carla::rpc::Actor &actor, const float percentage) {
145  DEBUG_ASSERT(_client != nullptr);
146  _client->call("set_percentage_ignore_walkers", actor, percentage);
147  }
148 
149  /// Method to specify the % chance of ignoring collisions with any vehicle.
150  void SetPercentageIgnoreVehicles(const carla::rpc::Actor &actor, const float percentage) {
151  DEBUG_ASSERT(_client != nullptr);
152  _client->call("set_percentage_ignore_vehicles", actor, percentage);
153  }
154 
155  /// Method to specify the % chance of running a traffic sign.
156  void SetPercentageRunningLight(const carla::rpc::Actor &actor, const float percentage) {
157  DEBUG_ASSERT(_client != nullptr);
158  _client->call("set_percentage_running_light", actor, percentage);
159  }
160 
161  /// Method to specify the % chance of running any traffic sign.
162  void SetPercentageRunningSign(const carla::rpc::Actor &actor, const float percentage) {
163  DEBUG_ASSERT(_client != nullptr);
164  _client->call("set_percentage_running_sign", actor, percentage);
165  }
166 
167  /// Method to switch traffic manager into synchronous execution.
168  void SetSynchronousMode(const bool mode) {
169  DEBUG_ASSERT(_client != nullptr);
170  _client->call("set_synchronous_mode", mode);
171  }
172 
173  /// Method to set tick timeout for synchronous execution.
174  void SetSynchronousModeTimeOutInMiliSecond(const double time) {
175  DEBUG_ASSERT(_client != nullptr);
176  _client->call("set_synchronous_mode_timeout_in_milisecond", time);
177  }
178 
179  /// Method to provide synchronous tick.
181  DEBUG_ASSERT(_client != nullptr);
182  return _client->call("synchronous_tick").as<bool>();
183  }
184 
185  /// Check if remote traffic manager is alive
187  DEBUG_ASSERT(_client != nullptr);
188  _client->call("health_check_remote_TM");
189  }
190 
191  /// Method to specify how much distance a vehicle should maintain to
192  /// the Global leading vehicle.
193  void SetGlobalDistanceToLeadingVehicle(const float distance) {
194  DEBUG_ASSERT(_client != nullptr);
195  _client->call("set_global_distance_to_leading_vehicle",distance);
196  }
197 
198  /// Method to set % to keep on the right lane.
199  void SetKeepRightPercentage(const carla::rpc::Actor &actor, const float percentage) {
200  DEBUG_ASSERT(_client != nullptr);
201  _client->call("keep_right_rule_percentage", actor, percentage);
202  }
203 
204  /// Method to set % to randomly do a left lane change.
205  void SetRandomLeftLaneChangePercentage(const carla::rpc::Actor &actor, const float percentage) {
206  DEBUG_ASSERT(_client != nullptr);
207  _client->call("random_left_lanechange_percentage", actor, percentage);
208  }
209 
210  /// Method to set % to randomly do a right lane change.
211  void SetRandomRightLaneChangePercentage(const carla::rpc::Actor &actor, const float percentage) {
212  DEBUG_ASSERT(_client != nullptr);
213  _client->call("random_right_lanechange_percentage", actor, percentage);
214  }
215 
216  /// Method to set hybrid physics mode.
217  void SetHybridPhysicsMode(const bool mode_switch) {
218  DEBUG_ASSERT(_client != nullptr);
219  _client->call("set_hybrid_physics_mode", mode_switch);
220  }
221 
222  /// Method to set hybrid physics mode.
223  void SetHybridPhysicsRadius(const float radius) {
224  DEBUG_ASSERT(_client != nullptr);
225  _client->call("set_hybrid_physics_radius", radius);
226  }
227 
228  /// Method to set randomization seed.
229  void SetRandomDeviceSeed(const uint64_t seed) {
230  DEBUG_ASSERT(_client != nullptr);
231  _client->call("set_random_device_seed", seed);
232  }
233 
234  /// Method to set Open Street Map mode.
235  void SetOSMMode(const bool mode_switch) {
236  DEBUG_ASSERT(_client != nullptr);
237  _client->call("set_osm_mode", mode_switch);
238  }
239 
240  /// Method to set our own imported path.
241  void SetCustomPath(const carla::rpc::Actor &actor, const Path path, const bool empty_buffer) {
242  DEBUG_ASSERT(_client != nullptr);
243  _client->call("set_path", actor, path, empty_buffer);
244  }
245 
246  /// Method to remove a list of points.
247  void RemoveUploadPath(const ActorId &actor_id, const bool remove_path) {
248  DEBUG_ASSERT(_client != nullptr);
249  _client->call("remove_custom_path", actor_id, remove_path);
250  }
251 
252  /// Method to update an already set list of points.
253  void UpdateUploadPath(const ActorId &actor_id, const Path path) {
254  DEBUG_ASSERT(_client != nullptr);
255  _client->call("update_custom_path", actor_id, path);
256  }
257 
258  /// Method to set our own imported route.
259  void SetImportedRoute(const carla::rpc::Actor &actor, const Route route, const bool empty_buffer) {
260  DEBUG_ASSERT(_client != nullptr);
261  _client->call("set_imported_route", actor, route, empty_buffer);
262  }
263 
264  /// Method to remove a route.
265  void RemoveImportedRoute(const ActorId &actor_id, const bool remove_path) {
266  DEBUG_ASSERT(_client != nullptr);
267  _client->call("remove_imported_route", actor_id, remove_path);
268  }
269 
270  /// Method to update an already set list of points.
271  void UpdateImportedRoute(const ActorId &actor_id, const Route route) {
272  DEBUG_ASSERT(_client != nullptr);
273  _client->call("update_imported_route", actor_id, route);
274  }
275 
276  /// Method to set automatic respawn of dormant vehicles.
277  void SetRespawnDormantVehicles(const bool mode_switch) {
278  DEBUG_ASSERT(_client != nullptr);
279  _client->call("set_respawn_dormant_vehicles", mode_switch);
280  }
281 
282  /// Method to set boundaries for respawning vehicles.
283  void SetBoundariesRespawnDormantVehicles(const float lower_bound, const float upper_bound) {
284  DEBUG_ASSERT(_client != nullptr);
285  _client->call("set_boundaries_respawn_dormant_vehicles", lower_bound, upper_bound);
286  }
287 
288  /// Method to set boundaries for respawning vehicles.
289  void SetMaxBoundaries(const float lower, const float upper) {
290  DEBUG_ASSERT(_client != nullptr);
291  _client->call("set_max_boundaries", lower, upper);
292  }
293 
294  /// Method to get the vehicle's next action.
295  Action GetNextAction(const ActorId &actor_id) {
296  DEBUG_ASSERT(_client != nullptr);
297  _client->call("get_next_action", actor_id);
298  return Action();
299  }
300 
301  /// Method to get the vehicle's action buffer.
303  DEBUG_ASSERT(_client != nullptr);
304  _client->call("get_all_actions", actor_id);
305  return ActionBuffer();
306  }
307 
308  void ShutDown() {
309  DEBUG_ASSERT(_client != nullptr);
310  _client->call("shut_down");
311  }
312 
313 private:
314 
315  /// RPC client.
316  ::rpc::client *_client = nullptr;
317 
318  /// Server port and host.
319  std::string tmhost;
320  uint16_t tmport;
321 };
322 
323 } // namespace traffic_manager
324 } // namespace carla
static const unsigned short TM_DEFAULT_PORT
Definition: Constants.h:22
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 SetOSMMode(const bool mode_switch)
Method to set Open Street Map mode.
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 setServerDetails(const std::string &_host, const uint16_t &_port)
Set parameters.
void SetKeepRightPercentage(const carla::rpc::Actor &actor, const float percentage)
Method to set % to keep on the right lane.
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.
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 SetSynchronousMode(const bool mode)
Method to switch traffic manager into synchronous execution.
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.
TrafficManagerClient & operator=(const TrafficManagerClient &)=default
std::string tmhost
Server port and host.
Action GetNextAction(const ActorId &actor_id)
Method to get the vehicle&#39;s next action.
void SetHybridPhysicsMode(const bool mode_switch)
Method to set hybrid physics mode.
#define DEBUG_ASSERT(predicate)
Definition: Debug.h:66
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 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.
TrafficManagerClient(const std::string &_host, const uint16_t &_port)
Parametric constructor to initialize the parameters.
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.
Provides communication with the rpc of TrafficManagerServer.
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.
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 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 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 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 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.
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.
bool SynchronousTick()
Method to provide synchronous tick.
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 SetImportedRoute(const carla::rpc::Actor &actor, const Route route, const bool empty_buffer)
Method to set our own imported route.
TrafficManagerClient(const TrafficManagerClient &)=default
void SetBoundariesRespawnDormantVehicles(const float lower_bound, const float upper_bound)
Method to set boundaries for respawning vehicles.