CARLA
 
载入中...
搜索中...
未找到
Navigation.h
浏览该文件的文档.
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
7#pragma once
8
9#include "carla/AtomicList.h"
12
13// 使用几何库相关功能
14#include "carla/geom/Location.h"
17
18// 使用远程过程调用相关功能
19#include "carla/rpc/ActorId.h"
20
21#include <recast/Recast.h>
22// 包含Recast/Detour导航网格生成和查询库的头文件
23
24// Recast库的主头文件,提供生成导航网格所需的基本函数和数据结构
25#include <recast/DetourCrowd.h>
26// 提供处理大量角色(crowd)同时导航的功能,确保它们不会相互碰撞或阻塞
27#include <recast/DetourNavMesh.h>
28// 定义导航网格(NavMesh)的数据结构和接口,表示环境中的可通行区域
29#include <recast/DetourNavMeshBuilder.h>
30// 包含构建导航网格的实用函数,从高度图或紧凑高度图构建导航网格
31#include <recast/DetourNavMeshQuery.h>
32// 提供查询导航网格的接口,如查找路径、获取多边形信息等
33#include <recast/DetourCommon.h>
34// 可能包含Recast/Detour库中使用的通用定义、枚举和数据结构
35
36namespace carla {
37// 定义命名空间carla,它是CARLA自动驾驶仿真平台的命名空间
38namespace nav {
39// 在carla命名空间内定义子命名空间nav,用于与导航相关的功能
40// 在这里,您可能会定义与导航网格生成和查询相关的函数、类或数据结构
41// 例如,您可能会实现一个函数来加载高度图并生成导航网格
42// 或者实现一个类来管理导航网格的查询和路径规划
43
44 // 导航区域
45 // 参考:https://openhutb.github.io/carla_doc/tuto_M_generate_pedestrian_navigation/
46 enum NavAreas {
48 CARLA_AREA_SIDEWALK, // 人行道
49 CARLA_AREA_CROSSWALK, // 人行横道:如果找不到地面,行人将在这些网格上行走作为第二种选择。
50 CARLA_AREA_ROAD, // 马路:行人只能通过这些网格过马路。
51 CARLA_AREA_GRASS // 草地:行人不会在此网格上行走,除非您指定一定比例的行人这样做。
52 };
53
55// 定义一个枚举类型SamplePolyFlags,用于表示导航网格中多边形(Poly)的不同类型或属性
56 {
58 // 没有任何特殊类型的标志,可能表示一个默认或未知的类型
60// 表示人行道,通常用于行人行走的区域
62// 表示斑马线,是行人过马路时专用的区域
64// 表示道路,通常用于车辆行驶
66// 表示草地,可能允许行人行走,但通常不允许车辆进入
68// 一个特殊的标志,表示上述所有类型的组合,常用于需要匹配所有类型的情况
69// 0xffff即二进制的1111 1111 1111 1111,代表所有位都被设置为1
70
72 // 一个方便的组合标志,表示所有可通行的类型,包括人行道、斑马线、草地和道路
73 // 但不包括没有任何特殊类型的标志(CARLA_TYPE_NONE)
74 };
75
76 /// 向人群发送有关车辆的信息的结构体
82
83 /// 管理行人导航,使用 Recast & Detour 库进行低层计算。
84 ///
85 /// 该类从服务器获取地图的二进制内容,这是查找路径所必需的。然后,这个类可以添加或删除行人,并为每个行人设置目标步行点。
86 class Navigation : private NonCopyable {
87
88 public:
89
90 Navigation();
92
93 /// 从磁盘中加载导航数据
94 bool Load(const std::string &filename);
95 /// 从内存中加载导航数据
96 bool Load(std::vector<uint8_t> content);
97 /// 返回从一个位置到另一个位置的路径点
98 bool GetPath(carla::geom::Location from, carla::geom::Location to, dtQueryFilter * filter,
99// GetPath 函数注释
100//
101// 功能:在给定的起点和终点之间查询导航路径。
102//
103// 参数:
104// - from:起点位置,使用carla::geom::Location类型表示。
105// - to:终点位置,使用carla::geom::Location类型表示。
106// - filter:一个指向dtQueryFilter的指针,用于在查询过程中筛选导航网格中的多边形。
107// dtQueryFilter是Detour库中的一个结构体,用于定义查询的过滤条件。
108// - path:一个引用,指向存储查询结果的路径的std::vector<carla::geom::Location>。
109// 查询成功后,该向量将包含从起点到终点的位置序列。
110// - area:一个引用,指向存储路径中每个位置所属区域的std::vector<unsigned char>。
111// 每个元素代表路径中对应位置的多边形区域类型。
112//
113// 返回值:
114// - 如果成功找到路径,则返回true;否则返回false
115 std::vector<carla::geom::Location> &path, std::vector<unsigned char> &area);
117
118// GetAgentRoute 函数注释
119//
120// 功能:为给定的代理(Agent,如自动驾驶车辆或行人)查询从起点到终点的路由。
121//
122// 参数:
123// - id:代理的唯一标识符,使用ActorId类型表示。在CARLA中,每个仿真对象(如车辆、行人等)都有一个唯一的ID。
124// - from:起点位置,使用carla::geom::Location类型表示。
125// - to:终点位置,使用carla::geom::Location类型表示。
126// - path:一个引用,指向存储查询结果的路径的std::vector<carla::geom::Location>。
127// 查询成功后,该向量将包含从起点到终点的位置序列,这些位置是根据代理的类型和规则优化过的。
128// - area:一个引用,指向存储路径中每个位置所属区域的std::vector<unsigned char>。
129// 每个元素代表路径中对应位置的多边形区域类型,这些信息可能用于进一步的决策或行为规划。
130//
131// 返回值:
132// - 如果成功找到路由,则返回true;否则返回false。
133//
134// 注意:
135// - 与GetPath函数相比,GetAgentRoute函数可能考虑了更多的因素,如代理的类型、尺寸、速度限制等,以生成更适合代理的路由。
136// - 在实际使用中,这些函数可能会依赖于CARLA仿真平台中的导航系统和地图数据来执行查询。
137 std::vector<carla::geom::Location> &path, std::vector<unsigned char> &area);
138
139 /// 引用模拟器来访问API函数
140 void SetSimulator(std::weak_ptr<carla::client::detail::Simulator> simulator);
141 /// 设置随机数种子
142 void SetSeed(unsigned int seed);
143 /// 创建人群对象
144 void CreateCrowd(void);
145 /// 创建新的行人
147 /// 在人群中创造一辆新的车辆,让行人避开
149 /// 移除代理
150 bool RemoveAgent(ActorId id);
151 /// 在人群中添加/更新/删除车辆
152 bool UpdateVehicles(std::vector<VehicleCollisionInfo> vehicles);
153 /// 设置新的最大速度
154 bool SetWalkerMaxSpeed(ActorId id, float max_speed);
155 /// 设置新的目标点以通过有事件的路线
157 // 设置新的目标点,直接前往没有事件发生的地方
160 /// 获取步行人当前变换
162 /// 获取行人的当前位置
164 /// 获取步行人速度
165 float GetWalkerSpeed(ActorId id);
166 /// 更新人群中的所有步行者
167 void UpdateCrowd(const client::detail::EpisodeState &state);
168 /// 获取导航的随机位置
169 bool GetRandomLocation(carla::geom::Location &location, dtQueryFilter * filter = nullptr) const;
170 /// 设置行人代理在路径跟随过程中穿过马路的概率
171 void SetPedestriansCrossFactor(float percentage);
172 /// 将人群中的代理设置为暂停
173 void PauseAgent(ActorId id, bool pause);
174 /// 如果代理在附近有车辆(作为邻居),则返回
175 bool HasVehicleNear(ActorId id, float distance, carla::geom::Location direction);
176 /// 让代理查看某个位置
178 /// 如果行人代理被车辆撞死,则返回
179 bool IsWalkerAlive(ActorId id, bool &alive);
180
181 dtCrowd *GetCrowd() { return _crowd; };
182
183 /// 返回最后增量秒数
184 double GetDeltaSeconds() { return _delta_seconds; };
185
186 private:
187
188 bool _ready { false };
189 std::vector<uint8_t> _binary_mesh;
190 double _delta_seconds { 0.0 };
191 /// 网格
192 dtNavMesh *_nav_mesh { nullptr };
193 dtNavMeshQuery *_nav_query { nullptr };
194 /// crowd
195 dtCrowd *_crowd { nullptr };
196 /// mapping Id
197 std::unordered_map<ActorId, int> _mapped_walkers_id;
198 std::unordered_map<ActorId, int> _mapped_vehicles_id;
199 // 也可以通过索引进行映射
200 std::unordered_map<int, ActorId> _mapped_by_index;
201 /// 存储上一个节拍的行人偏航角
202 std::unordered_map<ActorId, float> _yaw_walkers;
203 /// 每隔一段时间保存每个参与者的位置,并检查是否有参与者被阻挡
204 std::unordered_map<int, carla::geom::Vector3D> _walkers_blocked_position;
205 double _time_to_unblock { 0.0 };
206
207 /// 行人管理器负责带事件的路线规划
209
210 std::weak_ptr<carla::client::detail::Simulator> _simulator;
211
212 mutable std::mutex _mutex;
213
214 float _probability_crossing { 0.0f };
215
216 /// 为代理分配过滤索引
217 void SetAgentFilter(int agent_index, int filter_index);
218 };
219
220} // namespace nav
221} // namespace carla
这个类用于禁止拷贝和移动构造函数及赋值操作
表示某一帧的所有参与者的状态
管理行人导航,使用 Recast & Detour 库进行低层计算。
Definition Navigation.h:86
bool SetWalkerMaxSpeed(ActorId id, float max_speed)
设置新的最大速度
void UpdateCrowd(const client::detail::EpisodeState &state)
更新人群中的所有步行者
bool GetPath(carla::geom::Location from, carla::geom::Location to, dtQueryFilter *filter, std::vector< carla::geom::Location > &path, std::vector< unsigned char > &area)
返回从一个位置到另一个位置的路径点
void SetAgentFilter(int agent_index, int filter_index)
为代理分配过滤索引
std::unordered_map< ActorId, int > _mapped_vehicles_id
Definition Navigation.h:198
std::unordered_map< ActorId, int > _mapped_walkers_id
mapping Id
Definition Navigation.h:197
bool RemoveAgent(ActorId id)
移除代理
std::weak_ptr< carla::client::detail::Simulator > _simulator
Definition Navigation.h:210
bool GetWalkerPosition(ActorId id, carla::geom::Location &location)
获取行人的当前位置
float GetWalkerSpeed(ActorId id)
获取步行人速度
bool Load(const std::string &filename)
从磁盘中加载导航数据
bool SetWalkerTarget(ActorId id, carla::geom::Location to)
设置新的目标点以通过有事件的路线
void SetSeed(unsigned int seed)
设置随机数种子
dtNavMesh * _nav_mesh
网格
Definition Navigation.h:192
bool UpdateVehicles(std::vector< VehicleCollisionInfo > vehicles)
在人群中添加/更新/删除车辆
std::unordered_map< int, ActorId > _mapped_by_index
Definition Navigation.h:200
bool SetWalkerDirectTarget(ActorId id, carla::geom::Location to)
bool SetWalkerLookAt(ActorId id, carla::geom::Location location)
让代理查看某个位置
void PauseAgent(ActorId id, bool pause)
将人群中的代理设置为暂停
bool HasVehicleNear(ActorId id, float distance, carla::geom::Location direction)
如果代理在附近有车辆(作为邻居),则返回
bool GetWalkerTransform(ActorId id, carla::geom::Transform &trans)
获取步行人当前变换
bool GetRandomLocation(carla::geom::Location &location, dtQueryFilter *filter=nullptr) const
获取导航的随机位置
bool IsWalkerAlive(ActorId id, bool &alive)
如果行人代理被车辆撞死,则返回
WalkerManager _walker_manager
行人管理器负责带事件的路线规划
Definition Navigation.h:208
bool GetAgentRoute(ActorId id, carla::geom::Location from, carla::geom::Location to, std::vector< carla::geom::Location > &path, std::vector< unsigned char > &area)
void SetPedestriansCrossFactor(float percentage)
设置行人代理在路径跟随过程中穿过马路的概率
std::unordered_map< ActorId, float > _yaw_walkers
存储上一个节拍的行人偏航角
Definition Navigation.h:202
dtCrowd * _crowd
crowd
Definition Navigation.h:195
bool AddWalker(ActorId id, carla::geom::Location from)
创建新的行人
std::vector< uint8_t > _binary_mesh
Definition Navigation.h:189
double GetDeltaSeconds()
返回最后增量秒数
Definition Navigation.h:184
bool SetWalkerDirectTargetIndex(int index, carla::geom::Location to)
void SetSimulator(std::weak_ptr< carla::client::detail::Simulator > simulator)
引用模拟器来访问API函数
dtNavMeshQuery * _nav_query
Definition Navigation.h:193
void CreateCrowd(void)
创建人群对象
bool AddOrUpdateVehicle(VehicleCollisionInfo &vehicle)
在人群中创造一辆新的车辆,让行人避开
std::unordered_map< int, carla::geom::Vector3D > _walkers_blocked_position
每隔一段时间保存每个参与者的位置,并检查是否有参与者被阻挡
Definition Navigation.h:204
@ CARLA_AREA_CROSSWALK
Definition Navigation.h:49
@ CARLA_AREA_SIDEWALK
Definition Navigation.h:48
@ CARLA_AREA_GRASS
Definition Navigation.h:51
@ CARLA_AREA_BLOCK
Definition Navigation.h:47
@ CARLA_AREA_ROAD
Definition Navigation.h:50
@ CARLA_TYPE_CROSSWALK
Definition Navigation.h:61
@ CARLA_TYPE_GRASS
Definition Navigation.h:65
@ CARLA_TYPE_WALKABLE
Definition Navigation.h:71
@ CARLA_TYPE_SIDEWALK
Definition Navigation.h:59
@ CARLA_TYPE_NONE
Definition Navigation.h:57
@ CARLA_TYPE_ROAD
Definition Navigation.h:63
uint32_t ActorId
Definition ActorId.h:20
CARLA模拟器的主命名空间。
Definition Carla.cpp:139
rpc::ActorId ActorId
Definition ActorId.h:26
向人群发送有关车辆的信息的结构体
Definition Navigation.h:77
carla::geom::BoundingBox bounding
Definition Navigation.h:80
carla::geom::Transform transform
Definition Navigation.h:79