CARLA
Loading...
Searching...
No Matches
TrafficManagerServer.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
9#include <vector>
10
11#include "carla/Exception.h"
12#include "carla/client/Actor.h"
14#include "carla/rpc/Server.h"
17
18namespace carla {
19namespace traffic_manager {
20
22using Path = std::vector<cg::Location>;
23using Route = std::vector<uint8_t>;
24
25using namespace constants::Networking;
26
28public:
29
32
35
36 /// Here RPCPort is the traffic manager local instance RPC server port where
37 /// it can listen to remote traffic managers and apply the changes to
38 /// local instance through a TrafficManagerBase pointer.
40 uint16_t &RPCPort,
42 : _RPCPort(RPCPort) {
43
44 uint16_t counter = 0;
45 while(counter < MIN_TRY_COUNT) {
46 try {
47
48 /// Create server instance.
49 server = new ::rpc::server(RPCPort);
50
51 } catch(std::exception) {
52 using namespace std::chrono_literals;
53 /// Update port number and try again.
54 std::this_thread::sleep_for(500ms);
55 }
56
57 /// If server created.
58 if(server != nullptr) {
59 break;
60 }
61 counter ++;
62 }
63
64 /// If server still not created throw a runtime exception.
65 if(server == nullptr) {
66
67 carla::throw_exception(std::runtime_error(
68 "trying to create rpc server for traffic manager; "
69 "but the system failed to create because of bind error."));
70 } else {
71 /// If the server creation was successful we are
72 /// binding a lambda function to the name "register_vehicle".
73 server->bind("register_vehicle", [=](std :: vector <carla::rpc::Actor> _actor_list) {
74 std::vector<ActorPtr> actor_list;
75 for (auto &&actor : _actor_list) {
76 actor_list.emplace_back(carla::client::detail::ActorVariant(actor).Get(tm->GetEpisodeProxy()));
77 }
78 tm->RegisterVehicles(actor_list);
79 });
80
81
82 /// Binding a lambda function to the name "unregister_vehicle".
83 server->bind("unregister_vehicle", [=](std :: vector <carla::rpc::Actor> _actor_list) {
84 std::vector<ActorPtr> actor_list;
85 for (auto &&actor : _actor_list) {
86 actor_list.emplace_back(carla::client::detail::ActorVariant(actor).Get(tm->GetEpisodeProxy()));
87 }
88 tm->UnregisterVehicles(actor_list);
89 });
90
91 /// Method to set a vehicle's % decrease in velocity with respect to the speed limit.
92 /// If less than 0, it's a % increase.
93 server->bind("set_percentage_speed_difference", [=](carla::rpc::Actor actor, const float percentage) {
95 });
96
97 /// Method to set a lane offset displacement from the center line.
98 /// Positive values imply a right offset while negative ones mean a left one.
99 server->bind("set_lane_offset", [=](carla::rpc::Actor actor, const float offset) {
101 });
102
103 /// Set a vehicle's exact desired velocity.
104 server->bind("set_desired_speed", [=](carla::rpc::Actor actor, const float value) {
106 });
107
108 /// Method to set the automatic management of the vehicle lights
109 server->bind("update_vehicle_lights", [=](carla::rpc::Actor actor, const bool do_update) {
111 });
112
113 /// Method to set a global % decrease in velocity with respect to the speed limit.
114 /// If less than 0, it's a % increase.
115 server->bind("set_global_percentage_speed_difference", [=](const float percentage) {
117 });
118
119 /// Method to set a global lane offset displacement from the center line.
120 /// Positive values imply a right offset while negative ones mean a left one.
121 server->bind("set_global_lane_offset", [=](const float offset) {
122 tm->SetGlobalLaneOffset(offset);
123 });
124
125 /// Method to enable/disable the wide-turn manoeuvre for a single large vehicle.
126 server->bind("set_large_vehicle_wide_turn", [=](carla::rpc::Actor actor, const bool enable) {
128 });
129
130 /// Method to enable/disable the wide-turn manoeuvre globally for large vehicles.
131 server->bind("set_global_large_vehicle_wide_turn", [=](const bool enable) {
133 });
134
135
136 /// Method to set collision detection rules between vehicles.
137 server->bind("set_collision_detection", [=](const carla::rpc::Actor &reference_actor, const carla::rpc::Actor &other_actor, const bool detect_collision) {
138 const auto reference = carla::client::detail::ActorVariant(reference_actor).Get(tm->GetEpisodeProxy());
139 const auto other = carla::client::detail::ActorVariant(other_actor).Get(tm->GetEpisodeProxy());
140 tm->SetCollisionDetection(reference, other, detect_collision);
141 });
142
143 /// Method to force lane change on a vehicle.
144 /// Direction flag can be set to true for left and false for right.
145 server->bind("set_force_lane_change", [=](carla::rpc::Actor actor, const bool direction) {
147 });
148
149 /// Enable/disable automatic lane change on a vehicle.
150 server->bind("set_auto_lane_change", [=](carla::rpc::Actor actor, const bool enable) {
152 });
153
154 /// Method to specify how much distance a vehicle should maintain to
155 /// the leading vehicle.
156 server->bind("set_distance_to_leading_vehicle", [=](carla::rpc::Actor actor, const float distance) {
158 });
159
160 /// Method to the Global Distance to Leading vehicle
161
162 server->bind("set_global_distance_to_leading_vehicle", [=]( const float distance) {
164 });
165
166 /// Method to specify the % chance of running any traffic light.
167 server->bind("set_percentage_running_light", [=](carla::rpc::Actor actor, const float percentage) {
169 });
170
171 /// Method to specify the % chance of running any traffic sign.
172 server->bind("set_percentage_running_sign", [=](carla::rpc::Actor actor, const float percentage) {
174 });
175
176 /// Method to specify the % chance of ignoring collisions with any walker.
177 server->bind("set_percentage_ignore_walkers", [=](carla::rpc::Actor actor, const float percentage) {
179 });
180
181 /// Method to specify the % chance of ignoring collisions with any vehicle.
182 server->bind("set_percentage_ignore_vehicles", [=](carla::rpc::Actor actor, const float percentage) {
184 });
185
186 /// Method to set % to keep on the right lane.
187 server->bind("set_percentage_keep_slow_lane_rule", [=](carla::rpc::Actor actor, const float percentage) {
189 });
190
191
192 /// Method to set % to randomly do a left lane change.
193 server->bind("set_percentage_random_left_lanechange", [=](carla::rpc::Actor actor, const float percentage) {
195 });
196
197 /// Method to set % to randomly do a right lane change.
198 server->bind("set_percentage_random_right_lanechange", [=](carla::rpc::Actor actor, const float percentage) {
200 });
201
202 /// Method to set hybrid physics mode.
203 server->bind("set_hybrid_physics_mode", [=](const bool mode_switch) {
204 tm->SetHybridPhysicsMode(mode_switch);
205 });
206
207 /// Method to set hybrid physics radius.
208 server->bind("set_hybrid_physics_radius", [=](const float radius) {
209 tm->SetHybridPhysicsRadius(radius);
210 });
211
212 /// Method to set hybrid physics radius.
213 server->bind("set_osm_mode", [=](const bool mode_switch) {
214 tm->SetOSMMode(mode_switch);
215 });
216
217 /// Method to set our own imported path.
218 server->bind("set_path", [=](carla::rpc::Actor actor, const Path path, const bool empty_buffer) {
219 tm->SetCustomPath(carla::client::detail::ActorVariant(actor).Get(tm->GetEpisodeProxy()), path, empty_buffer);
220 });
221
222 /// Method to remove a list of points.
223 server->bind("remove_custom_path", [=](const ActorId actor_id, const bool remove_path) {
224 tm->RemoveUploadPath(actor_id, remove_path);
225 });
226
227 /// Method to update an already set list of points.
228 server->bind("update_custom_path", [=](const ActorId actor_id, const Path path) {
229 tm->UpdateUploadPath(actor_id, path);
230 });
231
232 /// Method to set our own imported route.
233 server->bind("set_imported_route", [=](carla::rpc::Actor actor, const Route route, const bool empty_buffer) {
234 tm->SetImportedRoute(carla::client::detail::ActorVariant(actor).Get(tm->GetEpisodeProxy()), route, empty_buffer);
235 });
236
237 /// Method to remove a route.
238 server->bind("remove_imported_route", [=](const ActorId actor_id, const bool remove_path) {
239 tm->RemoveImportedRoute(actor_id, remove_path);
240 });
241
242 /// Method to update an already set list of points.
243 server->bind("update_imported_route", [=](const ActorId actor_id, const Route route) {
244 tm->UpdateImportedRoute(actor_id, route);
245 });
246
247 /// Method to set respawn dormant vehicles mode.
248 server->bind("set_respawn_dormant_vehicles", [=](const bool mode_switch) {
249 tm->SetRespawnDormantVehicles(mode_switch);
250 });
251
252 /// Method to set respawn dormant vehicles mode.
253 server->bind("set_boundaries_respawn_dormant_vehicles", [=](const float lower_bound, const float upper_bound) {
254 tm->SetBoundariesRespawnDormantVehicles(lower_bound, upper_bound);
255 });
256
257 /// Method to get the vehicle's next action.
258 server->bind("get_next_action", [=](const ActorId actor_id) {
259 tm->GetNextAction(actor_id);
260 });
261
262 /// Method to get the vehicle's action buffer.
263 server->bind("get_all_actions", [=](const ActorId actor_id) {
264 tm->GetActionBuffer(actor_id);
265 });
266
267 server->bind("shut_down", [=]() {
268 tm->Release();
269 });
270
271 /// Method to set synchronous mode.
272 server->bind("set_synchronous_mode", [=](const bool mode) {
273 tm->SetSynchronousMode(mode);
274 });
275
276 /// Method to set tick timeout for synchronous execution.
277 server->bind("set_synchronous_mode_timeout_in_milisecond", [=](const double time) {
279 });
280
281 /// Method to set randomization seed.
282 server->bind("set_random_device_seed", [=](const uint64_t seed) {
283 tm->SetRandomDeviceSeed(seed);
284 });
285
286 /// Method to provide synchronous tick.
287 server->bind("synchronous_tick", [=]() -> bool {
288 return tm->SynchronousTick();
289 });
290
291 /// Method to check server is alive or not.
292 server->bind("health_check_remote_TM", [=](){});
293
294 /// Run traffic manager server to respond of any
295 /// user client in asynchronous mode.
296 server->async_run();
297 }
298
299 }
300
302 if(server) {
303 server->stop();
304 delete server;
305 server = nullptr;
306 }
307 }
308
309 uint16_t port() const {
310 return _RPCPort;
311 }
312
313private:
314
315 /// Traffic manager server RPC port
316 uint16_t _RPCPort;
317
318 /// Server instance
319 ::rpc::server *server = nullptr;
320
321};
322
323} // namespace traffic_manager
324} // namespace carla
float distance
Holds an Actor, but only instantiates it when needed.
SharedPtr< client::Actor > Get(EpisodeProxy episode) const
The function of this class is to integrate all the various stages of the traffic manager appropriatel...
virtual void SetKeepSlowLanePercentage(const ActorPtr &actor, const float percentage)=0
Method to set % to keep on the slow lane.
virtual void SetRandomDeviceSeed(const uint64_t seed)=0
Method to set randomization seed.
virtual void SetRandomLeftLaneChangePercentage(const ActorPtr &actor, const float percentage)=0
Method to set % to randomly do a left lane change.
virtual void SetBoundariesRespawnDormantVehicles(const float lower_bound, const float upper_bound)=0
Method to set boundaries for respawning vehicles.
virtual void SetPercentageIgnoreWalkers(const ActorPtr &actor, const float perc)=0
Method to specify the % chance of ignoring collisions with any walker.
virtual void SetPercentageRunningSign(const ActorPtr &actor, const float perc)=0
Method to specify the % chance of running any traffic sign.
virtual void SetPercentageSpeedDifference(const ActorPtr &actor, const float percentage)=0
Set a vehicle's % decrease in velocity with respect to the speed limit.
virtual carla::client::detail::EpisodeProxy & GetEpisodeProxy()=0
Get carla episode information.
virtual void Release()=0
To release the traffic manager.
virtual void UpdateImportedRoute(const ActorId &actor_id, const Route route)=0
Method to update an already set route.
virtual bool SynchronousTick()=0
Method to provide synchronous tick.
virtual void SetGlobalLaneOffset(float const offset)=0
Method to set a global lane offset displacement from the center line.
virtual void SetAutoLaneChange(const ActorPtr &actor, const bool enable)=0
Enable/disable automatic lane change on a vehicle.
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 RegisterVehicles(const std::vector< ActorPtr > &actor_list)=0
This method registers a vehicle with the traffic manager.
virtual void SetOSMMode(const bool mode_switch)=0
Method to set Open Street Map mode.
virtual void SetPercentageIgnoreVehicles(const ActorPtr &actor, const float perc)=0
Method to specify the % chance of ignoring collisions with any vehicle.
virtual Action GetNextAction(const ActorId &actor_id)=0
Method to get the vehicle's next action.
virtual void SetHybridPhysicsRadius(const float radius)=0
Method to set hybrid physics radius.
virtual void SetForceLaneChange(const ActorPtr &actor, const bool direction)=0
Method to force lane change on a vehicle.
virtual void SetSynchronousMode(bool mode)=0
Method to switch traffic manager into synchronous execution.
virtual ActionBuffer GetActionBuffer(const ActorId &actor_id)=0
Method to get the vehicle's action buffer.
virtual void SetLaneOffset(const ActorPtr &actor, const float offset)=0
Method to set a lane offset displacement from the center line.
virtual void SetRandomRightLaneChangePercentage(const ActorPtr &actor, const float percentage)=0
Method to set % to randomly do a right lane change.
virtual void SetSynchronousModeTimeOutInMiliSecond(double time)=0
Method to set Tick timeout for synchronous execution.
virtual void SetLargeVehicleWideTurn(const ActorPtr &actor, const bool enable)=0
Method to enable/disable the wide-turn manoeuvre for a single large vehicle.
virtual void SetRespawnDormantVehicles(const bool mode_switch)=0
Method to set automatic respawn of dormant vehicles.
virtual void RemoveImportedRoute(const ActorId &actor_id, const bool remove_path)=0
Method to remove a route.
virtual void SetHybridPhysicsMode(const bool mode_switch)=0
Method to set hybrid physics mode.
virtual void SetCustomPath(const ActorPtr &actor, const Path path, const bool empty_buffer)=0
Method to set our own imported path.
virtual void RemoveUploadPath(const ActorId &actor_id, const bool remove_path)=0
Method to remove a path.
virtual void SetDesiredSpeed(const ActorPtr &actor, const float value)=0
Set a vehicle's exact desired velocity.
virtual void SetGlobalLargeVehicleWideTurn(const bool enable)=0
Method to enable/disable the wide-turn manoeuvre globally for large vehicles.
virtual void SetUpdateVehicleLights(const ActorPtr &actor, const bool do_update)=0
Method to set the automatic management of the vehicle lights.
virtual void SetPercentageRunningLight(const ActorPtr &actor, const float perc)=0
Method to specify the % chance of running any traffic light.
virtual void UnregisterVehicles(const std::vector< ActorPtr > &actor_list)=0
This method unregisters a vehicle from traffic manager.
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 SetGlobalDistanceToLeadingVehicle(const float dist)=0
Method to set Global Distance to Leading Vehicle.
virtual void UpdateUploadPath(const ActorId &actor_id, const Path path)=0
Method to update an already set path.
virtual void SetImportedRoute(const ActorPtr &actor, const Route route, const bool empty_buffer)=0
Method to set our own imported route.
virtual void SetGlobalPercentageSpeedDifference(float const percentage)=0
Set a global % decrease in velocity with respect to the speed limit.
uint16_t _RPCPort
Traffic manager server RPC port.
TrafficManagerServer & operator=(TrafficManagerServer &&)=default
TrafficManagerServer & operator=(const TrafficManagerServer &)=default
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...
TrafficManagerServer(const TrafficManagerServer &)=default
TrafficManagerServer(TrafficManagerServer &&)=default
std::vector< uint8_t > Route
std::vector< cg::Location > Path
carla::SharedPtr< cc::Actor > ActorPtr
This file contains definitions of common data structures used in traffic manager.
std::shared_ptr< T > SharedPtr
Definition Memory.h:15
void throw_exception(const ExceptionT &e)
Templated on the static type so the thrown object keeps its dynamic type: taking a const std::excepti...
Definition Exception.h:44