CARLA
Loading...
Searching...
No Matches
TrafficManagerClient.h
Go to the documentation of this file.
1// Copyright (c) 2026 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
14namespace carla {
15namespace traffic_manager {
16
19
20/// Provides communication with the rpc of TrafficManagerServer.
22
23public:
24
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 enable/disable the wide-turn manoeuvre for a single large vehicle.
112 void SetLargeVehicleWideTurn(const carla::rpc::Actor &_actor, const bool enable) {
113 DEBUG_ASSERT(_client != nullptr);
114 _client->call("set_large_vehicle_wide_turn", std::move(_actor), enable);
115 }
116
117 /// Method to enable/disable the wide-turn manoeuvre globally for large vehicles.
118 void SetGlobalLargeVehicleWideTurn(const bool enable) {
119 DEBUG_ASSERT(_client != nullptr);
120 _client->call("set_global_large_vehicle_wide_turn", enable);
121 }
122
123 /// Method to set the automatic management of the vehicle lights
124 void SetUpdateVehicleLights(const carla::rpc::Actor &_actor, const bool do_update) {
125 DEBUG_ASSERT(_client != nullptr);
126 _client->call("update_vehicle_lights", std::move(_actor), do_update);
127 }
128
129 /// Method to set collision detection rules between vehicles.
130 void SetCollisionDetection(const carla::rpc::Actor &reference_actor, const carla::rpc::Actor &other_actor, const bool detect_collision) {
131 DEBUG_ASSERT(_client != nullptr);
132 _client->call("set_collision_detection", reference_actor, other_actor, detect_collision);
133 }
134
135 /// Method to force lane change on a vehicle.
136 /// Direction flag can be set to true for left and false for right.
137 void SetForceLaneChange(const carla::rpc::Actor &actor, const bool direction) {
138 DEBUG_ASSERT(_client != nullptr);
139 _client->call("set_force_lane_change", actor, direction);
140 }
141
142 /// Enable/disable automatic lane change on a vehicle.
143 void SetAutoLaneChange(const carla::rpc::Actor &actor, const bool enable) {
144 DEBUG_ASSERT(_client != nullptr);
145 _client->call("set_auto_lane_change", actor, enable);
146 }
147
148 /// Method to specify how much distance a vehicle should maintain to
149 /// the leading vehicle.
151 DEBUG_ASSERT(_client != nullptr);
152 _client->call("set_distance_to_leading_vehicle", actor, distance);
153 }
154
155 /// Method to specify the % chance of ignoring collisions with any walker.
156 void SetPercentageIgnoreWalkers(const carla::rpc::Actor &actor, const float percentage) {
157 DEBUG_ASSERT(_client != nullptr);
158 _client->call("set_percentage_ignore_walkers", actor, percentage);
159 }
160
161 /// Method to specify the % chance of ignoring collisions with any vehicle.
162 void SetPercentageIgnoreVehicles(const carla::rpc::Actor &actor, const float percentage) {
163 DEBUG_ASSERT(_client != nullptr);
164 _client->call("set_percentage_ignore_vehicles", actor, percentage);
165 }
166
167 /// Method to specify the % chance of running a traffic sign.
168 void SetPercentageRunningLight(const carla::rpc::Actor &actor, const float percentage) {
169 DEBUG_ASSERT(_client != nullptr);
170 _client->call("set_percentage_running_light", actor, percentage);
171 }
172
173 /// Method to specify the % chance of running any traffic sign.
174 void SetPercentageRunningSign(const carla::rpc::Actor &actor, const float percentage) {
175 DEBUG_ASSERT(_client != nullptr);
176 _client->call("set_percentage_running_sign", actor, percentage);
177 }
178
179 /// Method to switch traffic manager into synchronous execution.
180 void SetSynchronousMode(const bool mode) {
181 DEBUG_ASSERT(_client != nullptr);
182 _client->call("set_synchronous_mode", mode);
183 }
184
185 /// Method to set tick timeout for synchronous execution.
187 DEBUG_ASSERT(_client != nullptr);
188 _client->call("set_synchronous_mode_timeout_in_milisecond", time);
189 }
190
191 /// Method to provide synchronous tick.
193 DEBUG_ASSERT(_client != nullptr);
194 return _client->call("synchronous_tick").as<bool>();
195 }
196
197 /// Check if remote traffic manager is alive
199 DEBUG_ASSERT(_client != nullptr);
200 _client->call("health_check_remote_TM");
201 }
202
203 /// Method to specify how much distance a vehicle should maintain to
204 /// the Global leading vehicle.
206 DEBUG_ASSERT(_client != nullptr);
207 _client->call("set_global_distance_to_leading_vehicle",distance);
208 }
209
210 /// Method to set % to keep on the slow lane.
211 void SetKeepSlowLanePercentage(const carla::rpc::Actor &actor, const float percentage) {
212 DEBUG_ASSERT(_client != nullptr);
213 _client->call("keep_slow_lane_rule_percentage", actor, percentage);
214 }
215
216 /// Method to set % to randomly do a left lane change.
217 void SetRandomLeftLaneChangePercentage(const carla::rpc::Actor &actor, const float percentage) {
218 DEBUG_ASSERT(_client != nullptr);
219 _client->call("random_left_lanechange_percentage", actor, percentage);
220 }
221
222 /// Method to set % to randomly do a right lane change.
223 void SetRandomRightLaneChangePercentage(const carla::rpc::Actor &actor, const float percentage) {
224 DEBUG_ASSERT(_client != nullptr);
225 _client->call("random_right_lanechange_percentage", actor, percentage);
226 }
227
228 /// Method to set hybrid physics mode.
229 void SetHybridPhysicsMode(const bool mode_switch) {
230 DEBUG_ASSERT(_client != nullptr);
231 _client->call("set_hybrid_physics_mode", mode_switch);
232 }
233
234 /// Method to set hybrid physics mode.
235 void SetHybridPhysicsRadius(const float radius) {
236 DEBUG_ASSERT(_client != nullptr);
237 _client->call("set_hybrid_physics_radius", radius);
238 }
239
240 /// Method to set randomization seed.
241 void SetRandomDeviceSeed(const uint64_t seed) {
242 DEBUG_ASSERT(_client != nullptr);
243 _client->call("set_random_device_seed", seed);
244 }
245
246 /// Method to set Open Street Map mode.
247 void SetOSMMode(const bool mode_switch) {
248 DEBUG_ASSERT(_client != nullptr);
249 _client->call("set_osm_mode", mode_switch);
250 }
251
252 /// Method to set our own imported path.
253 void SetCustomPath(const carla::rpc::Actor &actor, const Path path, const bool empty_buffer) {
254 DEBUG_ASSERT(_client != nullptr);
255 _client->call("set_path", actor, path, empty_buffer);
256 }
257
258 /// Method to remove a list of points.
259 void RemoveUploadPath(const ActorId &actor_id, const bool remove_path) {
260 DEBUG_ASSERT(_client != nullptr);
261 _client->call("remove_custom_path", actor_id, remove_path);
262 }
263
264 /// Method to update an already set list of points.
265 void UpdateUploadPath(const ActorId &actor_id, const Path path) {
266 DEBUG_ASSERT(_client != nullptr);
267 _client->call("update_custom_path", actor_id, path);
268 }
269
270 /// Method to set our own imported route.
271 void SetImportedRoute(const carla::rpc::Actor &actor, const Route route, const bool empty_buffer) {
272 DEBUG_ASSERT(_client != nullptr);
273 _client->call("set_imported_route", actor, route, empty_buffer);
274 }
275
276 /// Method to remove a route.
277 void RemoveImportedRoute(const ActorId &actor_id, const bool remove_path) {
278 DEBUG_ASSERT(_client != nullptr);
279 _client->call("remove_imported_route", actor_id, remove_path);
280 }
281
282 /// Method to update an already set list of points.
283 void UpdateImportedRoute(const ActorId &actor_id, const Route route) {
284 DEBUG_ASSERT(_client != nullptr);
285 _client->call("update_imported_route", actor_id, route);
286 }
287
288 /// Method to set automatic respawn of dormant vehicles.
289 void SetRespawnDormantVehicles(const bool mode_switch) {
290 DEBUG_ASSERT(_client != nullptr);
291 _client->call("set_respawn_dormant_vehicles", mode_switch);
292 }
293
294 /// Method to set boundaries for respawning vehicles.
295 void SetBoundariesRespawnDormantVehicles(const float lower_bound, const float upper_bound) {
296 DEBUG_ASSERT(_client != nullptr);
297 _client->call("set_boundaries_respawn_dormant_vehicles", lower_bound, upper_bound);
298 }
299
300 /// Method to set boundaries for respawning vehicles.
301 void SetMaxBoundaries(const float lower, const float upper) {
302 DEBUG_ASSERT(_client != nullptr);
303 _client->call("set_max_boundaries", lower, upper);
304 }
305
306 /// Method to get the vehicle's next action.
307 Action GetNextAction(const ActorId &actor_id) {
308 DEBUG_ASSERT(_client != nullptr);
309 _client->call("get_next_action", actor_id);
310 return Action();
311 }
312
313 /// Method to get the vehicle's action buffer.
315 DEBUG_ASSERT(_client != nullptr);
316 _client->call("get_all_actions", actor_id);
317 return ActionBuffer();
318 }
319
320 void ShutDown() {
321 DEBUG_ASSERT(_client != nullptr);
322 _client->call("shut_down");
323 }
324
325private:
326
327 /// RPC client.
328 ::rpc::client *_client = nullptr;
329
330 /// Server port and host.
331 std::string tmhost;
332 uint16_t tmport;
333};
334
335} // namespace traffic_manager
336} // namespace carla
float distance
#define DEBUG_ASSERT(predicate)
Definition Debug.h:66
Provides communication with the rpc of TrafficManagerServer.
void SetGlobalLaneOffset(const float offset)
Method to set a global lane offset displacement from the center line.
Action GetNextAction(const ActorId &actor_id)
Method to get the vehicle's next action.
void HealthCheckRemoteTM()
Check if remote traffic manager is alive.
TrafficManagerClient & operator=(TrafficManagerClient &&)=default
void SetPercentageIgnoreWalkers(const carla::rpc::Actor &actor, const float percentage)
Method to specify the % chance of ignoring collisions with any walker.
void SetDesiredSpeed(const carla::rpc::Actor &_actor, const float value)
Set a vehicle's exact desired velocity.
void SetGlobalPercentageSpeedDifference(const float percentage)
Method to set a global % decrease in velocity with respect to the speed limit.
TrafficManagerClient(TrafficManagerClient &&)=default
void SetMaxBoundaries(const float lower, const float upper)
Method to set boundaries for respawning vehicles.
void SetBoundariesRespawnDormantVehicles(const float lower_bound, const float upper_bound)
Method to set boundaries for respawning vehicles.
TrafficManagerClient & operator=(const TrafficManagerClient &)=default
void SetGlobalLargeVehicleWideTurn(const bool enable)
Method to enable/disable the wide-turn manoeuvre globally for large vehicles.
void SetRandomDeviceSeed(const uint64_t seed)
Method to set randomization seed.
void SetSynchronousMode(const bool mode)
Method to switch traffic manager into synchronous execution.
TrafficManagerClient(const TrafficManagerClient &)=default
ActionBuffer GetActionBuffer(const ActorId &actor_id)
Method to get the vehicle's action buffer.
void RemoveUploadPath(const ActorId &actor_id, const bool remove_path)
Method to remove a list of points.
void SetPercentageRunningLight(const carla::rpc::Actor &actor, const float percentage)
Method to specify the % chance of running a traffic sign.
void RegisterVehicle(const std::vector< carla::rpc::Actor > &actor_list)
Register vehicles to remote traffic manager server via RPC client.
void UnregisterVehicle(const std::vector< carla::rpc::Actor > &actor_list)
Unregister vehicles to remote traffic manager server via RPC client.
void SetOSMMode(const bool mode_switch)
Method to set Open Street Map mode.
void getServerDetails(std::string &_host, uint16_t &_port)
Get parameters.
void SetPercentageSpeedDifference(const carla::rpc::Actor &_actor, const float percentage)
Method to set a vehicle's % decrease in velocity with respect to the speed limit.
void SetCustomPath(const carla::rpc::Actor &actor, const Path path, const bool empty_buffer)
Method to set our own imported path.
void UpdateImportedRoute(const ActorId &actor_id, const Route route)
Method to update an already set list of points.
void SetUpdateVehicleLights(const carla::rpc::Actor &_actor, const bool do_update)
Method to set the automatic management of the vehicle lights.
void SetRandomRightLaneChangePercentage(const carla::rpc::Actor &actor, const float percentage)
Method to set % to randomly do a right lane change.
void SetLargeVehicleWideTurn(const carla::rpc::Actor &_actor, const bool enable)
Method to enable/disable the wide-turn manoeuvre for a single large vehicle.
void SetPercentageIgnoreVehicles(const carla::rpc::Actor &actor, const float percentage)
Method to specify the % chance of ignoring collisions with any vehicle.
void SetForceLaneChange(const carla::rpc::Actor &actor, const bool direction)
Method to force lane change on a vehicle.
void SetDistanceToLeadingVehicle(const carla::rpc::Actor &actor, const float distance)
Method to specify how much distance a vehicle should maintain to the leading vehicle.
void SetLaneOffset(const carla::rpc::Actor &_actor, const float offset)
Method to set a lane offset displacement from the center line.
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 SetKeepSlowLanePercentage(const carla::rpc::Actor &actor, const float percentage)
Method to set % to keep on the slow lane.
void SetPercentageRunningSign(const carla::rpc::Actor &actor, const float percentage)
Method to specify the % chance of running any traffic sign.
void setServerDetails(const std::string &_host, const uint16_t &_port)
Set parameters.
void SetRandomLeftLaneChangePercentage(const carla::rpc::Actor &actor, const float percentage)
Method to set % to randomly do a left lane change.
void SetImportedRoute(const carla::rpc::Actor &actor, const Route route, const bool empty_buffer)
Method to set our own imported route.
void SetHybridPhysicsRadius(const float radius)
Method to set hybrid physics mode.
void SetGlobalDistanceToLeadingVehicle(const float distance)
Method to specify how much distance a vehicle should maintain to the Global leading vehicle.
TrafficManagerClient(const std::string &_host, const uint16_t &_port)
Parametric constructor to initialize the parameters.
void UpdateUploadPath(const ActorId &actor_id, const Path path)
Method to update an already set list of points.
void SetHybridPhysicsMode(const bool mode_switch)
Method to set hybrid physics mode.
bool SynchronousTick()
Method to provide synchronous tick.
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 SetRespawnDormantVehicles(const bool mode_switch)
Method to set automatic respawn of dormant vehicles.
void RemoveImportedRoute(const ActorId &actor_id, const bool remove_path)
Method to remove a route.
static const unsigned short TM_DEFAULT_PORT
Definition Constants.h:23
std::vector< uint8_t > Route
std::vector< cg::Location > Path
std::pair< RoadOption, WaypointPtr > Action
std::vector< Action > ActionBuffer
This file contains definitions of common data structures used in traffic manager.