CARLA
 
载入中...
搜索中...
未找到
TrafficManagerRemote.h
浏览该文件的文档.
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
9#include <condition_variable>/// @brief 引入条件变量类,用于线程间的同步
10#include <mutex>/// @brief 引入互斥锁类,用于保护共享数据的访问
11#include <vector>/// @brief 引入动态数组类,用于储存多个元素
12
13#include "carla/client/Actor.h"/// @brief CARLA客户端Actor类的头文件,包含了Actor(参与者)的定义和接口
14#include "carla/client/detail/EpisodeProxy.h"/// @brief CARLA客户端EpisodeProxy类的头文件,包含了与仿真片段(Episode)相关的操作和接口
15#include "carla/trafficmanager/TrafficManagerBase.h"/// @brief CARLA交通管理器基础类的头文件,提供了交通管理器的基本功能和接口
16#include "carla/trafficmanager/TrafficManagerClient.h"/// @brief CARLA交通管理器客户端类的头文件,用于与交通管理器进行通信和控制
17/**
18 * @namespace carla::traffic_manager
19 * @brief CARLA仿真环境中的交通管理器命名空间。
20 *
21 * 该命名空间包含了与交通管理相关的类、函数和类型定义,用于实现交通流的控制和管理。
22 */
23namespace carla {
24namespace traffic_manager {
25 /**
26 * @brief 定义一个智能指针类型,指向carla::client::Actor类。
27 *
28 * 用于在交通管理器中方便地管理Actor(如车辆、行人等)的引用。
29 */
31
32/**
33 * @brief 定义一个路径类型,由多个cg::Location组成。
34 *
35 * 用于表示一个Actor在地图上的移动路径。
36 */
37using Path = std::vector<cg::Location>;
38
39/**
40 * @brief 定义一个路线类型,由多个uint8_t组成。
41 *
42 * 用于表示一个Actor在交通网络中的路线信息。
43 */
44using Route = std::vector<uint8_t>;
45
46/**
47 * @class TrafficManagerRemote
48 * @brief 远程交通管理器类,通过消息传递机制整合交通管理的各个阶段。
49 *
50 * 该类继承自TrafficManagerBase,提供了远程交通管理的功能,包括启动、停止、重置以及车辆的注册、注销和速度控制等。
51 */
53
54public:
55
56 /**
57 * @brief 启动交通管理器。
58 *
59 * 初始化并启动交通管理器的各个组件,准备进行交通管理。
60 */
61 void Start();
62
63 /**
64 * @brief 停止交通管理器。
65 *
66 * 停止交通管理器的运行,释放相关资源。
67 */
68 void Stop();
69
70 /**
71 * @brief 释放交通管理器。
72 *
73 * 彻底释放交通管理器占用的资源,确保不再使用。
74 */
75 void Release();
76
77 /**
78 * @brief 重置交通管理器。
79 *
80 * 将交通管理器重置到初始状态,准备进行新的交通管理任务。
81 */
82 void Reset();
83
84 /**
85 * @brief 构造函数,存储远程服务器的位置信息。
86 *
87 * @param _serverTM 远程交通管理器的地址和端口号。
88 * @param episodeProxy 与当前仿真片段相关联的代理对象。
89 */
90 TrafficManagerRemote(const std::pair<std::string, uint16_t> &_serverTM, carla::client::detail::EpisodeProxy &episodeProxy);
91
92 /**
93 * @brief 析构函数。
94 *
95 * 释放TrafficManagerRemote对象占用的资源。
96 */
97 virtual ~TrafficManagerRemote();
98
99 /**
100 * @brief 注册车辆到交通管理器。
101 *
102 * @param actor_list 需要注册的车辆列表。
103 */
104 void RegisterVehicles(const std::vector<ActorPtr> &actor_list);
105
106 /**
107 * @brief 从交通管理器注销车辆。
108 *
109 * @param actor_list 需要注销的车辆列表。
110 */
111 void UnregisterVehicles(const std::vector<ActorPtr> &actor_list);
112
113 /**
114 * @brief 设置车辆相对于限速的速度差异百分比。
115 *
116 * @param actor 需要设置速度差异的车辆。
117 * @param percentage 速度差异的百分比。如果小于0,则表示速度增加。
118 */
119 void SetPercentageSpeedDifference(const ActorPtr &actor, const float percentage);
120
121 /**
122 * @brief 设置车辆相对于车道中心线的偏移量。
123 *
124 * @param actor 需要设置车道偏移的车辆。
125 * @param offset 车道偏移量。正值表示向右偏移,负值表示向左偏移。
126 */
127 void SetLaneOffset(const ActorPtr &actor, const float offset);
128
129 /**
130 * @brief 设置车辆的精确期望速度。
131 *
132 * @param actor 需要设置期望速度的车辆。
133 * @param value 车辆的期望速度。
134 */
135 void SetDesiredSpeed(const ActorPtr &actor, const float value);
136
137 /**
138 * @brief 设置全局速度相对于限速的百分比减少量。
139 *
140 * 如果传入的值小于0,则表示速度百分比增加。
141 *
142 * @param percentage 速度变化的百分比值。
143 */
144 void SetGlobalPercentageSpeedDifference(float const percentage);
145
146 /**
147 * @brief 设置全局车道偏移量,相对于车道中心线。
148 *
149 * 正值表示向右偏移,负值表示向左偏移。
150 *
151 * @param offset 车道偏移量的具体值。
152 */
153 void SetGlobalLaneOffset(float const offset);
154
155 /**
156 * @brief 设置车辆灯光的自动管理功能。
157 *
158 * @param actor 需要设置灯光自动管理的车辆对象。
159 * @param do_update 是否启用灯光自动管理功能。
160 */
161 void SetUpdateVehicleLights(const ActorPtr &actor, const bool do_update);
162
163 /**
164 * @brief 设置车辆之间的碰撞检测规则。
165 *
166 * @param reference_actor 参考车辆对象,即碰撞检测的一方。
167 * @param other_actor 另一车辆对象,即与参考车辆进行碰撞检测的对象。
168 * @param detect_collision 是否启用碰撞检测功能。
169 */
170 void SetCollisionDetection(const ActorPtr &reference_actor, const ActorPtr &other_actor, const bool detect_collision);
171
172 /**
173 * @brief 强制车辆进行换道。
174 *
175 * 方向标志设置为true表示向左换道,设置为false表示向右换道。
176 *
177 * @param actor 需要进行换道的车辆对象。
178 * @param direction 换道方向标志。
179 */
180 void SetForceLaneChange(const ActorPtr &actor, const bool direction);
181
182 /**
183 * @brief 启用/禁用车辆的自动换道功能。
184 *
185 * @param actor 需要设置自动换道功能的车辆对象。
186 * @param enable 是否启用自动换道功能。
187 */
188 void SetAutoLaneChange(const ActorPtr &actor, const bool enable);
189
190 /**
191 * @brief 设置车辆与前车应保持的距离。
192 *
193 * @param actor 需要设置距离的车辆对象。
194 * @param distance 车辆与前车应保持的具体距离值。
195 */
196 void SetDistanceToLeadingVehicle(const ActorPtr &actor, const float distance);
197
198 /**
199 * @brief 设置全局前车距离。
200 *
201 * @param distance 全局前车距离的具体值。
202 */
203 void SetGlobalDistanceToLeadingVehicle(const float distance);
204
205 /**
206 * @brief 设置车辆忽略行人碰撞的百分比概率。
207 *
208 * @param actor 需要设置碰撞忽略概率的车辆对象。
209 * @param perc 忽略行人碰撞的百分比概率值。
210 */
211 void SetPercentageIgnoreWalkers(const ActorPtr &actor, const float perc);
212
213 /**
214 * @brief 设置车辆忽略其他车辆碰撞的百分比概率。
215 *
216 * @param actor 需要设置碰撞忽略概率的车辆对象。
217 * @param perc 忽略其他车辆碰撞的百分比概率值。
218 */
219 void SetPercentageIgnoreVehicles(const ActorPtr &actor, const float perc);
220
221 /**
222 * @brief 设置车辆无视交通信号灯的概率百分比。
223 *
224 * @param actor 需要设置无视信号灯概率的车辆对象。
225 * @param perc 无视交通信号灯的概率百分比值。
226 */
227 void SetPercentageRunningLight(const ActorPtr &actor, const float perc);
228
229 /**
230 * @brief 设置车辆无视交通标志的概率百分比。
231 *
232 * @param actor 需要设置无视交通标志概率的车辆对象。
233 * @param perc 无视交通标志的概率百分比值。
234 */
235 void SetPercentageRunningSign(const ActorPtr &actor, const float perc);
236
237 /**
238 * @brief 切换交通管理器为同步执行模式。
239 *
240 * @param mode 是否启用同步执行模式。
241 */
242 void SetSynchronousMode(bool mode);
243
244 /**
245 * @brief 设置同步执行模式的Tick超时时间(毫秒)。
246 *
247 * @param time 同步执行模式的Tick超时时间值(毫秒)。
248 */
250
251 /**
252 * @brief 设置车辆保持在右侧车道的百分比概率。
253 *
254 * @param actor 需要设置保持在右侧车道概率的车辆对象。
255 * @param percentage 保持在右侧车道的百分比概率值。
256 */
257 void SetKeepRightPercentage(const ActorPtr &actor, const float percentage);
258
259 /**
260 * @brief 设置车辆随机进行左车道变换的百分比概率。
261 *
262 * @param actor 需要设置随机左车道变换概率的车辆对象。
263 * @param percentage 随机进行左车道变换的百分比概率值。
264 */
265 void SetRandomLeftLaneChangePercentage(const ActorPtr &actor, const float percentage);
266
267 /**
268 * @brief 设置车辆随机进行右车道变换的百分比概率。
269 *
270 * @param actor 需要设置随机右车道变换概率的车辆对象。
271 * @param percentage 随机进行右车道变换的百分比概率值。
272 */
273 void SetRandomRightLaneChangePercentage(const ActorPtr &actor, const float percentage);
274
275 /**
276 * @brief 设置混合物理模式。
277 *
278 * @param mode_switch 是否启用混合物理模式。
279 */
280 void SetHybridPhysicsMode(const bool mode_switch);
281
282 /**
283 * @brief 设置混合物理模式的半径。
284 *
285 * @param radius 混合物理模式的半径值。
286 */
287 void SetHybridPhysicsRadius(const float radius);
288
289 /**
290 * @brief 设置Open Street Map(OSM)模式。
291 *
292 * @param mode_switch 是否启用OSM模式。
293 */
294 void SetOSMMode(const bool mode_switch);
295
296 /**
297 * @brief 设置自定义路径。
298 *
299 * @param actor 需要设置自定义路径的车辆对象。
300 * @param path 自定义路径对象。
301 * @param empty_buffer 是否清空动作缓冲区。
302 */
303 void SetCustomPath(const ActorPtr &actor, const Path path, const bool empty_buffer);
304
305 /**
306 * @brief 移除路径。
307 *
308 * @param actor_id 需要移除路径的车辆ID。
309 * @param remove_path 是否移除路径。
310 */
311 void RemoveUploadPath(const ActorId &actor_id, const bool remove_path);
312
313 /**
314 * @brief 更新已设置的路径。
315 *
316 * @param actor_id 需要更新路径的车辆ID。
317 * @param path 新的路径对象。
318 */
319 void UpdateUploadPath(const ActorId &actor_id, const Path path);
320
321 /**
322 * @brief 设置自定义路线。
323 *
324 * @param actor 需要设置自定义路线的车辆对象。
325 * @param route 自定义路线对象。
326 * @param empty_buffer 是否清空动作缓冲区。
327 */
328 void SetImportedRoute(const ActorPtr &actor, const Route route, const bool empty_buffer);
329
330 /**
331 * @brief 移除路线。
332 *
333 * @param actor_id 需要移除路线的车辆ID。
334 * @param remove_path 是否移除路线。
335 */
336 void RemoveImportedRoute(const ActorId &actor_id, const bool remove_path);
337
338 /**
339 * @brief 更新已设置的路线。
340 *
341 * @param actor_id 需要更新路线的车辆ID。
342 * @param route 新的路线对象。
343 */
344 void UpdateImportedRoute(const ActorId &actor_id, const Route route);
345
346 /**
347 * @brief 设置自动重生休眠车辆的模式。
348 *
349 * @param mode_switch 是否启用自动重生休眠车辆模式。
350 */
351 void SetRespawnDormantVehicles(const bool mode_switch);
352
353 /**
354 * @brief 设置休眠车辆重生的边界。
355 *
356 * @param lower_bound 重生的下边界值。
357 * @param upper_bound 重生的上边界值。
358 */
359 void SetBoundariesRespawnDormantVehicles(const float lower_bound, const float upper_bound);
360
361 /**
362 * @brief 设置最大的重生边界。
363 *
364 * @param lower 最小的重生边界值。
365 * @param upper 最大的重生边界值。
366 */
367 void SetMaxBoundaries(const float lower, const float upper);
368 /**
369 * @brief 关闭交通管理器。
370 */
371 virtual void ShutDown();
372
373 /**
374 * @brief 获取车辆的下一个动作。
375 *
376 * @param actor_id 需要获取动作的车辆ID。
377 * @return 车辆的下一个动作。
378 */
379 Action GetNextAction(const ActorId &actor_id);
380
381 /**
382 * @brief 获取车辆的动作缓冲区。
383 *
384 * @param actor_id 需要获取动作缓冲区的车辆ID。
385 * @return 车辆的动作缓冲区。
386 */
387 ActionBuffer GetActionBuffer(const ActorId &actor_id);
388
389 /**
390 * @brief 提供同步Tick。
391 *
392 * @return 是否成功执行同步Tick。
393 */
394 bool SynchronousTick();
395
396 /**
397 * @brief 获取CARLA回合信息。
398 *
399 * @return CARLA回合信息的代理对象。
400 */
402
403 /**
404 * @brief 检查服务器是否存活。
405 */
406 void HealthCheckRemoteTM();
407
408 /**
409 * @brief 设置随机化种子。
410 *
411 * @param seed 随机化种子值。
412 */
413 void SetRandomDeviceSeed(const uint64_t seed);
414
415private:
416
417 /**
418 * @brief 远程客户端,使用IP和端口信息连接到远程RPC交通管理器服务器。
419 */
421
422 /**
423 * @brief CARLA客户端连接对象。
424 */
426 /**
427 * @brief 条件变量,用于线程同步。
428 */
429 std::condition_variable _cv;
430 /**
431 * @brief 互斥锁,用于保护共享资源。
432 */
433 std::mutex _mutex;
434 /**
435 * @brief 保持活动状态标志。
436 */
437 bool _keep_alive = true;
438};
439
440} // namespace traffic_manager
441} // namespace carla
提供与TrafficManagerServer的RPC通信功能。
远程交通管理器类,通过消息传递机制整合交通管理的各个阶段。
void SetPercentageIgnoreVehicles(const ActorPtr &actor, const float perc)
设置车辆忽略其他车辆碰撞的百分比概率。
carla::client::detail::EpisodeProxy & GetEpisodeProxy()
获取CARLA回合信息。
void SetUpdateVehicleLights(const ActorPtr &actor, const bool do_update)
设置车辆灯光的自动管理功能。
void SetGlobalPercentageSpeedDifference(float const percentage)
设置全局速度相对于限速的百分比减少量。
void RemoveUploadPath(const ActorId &actor_id, const bool remove_path)
移除路径。
carla::client::detail::EpisodeProxy episodeProxyTM
CARLA客户端连接对象。
void SetCollisionDetection(const ActorPtr &reference_actor, const ActorPtr &other_actor, const bool detect_collision)
设置车辆之间的碰撞检测规则。
void SetKeepRightPercentage(const ActorPtr &actor, const float percentage)
设置车辆保持在右侧车道的百分比概率。
void UpdateUploadPath(const ActorId &actor_id, const Path path)
更新已设置的路径。
void SetGlobalLaneOffset(float const offset)
设置全局车道偏移量,相对于车道中心线。
void UnregisterVehicles(const std::vector< ActorPtr > &actor_list)
从交通管理器注销车辆。
std::condition_variable _cv
条件变量,用于线程同步。
void SetHybridPhysicsMode(const bool mode_switch)
设置混合物理模式。
void RemoveImportedRoute(const ActorId &actor_id, const bool remove_path)
移除路线。
void RegisterVehicles(const std::vector< ActorPtr > &actor_list)
注册车辆到交通管理器。
void SetCustomPath(const ActorPtr &actor, const Path path, const bool empty_buffer)
设置自定义路径。
void SetDesiredSpeed(const ActorPtr &actor, const float value)
设置车辆的精确期望速度。
void SetLaneOffset(const ActorPtr &actor, const float offset)
设置车辆相对于车道中心线的偏移量。
void SetOSMMode(const bool mode_switch)
设置Open Street Map(OSM)模式。
void SetRandomDeviceSeed(const uint64_t seed)
设置随机化种子。
void UpdateImportedRoute(const ActorId &actor_id, const Route route)
更新已设置的路线。
void SetGlobalDistanceToLeadingVehicle(const float distance)
设置全局前车距离。
void SetRandomRightLaneChangePercentage(const ActorPtr &actor, const float percentage)
设置车辆随机进行右车道变换的百分比概率。
std::mutex _mutex
互斥锁,用于保护共享资源。
void SetRespawnDormantVehicles(const bool mode_switch)
设置自动重生休眠车辆的模式。
TrafficManagerClient client
远程客户端,使用IP和端口信息连接到远程RPC交通管理器服务器。
void SetAutoLaneChange(const ActorPtr &actor, const bool enable)
启用/禁用车辆的自动换道功能。
void SetBoundariesRespawnDormantVehicles(const float lower_bound, const float upper_bound)
设置休眠车辆重生的边界。
void SetImportedRoute(const ActorPtr &actor, const Route route, const bool empty_buffer)
设置自定义路线。
void SetPercentageIgnoreWalkers(const ActorPtr &actor, const float perc)
设置车辆忽略行人碰撞的百分比概率。
Action GetNextAction(const ActorId &actor_id)
获取车辆的下一个动作。
void SetPercentageRunningLight(const ActorPtr &actor, const float perc)
设置车辆无视交通信号灯的概率百分比。
void SetSynchronousModeTimeOutInMiliSecond(double time)
设置同步执行模式的Tick超时时间(毫秒)。
ActionBuffer GetActionBuffer(const ActorId &actor_id)
获取车辆的动作缓冲区。
void SetRandomLeftLaneChangePercentage(const ActorPtr &actor, const float percentage)
设置车辆随机进行左车道变换的百分比概率。
void SetPercentageSpeedDifference(const ActorPtr &actor, const float percentage)
设置车辆相对于限速的速度差异百分比。
TrafficManagerRemote(const std::pair< std::string, uint16_t > &_serverTM, carla::client::detail::EpisodeProxy &episodeProxy)
构造函数,存储远程服务器的位置信息。
void SetDistanceToLeadingVehicle(const ActorPtr &actor, const float distance)
设置车辆与前车应保持的距离。
void HealthCheckRemoteTM()
检查服务器是否存活。
void SetPercentageRunningSign(const ActorPtr &actor, const float perc)
设置车辆无视交通标志的概率百分比。
virtual void ShutDown()
关闭交通管理器。
void SetSynchronousMode(bool mode)
切换交通管理器为同步执行模式。
void SetForceLaneChange(const ActorPtr &actor, const bool direction)
强制车辆进行换道。
void SetHybridPhysicsRadius(const float radius)
设置混合物理模式的半径。
void SetMaxBoundaries(const float lower, const float upper)
设置最大的重生边界。
carla::SharedPtr< cc::Actor > ActorPtr
使用别名简化代码中的命名
std::vector< uint8_t > Route
路线类型,由一系列地理位置组成
std::vector< cg::Location > Path
参与者的唯一标识符类型
std::vector< Action > ActionBuffer
动作缓冲区类型别名。
std::pair< RoadOption, WaypointPtr > Action
动作类型别名。
carla::ActorId ActorId
参与者的智能指针类型
CARLA模拟器的主命名空间。
Definition Carla.cpp:139
boost::shared_ptr< T > SharedPtr
使用这个SharedPtr(boost::shared_ptr)以保持与boost::python的兼容性, 但未来如果可能的话,我们希望能为std::shared_ptr制作一个Python适配器。
Definition Memory.h:19