CARLA
 
载入中...
搜索中...
未找到
WalkerNavigation.cpp
浏览该文件的文档.
1// Copyright (c) 2019 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
14#include "carla/rpc/Command.h"
17
18#include <sstream>
19
20namespace carla {
21namespace client {
22namespace detail {
23
24 WalkerNavigation::WalkerNavigation(std::weak_ptr<Simulator> simulator) : _simulator(simulator), _next_check_index(0) {
25 _nav.SetSimulator(simulator);
26 // 这里调用服务器来检索导航网格数据。
27 auto files = _simulator.lock()->GetRequiredFiles("Nav");
28 if (!files.empty()) {
29 _nav.Load(_simulator.lock()->GetCacheFile(files[0], true));
30 }
31 }
32
33 void WalkerNavigation::Tick(std::shared_ptr<Episode> episode) {
34 auto walkers = _walkers.Load();
35 if (walkers->empty()) {
36 return;
37 }
38
39 // 获取当前状态
40 std::shared_ptr<const EpisodeState> state = episode->GetState();
41
42 // 清除所有可能的死亡行人
43 CheckIfWalkerExist(*walkers, *state);
44
45 // 在人群中添加/更新/删除所有车辆
46 UpdateVehiclesInCrowd(episode, false);
47
48 // 更新导航模块中的人群
49 _nav.UpdateCrowd(*state);
50
52 using Cmd = rpc::Command;
53 std::vector<Cmd> commands;
54 commands.reserve(walkers->size());
55 for (auto handle : *walkers) {
56 // 获取行人的变换
57 if (_nav.GetWalkerTransform(handle.walker, trans)) {
58 float speed = _nav.GetWalkerSpeed(handle.walker);
59 commands.emplace_back(Cmd::ApplyWalkerState{ handle.walker, trans, speed });
60 }
61 }
62 _simulator.lock()->ApplyBatchSync(std::move(commands), false);
63
64 // 检查是否所有代理已被杀死
65 bool alive;
66 for (auto handle : *walkers) {
67 // 获取代理状态
68 if (_nav.IsWalkerAlive(handle.walker, alive)) {
69 if (!alive) {
70 _simulator.lock()->SetActorCollisions(handle.walker, true);
71 _simulator.lock()->SetActorDead(handle.walker);
72 // 从人群中移除
73 _nav.RemoveAgent(handle.walker);
74 // 销毁控制器
75 _simulator.lock()->DestroyActor(handle.controller);
76 // 从列表中取消注册
77 UnregisterWalker(handle.walker, handle.controller);
78 }
79 }
80 }
81 }
82
83 void WalkerNavigation::CheckIfWalkerExist(std::vector<WalkerHandle> walkers, const EpisodeState &state) {
84
85 // 与总数进行核对
86 if (_next_check_index >= walkers.size())
88
89 // 检查存在
90 if (!state.ContainsActorSnapshot(walkers[_next_check_index].walker)) {
91 // 从人群中移除
92 _nav.RemoveAgent(walkers[_next_check_index].walker);
93 // 销毁控制器
94 _simulator.lock()->DestroyActor(walkers[_next_check_index].controller);
95 // 从列表中取消注册
96 UnregisterWalker(walkers[_next_check_index].walker, walkers[_next_check_index].controller);
97 }
98
100
101 }
102
103 // 添加/更新/删除人群中的所有车辆
104 void WalkerNavigation::UpdateVehiclesInCrowd(std::shared_ptr<Episode> episode, bool show_debug) {
105 std::vector<carla::nav::VehicleCollisionInfo> vehicles;
106
107 // 获取当前状态
108 std::shared_ptr<const EpisodeState> state = episode->GetState();
109
110 // 获取情节中的所有车辆
111 for (auto &&actor : episode->GetActors()) {
112 // 仅限车辆
113 if (actor.description.id.rfind("vehicle.", 0) == 0) {
114 // 获取快照
115 ActorSnapshot snapshot = state->GetActorSnapshot(actor.id);
116 // 添加到向量
117 vehicles.emplace_back(carla::nav::VehicleCollisionInfo{actor.id, snapshot.transform, actor.bounding_box});
118 }
119 }
120
121 // 更新找到的车辆
122 _nav.UpdateVehicles(vehicles);
123
124 // 可选的调试信息
125 if (show_debug) {
126 if (_nav.GetCrowd() == nullptr) return;
127
128 // 绘制边界框以进行调试
129 for (int i = 0; i < _nav.GetCrowd()->getAgentCount(); ++i) {
130 // 获取代理
131 const dtCrowdAgent *agent = _nav.GetCrowd()->getAgent(i);
132 if (agent && agent->params.useObb) {
133 // 为了调试进行绘制
134 carla::geom::Location p1, p2, p3, p4;
135 p1.x = agent->params.obb[0];
136 p1.z = agent->params.obb[1];
137 p1.y = agent->params.obb[2];
138 p2.x = agent->params.obb[3];
139 p2.z = agent->params.obb[4];
140 p2.y = agent->params.obb[5];
141 p3.x = agent->params.obb[6];
142 p3.z = agent->params.obb[7];
143 p3.y = agent->params.obb[8];
144 p4.x = agent->params.obb[9];
145 p4.z = agent->params.obb[10];
146 p4.y = agent->params.obb[11];
148 line1.life_time = 0.01f;
149 line1.persistent_lines = false;
150 // line 1
151 line1.primitive = carla::rpc::DebugShape::Line {p1, p2, 0.2f};
152 line1.color = { 0, 255, 0 };
153 _simulator.lock()->DrawDebugShape(line1);
154 // line 2
155 line1.primitive = carla::rpc::DebugShape::Line {p2, p3, 0.2f};
156 line1.color = { 255, 0, 0 };
157 _simulator.lock()->DrawDebugShape(line1);
158 // line 3
159 line1.primitive = carla::rpc::DebugShape::Line {p3, p4, 0.2f};
160 line1.color = { 0, 0, 255 };
161 _simulator.lock()->DrawDebugShape(line1);
162 // line 4
163 line1.primitive = carla::rpc::DebugShape::Line {p4, p1, 0.2f};
164 line1.color = { 255, 255, 0 };
165 _simulator.lock()->DrawDebugShape(line1);
166 }
167 }
168
169 // 为了调试绘制一些文本
170 for (int i = 0; i < _nav.GetCrowd()->getAgentCount(); ++i) {
171 // 获得智能体
172 const dtCrowdAgent *agent = _nav.GetCrowd()->getAgent(i);
173 if (agent) {
174 // 为了调试进行绘制
175 carla::geom::Location p1(agent->npos[0], agent->npos[2], agent->npos[1] + 1);
176 if (agent->params.userData) {
177 std::ostringstream out;
178 out << *(reinterpret_cast<const float *>(agent->params.userData));
180 text.life_time = 0.01f;
181 text.persistent_lines = false;
182 text.primitive = carla::rpc::DebugShape::String {p1, out.str(), false};
183 text.color = { 0, 255, 0 };
184 _simulator.lock()->DrawDebugShape(text);
185 }
186 }
187 }
188 }
189 }
190
191} // namespace detail
192} // namespace client
193} // namespace carla
表示某一帧的所有参与者的状态
bool ContainsActorSnapshot(ActorId actor_id) const
void UpdateVehiclesInCrowd(std::shared_ptr< Episode > episode, bool show_debug=false)
添加/更新/删除人群中的所有车辆
WalkerNavigation(std::weak_ptr< Simulator > simulator)
std::weak_ptr< Simulator > _simulator
void Tick(std::shared_ptr< Episode > episode)
void CheckIfWalkerExist(std::vector< WalkerHandle > walkers, const EpisodeState &state)
检查一些行人,如果不存在,则将其从人群中移除
void UnregisterWalker(ActorId walker_id, ActorId controller_id)
void UpdateCrowd(const client::detail::EpisodeState &state)
更新人群中的所有步行者
bool RemoveAgent(ActorId id)
移除代理
float GetWalkerSpeed(ActorId id)
获取步行人速度
bool Load(const std::string &filename)
从磁盘中加载导航数据
bool UpdateVehicles(std::vector< VehicleCollisionInfo > vehicles)
在人群中添加/更新/删除车辆
bool GetWalkerTransform(ActorId id, carla::geom::Transform &trans)
获取步行人当前变换
bool IsWalkerAlive(ActorId id, bool &alive)
如果行人代理被车辆撞死,则返回
void SetSimulator(std::weak_ptr< carla::client::detail::Simulator > simulator)
引用模拟器来访问API函数
boost::variant2::variant< Point, Line, Arrow, Box, String, HUDPoint, HUDLine, HUDArrow, HUDBox > primitive
Definition DebugShape.h:102
CARLA模拟器的主命名空间。
Definition Carla.cpp:139
包含CARLA客户端相关类和函数的命名空间。
向人群发送有关车辆的信息的结构体
Definition Navigation.h:77