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  /// Set a vehicle's exact desired velocity.
85  void SetDesiredSpeed(const carla::rpc::Actor &_actor, const float value) {
86  DEBUG_ASSERT(_client != nullptr);
87  _client->call("set_desired_speed", std::move(_actor), value);
88  }
89 
90  /// Method to set a global % decrease in velocity with respect to the speed limit.
91  /// If less than 0, it's a % increase.
92  void SetGlobalPercentageSpeedDifference(const float percentage) {
93  DEBUG_ASSERT(_client != nullptr);
94  _client->call("set_global_percentage_speed_difference", percentage);
95  }
96 
97  /// Method to set the automatic management of the vehicle lights
98  void SetUpdateVehicleLights(const carla::rpc::Actor &_actor, const bool do_update) {
99  DEBUG_ASSERT(_client != nullptr);
100  _client->call("update_vehicle_lights", std::move(_actor), do_update);
101  }
102 
103  /// Method to set collision detection rules between vehicles.
104  void SetCollisionDetection(const carla::rpc::Actor &reference_actor, const carla::rpc::Actor &other_actor, const bool detect_collision) {
105  DEBUG_ASSERT(_client != nullptr);
106  _client->call("set_collision_detection", reference_actor, other_actor, detect_collision);
107  }
108 
109  /// Method to force lane change on a vehicle.
110  /// Direction flag can be set to true for left and false for right.
111  void SetForceLaneChange(const carla::rpc::Actor &actor, const bool direction) {
112  DEBUG_ASSERT(_client != nullptr);
113  _client->call("set_force_lane_change", actor, direction);
114  }
115 
116  /// Enable/disable automatic lane change on a vehicle.
117  void SetAutoLaneChange(const carla::rpc::Actor &actor, const bool enable) {
118  DEBUG_ASSERT(_client != nullptr);
119  _client->call("set_auto_lane_change", actor, enable);
120  }
121 
122  /// Method to specify how much distance a vehicle should maintain to
123  /// the leading vehicle.
124  void SetDistanceToLeadingVehicle(const carla::rpc::Actor &actor, const float distance) {
125  DEBUG_ASSERT(_client != nullptr);
126  _client->call("set_distance_to_leading_vehicle", actor, distance);
127  }
128 
129  /// Method to specify the % chance of ignoring collisions with any walker.
130  void SetPercentageIgnoreWalkers(const carla::rpc::Actor &actor, const float percentage) {
131  DEBUG_ASSERT(_client != nullptr);
132  _client->call("set_percentage_ignore_walkers", actor, percentage);
133  }
134 
135  /// Method to specify the % chance of ignoring collisions with any vehicle.
136  void SetPercentageIgnoreVehicles(const carla::rpc::Actor &actor, const float percentage) {
137  DEBUG_ASSERT(_client != nullptr);
138  _client->call("set_percentage_ignore_vehicles", actor, percentage);
139  }
140 
141  /// Method to specify the % chance of running a traffic sign.
142  void SetPercentageRunningLight(const carla::rpc::Actor &actor, const float percentage) {
143  DEBUG_ASSERT(_client != nullptr);
144  _client->call("set_percentage_running_light", actor, percentage);
145  }
146 
147  /// Method to specify the % chance of running any traffic sign.
148  void SetPercentageRunningSign(const carla::rpc::Actor &actor, const float percentage) {
149  DEBUG_ASSERT(_client != nullptr);
150  _client->call("set_percentage_running_sign", actor, percentage);
151  }
152 
153  /// Method to switch traffic manager into synchronous execution.
154  void SetSynchronousMode(const bool mode) {
155  DEBUG_ASSERT(_client != nullptr);
156  _client->call("set_synchronous_mode", mode);
157  }
158 
159  /// Method to set tick timeout for synchronous execution.
160  void SetSynchronousModeTimeOutInMiliSecond(const double time) {
161  DEBUG_ASSERT(_client != nullptr);
162  _client->call("set_synchronous_mode_timeout_in_milisecond", time);
163  }
164 
165  /// Method to provide synchronous tick.
167  DEBUG_ASSERT(_client != nullptr);
168  return _client->call("synchronous_tick").as<bool>();
169  }
170 
171  /// Check if remote traffic manager is alive
173  DEBUG_ASSERT(_client != nullptr);
174  _client->call("health_check_remote_TM");
175  }
176 
177  /// Method to specify how much distance a vehicle should maintain to
178  /// the Global leading vehicle.
179  void SetGlobalDistanceToLeadingVehicle(const float distance) {
180  DEBUG_ASSERT(_client != nullptr);
181  _client->call("set_global_distance_to_leading_vehicle",distance);
182  }
183 
184  /// Method to set % to keep on the right lane.
185  void SetKeepRightPercentage(const carla::rpc::Actor &actor, const float percentage) {
186  DEBUG_ASSERT(_client != nullptr);
187  _client->call("keep_right_rule_percentage", actor, percentage);
188  }
189 
190  /// Method to set % to randomly do a left lane change.
191  void SetRandomLeftLaneChangePercentage(const carla::rpc::Actor &actor, const float percentage) {
192  DEBUG_ASSERT(_client != nullptr);
193  _client->call("random_left_lanechange_percentage", actor, percentage);
194  }
195 
196  /// Method to set % to randomly do a right lane change.
197  void SetRandomRightLaneChangePercentage(const carla::rpc::Actor &actor, const float percentage) {
198  DEBUG_ASSERT(_client != nullptr);
199  _client->call("random_right_lanechange_percentage", actor, percentage);
200  }
201 
202  /// Method to set hybrid physics mode.
203  void SetHybridPhysicsMode(const bool mode_switch) {
204  DEBUG_ASSERT(_client != nullptr);
205  _client->call("set_hybrid_physics_mode", mode_switch);
206  }
207 
208  /// Method to set hybrid physics mode.
209  void SetHybridPhysicsRadius(const float radius) {
210  DEBUG_ASSERT(_client != nullptr);
211  _client->call("set_hybrid_physics_radius", radius);
212  }
213 
214  /// Method to set randomization seed.
215  void SetRandomDeviceSeed(const uint64_t seed) {
216  DEBUG_ASSERT(_client != nullptr);
217  _client->call("set_random_device_seed", seed);
218  }
219 
220  /// Method to set Open Street Map mode.
221  void SetOSMMode(const bool mode_switch) {
222  DEBUG_ASSERT(_client != nullptr);
223  _client->call("set_osm_mode", mode_switch);
224  }
225 
226  /// Method to set our own imported path.
227  void SetCustomPath(const carla::rpc::Actor &actor, const Path path, const bool empty_buffer) {
228  DEBUG_ASSERT(_client != nullptr);
229  _client->call("set_path", actor, path, empty_buffer);
230  }
231 
232  /// Method to remove a list of points.
233  void RemoveUploadPath(const ActorId &actor_id, const bool remove_path) {
234  DEBUG_ASSERT(_client != nullptr);
235  _client->call("remove_custom_path", actor_id, remove_path);
236  }
237 
238  /// Method to update an already set list of points.
239  void UpdateUploadPath(const ActorId &actor_id, const Path path) {
240  DEBUG_ASSERT(_client != nullptr);
241  _client->call("update_custom_path", actor_id, path);
242  }
243 
244  /// Method to set our own imported route.
245  void SetImportedRoute(const carla::rpc::Actor &actor, const Route route, const bool empty_buffer) {
246  DEBUG_ASSERT(_client != nullptr);
247  _client->call("set_imported_route", actor, route, empty_buffer);
248  }
249 
250  /// Method to remove a route.
251  void RemoveImportedRoute(const ActorId &actor_id, const bool remove_path) {
252  DEBUG_ASSERT(_client != nullptr);
253  _client->call("remove_imported_route", actor_id, remove_path);
254  }
255 
256  /// Method to update an already set list of points.
257  void UpdateImportedRoute(const ActorId &actor_id, const Route route) {
258  DEBUG_ASSERT(_client != nullptr);
259  _client->call("update_imported_route", actor_id, route);
260  }
261 
262  /// Method to set automatic respawn of dormant vehicles.
263  void SetRespawnDormantVehicles(const bool mode_switch) {
264  DEBUG_ASSERT(_client != nullptr);
265  _client->call("set_respawn_dormant_vehicles", mode_switch);
266  }
267 
268  /// Method to set boundaries for respawning vehicles.
269  void SetBoundariesRespawnDormantVehicles(const float lower_bound, const float upper_bound) {
270  DEBUG_ASSERT(_client != nullptr);
271  _client->call("set_boundaries_respawn_dormant_vehicles", lower_bound, upper_bound);
272  }
273 
274  /// Method to set boundaries for respawning vehicles.
275  void SetMaxBoundaries(const float lower, const float upper) {
276  DEBUG_ASSERT(_client != nullptr);
277  _client->call("set_max_boundaries", lower, upper);
278  }
279 
280  /// Method to get the vehicle's next action.
281  Action GetNextAction(const ActorId &actor_id) {
282  DEBUG_ASSERT(_client != nullptr);
283  _client->call("get_next_action", actor_id);
284  return Action();
285  }
286 
287  /// Method to get the vehicle's action buffer.
289  DEBUG_ASSERT(_client != nullptr);
290  _client->call("get_all_actions", actor_id);
291  return ActionBuffer();
292  }
293 
294  void ShutDown() {
295  DEBUG_ASSERT(_client != nullptr);
296  _client->call("shut_down");
297  }
298 
299 private:
300 
301  /// RPC client.
302  ::rpc::client *_client = nullptr;
303 
304  /// Server port and host.
305  std::string tmhost;
306  uint16_t tmport;
307 };
308 
309 } // namespace traffic_manager
310 } // 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
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.
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.