CARLA
TrafficManagerRemote.cpp
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 
8 
10 
11 namespace carla {
12 namespace traffic_manager {
13 
15  const std::pair<std::string, uint16_t> &_serverTM,
17  : client(_serverTM.first, _serverTM.second),
18  episodeProxyTM(episodeProxy) {
19 
20  Start();
21 
22 }
23 
24 /// Destructor.
26  Release();
27 }
28 
30  _keep_alive = true;
31 
32  std::thread _thread = std::thread([this] () {
33  std::chrono::milliseconds wait_time(TM_TIMEOUT);
34  try {
35  do {
36  std::this_thread::sleep_for(wait_time);
37 
39 
40  /// Until connection active
41  } while (_keep_alive);
42  } catch (...) {
43 
44  std::string rhost("");
45  uint16_t rport = 0;
46 
47  client.getServerDetails(rhost, rport);
48 
49  std::string strtmserver(rhost + ":" + std::to_string(rport));
50 
51  /// Create error msg
52  std::string errmsg(
53  "Trying to connect rpc server of traffic manager; "
54  "but the system failed to connect at " + strtmserver);
55 
56  /// TSet the error message
57  if(_keep_alive) {
58  this->episodeProxyTM.Lock()->AddPendingException(errmsg);
59  }
60  }
61  _keep_alive = false;
62  _cv.notify_one();
63  });
64 
65  _thread.detach();
66 }
67 
69  if(_keep_alive) {
70  _keep_alive = false;
71  std::unique_lock<std::mutex> lock(_mutex);
72  std::chrono::milliseconds wait_time(TM_TIMEOUT + 1000);
73  _cv.wait_for(lock, wait_time);
74  }
75 }
76 
78  Stop();
79 }
80 
82  Stop();
83 
84  carla::client::detail::EpisodeProxy episode_proxy = episodeProxyTM.Lock()->GetCurrentEpisode();
85  episodeProxyTM = episode_proxy;
86 
87  Start();
88 }
89 
90 void TrafficManagerRemote::RegisterVehicles(const std::vector<ActorPtr> &_actor_list) {
91  std::vector<carla::rpc::Actor> actor_list;
92  for (auto &&actor : _actor_list) {
93  actor_list.emplace_back(actor->Serialize());
94  }
95  client.RegisterVehicle(actor_list);
96 }
97 
98 void TrafficManagerRemote::UnregisterVehicles(const std::vector<ActorPtr> &_actor_list) {
99  std::vector<carla::rpc::Actor> actor_list;
100  for (auto &&actor : _actor_list) {
101  actor_list.emplace_back(actor->Serialize());
102  }
103  client.UnregisterVehicle(actor_list);
104 }
105 
106 void TrafficManagerRemote::SetPercentageSpeedDifference(const ActorPtr &_actor, const float percentage) {
107  carla::rpc::Actor actor(_actor->Serialize());
108 
109  client.SetPercentageSpeedDifference(actor, percentage);
110 }
111 
114 }
115 
116 void TrafficManagerRemote::SetCollisionDetection(const ActorPtr &_reference_actor, const ActorPtr &_other_actor, const bool detect_collision) {
117  carla::rpc::Actor reference_actor(_reference_actor->Serialize());
118  carla::rpc::Actor other_actor(_other_actor->Serialize());
119 
120  client.SetCollisionDetection(reference_actor, other_actor, detect_collision);
121 }
122 
123 void TrafficManagerRemote::SetForceLaneChange(const ActorPtr &_actor, const bool direction) {
124  carla::rpc::Actor actor(_actor->Serialize());
125 
126  client.SetForceLaneChange(actor, direction);
127 }
128 
129 void TrafficManagerRemote::SetAutoLaneChange(const ActorPtr &_actor, const bool enable) {
130  carla::rpc::Actor actor(_actor->Serialize());
131 
132  client.SetAutoLaneChange(actor, enable);
133 }
134 
135 void TrafficManagerRemote::SetDistanceToLeadingVehicle(const ActorPtr &_actor, const float distance) {
136  carla::rpc::Actor actor(_actor->Serialize());
137 
138  client.SetDistanceToLeadingVehicle(actor, distance);
139 }
140 
143 }
144 
145 
146 void TrafficManagerRemote::SetPercentageIgnoreWalkers(const ActorPtr &_actor, const float percentage) {
147  carla::rpc::Actor actor(_actor->Serialize());
148 
149  client.SetPercentageIgnoreWalkers(actor, percentage);
150 }
151 
152 void TrafficManagerRemote::SetPercentageIgnoreVehicles(const ActorPtr &_actor, const float percentage) {
153  carla::rpc::Actor actor(_actor->Serialize());
154 
155  client.SetPercentageIgnoreVehicles(actor, percentage);
156 }
157 
158 void TrafficManagerRemote::SetPercentageRunningLight(const ActorPtr &_actor, const float percentage) {
159  carla::rpc::Actor actor(_actor->Serialize());
160 
161  client.SetPercentageRunningLight(actor, percentage);
162 }
163 
164 void TrafficManagerRemote::SetPercentageRunningSign(const ActorPtr &_actor, const float percentage) {
165  carla::rpc::Actor actor(_actor->Serialize());
166 
167  client.SetPercentageRunningSign(actor, percentage);
168 }
169 
170 void TrafficManagerRemote::SetKeepRightPercentage(const ActorPtr &_actor, const float percentage) {
171  carla::rpc::Actor actor(_actor->Serialize());
172 
173  client.SetKeepRightPercentage(actor, percentage);
174 }
175 
176 void TrafficManagerRemote::SetHybridPhysicsMode(const bool mode_switch) {
177  client.SetHybridPhysicsMode(mode_switch);
178 }
179 
182 }
183 
184 void TrafficManagerRemote::SetOSMMode(const bool mode_switch) {
185  client.SetOSMMode(mode_switch);
186 }
187 
190 }
191 
194 }
195 
197  return false;
198 }
199 
202 }
203 
205  return episodeProxyTM;
206 }
207 
208 void TrafficManagerRemote::SetRandomDeviceSeed(const uint64_t seed) {
210 }
211 
212 } // namespace traffic_manager
213 } // namespace carla
TrafficManagerClient client
Remote client using the IP and port information it connects to as remote RPC traffic manager server...
void SetForceLaneChange(const ActorPtr &actor, const bool direction)
Method to force lane change on a vehicle.
void SetPercentageIgnoreVehicles(const ActorPtr &actor, const float perc)
Method to specify the % chance of ignoring collisions with any vehicle.
void SetAutoLaneChange(const ActorPtr &actor, const bool enable)
Enable/disable automatic lane change on a vehicle.
void SetPercentageRunningSign(const ActorPtr &actor, const float perc)
Method to specify the % chance of running any traffic sign.
void SetHybridPhysicsMode(const bool mode_switch)
Method to set hybrid physics mode.
carla::client::detail::EpisodeProxy episodeProxyTM
CARLA client connection object.
void SetSynchronousModeTimeOutInMiliSecond(double time)
Method to set Tick timeout for synchronous execution.
void SetForceLaneChange(const carla::rpc::Actor &actor, const bool direction)
Method to force lane change on a vehicle.
void SetGlobalDistanceToLeadingVehicle(const float distance)
Method to specify Global Distance.
void SetOSMMode(const bool mode_switch)
Method to set Open Street Map mode.
void Reset()
To reset the traffic manager.
carla::SharedPtr< cc::Actor > ActorPtr
void SetHybridPhysicsRadius(const float radius)
Method to set hybrid physics radius.
void SetSynchronousModeTimeOutInMiliSecond(const double time)
Method to set tick timeout for synchronous execution.
void UnregisterVehicles(const std::vector< ActorPtr > &actor_list)
This method unregisters a vehicle from traffic manager.
void SetAutoLaneChange(const carla::rpc::Actor &actor, const bool enable)
Enable/disable automatic lane change on a vehicle.
void SetSynchronousMode(bool mode)
Method to switch traffic manager into synchronous execution.
void SetKeepRightPercentage(const carla::rpc::Actor &actor, const float percentage)
Method to set probabilistic preference to keep on the right lane.
bool SynchronousTick()
Method to provide synchronous tick.
void SetCollisionDetection(const ActorPtr &reference_actor, const ActorPtr &other_actor, const bool detect_collision)
Method to set collision detection rules between vehicles.
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 RegisterVehicles(const std::vector< ActorPtr > &actor_list)
This method registers a vehicle with the traffic manager.
void SetPercentageIgnoreWalkers(const ActorPtr &actor, const float perc)
Method to specify the % chance of ignoring collisions with any walker.
void SetPercentageRunningLight(const ActorPtr &actor, const float perc)
Method to specify the % chance of running any traffic light.
void SetSynchronousMode(const bool mode)
Method to switch traffic manager into synchronous execution.
void SetGlobalPercentageSpeedDifference(float const percentage)
Method to set a global % decrease in velocity with respect to the speed limit.
void SetRandomDeviceSeed(const uint64_t seed)
Method to set randomization seed.
void SetPercentageSpeedDifference(const ActorPtr &actor, const float percentage)
Method to set a vehicle&#39;s % decrease in velocity with respect to the speed limit. ...
void SetHybridPhysicsMode(const bool mode_switch)
Method to set hybrid physics mode.
void Start()
To start the TrafficManager.
void SetHybridPhysicsRadius(const float radius)
Method to set hybrid physics mode.
void HealthCheckRemoteTM()
Check if remote traffic manager is alive.
void SetOSMMode(const bool mode_switch)
Method to set Open Street Map mode.
void SetPercentageRunningLight(const carla::rpc::Actor &actor, const float percentage)
Method to specify the % chance of running a traffic sign.
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.
TrafficManagerRemote(const std::pair< std::string, uint16_t > &_serverTM, carla::client::detail::EpisodeProxy &episodeProxy)
Constructor store remote location information.
void SetGlobalDistanceToLeadingVehicle(const float distance)
Method to specify how much distance a vehicle should maintain to the Global leading vehicle...
void SetDistanceToLeadingVehicle(const ActorPtr &actor, const float distance)
Method to specify how much distance a vehicle should maintain to the 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 Release()
To release the traffic manager.
void SetPercentageRunningSign(const carla::rpc::Actor &actor, const float percentage)
Method to specify the % chance of running any traffic sign.
SharedPtrType Lock() const
Same as TryLock but never return nullptr.
void UnregisterVehicle(const std::vector< carla::rpc::Actor > &actor_list)
Unregister vehicles to remote traffic manager server via RPC client.
void SetKeepRightPercentage(const ActorPtr &actor, const float percentage)
Method to set probabilistic preference to keep on the right lane.
void SetGlobalPercentageSpeedDifference(const float percentage)
Method to set a global % decrease in velocity with respect to the speed limit.
void HealthCheckRemoteTM()
Method to check server is alive or not.
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 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 SetRandomDeviceSeed(const uint64_t seed)
Method to set randomization seed.
carla::client::detail::EpisodeProxy & GetEpisodeProxy()
Get CARLA episode information.