CARLA
 
载入中...
搜索中...
未找到
TrafficLight.cpp
浏览该文件的文档.
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#include "carla/client/TrafficLight.h" // TrafficLight类的定义文件
8// 提供了与模拟器交互和演员列表相关的功能
11
12#include <unordered_map>
13#include <unordered_set>
14
15namespace carla {
16namespace client {
17 // 设置交通信号灯的当前状态(如红灯、黄灯、绿灯)
18// 接收一个rpc::TrafficLightState 类型的参数,表示要设置的交通信号灯的状态
20 GetEpisode().Lock()->SetTrafficLightState(*this, state);
21 }
22
23 //获取交通信号灯的当前状态
24
26 return GetEpisode().Lock()->GetActorSnapshot(*this).state.traffic_light_data.state;
27 }
28 // 设置绿灯持续时间
29// 接收一个浮点数参数,表示绿灯持续时间
30 void TrafficLight::SetGreenTime(float green_time) {
31 GetEpisode().Lock()->SetTrafficLightGreenTime(*this, green_time);
32 }
33
34 // 获取绿灯持续时间
36 return GetEpisode().Lock()->GetActorSnapshot(*this).state.traffic_light_data.green_time;
37 }
38
39 //设置黄灯持续时间
40 void TrafficLight::SetYellowTime(float yellow_time) {
41 GetEpisode().Lock()->SetTrafficLightYellowTime(*this, yellow_time);
42 }
43
44 //获取黄灯持续时间
46 return GetEpisode().Lock()->GetActorSnapshot(*this).state.traffic_light_data.yellow_time;
47 }
48
49 // 设置红灯持续时间
50 void TrafficLight::SetRedTime(float red_time) {
51 GetEpisode().Lock()->SetTrafficLightRedTime(*this, red_time);
52 }
53
54 // 获取红灯持续时间
56 return GetEpisode().Lock()->GetActorSnapshot(*this).state.traffic_light_data.red_time;
57 }
58
59 // 获取当前信号灯周期内已过去的时间
61 return GetEpisode().Lock()->GetActorSnapshot(*this).state.traffic_light_data.elapsed_time;
62 }
63
64 // 冻结或解冻信号灯的状态
65 void TrafficLight::Freeze(bool freeze) {
66 // 冻结或解冻所有信号灯
67 GetEpisode().Lock()->FreezeAllTrafficLights(freeze);
68 }
69
70 //检查信号灯是否被冻结
72 return GetEpisode().Lock()->GetActorSnapshot(*this).state.traffic_light_data.time_is_frozen;
73 }
74
75 // 获取当前信号灯的杆编号
77 {
78 return GetEpisode().Lock()->GetActorSnapshot(*this).state.traffic_light_data.pole_index;
79 }
80
81 // 获取属于同一组的所有信号灯
82 std::vector<SharedPtr<TrafficLight>> TrafficLight::GetGroupTrafficLights() {
83 std::vector<SharedPtr<TrafficLight>> result;
84 //获取同组的信号灯ID列表
85 auto ids = GetEpisode().Lock()->GetGroupTrafficLights(*this);
86 for (auto id : ids) {
87 // 查找每个ID对应的参与者,并转换为TrafficLight类型,加入结果列表
88 SharedPtr<Actor> actor = GetWorld().GetActors()->Find(id);
89 result.push_back(boost::static_pointer_cast<TrafficLight>(actor));
90 }
91 return result;
92 }
93
94 // 重置交通信号灯组
96 // 调用当前交通信号灯所属的交通信号灯组的重置方法
97 GetEpisode().Lock()->ResetTrafficLightGroup(*this);
98 }
99
100 // 获取交通信号灯影响的车道的路点
101 std::vector<SharedPtr<Waypoint>> TrafficLight::GetAffectedLaneWaypoints() const {
102 std::vector<SharedPtr<Waypoint>> result; // 存储受影响车道的路点结果
103 SharedPtr<Map> carla_map = GetEpisode().Lock()->GetCurrentMap(); // 获取当前的地图对象
104 std::vector<SharedPtr<Landmark>> landmarks = carla_map->GetLandmarksFromId(GetOpenDRIVEID()); // 获取与交通信号灯相关的地标
105 for (auto& landmark : landmarks) {
106 // 遍历地标,检查其影响的车道范围
107 for (const road::LaneValidity& validity : landmark->GetValidities()) {
108 // 如果车道范围是从小到大的
109 if (validity._from_lane < validity._to_lane) {
110 for (int lane_id = validity._from_lane; lane_id <= validity._to_lane; ++lane_id) {
111 if(lane_id == 0) continue; // 跳过中心车道
112 result.emplace_back(
113 carla_map->GetWaypointXODR(
114 landmark->GetRoadId(), lane_id, static_cast<float>(landmark->GetS()))); // 获取车道路点
115 }
116 } else { // 车道范围是从大到小的
117 for (int lane_id = validity._from_lane; lane_id >= validity._to_lane; --lane_id) {
118 if(lane_id == 0) continue; // 跳过中心车道
119 result.emplace_back(
120 carla_map->GetWaypointXODR(
121 landmark->GetRoadId(), lane_id, static_cast<float>(landmark->GetS()))); // 获取车道路点
122 }
123 }
124 }
125 }
126 return result; //返回受影响的车道路点
127 }
128
129 // 获取交通信号灯的边界框方法
130 std::vector<geom::BoundingBox> TrafficLight::GetLightBoxes() const {
131 return GetEpisode().Lock()->GetLightBoxes(*this);
132 }
133
134 // 获取交通信号灯的OpenDRIVE标识符
136 // 通过交通信号灯的快照数据获取其OpenDRIVE的标识符
137 return GetEpisode().Lock()->GetActorSnapshot(*this).state.traffic_light_data.sign_id;
138 }
139
140 std::vector<SharedPtr<Waypoint>> TrafficLight::GetStopWaypoints() const {
141 // 定义一个向量用于存储结果(停止点的路标)
142 std::vector<SharedPtr<Waypoint>> result;
143 // 获取当前地图的指针
144 SharedPtr<Map> carla_map = GetEpisode().Lock()->GetCurrentMap();
145 //获取交通信号灯的触发范围
147 // 获取交通信号灯的全局变换
148 geom::Transform transform = GetTransform();
149 // 提取Bounding Box 的中心位置
150 geom::Location box_position = box.location;
151 // 将 Bounding Box 的位置转换为全局坐标
152 transform.TransformPoint(box_position);
153 // 获取信号灯面朝的方向向量
154 geom::Vector3D right_direction = transform.GetForwardVector();
155 // 定义遍历 Bounding Box 沿x轴的范围:从最小x值到最大x值
156 float min_x = -0.9f*box.extent.x; // x的最小范围
157 float max_x = 0.9f*box.extent.x; // x的最大范围
158 float current_x = min_x; //初始化当前x值为最小范围
159 // 定义一个哈希表,用于记录已经访问过的道路和车道ID
160 std::unordered_map<road::RoadId, std::unordered_set<road::LaneId>> road_lanes_map;
161 // 遍历Bounding Box 沿x轴的范围
162 while (current_x < max_x) {
163 // 根据当前x值计算查询点的全局位置
164 geom::Location query_point = box_position + geom::Location(right_direction*current_x);
165 // 获取查询点对应的路标
166 SharedPtr<Waypoint> waypoint = carla_map->GetWaypoint(query_point);
167 // 检查当前的路标是否已经被处理过
168 if (road_lanes_map[waypoint->GetRoadId()].count(waypoint->GetLaneId()) == 0) {
169 // 如果未处理过,则将道路ID和车道ID记录到哈希表中
170 road_lanes_map[waypoint->GetRoadId()].insert(waypoint->GetLaneId());
171 // 将路标加入结果列表
172 result.emplace_back(waypoint);
173 }
174 // 将当前x值增加,继续处理下一个点
175 current_x += 1.f;
176 }
177 return result; // 返回结果列表
178 }
179
180} // namespace client
181} // namespace carla
geom::Transform GetTransform() const
返回行为体的当前变换(位置和方向)。
void SetGreenTime(float green_time)
void SetYellowTime(float yellow_time)
rpc::TrafficLightState GetState() const
返回交通灯的当前状态。
road::SignId GetOpenDRIVEID() const
std::vector< SharedPtr< TrafficLight > > GetGroupTrafficLights()
返回该交通灯所属组中的所有交通灯。
void SetState(rpc::TrafficLightState state)
uint32_t GetPoleIndex()
返回交通信号灯组中灯杆的索引 // 获取交通信号灯组中灯杆的索引值的函数,返回一个uint32_t类型的索引值,用于标识交通灯在所属灯组中的具体位置等相关信息
std::vector< geom::BoundingBox > GetLightBoxes() const
std::vector< SharedPtr< Waypoint > > GetStopWaypoints() const
std::vector< SharedPtr< Waypoint > > GetAffectedLaneWaypoints() const
void SetRedTime(float red_time)
const geom::BoundingBox & GetTriggerVolume() const
Definition TrafficSign.h:20
SharedPtr< ActorList > GetActors() const
返回一个包含当前世界上所有存在的参与者(actor)的列表。 获取整个模拟世界中所有参与者的集合,便于进行批量操作、统计或者遍历所有实体执行某些通用的操作等。
Definition World.cpp:114
SharedPtrType Lock() const
与 TryLock 相同,但永远不会返回 nullptr。
Location location
边界框的中心位置(本地坐标系下)
Vector3D extent
边界框的半尺寸(本地坐标系下,表示在每个轴方向上的半宽、半高和半深)
void TransformPoint(Vector3D &in_point) const
将此变换应用于 in_point(先平移然后旋转)。 具体操作是先根据当前的旋转信息对输入点进行旋转操作,然后再根据当前的位置信息进行平移操作,最终将结果赋值回输入点
Vector3D GetForwardVector() const
std::string SignId
Definition RoadTypes.h:35
CARLA模拟器的主命名空间。
Definition Carla.cpp:139
boost::shared_ptr< T > SharedPtr
使用这个SharedPtr(boost::shared_ptr)以保持与boost::python的兼容性, 但未来如果可能的话,我们希望能为std::shared_ptr制作一个Python适配器。
Definition Memory.h:19
包含CARLA客户端相关类和函数的命名空间。