CARLA
 
载入中...
搜索中...
未找到
Episode.h
浏览该文件的文档.
1// Copyright (c) 2017 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 "carla/AtomicSharedPtr.h" // 引入原子共享指针
10#include "carla/NonCopyable.h" // 引入不可复制类
11#include "carla/RecurrentSharedFuture.h" // 引入递归共享未来
12#include "carla/client/Timestamp.h" // 引入时间戳
13#include "carla/client/WorldSnapshot.h" // 引入世界快照
14#include "carla/client/detail/CachedActorList.h" // 引入缓存参与者列表
15#include "carla/client/detail/CallbackList.h" // 引入回调列表
16#include "carla/client/detail/EpisodeState.h" // 引入剧集状态
17#include "carla/client/detail/EpisodeProxy.h" // 引入剧集代理
18#include "carla/rpc/EpisodeInfo.h" // 引入剧集信息
19
20#include <vector> // 引入向量类
21
22namespace carla {
23namespace client {
24namespace detail {
25
26 class Client; // 前向声明 Client 类
27 class WalkerNavigation; // 前向声明 WalkerNavigation 类
28
29 /// 持有当前剧集及当前剧集状态
30 ///
31 /// 每当接收到世界 tick 时,剧集状态在后台变化。
32 /// 如果模拟器加载了新剧集,剧集可能会随任何后台更新而变化。
33 class Episode
34 : public std::enable_shared_from_this<Episode>, // 支持共享指针
35 private NonCopyable { // 继承不可复制类
36 public:
37
38 explicit Episode(Client &client, std::weak_ptr<Simulator> simulator); // 构造函数
39
40 ~Episode(); // 析构函数
41
42 void Listen(); // 监听事件
43
44 auto GetId() const { // 获取剧集 ID
45 return GetState()->GetEpisodeId();
46 }
47
48 std::shared_ptr<const EpisodeState> GetState() const { // 获取剧集状态
49 return _state.load();
50 }
51
52 void RegisterParticipant(rpc::Actor actor) { // 注册参与者
53 _actors.Insert(std::move(actor));
54 }
55
56 boost::optional<rpc::Actor> GetParticipantById(ActorId id); // 根据 ID 获取参与者
57
58 std::vector<rpc::Actor> GetParticipantsById(const std::vector<ActorId> &actor_ids); // 根据 ID 列表获取参与者
59
60 std::vector<rpc::Actor> GetParticipants(); // 获取所有参与者
61
62 boost::optional<WorldSnapshot> WaitForState(time_duration timeout) { // 等待状态变化
63 return _snapshot.WaitFor(timeout);
64 }
65
66 size_t RegisterOnTickEvent(std::function<void(WorldSnapshot)> callback) { // 注册 tick 事件回调
67 return _on_tick_callbacks.Push(std::move(callback));
68 }
69
70 void RemoveOnTickEvent(size_t id) { // 移除 tick 事件回调
71 _on_tick_callbacks.Remove(id);
72 }
73
74 size_t RegisterOnMapChangeEvent(std::function<void(WorldSnapshot)> callback) { // 注册地图变化事件回调
75 return _on_map_change_callbacks.Push(std::move(callback));
76 }
77
78 void RemoveOnMapChangeEvent(size_t id) { // 移除地图变化事件回调
79 _on_map_change_callbacks.Remove(id);
80 }
81
82 size_t RegisterLightUpdateChangeEvent(std::function<void(WorldSnapshot)> callback) { // 注册光照更新事件回调
83 return _on_light_update_callbacks.Push(std::move(callback));
84 }
85
86 void RemoveLightUpdateChangeEvent(size_t id) { // 移除光照更新事件回调
88 }
89
90 void SetPedestriansCrossFactor(float percentage); // 设置行人过马路的概率
91
92 void SetPedestriansSeed(unsigned int seed); // 设置行人种子值
93
94 void AddPendingException(std::string e) { // 添加待处理异常
97 }
98
99 bool HasMapChangedSinceLastCall(); // 检查地图是否自上次调用后变化
100
101 std::shared_ptr<WalkerNavigation> CreateNavigationIfMissing(); // 如果缺失则创建导航
102
103 private:
104
105 Episode(Client &client, const rpc::EpisodeInfo &info, std::weak_ptr<Simulator> simulator); // 私有构造函数
106
107 void OnEpisodeStarted(); // 处理剧集开始事件
108
109 void OnEpisodeChanged(); // 处理剧集变化事件
110
111 Client &_client; // 引用客户端
112
113 AtomicSharedPtr<const EpisodeState> _state; // 原子共享指针指向剧集状态
114
115 std::string _pending_exceptions_msg; // 待处理异常消息
116
117 CachedActorList _actors; // 缓存的参与者列表
118
120
121 CallbackList<WorldSnapshot> _on_map_change_callbacks; // 地图变化事件回调列表
122
124
125 RecurrentSharedFuture<WorldSnapshot> _snapshot; // 递归共享未来的世界快照
126
127 AtomicSharedPtr<WalkerNavigation> _walker_navigation; // 原子共享指针指向 WalkerNavigation
128
129 const streaming::Token _token; // 令牌
130
131 bool _pending_exceptions = false; // 是否有待处理异常
132
133 bool _should_update_map = true; // 是否应该更新地图
134
135 std::weak_ptr<Simulator> _simulator; // 弱指针指向模拟器
136 };
137
138} // namespace detail
139} // namespace client
140} // namespace carla
线程安全的原子智能指针封装类
这个类用于禁止拷贝和移动构造函数及赋值操作
这个类类似于共享未来(shared future)的使用方式,但是它的值可以被设置任意次数的值。 未来设计模式的核心思想是异步调用。 Future接口象征着异步执行任务的结果即执行一个耗时任务完全可以另...
保留参与者描述列表,以避免每次都向服务器请求描述。
void Insert(rpc::Actor actor)
将参与者插入到列表中。
提供与 CARLA 模拟器的 rpc 和流媒体服务器的通信。
持有当前剧集及当前剧集状态
Definition Episode.h:35
void RemoveOnTickEvent(size_t id)
Definition Episode.h:70
RecurrentSharedFuture< WorldSnapshot > _snapshot
Definition Episode.h:125
size_t RegisterLightUpdateChangeEvent(std::function< void(WorldSnapshot)> callback)
Definition Episode.h:82
AtomicSharedPtr< const EpisodeState > _state
Definition Episode.h:113
std::weak_ptr< Simulator > _simulator
Definition Episode.h:135
CallbackList< WorldSnapshot > _on_tick_callbacks
Definition Episode.h:119
boost::optional< rpc::Actor > GetParticipantById(ActorId id)
void AddPendingException(std::string e)
Definition Episode.h:94
void RegisterParticipant(rpc::Actor actor)
Definition Episode.h:52
size_t RegisterOnTickEvent(std::function< void(WorldSnapshot)> callback)
Definition Episode.h:66
std::shared_ptr< WalkerNavigation > CreateNavigationIfMissing()
Definition Episode.cpp:174
boost::optional< WorldSnapshot > WaitForState(time_duration timeout)
Definition Episode.h:62
std::string _pending_exceptions_msg
Definition Episode.h:115
size_t RegisterOnMapChangeEvent(std::function< void(WorldSnapshot)> callback)
Definition Episode.h:74
std::shared_ptr< const EpisodeState > GetState() const
Definition Episode.h:48
const streaming::Token _token
Definition Episode.h:129
AtomicSharedPtr< WalkerNavigation > _walker_navigation
Definition Episode.h:127
void RemoveLightUpdateChangeEvent(size_t id)
Definition Episode.h:86
std::vector< rpc::Actor > GetParticipantsById(const std::vector< ActorId > &actor_ids)
std::vector< rpc::Actor > GetParticipants()
void SetPedestriansSeed(unsigned int seed)
void RemoveOnMapChangeEvent(size_t id)
Definition Episode.h:78
void SetPedestriansCrossFactor(float percentage)
CallbackList< WorldSnapshot > _on_light_update_callbacks
Definition Episode.h:123
CallbackList< WorldSnapshot > _on_map_change_callbacks
Definition Episode.h:121
Episode(Client &client, std::weak_ptr< Simulator > simulator)
Definition Episode.cpp:41
Positive time duration up to milliseconds resolution.
Definition Time.h:19
carla::ActorId ActorId
参与者的智能指针类型
CARLA模拟器的主命名空间。
Definition Carla.cpp:139
包含CARLA客户端相关类和函数的命名空间。