16namespace traffic_manager {
18using namespace constants::FrameMemory;
23 std::vector<float> longitudinal_PID_parameters,
24 std::vector<float> longitudinal_highway_PID_parameters,
25 std::vector<float> lateral_PID_parameters,
26 std::vector<float> lateral_highway_PID_parameters,。
27 float perc_difference_from_limit,
28 cc::detail::EpisodeProxy &episode_proxy,
31 : longitudinal_PID_parameters(longitudinal_PID_parameters),
32 longitudinal_highway_PID_parameters(longitudinal_highway_PID_parameters),
33 lateral_PID_parameters(lateral_PID_parameters),
34 lateral_highway_PID_parameters(lateral_highway_PID_parameters),
36 episode_proxy(episode_proxy),
37 world(
cc::
World(episode_proxy)),
70 longitudinal_PID_parameters,
71 longitudinal_highway_PID_parameters,
72 lateral_PID_parameters,
73 lateral_highway_PID_parameters,
88 alsm(ALSM(registered_vehicles,
100 vehicle_light_stage)),
121 local_map = std::make_shared<InMemoryMap>(world_map);
124 if (!files.empty()) {
126 if (content.size() != 0) {
129 log_warning(
"No InMemoryMap cache found. Setting up local map. This may take a while...");
133 log_warning(
"No InMemoryMap cache found. Setting up local map. This may take a while...");
151 size_t last_frame = 0;
158 if (synchronous_mode) {
165 if (!synchronous_mode && hybrid_physics_mode) {
166 TimePoint current_instance = chr::system_clock::now();
168 chr::duration<float> time_to_wait = chr::duration<float>(HYBRID_MODE_DT) - elapsed_time;
169 if (time_to_wait > chr::duration<float>(0.0f)) {
170 std::this_thread::sleep_for(time_to_wait);
176 if (!synchronous_mode) {
178 if (timestamp.
frame == last_frame) {
181 last_frame = timestamp.
frame;
196 uint64_t growth_factor =
static_cast<uint64_t
>(
static_cast<float>(number_of_vehicles) *
INV_GROWTH_STEP_SIZE);
201 tl_frame.reserve(new_frame_capacity);
214 tl_frame.resize(number_of_vehicles);
224 for (
unsigned long index = 0u; index <
vehicle_id_list.size(); ++index) {
227 for (
unsigned long index = 0u; index <
vehicle_id_list.size(); ++index) {
232 for (
unsigned long index = 0u; index <
vehicle_id_list.size(); ++index) {
238 registration_lock.unlock();
241 if (synchronous_mode) {
305void TrafficManagerLocal::Release() {
312void TrafficManagerLocal::Reset() {
314 episode_proxy = episode_proxy.Lock()->GetCurrentEpisode();
320void TrafficManagerLocal::RegisterVehicles(
const std::vector<ActorPtr> &vehicle_list) {
321 std::lock_guard<std::mutex> registration_lock(registration_mutex);
322 registered_vehicles.Insert(vehicle_list);
325void TrafficManagerLocal::UnregisterVehicles(
const std::vector<ActorPtr> &actor_list) {
326 std::lock_guard<std::mutex> registration_lock(registration_mutex);
327 std::vector<ActorId> actor_id_list;
328 for (
auto &actor : actor_list) {
329 alsm.RemoveActor(actor->GetId(),
true);
333void TrafficManagerLocal::SetPercentageSpeedDifference(
const ActorPtr &actor,
const float percentage) {
334 parameters.SetPercentageSpeedDifference(actor, percentage);
337void TrafficManagerLocal::SetGlobalPercentageSpeedDifference(
const float percentage) {
338 parameters.SetGlobalPercentageSpeedDifference(percentage);
341void TrafficManagerLocal::SetLaneOffset(
const ActorPtr &actor,
const float offset) {
342 parameters.SetLaneOffset(actor, offset);
345void TrafficManagerLocal::SetGlobalLaneOffset(
const float offset)
348void TrafficManagerLocal::SetDesiredSpeed(
const ActorPtr &actor,
const float value) {
349 parameters.SetDesiredSpeed(actor, value);
353void TrafficManagerLocal::SetUpdateVehicleLights(
const ActorPtr &actor,
const bool do_update) {
354 parameters.SetUpdateVehicleLights(actor, do_update);
357void TrafficManagerLocal::SetCollisionDetection(
const ActorPtr &reference_actor,
const ActorPtr &other_actor,
const bool detect_collision) {
358 parameters.SetCollisionDetection(reference_actor, other_actor, detect_collision);
361void TrafficManagerLocal::SetForceLaneChange(
const ActorPtr &actor,
const bool direction) {
362 parameters.SetForceLaneChange(actor, direction);
365void TrafficManagerLocal::SetAutoLaneChange(
const ActorPtr &actor,
const bool enable) {
366 parameters.SetAutoLaneChange(actor, enable);
369void TrafficManagerLocal::SetDistanceToLeadingVehicle(
const ActorPtr &actor,
const float distance) {
370 parameters.SetDistanceToLeadingVehicle(actor, distance);
373void TrafficManagerLocal::SetGlobalDistanceToLeadingVehicle(
const float distance) {
374 parameters.SetGlobalDistanceToLeadingVehicle(distance);
377void TrafficManagerLocal::SetPercentageIgnoreWalkers(
const ActorPtr &actor,
const float perc) {
378 parameters.SetPercentageIgnoreWalkers(actor, perc);
381void TrafficManagerLocal::SetPercentageIgnoreVehicles(
const ActorPtr &actor,
const float perc) {
382 parameters.SetPercentageIgnoreVehicles(actor, perc);
385void TrafficManagerLocal::SetPercentageRunningLight(
const ActorPtr &actor,
const float perc) {
386 parameters.SetPercentageRunningLight(actor, perc);
389void TrafficManagerLocal::SetPercentageRunningSign(
const ActorPtr &actor,
const float perc) {
390 parameters.SetPercentageRunningSign(actor, perc);
393void TrafficManagerLocal::SetKeepRightPercentage(
const ActorPtr &actor,
const float percentage) {
394 parameters.SetKeepRightPercentage(actor, percentage);
397void TrafficManagerLocal::SetRandomLeftLaneChangePercentage(
const ActorPtr &actor,
const float percentage) {
398 parameters.SetRandomLeftLaneChangePercentage(actor, percentage);
401void TrafficManagerLocal::SetRandomRightLaneChangePercentage(
const ActorPtr &actor,
const float percentage) {
402 parameters.SetRandomRightLaneChangePercentage(actor, percentage);
405void TrafficManagerLocal::SetHybridPhysicsMode(
const bool mode_switch) {
406 parameters.SetHybridPhysicsMode(mode_switch);
409void TrafficManagerLocal::SetHybridPhysicsRadius(
const float radius) {
410 parameters.SetHybridPhysicsRadius(radius);
413void TrafficManagerLocal::SetOSMMode(
const bool mode_switch) {
414 parameters.SetOSMMode(mode_switch);
417void TrafficManagerLocal::SetCustomPath(
const ActorPtr &actor,
const Path path,
const bool empty_buffer) {
418 parameters.SetCustomPath(actor, path, empty_buffer);
421void TrafficManagerLocal::RemoveUploadPath(
const ActorId &actor_id,
const bool remove_path) {
422 parameters.RemoveUploadPath(actor_id, remove_path);
425void TrafficManagerLocal::UpdateUploadPath(
const ActorId &actor_id,
const Path path) {
426 parameters.UpdateUploadPath(actor_id, path);
429void TrafficManagerLocal::SetImportedRoute(
const ActorPtr &actor,
const Route route,
const bool empty_buffer) {
430 parameters.SetImportedRoute(actor, route, empty_buffer);
433void TrafficManagerLocal::RemoveImportedRoute(
const ActorId &actor_id,
const bool remove_path) {
434 parameters.RemoveImportedRoute(actor_id, remove_path);
437void TrafficManagerLocal::UpdateImportedRoute(
const ActorId &actor_id,
const Route route) {
438 parameters.UpdateImportedRoute(actor_id, route);
441void TrafficManagerLocal::SetRespawnDormantVehicles(
const bool mode_switch) {
442 parameters.SetRespawnDormantVehicles(mode_switch);
445void TrafficManagerLocal::SetBoundariesRespawnDormantVehicles(
const float lower_bound,
const float upper_bound) {
446 parameters.SetBoundariesRespawnDormantVehicles(lower_bound, upper_bound);
449void TrafficManagerLocal::SetMaxBoundaries(
const float lower,
const float upper) {
450 parameters.SetMaxBoundaries(lower, upper);
454 return localization_stage.ComputeNextAction(actor_id);
458 return localization_stage.ComputeActionBuffer(actor_id);
461bool TrafficManagerLocal::CheckAllFrozen(
TLGroup tl_to_freeze) {
462 for (
auto &elem : tl_to_freeze) {
463 if (!elem->IsFrozen() || elem->GetState() != TLS::Red) {
470void TrafficManagerLocal::SetSynchronousMode(
bool mode) {
471 const bool previous_mode = parameters.GetSynchronousMode();
472 parameters.SetSynchronousMode(mode);
473 if (previous_mode && !mode) {
474 step_begin.store(
true);
475 step_begin_trigger.notify_one();
479void TrafficManagerLocal::SetSynchronousModeTimeOutInMiliSecond(
double time) {
480 parameters.SetSynchronousModeTimeOutInMiliSecond(time);
484 return episode_proxy;
487std::vector<ActorId> TrafficManagerLocal::GetRegisteredVehiclesIDs() {
488 return registered_vehicles.GetIDList();
491void TrafficManagerLocal::SetRandomDeviceSeed(
const uint64_t _seed) {
494 world.ResetAllTrafficLights();
TSharedPtr< const FActorInfo > carla::rpc::ActorState UWorld * World
std::size_t frame
自模拟器启动以来经过的帧数。
const Timestamp & GetTimestamp() const
WorldSnapshot GetSnapshot() const
返回当前世界的快照。 快照(Snapshot)包含了模拟世界在某一时刻的整体状态信息,例如所有参与者的位置、状态,天气情况等, 可以用于记录、对比不同时刻的世界状态或者进行一些基于特定时刻状态的分析和操...
SharedPtr< Map > GetMap() const
返回描述这个世界的地图。 返回的是一个智能指针指向的Map对象,该Map对象包含了模拟世界中的地理信息、道路布局等地图相关的数据结构, 供外部调用者进一步查询和操作地图相关的功能。
SharedPtrType Lock() const
与 TryLock 相同,但永远不会返回 nullptr。
std::vector< ActorId > GetIDList()
void Update(const unsigned long index) override
更新方法。
void Reset() override
重置方法。
此类功能是维护车辆前方的路径点以供跟随。 该类还负责管理车道变更决策并适当地修改路径轨迹。
void Reset() override
重置方法。
void Update(const unsigned long index) override
更新方法。
void Update(const unsigned long index)
更新方法。
void SetMaxBoundaries(const float lower, const float upper)
设置重生休眠车辆时的边界限制的方法
bool GetSynchronousMode() const
获取同步模式的方法
void SetGlobalPercentageSpeedDifference(float const percentage)
设置全局相对于速度限制的速度降低百分比 如果小于0,则表示速度增加百分比
bool GetHybridPhysicsMode() const
获取混合物理模式的方法
void Update(const unsigned long index) override
更新方法。
void Reset() override
重置方法。
int registered_vehicles_state
用于跟踪注册参与者状态变化的计数器
LocalizationStage localization_stage
表示交通管理器核心操作的各种阶段 这些阶段包括定位、碰撞避免、交通灯响应、运动规划和车辆灯光控制等
std::mutex step_execution_mutex
用于同步执行进度的互斥锁 std::mutex用于保护共享资源,确保同一时间只有一个线程可以访问
TimePoint previous_update_instance
在异步模式下用于计算时间差(dt)的时间实例 使用TimePoint类型表示一个具体的时间点,用于与上一个更新时间点进行比较,以计算时间差
AtomicActorSet registered_vehicles
注册到交通管理器的所有参与者集合。
std::unique_ptr< std::thread > worker_thread
用于顺序执行子组件的单个工作线程 使用std::unique_ptr<std::thread>管理线程的生命周期,确保线程在不再需要时能够被正确销毁
MotionPlanStage motion_plan_stage
void SetupLocalMap()
设置内存中的地图 此方法用于初始化或设置TrafficManagerLocal所使用的内存地图
void Start()
启动交通管理器 此方法用于启动TrafficManagerLocal,开始其管理交通的功能
TrafficLightStage traffic_light_stage
std::condition_variable step_begin_trigger
用于同步执行进度的条件变量 std::condition_variable用于线程间的同步,当一个线程需要等待某个条件成立时,可以阻塞在该条件变量上,直到另一个线程通知条件已成立
ControlFrame control_frame
存储运动规划阶段输出数据的数组 用于存储运动规划阶段产生的控制指令
Parameters parameters
参数化对象 包含交通管理器运行所需的各种配置参数
std::vector< ActorId > vehicle_id_list
当前更新周期中注册到交通管理器的车辆列表 使用ActorId类型存储车辆的唯一标识符
BufferMap buffer_map
存储所有车辆路径点的缓冲区结构映射 使用BufferMap类型(可能是自定义的映射类型)来存储每个车辆的路径点缓冲区
std::condition_variable step_end_trigger
TrackTraffic track_traffic
用于跟踪交通车辆路径的对象 TrackTraffic对象可能包含有关交通车辆当前位置和预期路径的信息
CollisionFrame collision_frame
存储碰撞避免阶段输出数据的数组 用于存储碰撞避免阶段产生的数据
void Run()
启动线程以顺序运行交通管理器 此方法将创建一个新线程(如果尚未创建),并在该线程中顺序运行交通管理器的逻辑
TLFrame tl_frame
存储交通灯响应阶段输出数据的数组 用于存储交通灯响应阶段产生的数据
TrafficManagerLocal(std::vector< float > longitudinal_PID_parameters, std::vector< float > longitudinal_highway_PID_parameters, std::vector< float > lateral_PID_parameters, std::vector< float > lateral_highway_PID_parameters, float perc_decrease_from_limit, cc::detail::EpisodeProxy &episode_proxy, uint16_t &RPCportTM)
私有构造函数,用于单例生命周期管理
uint64_t current_reserved_capacity
用于跟踪当前为帧保留的数组空间的变量 这是一个无符号64位整数,用于记录为各个帧数组预留的空间大小
LocalizationFrame localization_frame
存储定位阶段输出数据的数组 用于存储定位阶段产生的数据
ALSM alsm
自动驾驶局部路径规划模块(ALSM) ALSM可能是一个用于生成局部路径规划算法的模块或对象
CollisionStage collision_stage
VehicleLightStage vehicle_light_stage
cc::World world
CARLA 世界对象。
bool SynchronousTick()
提供同步Tick。
LocalMapPtr local_map
指向本地地图缓存的指针 使用智能指针管理InMemoryMap对象,用于存储和访问地图数据
carla::client::detail::EpisodeProxy episode_proxy
CARLA 客户端连接对象。
virtual ~TrafficManagerLocal()
析构函数 虚拟析构函数,用于确保派生类能够正确地被销毁
std::atomic< bool > run_traffic_manger
用于打开/关闭交通管理器的开关 这是一个原子布尔变量,用于线程安全地控制交通管理器的运行状态
std::atomic< bool > step_end
void Release()
释放交通管理器 此方法用于释放TrafficManagerLocal所占用的资源,例如线程、内存等
SimulationState simulation_state
包含模拟中所有参与者当前状态的类型 用于表示模拟中所有参与者的状态信息
void Stop()
停止交通管理器 此方法用于停止TrafficManagerLocal的运行,并可能进行必要的清理工作
std::atomic< bool > step_begin
用于标记步骤开始和结束的标志 使用std::atomic<bool>确保跨线程的原子操作,避免数据竞争
std::mutex registration_mutex
防止在帧数组重新分配期间注册车辆的互斥锁 用于保护车辆注册操作,确保在帧数组重新分配时不会有新的车辆被注册
交通管理服务器类,负责处理远程交通管理器的请求并应用更改到本地实例。
VehicleLightStage类负责根据车辆当前的状态和周围环境来开启或关闭车辆的灯光
void Update(const unsigned long index) override
更新方法。
static const uint64_t GROWTH_STEP_SIZE
static const float INV_GROWTH_STEP_SIZE
static const uint64_t INITIAL_SIZE
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
参与者的智能指针类型
chr::time_point< chr::system_clock, chr::nanoseconds > TimePoint
时间点类型,使用系统时钟和纳秒精度
std::vector< carla::SharedPtr< carla::client::TrafficLight > > TLGroup
交通灯组类型,使用CARLA的智能指针管理交通灯对象的集合
static void log_warning(Args &&... args)
boost::shared_ptr< T > SharedPtr
使用这个SharedPtr(boost::shared_ptr)以保持与boost::python的兼容性, 但未来如果可能的话,我们希望能为std::shared_ptr制作一个Python适配器。