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 global % decrease in velocity with respect to the speed limit.
85  /// If less than 0, it's a % increase.
86  void SetGlobalPercentageSpeedDifference(const float percentage) {
87  DEBUG_ASSERT(_client != nullptr);
88  _client->call("set_global_percentage_speed_difference", percentage);
89  }
90 
91  /// Method to set collision detection rules between vehicles.
92  void SetCollisionDetection(const carla::rpc::Actor &reference_actor, const carla::rpc::Actor &other_actor, const bool detect_collision) {
93  DEBUG_ASSERT(_client != nullptr);
94  _client->call("set_collision_detection", reference_actor, other_actor, detect_collision);
95  }
96 
97  /// Method to force lane change on a vehicle.
98  /// Direction flag can be set to true for left and false for right.
99  void SetForceLaneChange(const carla::rpc::Actor &actor, const bool direction) {
100  DEBUG_ASSERT(_client != nullptr);
101  _client->call("set_force_lane_change", actor, direction);
102  }
103 
104  /// Enable/disable automatic lane change on a vehicle.
105  void SetAutoLaneChange(const carla::rpc::Actor &actor, const bool enable) {
106  DEBUG_ASSERT(_client != nullptr);
107  _client->call("set_auto_lane_change", actor, enable);
108  }
109 
110  /// Method to specify how much distance a vehicle should maintain to
111  /// the leading vehicle.
112  void SetDistanceToLeadingVehicle(const carla::rpc::Actor &actor, const float distance) {
113  DEBUG_ASSERT(_client != nullptr);
114  _client->call("set_distance_to_leading_vehicle", actor, distance);
115  }
116 
117  /// Method to specify the % chance of ignoring collisions with any walker.
118  void SetPercentageIgnoreWalkers(const carla::rpc::Actor &actor, const float percentage) {
119  DEBUG_ASSERT(_client != nullptr);
120  _client->call("set_percentage_ignore_walkers", actor, percentage);
121  }
122 
123  /// Method to specify the % chance of ignoring collisions with any vehicle.
124  void SetPercentageIgnoreVehicles(const carla::rpc::Actor &actor, const float percentage) {
125  DEBUG_ASSERT(_client != nullptr);
126  _client->call("set_percentage_ignore_vehicles", actor, percentage);
127  }
128 
129  /// Method to specify the % chance of running a traffic sign.
130  void SetPercentageRunningLight(const carla::rpc::Actor &actor, const float percentage) {
131  DEBUG_ASSERT(_client != nullptr);
132  _client->call("set_percentage_running_light", actor, percentage);
133  }
134 
135  /// Method to specify the % chance of running any traffic sign.
136  void SetPercentageRunningSign(const carla::rpc::Actor &actor, const float percentage) {
137  DEBUG_ASSERT(_client != nullptr);
138  _client->call("set_percentage_running_sign", actor, percentage);
139  }
140 
141  /// Method to switch traffic manager into synchronous execution.
142  void SetSynchronousMode(const bool mode) {
143  DEBUG_ASSERT(_client != nullptr);
144  _client->call("set_synchronous_mode", mode);
145  }
146 
147  /// Method to set tick timeout for synchronous execution.
148  void SetSynchronousModeTimeOutInMiliSecond(const double time) {
149  DEBUG_ASSERT(_client != nullptr);
150  _client->call("set_synchronous_mode_timeout_in_milisecond", time);
151  }
152 
153  /// Method to provide synchronous tick.
155  DEBUG_ASSERT(_client != nullptr);
156  return _client->call("synchronous_tick").as<bool>();
157  }
158 
159  /// Check if remote traffic manager is alive
161  DEBUG_ASSERT(_client != nullptr);
162  _client->call("health_check_remote_TM");
163  }
164 
165  /// Method to specify how much distance a vehicle should maintain to
166  /// the Global leading vehicle.
167  void SetGlobalDistanceToLeadingVehicle(const float distance) {
168  DEBUG_ASSERT(_client != nullptr);
169  _client->call("set_global_distance_to_leading_vehicle",distance);
170  }
171 
172  /// Method to set probabilistic preference to keep on the right lane.
173  void SetKeepRightPercentage(const carla::rpc::Actor &actor, const float percentage) {
174  DEBUG_ASSERT(_client != nullptr);
175  _client->call("set_percentage_keep_right_rule", actor, percentage);
176  }
177 
178  /// Method to set hybrid physics mode.
179  void SetHybridPhysicsMode(const bool mode_switch) {
180  DEBUG_ASSERT(_client != nullptr);
181  _client->call("set_hybrid_physics_mode", mode_switch);
182  }
183 
184  /// Method to set hybrid physics mode.
185  void SetHybridPhysicsRadius(const float radius) {
186  DEBUG_ASSERT(_client != nullptr);
187  _client->call("set_hybrid_physics_radius", radius);
188  }
189 
190  /// Method to set randomization seed.
191  void SetRandomDeviceSeed(const uint64_t seed) {
192  DEBUG_ASSERT(_client != nullptr);
193  _client->call("set_random_device_seed", seed);
194  }
195 
196  /// Method to set Open Street Map mode.
197  void SetOSMMode(const bool mode_switch) {
198  DEBUG_ASSERT(_client != nullptr);
199  _client->call("set_osm_mode", mode_switch);
200  }
201 
202 private:
203 
204  /// RPC client.
205  ::rpc::client *_client = nullptr;
206 
207  /// Server port and host.
208  std::string tmhost;
209  uint16_t tmport;
210 };
211 
212 } // namespace traffic_manager
213 } // namespace carla
static const unsigned short TM_DEFAULT_PORT
Definition: Constants.h:20
void SetForceLaneChange(const carla::rpc::Actor &actor, const bool direction)
Method to force lane change on a vehicle.
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 probabilistic preference to keep on the right lane.
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:99
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.
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.
void HealthCheckRemoteTM()
Check if remote traffic manager is alive.
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.
void getServerDetails(std::string &_host, uint16_t &_port)
Get parameters.
Provides communication with the rpc of TrafficManagerServer.
void SetGlobalDistanceToLeadingVehicle(const float distance)
Method to specify how much distance a vehicle should maintain to the Global leading vehicle...
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 UnregisterVehicle(const std::vector< carla::rpc::Actor > &actor_list)
Unregister vehicles to remote traffic manager server via RPC client.
void SetGlobalPercentageSpeedDifference(const float percentage)
Method to set a global % decrease in velocity with respect to the speed limit.
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. ...
TrafficManagerClient(const TrafficManagerClient &)=default