forked from autowarefoundation/autoware.universe
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathscene_intersection.cpp
1415 lines (1335 loc) · 63.7 KB
/
scene_intersection.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2020 Tier IV, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "scene_intersection.hpp"
#include "util.hpp"
#include <behavior_velocity_planner_common/utilization/boost_geometry_helper.hpp> // for toGeomPoly
#include <behavior_velocity_planner_common/utilization/util.hpp>
#include <lanelet2_extension/regulatory_elements/autoware_traffic_light.hpp>
#include <lanelet2_extension/utility/utilities.hpp>
#include <motion_utils/trajectory/trajectory.hpp>
#include <tier4_autoware_utils/geometry/boost_polygon_utils.hpp> // for toPolygon2d
#include <tier4_autoware_utils/geometry/geometry.hpp>
#include <tier4_autoware_utils/ros/uuid_helper.hpp>
#include <boost/geometry/algorithms/within.hpp>
#include <lanelet2_core/geometry/Polygon.h>
#include <lanelet2_core/primitives/BasicRegulatoryElements.h>
#include <lanelet2_core/primitives/CompoundPolygon.h>
#include <lanelet2_core/primitives/Lanelet.h>
#include <algorithm>
#include <limits>
#include <memory>
#include <vector>
namespace behavior_velocity_planner
{
namespace bg = boost::geometry;
using intersection::make_err;
using intersection::make_ok;
using intersection::Result;
IntersectionModule::IntersectionModule(
const int64_t module_id, const int64_t lane_id,
[[maybe_unused]] std::shared_ptr<const PlannerData> planner_data,
const PlannerParam & planner_param, const std::set<lanelet::Id> & associative_ids,
const std::string & turn_direction, const bool has_traffic_light, rclcpp::Node & node,
const rclcpp::Logger logger, const rclcpp::Clock::SharedPtr clock)
: SceneModuleInterface(module_id, logger, clock),
planner_param_(planner_param),
lane_id_(lane_id),
associative_ids_(associative_ids),
turn_direction_(turn_direction),
has_traffic_light_(has_traffic_light),
occlusion_uuid_(tier4_autoware_utils::generateUUID())
{
velocity_factor_.init(PlanningBehavior::INTERSECTION);
{
collision_state_machine_.setMarginTime(
planner_param_.collision_detection.collision_detection_hold_time);
}
{
before_creep_state_machine_.setMarginTime(
planner_param_.occlusion.temporal_stop_time_before_peeking);
before_creep_state_machine_.setState(StateMachine::State::STOP);
}
{
occlusion_stop_state_machine_.setMarginTime(
planner_param_.occlusion.occlusion_detection_hold_time);
occlusion_stop_state_machine_.setState(StateMachine::State::GO);
}
{
temporal_stop_before_attention_state_machine_.setMarginTime(
planner_param_.occlusion.occlusion_detection_hold_time);
temporal_stop_before_attention_state_machine_.setState(StateMachine::State::STOP);
}
{
static_occlusion_timeout_state_machine_.setMarginTime(
planner_param_.occlusion.static_occlusion_with_traffic_light_timeout);
static_occlusion_timeout_state_machine_.setState(StateMachine::State::STOP);
}
ego_ttc_pub_ = node.create_publisher<tier4_debug_msgs::msg::Float64MultiArrayStamped>(
"~/debug/intersection/ego_ttc", 1);
object_ttc_pub_ = node.create_publisher<tier4_debug_msgs::msg::Float64MultiArrayStamped>(
"~/debug/intersection/object_ttc", 1);
}
bool IntersectionModule::modifyPathVelocity(PathWithLaneId * path, StopReason * stop_reason)
{
debug_data_ = DebugData();
*stop_reason = planning_utils::initializeStopReason(StopReason::INTERSECTION);
initializeRTCStatus();
const auto decision_result = modifyPathVelocityDetail(path, stop_reason);
prev_decision_result_ = decision_result;
{
const std::string decision_type = "intersection" + std::to_string(module_id_) + " : " +
intersection::formatDecisionResult(decision_result);
internal_debug_data_.decision_type = decision_type;
}
prepareRTCStatus(decision_result, *path);
reactRTCApproval(decision_result, path, stop_reason);
return true;
}
void IntersectionModule::initializeRTCStatus()
{
setSafe(true);
setDistance(std::numeric_limits<double>::lowest());
// occlusion
occlusion_safety_ = true;
occlusion_stop_distance_ = std::numeric_limits<double>::lowest();
occlusion_first_stop_required_ = false;
// activated_ and occlusion_activated_ must be set from manager's RTC callback
}
static std::string formatOcclusionType(const IntersectionModule::OcclusionType & type)
{
if (std::holds_alternative<IntersectionModule::NotOccluded>(type)) {
return "NotOccluded and the best occlusion clearance is " +
std::to_string(std::get<IntersectionModule::NotOccluded>(type).best_clearance_distance);
}
if (std::holds_alternative<IntersectionModule::StaticallyOccluded>(type)) {
return "StaticallyOccluded and the best occlusion clearance is " +
std::to_string(
std::get<IntersectionModule::StaticallyOccluded>(type).best_clearance_distance);
}
if (std::holds_alternative<IntersectionModule::DynamicallyOccluded>(type)) {
return "DynamicallyOccluded and the best occlusion clearance is " +
std::to_string(
std::get<IntersectionModule::DynamicallyOccluded>(type).best_clearance_distance);
}
return "RTCOccluded";
}
intersection::DecisionResult IntersectionModule::modifyPathVelocityDetail(
PathWithLaneId * path, [[maybe_unused]] StopReason * stop_reason)
{
const auto prepare_data = prepareIntersectionData(path);
if (!prepare_data) {
return prepare_data.err();
}
const auto [interpolated_path_info, intersection_stoplines, path_lanelets] = prepare_data.ok();
const auto & intersection_lanelets = intersection_lanelets_.value();
// NOTE: this level is based on the updateTrafficSignalObservation() which is latest
const auto traffic_prioritized_level = getTrafficPrioritizedLevel();
const bool is_prioritized =
traffic_prioritized_level == TrafficPrioritizedLevel::FULLY_PRIORITIZED;
// ==========================================================================================
// stuck detection
//
// stuck vehicle detection is viable even if attention area is empty
// so this needs to be checked before attention area validation
// ==========================================================================================
const auto is_stuck_status = isStuckStatus(*path, intersection_stoplines, path_lanelets);
if (is_stuck_status) {
return is_stuck_status.value();
}
// ==========================================================================================
// basic data validation
//
// if attention area is empty, collision/occlusion detection is impossible
//
// if attention area is not null but default stop line is not available, ego/backward-path has
// already passed the stop line, so ego is already in the middle of the intersection, or the
// end of the ego path has just entered the entry of this intersection
//
// occlusion stop line is generated from the intersection of ego footprint along the path with the
// attention area, so if this is null, ego has already passed the intersection, or the end of the
// ego path has just entered the entry of this intersection
// ==========================================================================================
if (!intersection_lanelets.first_attention_area()) {
return intersection::InternalError{"attention area is empty"};
}
const auto first_attention_area = intersection_lanelets.first_attention_area().value();
const auto default_stopline_idx_opt = intersection_stoplines.default_stopline;
if (!default_stopline_idx_opt) {
return intersection::InternalError{"default stop line is null"};
}
const auto default_stopline_idx = default_stopline_idx_opt.value();
const auto first_attention_stopline_idx_opt = intersection_stoplines.first_attention_stopline;
const auto occlusion_peeking_stopline_idx_opt = intersection_stoplines.occlusion_peeking_stopline;
if (!first_attention_stopline_idx_opt || !occlusion_peeking_stopline_idx_opt) {
return intersection::InternalError{"occlusion stop line is null"};
}
const auto first_attention_stopline_idx = first_attention_stopline_idx_opt.value();
const auto occlusion_stopline_idx = occlusion_peeking_stopline_idx_opt.value();
// ==========================================================================================
// classify the objects to attention_area/intersection_area and update their position, velocity,
// belonging attention lanelet, distance to corresponding stopline
// ==========================================================================================
updateObjectInfoManagerArea();
// ==========================================================================================
// occlusion_status is type of occlusion,
// is_occlusion_cleared_with_margin indicates if occlusion is physically detected
// is_occlusion_state indicates if occlusion is detected. OR occlusion is not detected but RTC for
// intersection_occlusion is disapproved, which means ego is virtually occluded
//
// so is_occlusion_cleared_with_margin should be sent to RTC as module decision
// and is_occlusion_status should be only used to decide ego action
// !is_occlusion_state == !physically_occluded && !externally_occluded, so even if occlusion is
// not detected but not approved, SAFE is not sent.
// ==========================================================================================
const auto [occlusion_status, is_occlusion_cleared_with_margin, is_occlusion_state] =
getOcclusionStatus(traffic_prioritized_level, interpolated_path_info);
const auto
[is_over_1st_pass_judge_line, is_over_2nd_pass_judge_line, safely_passed_1st_judge_line,
safely_passed_2nd_judge_line] =
isOverPassJudgeLinesStatus(*path, is_occlusion_state, intersection_stoplines);
// ==========================================================================================
// calculate the expected vehicle speed and obtain the spatiotemporal profile of ego to the
// exit of intersection
// ==========================================================================================
tier4_debug_msgs::msg::Float64MultiArrayStamped ego_ttc_time_array;
const auto time_distance_array =
calcIntersectionPassingTime(*path, is_prioritized, intersection_stoplines, &ego_ttc_time_array);
// ==========================================================================================
// run collision checking for each objects considering traffic light level. Also if ego just
// passed each pass judge line for the first time, save current collision status for late
// diagnosis
// ==========================================================================================
updateObjectInfoManagerCollision(
path_lanelets, time_distance_array, traffic_prioritized_level, safely_passed_1st_judge_line,
safely_passed_2nd_judge_line);
for (const auto & object_info : object_info_manager_.attentionObjects()) {
if (!object_info->unsafe_info()) {
continue;
}
setObjectsOfInterestData(
object_info->predicted_object().kinematics.initial_pose_with_covariance.pose,
object_info->predicted_object().shape, ColorName::RED);
}
const auto [has_collision, collision_position, too_late_detect_objects, misjudge_objects] =
detectCollision(is_over_1st_pass_judge_line, is_over_2nd_pass_judge_line);
const std::string safety_diag =
generateDetectionBlameDiagnosis(too_late_detect_objects, misjudge_objects);
const std::string occlusion_diag = formatOcclusionType(occlusion_status);
if (is_permanent_go_) {
if (has_collision) {
const auto closest_idx = intersection_stoplines.closest_idx;
const std::string evasive_diag = generateEgoRiskEvasiveDiagnosis(
*path, closest_idx, time_distance_array, too_late_detect_objects, misjudge_objects);
return intersection::OverPassJudge{safety_diag, evasive_diag};
}
return intersection::OverPassJudge{
"no collision is detected", "ego can safely pass the intersection at this rate"};
}
// ==========================================================================================
// this state is very dangerous because ego is very close/over the boundary of 1st attention lane
// and collision is detected on the 1st lane. Since the 2nd attention lane also exists in this
// case, possible another collision may be expected on the 2nd attention lane too.
// ==========================================================================================
std::string safety_report = safety_diag;
if (const bool collision_on_1st_attention_lane =
has_collision &&
(collision_position == intersection::CollisionInterval::LanePosition::FIRST);
is_over_1st_pass_judge_line && is_over_2nd_pass_judge_line.has_value() &&
!is_over_2nd_pass_judge_line.value() && collision_on_1st_attention_lane) {
safety_report +=
"\nego is between the 1st and 2nd pass judge line but collision is expected on the 1st "
"attention lane, which is dangerous.";
}
const auto closest_idx = intersection_stoplines.closest_idx;
const bool is_over_default_stopline = util::isOverTargetIndex(
*path, closest_idx, planner_data_->current_odometry->pose, default_stopline_idx);
const auto collision_stopline_idx = is_over_default_stopline ? closest_idx : default_stopline_idx;
// ==========================================================================================
// pseudo collision detection on green light
// ==========================================================================================
const auto is_green_pseudo_collision_status =
isGreenPseudoCollisionStatus(closest_idx, collision_stopline_idx, intersection_stoplines);
if (is_green_pseudo_collision_status) {
return is_green_pseudo_collision_status.value();
}
// ==========================================================================================
// yield stuck detection
// ==========================================================================================
const auto is_yield_stuck_status =
isYieldStuckStatus(*path, interpolated_path_info, intersection_stoplines);
if (is_yield_stuck_status) {
auto yield_stuck = is_yield_stuck_status.value();
yield_stuck.occlusion_report = occlusion_diag;
return yield_stuck;
}
collision_state_machine_.setStateWithMarginTime(
has_collision ? StateMachine::State::STOP : StateMachine::State::GO,
logger_.get_child("collision state_machine"), *clock_);
const bool has_collision_with_margin =
collision_state_machine_.getState() == StateMachine::State::STOP;
if (is_prioritized) {
return intersection::FullyPrioritized{
has_collision_with_margin, closest_idx, collision_stopline_idx, occlusion_stopline_idx,
safety_report};
}
// Safe
if (!is_occlusion_state && !has_collision_with_margin) {
return intersection::Safe{
closest_idx, collision_stopline_idx, occlusion_stopline_idx, occlusion_diag};
}
// Only collision
if (!is_occlusion_state && has_collision_with_margin) {
return intersection::NonOccludedCollisionStop{
closest_idx, collision_stopline_idx, occlusion_stopline_idx, occlusion_diag};
}
// Occluded
// utility functions
auto fromEgoDist = [&](const size_t index) {
return motion_utils::calcSignedArcLength(path->points, closest_idx, index);
};
auto stoppedForDuration =
[&](const size_t pos, const double duration, StateMachine & state_machine) {
const double dist_stopline = fromEgoDist(pos);
const bool approached_dist_stopline =
(std::fabs(dist_stopline) < planner_param_.common.stopline_overshoot_margin);
const bool over_stopline = (dist_stopline < 0.0);
const bool is_stopped_duration = planner_data_->isVehicleStopped(duration);
if (over_stopline) {
state_machine.setState(StateMachine::State::GO);
} else if (is_stopped_duration && approached_dist_stopline) {
state_machine.setState(StateMachine::State::GO);
}
return state_machine.getState() == StateMachine::State::GO;
};
auto stoppedAtPosition = [&](const size_t pos, const double duration) {
const double dist_stopline = fromEgoDist(pos);
const bool approached_dist_stopline =
(std::fabs(dist_stopline) < planner_param_.common.stopline_overshoot_margin);
const bool over_stopline = (dist_stopline < -planner_param_.common.stopline_overshoot_margin);
const bool is_stopped = planner_data_->isVehicleStopped(duration);
if (over_stopline) {
return true;
} else if (is_stopped && approached_dist_stopline) {
return true;
}
return false;
};
const auto occlusion_wo_tl_pass_judge_line_idx =
intersection_stoplines.occlusion_wo_tl_pass_judge_line;
const bool stopped_at_default_line = stoppedForDuration(
default_stopline_idx, planner_param_.occlusion.temporal_stop_time_before_peeking,
before_creep_state_machine_);
if (stopped_at_default_line) {
// ==========================================================================================
// if specified the parameter occlusion.temporal_stop_before_attention_area OR
// has_no_traffic_light_, ego will temporarily stop before entering attention area
// ==========================================================================================
const bool temporal_stop_before_attention_required =
(planner_param_.occlusion.temporal_stop_before_attention_area || !has_traffic_light_)
? !stoppedForDuration(
first_attention_stopline_idx,
planner_param_.occlusion.temporal_stop_time_before_peeking,
temporal_stop_before_attention_state_machine_)
: false;
if (!has_traffic_light_) {
if (fromEgoDist(occlusion_wo_tl_pass_judge_line_idx) < 0) {
if (has_collision) {
const auto closest_idx = intersection_stoplines.closest_idx;
const std::string evasive_diag = generateEgoRiskEvasiveDiagnosis(
*path, closest_idx, time_distance_array, too_late_detect_objects, misjudge_objects);
return intersection::OverPassJudge{
"already passed maximum peeking line in the absence of traffic light.\n" +
safety_report,
evasive_diag};
}
return intersection::OverPassJudge{
"already passed maximum peeking line in the absence of traffic light safely",
"no evasive action required"};
}
return intersection::OccludedAbsenceTrafficLight{
is_occlusion_cleared_with_margin,
has_collision_with_margin,
temporal_stop_before_attention_required,
closest_idx,
first_attention_stopline_idx,
occlusion_wo_tl_pass_judge_line_idx,
occlusion_diag};
}
// ==========================================================================================
// following remaining block is "has_traffic_light_"
//
// if ego is stuck by static occlusion in the presence of traffic light, start timeout count
// ==========================================================================================
const bool is_static_occlusion = std::holds_alternative<StaticallyOccluded>(occlusion_status);
const bool is_stuck_by_static_occlusion =
stoppedAtPosition(
occlusion_stopline_idx, planner_param_.occlusion.temporal_stop_time_before_peeking) &&
is_static_occlusion;
if (has_collision_with_margin) {
// if collision is detected, timeout is reset
static_occlusion_timeout_state_machine_.setState(StateMachine::State::STOP);
} else if (is_stuck_by_static_occlusion) {
static_occlusion_timeout_state_machine_.setStateWithMarginTime(
StateMachine::State::GO, logger_.get_child("static_occlusion"), *clock_);
}
const bool release_static_occlusion_stuck =
(static_occlusion_timeout_state_machine_.getState() == StateMachine::State::GO);
if (!has_collision_with_margin && release_static_occlusion_stuck) {
return intersection::Safe{
closest_idx, collision_stopline_idx, occlusion_stopline_idx, occlusion_diag};
}
// occlusion_status is either STATICALLY_OCCLUDED or DYNAMICALLY_OCCLUDED
const double max_timeout =
planner_param_.occlusion.static_occlusion_with_traffic_light_timeout +
planner_param_.occlusion.occlusion_detection_hold_time;
const std::optional<double> static_occlusion_timeout =
is_stuck_by_static_occlusion
? std::make_optional<double>(
max_timeout - static_occlusion_timeout_state_machine_.getDuration() -
occlusion_stop_state_machine_.getDuration())
: (is_static_occlusion ? std::make_optional<double>(max_timeout) : std::nullopt);
if (has_collision_with_margin) {
return intersection::OccludedCollisionStop{
is_occlusion_cleared_with_margin,
temporal_stop_before_attention_required,
closest_idx,
collision_stopline_idx,
first_attention_stopline_idx,
occlusion_stopline_idx,
static_occlusion_timeout,
occlusion_diag};
} else {
return intersection::PeekingTowardOcclusion{
is_occlusion_cleared_with_margin,
temporal_stop_before_attention_required,
closest_idx,
collision_stopline_idx,
first_attention_stopline_idx,
occlusion_stopline_idx,
static_occlusion_timeout,
occlusion_diag};
}
} else {
const auto occlusion_stopline =
(planner_param_.occlusion.temporal_stop_before_attention_area || !has_traffic_light_)
? first_attention_stopline_idx
: occlusion_stopline_idx;
return intersection::FirstWaitBeforeOcclusion{
is_occlusion_cleared_with_margin, closest_idx, default_stopline_idx, occlusion_stopline,
occlusion_diag};
}
}
// template-specification based visitor pattern
// https://en.cppreference.com/w/cpp/utility/variant/visit
template <class... Ts>
struct VisitorSwitch : Ts...
{
using Ts::operator()...;
};
template <class... Ts>
VisitorSwitch(Ts...) -> VisitorSwitch<Ts...>;
template <typename T>
void prepareRTCByDecisionResult(
const T & result, const autoware_auto_planning_msgs::msg::PathWithLaneId & path,
bool * default_safety, double * default_distance, bool * occlusion_safety,
double * occlusion_distance)
{
static_assert("Unsupported type passed to prepareRTCByDecisionResult");
return;
}
template <>
void prepareRTCByDecisionResult(
[[maybe_unused]] const intersection::InternalError & result,
[[maybe_unused]] const autoware_auto_planning_msgs::msg::PathWithLaneId & path,
[[maybe_unused]] bool * default_safety, [[maybe_unused]] double * default_distance,
[[maybe_unused]] bool * occlusion_safety, [[maybe_unused]] double * occlusion_distance)
{
return;
}
template <>
void prepareRTCByDecisionResult(
[[maybe_unused]] const intersection::OverPassJudge & result,
[[maybe_unused]] const autoware_auto_planning_msgs::msg::PathWithLaneId & path,
[[maybe_unused]] bool * default_safety, [[maybe_unused]] double * default_distance,
[[maybe_unused]] bool * occlusion_safety, [[maybe_unused]] double * occlusion_distance)
{
return;
}
template <>
void prepareRTCByDecisionResult(
const intersection::StuckStop & result,
const autoware_auto_planning_msgs::msg::PathWithLaneId & path, bool * default_safety,
double * default_distance, bool * occlusion_safety, double * occlusion_distance)
{
RCLCPP_DEBUG(rclcpp::get_logger("prepareRTCByDecisionResult"), "StuckStop");
const auto closest_idx = result.closest_idx;
const auto stopline_idx = result.stuck_stopline_idx;
*default_safety = false;
*default_distance = motion_utils::calcSignedArcLength(path.points, closest_idx, stopline_idx);
*occlusion_safety = true;
if (result.occlusion_stopline_idx) {
const auto occlusion_stopline_idx = result.occlusion_stopline_idx.value();
*occlusion_distance =
motion_utils::calcSignedArcLength(path.points, closest_idx, occlusion_stopline_idx);
}
return;
}
template <>
void prepareRTCByDecisionResult(
const intersection::YieldStuckStop & result,
const autoware_auto_planning_msgs::msg::PathWithLaneId & path, bool * default_safety,
double * default_distance, bool * occlusion_safety, [[maybe_unused]] double * occlusion_distance)
{
RCLCPP_DEBUG(rclcpp::get_logger("prepareRTCByDecisionResult"), "YieldStuckStop");
const auto closest_idx = result.closest_idx;
const auto stopline_idx = result.stuck_stopline_idx;
*default_safety = false;
*default_distance = motion_utils::calcSignedArcLength(path.points, closest_idx, stopline_idx);
*occlusion_safety = true;
return;
}
template <>
void prepareRTCByDecisionResult(
const intersection::NonOccludedCollisionStop & result,
const autoware_auto_planning_msgs::msg::PathWithLaneId & path, bool * default_safety,
double * default_distance, bool * occlusion_safety, double * occlusion_distance)
{
RCLCPP_DEBUG(rclcpp::get_logger("prepareRTCByDecisionResult"), "NonOccludedCollisionStop");
const auto closest_idx = result.closest_idx;
const auto collision_stopline_idx = result.collision_stopline_idx;
*default_safety = false;
*default_distance =
motion_utils::calcSignedArcLength(path.points, closest_idx, collision_stopline_idx);
const auto occlusion_stopline = result.occlusion_stopline_idx;
*occlusion_safety = true;
*occlusion_distance =
motion_utils::calcSignedArcLength(path.points, closest_idx, occlusion_stopline);
return;
}
template <>
void prepareRTCByDecisionResult(
const intersection::FirstWaitBeforeOcclusion & result,
const autoware_auto_planning_msgs::msg::PathWithLaneId & path, bool * default_safety,
double * default_distance, bool * occlusion_safety, double * occlusion_distance)
{
RCLCPP_DEBUG(rclcpp::get_logger("prepareRTCByDecisionResult"), "FirstWaitBeforeOcclusion");
const auto closest_idx = result.closest_idx;
const auto first_stopline_idx = result.first_stopline_idx;
const auto occlusion_stopline_idx = result.occlusion_stopline_idx;
*default_safety = false;
*default_distance =
motion_utils::calcSignedArcLength(path.points, closest_idx, first_stopline_idx);
*occlusion_safety = result.is_actually_occlusion_cleared;
*occlusion_distance =
motion_utils::calcSignedArcLength(path.points, closest_idx, occlusion_stopline_idx);
return;
}
template <>
void prepareRTCByDecisionResult(
const intersection::PeekingTowardOcclusion & result,
const autoware_auto_planning_msgs::msg::PathWithLaneId & path, bool * default_safety,
double * default_distance, bool * occlusion_safety, double * occlusion_distance)
{
RCLCPP_DEBUG(rclcpp::get_logger("prepareRTCByDecisionResult"), "PeekingTowardOcclusion");
const auto closest_idx = result.closest_idx;
const auto collision_stopline_idx = result.collision_stopline_idx;
const auto occlusion_stopline_idx = result.occlusion_stopline_idx;
*default_safety = true;
*default_distance =
motion_utils::calcSignedArcLength(path.points, closest_idx, collision_stopline_idx);
*occlusion_safety = result.is_actually_occlusion_cleared;
*occlusion_distance =
motion_utils::calcSignedArcLength(path.points, closest_idx, occlusion_stopline_idx);
return;
}
template <>
void prepareRTCByDecisionResult(
const intersection::OccludedAbsenceTrafficLight & result,
const autoware_auto_planning_msgs::msg::PathWithLaneId & path, bool * default_safety,
double * default_distance, bool * occlusion_safety, double * occlusion_distance)
{
RCLCPP_DEBUG(rclcpp::get_logger("prepareRTCByDecisionResult"), "OccludedAbsenceTrafficLight");
const auto closest_idx = result.closest_idx;
const auto collision_stopline_idx = result.closest_idx;
*default_safety = !result.collision_detected;
*default_distance =
motion_utils::calcSignedArcLength(path.points, closest_idx, collision_stopline_idx);
*occlusion_safety = result.is_actually_occlusion_cleared;
*occlusion_distance = 0;
return;
}
template <>
void prepareRTCByDecisionResult(
const intersection::OccludedCollisionStop & result,
const autoware_auto_planning_msgs::msg::PathWithLaneId & path, bool * default_safety,
double * default_distance, bool * occlusion_safety, double * occlusion_distance)
{
RCLCPP_DEBUG(rclcpp::get_logger("prepareRTCByDecisionResult"), "OccludedCollisionStop");
const auto closest_idx = result.closest_idx;
const auto collision_stopline_idx = result.collision_stopline_idx;
const auto occlusion_stopline_idx = result.occlusion_stopline_idx;
*default_safety = false;
*default_distance =
motion_utils::calcSignedArcLength(path.points, closest_idx, collision_stopline_idx);
*occlusion_safety = result.is_actually_occlusion_cleared;
*occlusion_distance =
motion_utils::calcSignedArcLength(path.points, closest_idx, occlusion_stopline_idx);
return;
}
template <>
void prepareRTCByDecisionResult(
const intersection::Safe & result, const autoware_auto_planning_msgs::msg::PathWithLaneId & path,
bool * default_safety, double * default_distance, bool * occlusion_safety,
double * occlusion_distance)
{
RCLCPP_DEBUG(rclcpp::get_logger("prepareRTCByDecisionResult"), "Safe");
const auto closest_idx = result.closest_idx;
const auto collision_stopline_idx = result.collision_stopline_idx;
const auto occlusion_stopline_idx = result.occlusion_stopline_idx;
*default_safety = true;
*default_distance =
motion_utils::calcSignedArcLength(path.points, closest_idx, collision_stopline_idx);
*occlusion_safety = true;
*occlusion_distance =
motion_utils::calcSignedArcLength(path.points, closest_idx, occlusion_stopline_idx);
return;
}
template <>
void prepareRTCByDecisionResult(
const intersection::FullyPrioritized & result,
const autoware_auto_planning_msgs::msg::PathWithLaneId & path, bool * default_safety,
double * default_distance, bool * occlusion_safety, double * occlusion_distance)
{
RCLCPP_DEBUG(rclcpp::get_logger("prepareRTCByDecisionResult"), "FullyPrioritized");
const auto closest_idx = result.closest_idx;
const auto collision_stopline_idx = result.collision_stopline_idx;
const auto occlusion_stopline_idx = result.occlusion_stopline_idx;
*default_safety = !result.collision_detected;
*default_distance =
motion_utils::calcSignedArcLength(path.points, closest_idx, collision_stopline_idx);
*occlusion_safety = true;
*occlusion_distance =
motion_utils::calcSignedArcLength(path.points, closest_idx, occlusion_stopline_idx);
return;
}
void IntersectionModule::prepareRTCStatus(
const intersection::DecisionResult & decision_result,
const autoware_auto_planning_msgs::msg::PathWithLaneId & path)
{
bool default_safety = true;
double default_distance = std::numeric_limits<double>::lowest();
std::visit(
VisitorSwitch{[&](const auto & decision) {
prepareRTCByDecisionResult(
decision, path, &default_safety, &default_distance, &occlusion_safety_,
&occlusion_stop_distance_);
}},
decision_result);
setSafe(default_safety);
setDistance(default_distance);
occlusion_first_stop_required_ =
std::holds_alternative<intersection::FirstWaitBeforeOcclusion>(decision_result);
}
template <typename T>
void reactRTCApprovalByDecisionResult(
const bool rtc_default_approved, const bool rtc_occlusion_approved, const T & decision_result,
const IntersectionModule::PlannerParam & planner_param, const double baselink2front,
autoware_auto_planning_msgs::msg::PathWithLaneId * path, StopReason * stop_reason,
VelocityFactorInterface * velocity_factor, IntersectionModule::DebugData * debug_data)
{
static_assert("Unsupported type passed to reactRTCByDecisionResult");
return;
}
template <>
void reactRTCApprovalByDecisionResult(
[[maybe_unused]] const bool rtc_default_approved,
[[maybe_unused]] const bool rtc_occlusion_approved,
[[maybe_unused]] const intersection::InternalError & decision_result,
[[maybe_unused]] const IntersectionModule::PlannerParam & planner_param,
[[maybe_unused]] const double baselink2front,
[[maybe_unused]] autoware_auto_planning_msgs::msg::PathWithLaneId * path,
[[maybe_unused]] StopReason * stop_reason,
[[maybe_unused]] VelocityFactorInterface * velocity_factor,
[[maybe_unused]] IntersectionModule::DebugData * debug_data)
{
return;
}
template <>
void reactRTCApprovalByDecisionResult(
[[maybe_unused]] const bool rtc_default_approved,
[[maybe_unused]] const bool rtc_occlusion_approved,
[[maybe_unused]] const intersection::OverPassJudge & decision_result,
[[maybe_unused]] const IntersectionModule::PlannerParam & planner_param,
[[maybe_unused]] const double baselink2front,
[[maybe_unused]] autoware_auto_planning_msgs::msg::PathWithLaneId * path,
[[maybe_unused]] StopReason * stop_reason,
[[maybe_unused]] VelocityFactorInterface * velocity_factor,
[[maybe_unused]] IntersectionModule::DebugData * debug_data)
{
return;
}
template <>
void reactRTCApprovalByDecisionResult(
const bool rtc_default_approved, const bool rtc_occlusion_approved,
const intersection::StuckStop & decision_result,
[[maybe_unused]] const IntersectionModule::PlannerParam & planner_param,
const double baselink2front, autoware_auto_planning_msgs::msg::PathWithLaneId * path,
StopReason * stop_reason, VelocityFactorInterface * velocity_factor,
IntersectionModule::DebugData * debug_data)
{
RCLCPP_DEBUG(
rclcpp::get_logger("reactRTCApprovalByDecisionResult"),
"StuckStop, approval = (default: %d, occlusion: %d)", rtc_default_approved,
rtc_occlusion_approved);
const auto closest_idx = decision_result.closest_idx;
if (!rtc_default_approved) {
// use default_rtc uuid for stuck vehicle detection
const auto stopline_idx = decision_result.stuck_stopline_idx;
planning_utils::setVelocityFromIndex(stopline_idx, 0.0, path);
debug_data->collision_stop_wall_pose =
planning_utils::getAheadPose(stopline_idx, baselink2front, *path);
{
tier4_planning_msgs::msg::StopFactor stop_factor;
stop_factor.stop_pose = path->points.at(stopline_idx).point.pose;
stop_factor.stop_factor_points = planning_utils::toRosPoints(debug_data->unsafe_targets);
planning_utils::appendStopReason(stop_factor, stop_reason);
velocity_factor->set(
path->points, path->points.at(closest_idx).point.pose,
path->points.at(stopline_idx).point.pose, VelocityFactor::UNKNOWN);
}
}
if (
!rtc_occlusion_approved && decision_result.occlusion_stopline_idx &&
planner_param.occlusion.enable) {
const auto occlusion_stopline_idx = decision_result.occlusion_stopline_idx.value();
planning_utils::setVelocityFromIndex(occlusion_stopline_idx, 0.0, path);
debug_data->occlusion_stop_wall_pose =
planning_utils::getAheadPose(occlusion_stopline_idx, baselink2front, *path);
{
tier4_planning_msgs::msg::StopFactor stop_factor;
stop_factor.stop_pose = path->points.at(occlusion_stopline_idx).point.pose;
planning_utils::appendStopReason(stop_factor, stop_reason);
velocity_factor->set(
path->points, path->points.at(closest_idx).point.pose,
path->points.at(occlusion_stopline_idx).point.pose, VelocityFactor::UNKNOWN);
}
}
return;
}
template <>
void reactRTCApprovalByDecisionResult(
const bool rtc_default_approved, const bool rtc_occlusion_approved,
const intersection::YieldStuckStop & decision_result,
[[maybe_unused]] const IntersectionModule::PlannerParam & planner_param,
const double baselink2front, autoware_auto_planning_msgs::msg::PathWithLaneId * path,
StopReason * stop_reason, VelocityFactorInterface * velocity_factor,
IntersectionModule::DebugData * debug_data)
{
RCLCPP_DEBUG(
rclcpp::get_logger("reactRTCApprovalByDecisionResult"),
"YieldStuckStop, approval = (default: %d, occlusion: %d)", rtc_default_approved,
rtc_occlusion_approved);
const auto closest_idx = decision_result.closest_idx;
if (!rtc_default_approved) {
// use default_rtc uuid for stuck vehicle detection
const auto stopline_idx = decision_result.stuck_stopline_idx;
planning_utils::setVelocityFromIndex(stopline_idx, 0.0, path);
debug_data->collision_stop_wall_pose =
planning_utils::getAheadPose(stopline_idx, baselink2front, *path);
{
tier4_planning_msgs::msg::StopFactor stop_factor;
stop_factor.stop_pose = path->points.at(stopline_idx).point.pose;
stop_factor.stop_factor_points = planning_utils::toRosPoints(debug_data->unsafe_targets);
planning_utils::appendStopReason(stop_factor, stop_reason);
velocity_factor->set(
path->points, path->points.at(closest_idx).point.pose,
path->points.at(stopline_idx).point.pose, VelocityFactor::UNKNOWN);
}
}
return;
}
template <>
void reactRTCApprovalByDecisionResult(
const bool rtc_default_approved, const bool rtc_occlusion_approved,
const intersection::NonOccludedCollisionStop & decision_result,
[[maybe_unused]] const IntersectionModule::PlannerParam & planner_param,
const double baselink2front, autoware_auto_planning_msgs::msg::PathWithLaneId * path,
StopReason * stop_reason, VelocityFactorInterface * velocity_factor,
IntersectionModule::DebugData * debug_data)
{
RCLCPP_DEBUG(
rclcpp::get_logger("reactRTCApprovalByDecisionResult"),
"NonOccludedCollisionStop, approval = (default: %d, occlusion: %d)", rtc_default_approved,
rtc_occlusion_approved);
if (!rtc_default_approved) {
const auto stopline_idx = decision_result.collision_stopline_idx;
planning_utils::setVelocityFromIndex(stopline_idx, 0.0, path);
debug_data->collision_stop_wall_pose =
planning_utils::getAheadPose(stopline_idx, baselink2front, *path);
{
tier4_planning_msgs::msg::StopFactor stop_factor;
stop_factor.stop_pose = path->points.at(stopline_idx).point.pose;
planning_utils::appendStopReason(stop_factor, stop_reason);
velocity_factor->set(
path->points, path->points.at(decision_result.closest_idx).point.pose,
path->points.at(stopline_idx).point.pose, VelocityFactor::UNKNOWN);
}
}
if (!rtc_occlusion_approved && planner_param.occlusion.enable) {
const auto stopline_idx = decision_result.occlusion_stopline_idx;
planning_utils::setVelocityFromIndex(stopline_idx, 0.0, path);
debug_data->occlusion_stop_wall_pose =
planning_utils::getAheadPose(stopline_idx, baselink2front, *path);
{
tier4_planning_msgs::msg::StopFactor stop_factor;
stop_factor.stop_pose = path->points.at(stopline_idx).point.pose;
planning_utils::appendStopReason(stop_factor, stop_reason);
velocity_factor->set(
path->points, path->points.at(decision_result.closest_idx).point.pose,
path->points.at(stopline_idx).point.pose, VelocityFactor::UNKNOWN);
}
}
return;
}
template <>
void reactRTCApprovalByDecisionResult(
const bool rtc_default_approved, const bool rtc_occlusion_approved,
const intersection::FirstWaitBeforeOcclusion & decision_result,
const IntersectionModule::PlannerParam & planner_param, const double baselink2front,
autoware_auto_planning_msgs::msg::PathWithLaneId * path, StopReason * stop_reason,
VelocityFactorInterface * velocity_factor, IntersectionModule::DebugData * debug_data)
{
RCLCPP_DEBUG(
rclcpp::get_logger("reactRTCApprovalByDecisionResult"),
"FirstWaitBeforeOcclusion, approval = (default: %d, occlusion: %d)", rtc_default_approved,
rtc_occlusion_approved);
if (!rtc_default_approved) {
const auto stopline_idx = decision_result.first_stopline_idx;
planning_utils::setVelocityFromIndex(stopline_idx, 0.0, path);
debug_data->occlusion_first_stop_wall_pose =
planning_utils::getAheadPose(stopline_idx, baselink2front, *path);
{
tier4_planning_msgs::msg::StopFactor stop_factor;
stop_factor.stop_pose = path->points.at(stopline_idx).point.pose;
planning_utils::appendStopReason(stop_factor, stop_reason);
velocity_factor->set(
path->points, path->points.at(decision_result.closest_idx).point.pose,
path->points.at(stopline_idx).point.pose, VelocityFactor::UNKNOWN);
}
}
if (!rtc_occlusion_approved && planner_param.occlusion.enable) {
if (planner_param.occlusion.creep_during_peeking.enable) {
const size_t occlusion_peeking_stopline = decision_result.occlusion_stopline_idx;
const size_t closest_idx = decision_result.closest_idx;
for (size_t i = closest_idx; i < occlusion_peeking_stopline; i++) {
planning_utils::setVelocityFromIndex(
i, planner_param.occlusion.creep_during_peeking.creep_velocity, path);
}
}
const auto stopline_idx = decision_result.occlusion_stopline_idx;
planning_utils::setVelocityFromIndex(stopline_idx, 0.0, path);
debug_data->occlusion_stop_wall_pose =
planning_utils::getAheadPose(stopline_idx, baselink2front, *path);
{
tier4_planning_msgs::msg::StopFactor stop_factor;
stop_factor.stop_pose = path->points.at(stopline_idx).point.pose;
planning_utils::appendStopReason(stop_factor, stop_reason);
velocity_factor->set(
path->points, path->points.at(decision_result.closest_idx).point.pose,
path->points.at(stopline_idx).point.pose, VelocityFactor::UNKNOWN);
}
}
return;
}
template <>
void reactRTCApprovalByDecisionResult(
const bool rtc_default_approved, const bool rtc_occlusion_approved,
const intersection::PeekingTowardOcclusion & decision_result,
const IntersectionModule::PlannerParam & planner_param, const double baselink2front,
autoware_auto_planning_msgs::msg::PathWithLaneId * path, StopReason * stop_reason,
VelocityFactorInterface * velocity_factor, IntersectionModule::DebugData * debug_data)
{
RCLCPP_DEBUG(
rclcpp::get_logger("reactRTCApprovalByDecisionResult"),
"PeekingTowardOcclusion, approval = (default: %d, occlusion: %d)", rtc_default_approved,
rtc_occlusion_approved);
// NOTE: creep_velocity should be inserted first at closest_idx if !rtc_default_approved
if (!rtc_occlusion_approved && planner_param.occlusion.enable) {
const size_t occlusion_peeking_stopline =
decision_result.temporal_stop_before_attention_required
? decision_result.first_attention_stopline_idx
: decision_result.occlusion_stopline_idx;
if (planner_param.occlusion.creep_during_peeking.enable) {
const size_t closest_idx = decision_result.closest_idx;
for (size_t i = closest_idx; i < occlusion_peeking_stopline; i++) {
planning_utils::setVelocityFromIndex(
i, planner_param.occlusion.creep_during_peeking.creep_velocity, path);
}
}
planning_utils::setVelocityFromIndex(occlusion_peeking_stopline, 0.0, path);
debug_data->occlusion_stop_wall_pose =
planning_utils::getAheadPose(occlusion_peeking_stopline, baselink2front, *path);
debug_data->static_occlusion_with_traffic_light_timeout =
decision_result.static_occlusion_timeout;
{
tier4_planning_msgs::msg::StopFactor stop_factor;
stop_factor.stop_pose = path->points.at(occlusion_peeking_stopline).point.pose;
planning_utils::appendStopReason(stop_factor, stop_reason);
velocity_factor->set(
path->points, path->points.at(decision_result.closest_idx).point.pose,
path->points.at(occlusion_peeking_stopline).point.pose, VelocityFactor::UNKNOWN);
}
}
if (!rtc_default_approved) {
const auto stopline_idx = decision_result.collision_stopline_idx;
planning_utils::setVelocityFromIndex(stopline_idx, 0.0, path);
debug_data->collision_stop_wall_pose =
planning_utils::getAheadPose(stopline_idx, baselink2front, *path);
{
tier4_planning_msgs::msg::StopFactor stop_factor;
stop_factor.stop_pose = path->points.at(stopline_idx).point.pose;
planning_utils::appendStopReason(stop_factor, stop_reason);
velocity_factor->set(
path->points, path->points.at(decision_result.closest_idx).point.pose,
path->points.at(stopline_idx).point.pose, VelocityFactor::UNKNOWN);
}
}
return;
}
template <>
void reactRTCApprovalByDecisionResult(
const bool rtc_default_approved, const bool rtc_occlusion_approved,
const intersection::OccludedCollisionStop & decision_result,
[[maybe_unused]] const IntersectionModule::PlannerParam & planner_param,
const double baselink2front, autoware_auto_planning_msgs::msg::PathWithLaneId * path,
StopReason * stop_reason, VelocityFactorInterface * velocity_factor,
IntersectionModule::DebugData * debug_data)
{
RCLCPP_DEBUG(
rclcpp::get_logger("reactRTCApprovalByDecisionResult"),
"OccludedCollisionStop, approval = (default: %d, occlusion: %d)", rtc_default_approved,
rtc_occlusion_approved);
if (!rtc_default_approved) {
const auto stopline_idx = decision_result.collision_stopline_idx;
planning_utils::setVelocityFromIndex(stopline_idx, 0.0, path);
debug_data->collision_stop_wall_pose =
planning_utils::getAheadPose(stopline_idx, baselink2front, *path);
{
tier4_planning_msgs::msg::StopFactor stop_factor;
stop_factor.stop_pose = path->points.at(stopline_idx).point.pose;
planning_utils::appendStopReason(stop_factor, stop_reason);
velocity_factor->set(
path->points, path->points.at(decision_result.closest_idx).point.pose,
path->points.at(stopline_idx).point.pose, VelocityFactor::UNKNOWN);
}
}
if (!rtc_occlusion_approved && planner_param.occlusion.enable) {