CARLA
 
载入中...
搜索中...
未找到
LocalizationStage.cpp
浏览该文件的文档.
1
3
5
6namespace carla {
7namespace traffic_manager {
8
9 // 引入常量定义,简化代码中的常量使用
10using namespace constants::PathBufferUpdate;
11using namespace constants::LaneChange;
12using namespace constants::WaypointSelection;
13using namespace constants::SpeedThreshold;
14using namespace constants::Collision;
15using namespace constants::MotionPlan;
16
17// LocalizationStage类构造函数定义
19 const std::vector<ActorId> &vehicle_id_list, // 车辆ID列表,用于标识哪些车辆将进行本地化操作
20 BufferMap &buffer_map, // 缓冲区映射表,存储每辆车的路径点数据
21 const SimulationState &simulation_state, // 模拟状态,提供车辆的位置信息、速度等
22 TrackTraffic &track_traffic, // 交通管理器,用于管理道路上的交通信息
23 const LocalMapPtr &local_map, // 本地地图指针,用于存储当前的道路与交通信息
24 Parameters &parameters, // 参数配置,包含各种控制参数
25 std::vector<ActorId>& marked_for_removal, // 需要移除的车辆ID列表
26 LocalizationFrame &output_array, // 输出数组,存储每次更新的本地化结果
27 RandomGenerator &random_device) // 随机数生成器,用于某些随机操作
28 : vehicle_id_list(vehicle_id_list), // 初始化车辆ID列表
29 buffer_map(buffer_map), // 初始化缓冲区映射表
30 simulation_state(simulation_state),// 初始化模拟状态
31 track_traffic(track_traffic), // 初始化交通管理器
32 local_map(local_map), // 初始化本地地图
33 parameters(parameters), // 初始化参数配置
34 marked_for_removal(marked_for_removal), // 初始化待移除的车辆列表
35 output_array(output_array), // 初始化输出数组
36 random_device(random_device){} // 初始化随机数生成器
37
38// 更新本地化信息
39void LocalizationStage::Update(const unsigned long index) {
40
41 // 获取当前车辆的ID和相关信息
42 const ActorId actor_id = vehicle_id_list.at(index);
43 const cg::Location vehicle_location = simulation_state.GetLocation(actor_id);
44 const cg::Vector3D heading_vector = simulation_state.GetHeading(actor_id);
45 const cg::Vector3D vehicle_velocity_vector = simulation_state.GetVelocity(actor_id);
46 const float vehicle_speed = vehicle_velocity_vector.Length();
47
48 // 速度相关的航点视野长度
49 float horizon_length = std::max(vehicle_speed * HORIZON_RATE, MINIMUM_HORIZON_LENGTH);
50 // HORIZON_RATE是一个常量,用于根据车辆的速度计算视野长度
51 // 如果车辆的速度超过高速公路限速,则使用一个更大的视野范围
52 if (vehicle_speed > HIGHWAY_SPEED) {
53 horizon_length = std::max(vehicle_speed * HIGH_SPEED_HORIZON_RATE, MINIMUM_HORIZON_LENGTH);
54 }
55 const float horizon_square = SQUARE(horizon_length);
56
57 // 如果当前车辆ID在缓冲区映射表中没有记录,则插入一个新的缓冲区
58 if (buffer_map.find(actor_id) == buffer_map.end()) {
59 buffer_map.insert({actor_id, Buffer()});
60 }
61 Buffer &waypoint_buffer = buffer_map.at(actor_id);
62
63 // 如果车辆离缓冲区里的第一个路点过远则清理路点缓冲区。
64 if (!waypoint_buffer.empty() &&
65 cg::Math::DistanceSquared(waypoint_buffer.front()->GetLocation(),
66 vehicle_location) > SQUARE(MAX_START_DISTANCE)) {
67
68 // 如果第一个路径点距离过远,清除缓冲区中的所有路径点
69 auto number_of_pops = waypoint_buffer.size(); // 获取缓冲区中路径点的数量
70 for (uint64_t j = 0u; j < number_of_pops; ++j) {
71 PopWaypoint(actor_id, track_traffic, waypoint_buffer); // 从缓冲区弹出路径点
72 }
73 }
74
75 bool is_at_junction_entrance = false;
76 if (!waypoint_buffer.empty()) {
77 // 清除已通过的航点
78 float dot_product = DeviationDotProduct(vehicle_location, heading_vector, waypoint_buffer.front()->GetLocation());
79 while (dot_product <= 0.0f && !waypoint_buffer.empty()) {
80 PopWaypoint(actor_id, track_traffic, waypoint_buffer);
81 if (!waypoint_buffer.empty()) {
82 dot_product = DeviationDotProduct(vehicle_location, heading_vector, waypoint_buffer.front()->GetLocation());
83 }
84 }
85
86 if (!waypoint_buffer.empty()) {
87 // 确定车辆是否在交叉路口入口处
88 SimpleWaypointPtr look_ahead_point = GetTargetWaypoint(waypoint_buffer, JUNCTION_LOOK_AHEAD).first;
89 SimpleWaypointPtr front_waypoint = waypoint_buffer.front();
90 bool front_waypoint_junction = front_waypoint->CheckJunction();
91 is_at_junction_entrance = !front_waypoint_junction && look_ahead_point->CheckJunction();
92 if (!is_at_junction_entrance) {
93 std::vector<SimpleWaypointPtr> last_passed_waypoints = front_waypoint->GetPreviousWaypoint();
94 if (last_passed_waypoints.size() == 1) {
95 is_at_junction_entrance = !last_passed_waypoints.front()->CheckJunction() && front_waypoint_junction;
96 }
97 }
98 if (is_at_junction_entrance
99 //Town03中的环岛例外情况
100 && local_map->GetMapName() == "Carla/Maps/Town03"
101 && vehicle_location.SquaredLength() < SQUARE(30)) {
102 is_at_junction_entrance = false;
103 }
104 }
105
106 // 清除缓冲区前端太远的航点,但如果已经到达一个路口,则不要清除
107 while (!is_at_junction_entrance
108 && !waypoint_buffer.empty()
109 && waypoint_buffer.back()->DistanceSquared(waypoint_buffer.front()) > horizon_square + horizon_square
110 && !waypoint_buffer.back()->CheckJunction()) {
111 PopWaypoint(actor_id, track_traffic, waypoint_buffer, false);
112 }
113 }
114
115 // 如果缓冲区为空,则进行初始化
116 if (waypoint_buffer.empty()) {
117 SimpleWaypointPtr closest_waypoint = local_map->GetWaypoint(vehicle_location);
118 PushWaypoint(actor_id, track_traffic, waypoint_buffer, closest_waypoint);
119 }
120
121 // 分配变道
122 const ChangeLaneInfo lane_change_info = parameters.GetForceLaneChange(actor_id);
123 bool force_lane_change = lane_change_info.change_lane;
124 bool lane_change_direction = lane_change_info.direction;
125
126 //应用保持右侧规则和随机变道参数
127 if (!force_lane_change && vehicle_speed > MIN_LANE_CHANGE_SPEED){
128 const float perc_keep_right = parameters.GetKeepRightPercentage(actor_id);
129 const float perc_random_leftlanechange = parameters.GetRandomLeftLaneChangePercentage(actor_id);
130 const float perc_random_rightlanechange = parameters.GetRandomRightLaneChangePercentage(actor_id);
131 const bool is_keep_right = perc_keep_right > random_device.next();
132 const bool is_random_left_change = perc_random_leftlanechange >= random_device.next();
133 const bool is_random_right_change = perc_random_rightlanechange >= random_device.next();
134
135 //确定应应用的参数
136 if (is_keep_right || is_random_right_change) {
137 force_lane_change = true;
138 lane_change_direction = true;
139 }
140 if (is_random_left_change) {
141 if (!force_lane_change) {
142 force_lane_change = true;
143 lane_change_direction = false;
144 } else {
145 // 左右车道变更都是强制性的。请在其中选择一个
146 lane_change_direction = FIFTYPERC > random_device.next();
147 }
148 }
149 }
150
151 const SimpleWaypointPtr front_waypoint = waypoint_buffer.front();
152 const float lane_change_distance = SQUARE(std::max(10.0f * vehicle_speed, INTER_LANE_CHANGE_DISTANCE));
153
154 bool recently_not_executed_lane_change = last_lane_change_swpt.find(actor_id) == last_lane_change_swpt.end();
155 bool done_with_previous_lane_change = true;
156 if (!recently_not_executed_lane_change) {
157 float distance_frm_previous = cg::Math::DistanceSquared(last_lane_change_swpt.at(actor_id)->GetLocation(), vehicle_location);
158 done_with_previous_lane_change = distance_frm_previous > lane_change_distance;
159 if (done_with_previous_lane_change) last_lane_change_swpt.erase(actor_id);
160 }
161 bool auto_or_force_lane_change = parameters.GetAutoLaneChange(actor_id) || force_lane_change;
162 bool front_waypoint_not_junction = !front_waypoint->CheckJunction();
163
164 if (auto_or_force_lane_change
165 && front_waypoint_not_junction
166 && (recently_not_executed_lane_change || done_with_previous_lane_change)) {
167
168 SimpleWaypointPtr change_over_point = AssignLaneChange(actor_id, vehicle_location, vehicle_speed,
169 force_lane_change, lane_change_direction);
170
171 if (change_over_point != nullptr) {
172 if (last_lane_change_swpt.find(actor_id) != last_lane_change_swpt.end()) {
173 last_lane_change_swpt.at(actor_id) = change_over_point;
174 } else {
175 last_lane_change_swpt.insert({actor_id, change_over_point});
176 }
177 auto number_of_pops = waypoint_buffer.size();
178 for (uint64_t j = 0u; j < number_of_pops; ++j) {
179 PopWaypoint(actor_id, track_traffic, waypoint_buffer);
180 }
181 PushWaypoint(actor_id, track_traffic, waypoint_buffer, change_over_point);
182 }
183 }
184
185 Path imported_path = parameters.GetCustomPath(actor_id);
186 Route imported_actions = parameters.GetImportedRoute(actor_id);
187 // 我们实际上是在导入一个路径
188 if (!imported_path.empty()) {
189
190 ImportPath(imported_path, waypoint_buffer, actor_id, horizon_square);
191
192 } else if (!imported_actions.empty()) {
193
194 ImportRoute(imported_actions, waypoint_buffer, actor_id, horizon_square);
195
196 }
197
198 // 通过随机选择航点填充缓冲区
199 else {
200 while (waypoint_buffer.back()->DistanceSquared(waypoint_buffer.front()) <= horizon_square) {
201 SimpleWaypointPtr furthest_waypoint = waypoint_buffer.back();
202 std::vector<SimpleWaypointPtr> next_waypoints = furthest_waypoint->GetNextWaypoint();
203 uint64_t selection_index = 0u;
204 // 伪随机路径选择,如果发现多个选择
205 if (next_waypoints.size() > 1) {
206 double r_sample = random_device.next();
207 selection_index = static_cast<uint64_t>(r_sample*next_waypoints.size()*0.01);
208 } else if (next_waypoints.size() == 0) {
209 if (!parameters.GetOSMMode()) {
210 std::cout << "This map has dead-end roads, please change the set_open_street_map parameter to true" << std::endl;
211 }
212 marked_for_removal.push_back(actor_id);
213 break;
214 }
215 SimpleWaypointPtr next_wp_selection = next_waypoints.at(selection_index);
216 PushWaypoint(actor_id, track_traffic, waypoint_buffer, next_wp_selection);
217 if (next_wp_selection->GetId() == waypoint_buffer.front()->GetId()){
218 // 发现了一个环,停止。不要使用零距离,因为可能有两个航点在同一位置
219 break;
220 }
221 }
222 }
223 ExtendAndFindSafeSpace(actor_id, is_at_junction_entrance, waypoint_buffer);
224
225 // 编辑输出数组
226 LocalizationData &output = output_array.at(index);
227 output.is_at_junction_entrance = is_at_junction_entrance;
228
229 if (is_at_junction_entrance) {
230 const SimpleWaypointPair &safe_space_end_points = vehicles_at_junction_entrance.at(actor_id);
231 output.junction_end_point = safe_space_end_points.first;
232 output.safe_point = safe_space_end_points.second;
233 } else {
234 output.junction_end_point = nullptr;
235 output.safe_point = nullptr;
236 }
237
238 // 更新参与者的测地线网格位置
239 track_traffic.UpdateGridPosition(actor_id, waypoint_buffer);
240}
241
243 const bool is_at_junction_entrance,
244 Buffer &waypoint_buffer) {
245
246 SimpleWaypointPtr junction_end_point = nullptr;
247 SimpleWaypointPtr safe_point_after_junction = nullptr;
248
249 if (is_at_junction_entrance
251
252 bool entered_junction = false;
253 bool past_junction = false;
254 bool safe_point_found = false;
255 SimpleWaypointPtr current_waypoint = nullptr;
256 SimpleWaypointPtr junction_begin_point = nullptr;
257 float safe_distance_squared = SQUARE(SAFE_DISTANCE_AFTER_JUNCTION);
258
259 // 扫描现有缓冲点
260 for (unsigned long i = 0u; i < waypoint_buffer.size() && !safe_point_found; ++i) {
261 current_waypoint = waypoint_buffer.at(i);
262 if (!entered_junction && current_waypoint->CheckJunction()) {
263 entered_junction = true;
264 junction_begin_point = current_waypoint;
265 }
266 if (entered_junction && !past_junction && !current_waypoint->CheckJunction()) {
267 past_junction = true;
268 junction_end_point = current_waypoint;
269 }
270 if (past_junction && junction_end_point->DistanceSquared(current_waypoint) > safe_distance_squared) {
271 safe_point_found = true;
272 safe_point_after_junction = current_waypoint;
273 }
274 }
275
276 // 如果未找到安全点,则扩展缓冲区
277 if (!safe_point_found) {
278 bool abort = false;
279
280 while (!past_junction && !abort) {
281 NodeList next_waypoints = current_waypoint->GetNextWaypoint();
282 if (!next_waypoints.empty()) {
283 current_waypoint = next_waypoints.front();
284 PushWaypoint(actor_id, track_traffic, waypoint_buffer, current_waypoint);
285 if (!current_waypoint->CheckJunction()) {
286 past_junction = true;
287 junction_end_point = current_waypoint;
288 }
289 } else {
290 abort = true;
291 }
292 }
293
294 while (!safe_point_found && !abort) {
295 std::vector<SimpleWaypointPtr> next_waypoints = current_waypoint->GetNextWaypoint();
296 if ((junction_end_point->DistanceSquared(current_waypoint) > safe_distance_squared)
297 || next_waypoints.size() > 1
298 || current_waypoint->CheckJunction()) {
299
300 safe_point_found = true;
301 safe_point_after_junction = current_waypoint;
302 } else {
303 if (!next_waypoints.empty()) {
304 current_waypoint = next_waypoints.front();
305 PushWaypoint(actor_id, track_traffic, waypoint_buffer, current_waypoint);
306 } else {
307 abort = true;
308 }
309 }
310 }
311 }
312
313 if (junction_end_point != nullptr &&
314 safe_point_after_junction != nullptr &&
315 junction_begin_point->DistanceSquared(junction_end_point) < SQUARE(MIN_JUNCTION_LENGTH)) {
316
317 junction_end_point = nullptr;
318 safe_point_after_junction = nullptr;
319 }
320
321 vehicles_at_junction_entrance.insert({actor_id, {junction_end_point, safe_point_after_junction}});
322 }
323 else if (!is_at_junction_entrance
325
326 vehicles_at_junction_entrance.erase(actor_id);
327 }
328}
329
331 last_lane_change_swpt.erase(actor_id);
332 vehicles_at_junction.erase(actor_id);
333}
334
339
341 const cg::Location vehicle_location,
342 const float vehicle_speed,
343 bool force, bool direction) {
344
345 // 航点表示航点缓冲区的新起点
346 // 由于车道变更。如果车道变更不可行,则保持为空指针
347 SimpleWaypointPtr change_over_point = nullptr;
348
349 // 获取当前车辆的航点缓冲区
350 const Buffer &waypoint_buffer = buffer_map.at(actor_id);
351
352 // 检查缓冲区是否不为空
353 if (!waypoint_buffer.empty()) {
354 // 获取当前最近航点的左右航点
355 const SimpleWaypointPtr &current_waypoint = waypoint_buffer.front();
356 const SimpleWaypointPtr left_waypoint = current_waypoint->GetLeftWaypoint();
357 const SimpleWaypointPtr right_waypoint = current_waypoint->GetRightWaypoint();
358
359 // 检索与当前车辆重叠路径点缓冲区的车辆
360 const auto blocking_vehicles = track_traffic.GetOverlappingVehicles(actor_id);
361
362 // 查找车道内即时障碍物,并检查是否有障碍物距离过近,无法进行变道
363 bool obstacle_too_close = false;
364 float minimum_squared_distance = std::numeric_limits<float>::infinity();
365 ActorId obstacle_actor_id = 0u;
366 for (auto i = blocking_vehicles.begin();
367 i != blocking_vehicles.end() && !obstacle_too_close && !force;
368 ++i) {
369 const ActorId &other_actor_id = *i;
370 // 在缓冲区地图中查找车辆,并检查其缓冲区是否不为空
371 if (buffer_map.find(other_actor_id) != buffer_map.end() && !buffer_map.at(other_actor_id).empty()) {
372 const Buffer &other_buffer = buffer_map.at(other_actor_id);
373 const SimpleWaypointPtr &other_current_waypoint = other_buffer.front();
374 const cg::Location other_location = other_current_waypoint->GetLocation();
375
376 const cg::Vector3D reference_heading = current_waypoint->GetForwardVector();
377 cg::Vector3D reference_to_other = other_location - current_waypoint->GetLocation();
378 const cg::Vector3D other_heading = other_current_waypoint->GetForwardVector();
379
380 WaypointPtr current_raw_waypoint = current_waypoint->GetWaypoint();
381 WaypointPtr other_current_raw_waypoint = other_current_waypoint->GetWaypoint();
382 // 检查两辆车是否都不在交叉路口
383 //检查另一辆车是否在当前车辆的前方
384 // 检查两辆车的航向之间是否存在可接受的角偏差
385 if (!current_waypoint->CheckJunction()
386 && !other_current_waypoint->CheckJunction()
387 && other_current_raw_waypoint->GetRoadId() == current_raw_waypoint->GetRoadId()
388 && other_current_raw_waypoint->GetLaneId() == current_raw_waypoint->GetLaneId()
389 && cg::Math::Dot(reference_heading, reference_to_other) > 0.0f
390 && cg::Math::Dot(reference_heading, other_heading) > MAXIMUM_LANE_OBSTACLE_CURVATURE) {
391 float squared_distance = cg::Math::DistanceSquared(vehicle_location, other_location);
392 // 如果障碍物太近,则中止
393 if (squared_distance > SQUARE(MINIMUM_LANE_CHANGE_DISTANCE)) {
394 // 如果新车辆更靠近就记住
395 if (squared_distance < minimum_squared_distance && squared_distance < SQUARE(MAXIMUM_LANE_OBSTACLE_DISTANCE)) {
396 minimum_squared_distance = squared_distance;
397 obstacle_actor_id = other_actor_id;
398 }
399 } else {
400 obstacle_too_close = true;
401 }
402 }
403 }
404 }
405
406 // 如果发现有效的即时障碍
407 if (!obstacle_too_close && obstacle_actor_id != 0u && !force) {
408 const Buffer &other_buffer = buffer_map.at(obstacle_actor_id);
409 const SimpleWaypointPtr &other_current_waypoint = other_buffer.front();
410 const auto other_neighbouring_lanes = {other_current_waypoint->GetLeftWaypoint(),
411 other_current_waypoint->GetRightWaypoint()};
412
413 // 反映障碍物附近相邻车道是否畅通的标志
414 bool distant_left_lane_free = false;
415 bool distant_right_lane_free = false;
416
417 // 检查阻碍车辆附近的相邻车道是否没有其他车辆
418 bool left_right = true;
419 for (auto &candidate_lane_wp : other_neighbouring_lanes) {
420 if (candidate_lane_wp != nullptr &&
421 track_traffic.GetPassingVehicles(candidate_lane_wp->GetId()).size() == 0) {
422
423 if (left_right)
424 distant_left_lane_free = true;
425 else
426 distant_right_lane_free = true;
427 }
428 left_right = !left_right;
429 }
430
431 //基于障碍物附近哪些车道是空闲的,
432 // 找到没有车辆通过的变更点
433 if (distant_right_lane_free && right_waypoint != nullptr
434 && track_traffic.GetPassingVehicles(right_waypoint->GetId()).size() == 0) {
435 change_over_point = right_waypoint;
436 } else if (distant_left_lane_free && left_waypoint != nullptr
437 && track_traffic.GetPassingVehicles(left_waypoint->GetId()).size() == 0) {
438 change_over_point = left_waypoint;
439 }
440 } else if (force) {
441 if (direction && right_waypoint != nullptr) {
442 change_over_point = right_waypoint;
443 } else if (!direction && left_waypoint != nullptr) {
444 change_over_point = left_waypoint;
445 }
446 }
447
448 if (change_over_point != nullptr) {
449 const float change_over_distance = cg::Math::Clamp(1.5f * vehicle_speed, MIN_WPT_DISTANCE, MAX_WPT_DISTANCE);
450 const SimpleWaypointPtr starting_point = change_over_point;
451 while (change_over_point->DistanceSquared(starting_point) < SQUARE(change_over_distance) &&
452 !change_over_point->CheckJunction()) {
453 change_over_point = change_over_point->GetNextWaypoint().front();
454 }
455 }
456 }
457
458 return change_over_point;
459}
460
461void LocalizationStage::ImportPath(Path &imported_path, Buffer &waypoint_buffer, const ActorId actor_id, const float horizon_square) {
462 // 移除已添加到路径中的航点,除了第一个
463 if (parameters.GetUploadPath(actor_id)) {
464 auto number_of_pops = waypoint_buffer.size();
465 for (uint64_t j = 0u; j < number_of_pops - 1; ++j) {
466 PopWaypoint(actor_id, track_traffic, waypoint_buffer, false);
467 }
468 // 我们已经成功导入了该路径。请将其从待导入路径列表中移除
469 parameters.RemoveUploadPath(actor_id, false);
470 }
471
472 // 获取最新的导入航点,并在TM的InMemoryMap中找到与其最近的航点
473 cg::Location latest_imported = imported_path.front();
474 SimpleWaypointPtr imported = local_map->GetWaypoint(latest_imported);
475
476 //我们需要生成一条与TM航点兼容的路径
477 while (!imported_path.empty() && waypoint_buffer.back()->DistanceSquared(waypoint_buffer.front()) <= horizon_square) {
478 // 获取我们添加到列表中的最新点。如果从起点开始,这将是与车辆位置相关的点
479 SimpleWaypointPtr latest_waypoint = waypoint_buffer.back();
480
481 // 尝试将最新的航点与导入的航点进行关联
482 std::vector<SimpleWaypointPtr> next_waypoints = latest_waypoint->GetNextWaypoint();
483 uint64_t selection_index = 0u;
484
485 // 选择正确的路径
486 if (next_waypoints.size() > 1) {
487 const float imported_road_id = imported->GetWaypoint()->GetRoadId();
488 float min_distance = std::numeric_limits<float>::infinity();
489 for (uint64_t k = 0u; k < next_waypoints.size(); ++k) {
490 SimpleWaypointPtr junction_end_point = next_waypoints.at(k);
491 while (!junction_end_point->CheckJunction()) {
492 junction_end_point = junction_end_point->GetNextWaypoint().front();
493 }
494 while (junction_end_point->CheckJunction()) {
495 junction_end_point = junction_end_point->GetNextWaypoint().front();
496 }
497 while (next_waypoints.at(k)->DistanceSquared(junction_end_point) < 50.0f) {
498 junction_end_point = junction_end_point->GetNextWaypoint().front();
499 }
500 float jep_road_id = junction_end_point->GetWaypoint()->GetRoadId();
501 if (jep_road_id == imported_road_id) {
502 selection_index = k;
503 break;
504 }
505 float distance = junction_end_point->DistanceSquared(imported);
506 if (distance < min_distance) {
507 min_distance = distance;
508 selection_index = k;
509 }
510 }
511 } else if (next_waypoints.size() == 0) {
512 if (!parameters.GetOSMMode()) {
513 std::cout << "This map has dead-end roads, please change the set_open_street_map parameter to true" << std::endl;
514 }
515 marked_for_removal.push_back(actor_id);
516 break;
517 }
518 SimpleWaypointPtr next_wp_selection = next_waypoints.at(selection_index);
519
520 // 如果导入的路点接近最后一个路点,则将其从路径中移除
521 if (next_wp_selection->DistanceSquared(imported) < 30.0f) {
522 imported_path.erase(imported_path.begin());
523 std::vector<SimpleWaypointPtr> possible_waypoints = next_wp_selection->GetNextWaypoint();
524 if (std::find(possible_waypoints.begin(), possible_waypoints.end(), imported) != possible_waypoints.end()) {
525 //如果正在变道,只需推送新的路径点
526 PushWaypoint(actor_id, track_traffic, waypoint_buffer, next_wp_selection);
527 }
528 PushWaypoint(actor_id, track_traffic, waypoint_buffer, imported);
529 latest_imported = imported_path.front();
530 imported = local_map->GetWaypoint(latest_imported);
531 } else {
532 PushWaypoint(actor_id, track_traffic, waypoint_buffer, next_wp_selection);
533 }
534 }
535 if (imported_path.empty()) {
536 // 一旦完成,检查是否可以清除该结构
537 parameters.RemoveUploadPath(actor_id, true);
538 } else {
539 // 否则,使用我们仍需导入的路点更新结构
540 parameters.UpdateUploadPath(actor_id, imported_path);
541 }
542}
543
544void LocalizationStage::ImportRoute(Route &imported_actions, Buffer &waypoint_buffer, const ActorId actor_id, const float horizon_square) {
545
546 if (parameters.GetUploadRoute(actor_id)) {
547 auto number_of_pops = waypoint_buffer.size();
548 for (uint64_t j = 0u; j < number_of_pops - 1; ++j) {
549 PopWaypoint(actor_id, track_traffic, waypoint_buffer, false);
550 }
551 //我们已成功导入该路由。请将其从待导入路由列表中移除
552 parameters.RemoveImportedRoute(actor_id, false);
553 }
554
555 RoadOption next_road_option = static_cast<RoadOption>(imported_actions.front());
556 while (!imported_actions.empty() && waypoint_buffer.back()->DistanceSquared(waypoint_buffer.front()) <= horizon_square) {
557 // 获取我们添加到列表中的最新点。如果是起点,这将是与车辆位置相关的点
558 SimpleWaypointPtr latest_waypoint = waypoint_buffer.back();
559 RoadOption latest_road_option = latest_waypoint->GetRoadOption();
560 // 尝试将最新的航点与正确的下一个路线选项关联起来
561 std::vector<SimpleWaypointPtr> next_waypoints = latest_waypoint->GetNextWaypoint();
562 uint16_t selection_index = 0u;
563 if (next_waypoints.size() > 1) {
564 for (uint16_t i=0; i<next_waypoints.size(); ++i) {
565 if (next_waypoints.at(i)->GetRoadOption() == next_road_option) {
566 selection_index = i;
567 break;
568 } else {
569 if (i == next_waypoints.size() - 1) {
570 std::cout << "We couldn't find the RoadOption you were looking for. This route might diverge from the one expected." << std::endl;
571 }
572 }
573 }
574 } else if (next_waypoints.size() == 0) {
575 if (!parameters.GetOSMMode()) {
576 std::cout << "This map has dead-end roads, please change the set_open_street_map parameter to true" << std::endl;
577 }
578 marked_for_removal.push_back(actor_id);
579 break;
580 }
581
582 SimpleWaypointPtr next_wp_selection = next_waypoints.at(selection_index);
583 PushWaypoint(actor_id, track_traffic, waypoint_buffer, next_wp_selection);
584
585 // 如果我们正在切换到新的道路选项,这意味着当前的道路选项已经完全导入
586 if (latest_road_option != next_wp_selection->GetRoadOption() && next_road_option == next_wp_selection->GetRoadOption()) {
587 imported_actions.erase(imported_actions.begin());
588 next_road_option = static_cast<RoadOption>(imported_actions.front());
589 }
590 }
591 if (imported_actions.empty()) {
592 // 一旦完成,检查我们是否可以清除该结构
593 parameters.RemoveImportedRoute(actor_id, true);
594 } else {
595 // 否则,使用我们仍然需要导入的路点更新结构
596 parameters.UpdateImportedRoute(actor_id, imported_actions);
597 }
598}
599
601 auto waypoint_buffer = buffer_map.at(actor_id);
602 auto next_action = std::make_pair(RoadOption::LaneFollow, waypoint_buffer.back()->GetWaypoint());
603 bool is_lane_change = false;
604 if (last_lane_change_swpt.find(actor_id) != last_lane_change_swpt.end()) {
605 // 正在发生车道变更
606 is_lane_change = true;
607 const cg::Vector3D heading_vector = simulation_state.GetHeading(actor_id);
608 const cg::Vector3D relative_vector = simulation_state.GetLocation(actor_id) - last_lane_change_swpt.at(actor_id)->GetLocation();
609 bool left_heading = (heading_vector.x * relative_vector.y - heading_vector.y * relative_vector.x) > 0.0f;
610 if (left_heading) next_action = std::make_pair(RoadOption::ChangeLaneLeft, last_lane_change_swpt.at(actor_id)->GetWaypoint());
611 else next_action = std::make_pair(RoadOption::ChangeLaneRight, last_lane_change_swpt.at(actor_id)->GetWaypoint());
612 }
613 for (auto &swpt : waypoint_buffer) {
614 RoadOption road_opt = swpt->GetRoadOption();
615 if (road_opt != RoadOption::LaneFollow) {
616 if (!is_lane_change) {
617 // 没有看到变道的迹象,我们可以假设这将是下一个动作
618 return std::make_pair(road_opt, swpt->GetWaypoint());
619 } else {
620 // 变道和另一个动作都会发生,我们需要弄清楚哪一个会先发生
621 cg::Location lane_change = last_lane_change_swpt.at(actor_id)->GetLocation();
622 cg::Location actual_location = simulation_state.GetLocation(actor_id);
623 auto distance_lane_change = cg::Math::DistanceSquared(actual_location, lane_change);
624 auto distance_other_action = cg::Math::DistanceSquared(actual_location, swpt->GetLocation());
625 if (distance_lane_change < distance_other_action) return next_action;
626 else return std::make_pair(road_opt, swpt->GetWaypoint());
627 }
628 }
629 }
630 return next_action;
631}
632
634
635 auto waypoint_buffer = buffer_map.at(actor_id);
636 ActionBuffer action_buffer;
637 Action lane_change;
638 bool is_lane_change = false;
639 SimpleWaypointPtr buffer_front = waypoint_buffer.front();
640 RoadOption last_road_opt = buffer_front->GetRoadOption();
641 action_buffer.push_back(std::make_pair(last_road_opt, buffer_front->GetWaypoint()));
642 if (last_lane_change_swpt.find(actor_id) != last_lane_change_swpt.end()) {
643 // 正在发生变道
644 is_lane_change = true;
645 const cg::Vector3D heading_vector = simulation_state.GetHeading(actor_id);
646 const cg::Vector3D relative_vector = simulation_state.GetLocation(actor_id) - last_lane_change_swpt.at(actor_id)->GetLocation();
647 bool left_heading = (heading_vector.x * relative_vector.y - heading_vector.y * relative_vector.x) > 0.0f;
648 if (left_heading) lane_change = std::make_pair(RoadOption::ChangeLaneLeft, last_lane_change_swpt.at(actor_id)->GetWaypoint());
649 else lane_change = std::make_pair(RoadOption::ChangeLaneRight, last_lane_change_swpt.at(actor_id)->GetWaypoint());
650 }
651 for (auto &wpt : waypoint_buffer) {
652 RoadOption current_road_opt = wpt->GetRoadOption();
653 if (current_road_opt != last_road_opt) {
654 action_buffer.push_back(std::make_pair(current_road_opt, wpt->GetWaypoint()));
655 last_road_opt = current_road_opt;
656 }
657 }
658 if (is_lane_change) {
659 // 在动作缓冲区的适当部分插入变道动作
660 auto distance_lane_change = cg::Math::DistanceSquared(waypoint_buffer.front()->GetLocation(), lane_change.second->GetTransform().location);
661 for (uint16_t i = 0; i < action_buffer.size(); ++i) {
662 auto distance_action = cg::Math::DistanceSquared(waypoint_buffer.front()->GetLocation(), waypoint_buffer.at(i)->GetLocation());
663 // 如果与下一步行动相关的方式点距离变道的方式点更远,请在此处插入变道行动
664 // 如果我们到达了缓冲区的末尾,则将操作放在末尾
665 if (i == action_buffer.size()-1) {
666 action_buffer.push_back(lane_change);
667 break;
668 } else if (distance_action > distance_lane_change) {
669 action_buffer.insert(action_buffer.begin()+i, lane_change);
670 break;
671 }
672 }
673 }
674 return action_buffer;
675}
676
677} // namespace traffic_manager
678} // namespace carla
#define SQUARE(a)
This file contains various constants used in traffic manager arranged into sensible namespaces for re...
Definition Constants.h:13
float SquaredLength() const
ActionBuffer ComputeActionBuffer(const ActorId &actor_id)
void ImportRoute(Route &imported_actions, Buffer &waypoint_buffer, const ActorId actor_id, const float horizon_square)
LocalizationStage(const std::vector< ActorId > &vehicle_id_list, BufferMap &buffer_map, const SimulationState &simulation_state, TrackTraffic &track_traffic, const LocalMapPtr &local_map, Parameters &parameters, std::vector< ActorId > &marked_for_removal, LocalizationFrame &output_array, RandomGenerator &random_device)
void ImportPath(Path &imported_path, Buffer &waypoint_buffer, const ActorId actor_id, const float horizon_square)
void ExtendAndFindSafeSpace(const ActorId actor_id, const bool is_at_junction_entrance, Buffer &waypoint_buffer)
Action ComputeNextAction(const ActorId &actor_id)
std::unordered_map< ActorId, SimpleWaypointPair > vehicles_at_junction_entrance
const std::vector< ActorId > & vehicle_id_list
SimpleWaypointPtr AssignLaneChange(const ActorId actor_id, const cg::Location vehicle_location, const float vehicle_speed, bool force, bool direction)
void Update(const unsigned long index) override
更新方法。
void RemoveActor(const ActorId actor_id) override
移除参与者方法。
std::pair< SimpleWaypointPtr, SimpleWaypointPtr > SimpleWaypointPair
float GetRandomLeftLaneChangePercentage(const ActorId &actor_id)
查询车辆随机右变道百分比概率的方法
ChangeLaneInfo GetForceLaneChange(const ActorId &actor_id)
查询车辆变道指令的方法
Route GetImportedRoute(const ActorId &actor_id) const
获取自定义路由的方法
bool GetUploadRoute(const ActorId &actor_id) const
获取是否正在上传路线的方法
void UpdateUploadPath(const ActorId &actor_id, const Path path)
更新已设置的点列表的方法
float GetKeepRightPercentage(const ActorId &actor_id)
查询车辆保持右侧规则的百分比概率的方法
bool GetOSMMode() const
获取Open Street Map模式的方法
void UpdateImportedRoute(const ActorId &actor_id, const Route route)
更新已设置路线的方法
bool GetUploadPath(const ActorId &actor_id) const
获取是否正在上传路径的方法
float GetRandomRightLaneChangePercentage(const ActorId &actor_id)
查询车辆随机向左变道百分比概率的方法
void RemoveImportedRoute(const ActorId &actor_id, const bool remove_path)
移除路线的方法
bool GetAutoLaneChange(const ActorId &actor_id) const
查询车辆自动变道规则的方法
Path GetCustomPath(const ActorId &actor_id) const
获取自定义路径的方法
void RemoveUploadPath(const ActorId &actor_id, const bool remove_path)
移除一组点的方法
该类保持了仿真中所有车辆的状态。
cg::Location GetLocation(const ActorId actor_id) const
cg::Vector3D GetHeading(const ActorId actor_id) const
cg::Vector3D GetVelocity(const ActorId actor_id) const
void UpdateGridPosition(const ActorId actor_id, const Buffer &buffer)
ActorIdSet GetPassingVehicles(uint64_t waypoint_id) const
ActorIdSet GetOverlappingVehicles(ActorId actor_id) const
carla::SharedPtr< cc::Waypoint > WaypointPtr
Waypoint的智能指针类型别名。
Definition InMemoryMap.h:45
void PopWaypoint(ActorId actor_id, TrackTraffic &track_traffic, Buffer &buffer, bool front_or_back)
std::vector< SimpleWaypointPtr > NodeList
Definition InMemoryMap.h:47
std::vector< uint8_t > Route
路线类型,由一系列地理位置组成
std::vector< cg::Location > Path
参与者的唯一标识符类型
std::vector< Action > ActionBuffer
动作缓冲区类型别名。
std::deque< std::shared_ptr< SimpleWaypoint > > Buffer
void PushWaypoint(ActorId actor_id, TrackTraffic &track_traffic, Buffer &buffer, SimpleWaypointPtr &waypoint)
std::shared_ptr< InMemoryMap > LocalMapPtr
本地地图指针类型,使用智能指针管理InMemoryMap对象
std::shared_ptr< SimpleWaypoint > SimpleWaypointPtr
std::pair< RoadOption, WaypointPtr > Action
动作类型别名。
TargetWPInfo GetTargetWaypoint(const Buffer &waypoint_buffer, const float &target_point_distance)
carla::ActorId ActorId
参与者的智能指针类型
std::vector< LocalizationData > LocalizationFrame
std::unordered_map< carla::ActorId, Buffer > BufferMap
float DeviationDotProduct(const cg::Location &reference_location, const cg::Vector3D &heading_vector, const cg::Location &target_location)
返回车辆朝向向量与指向下一个目标路点方向向量之间的点积
CARLA模拟器的主命名空间。
Definition Carla.cpp:139
路线类型,由一系列字节组成,表示路线信息
Definition Parameters.h:32