Skip to content

Commit 42fe0e9

Browse files
committed
wip
Signed-off-by: Tomohito Ando <[email protected]>
1 parent 61d5bc6 commit 42fe0e9

File tree

2 files changed

+56
-24
lines changed

2 files changed

+56
-24
lines changed

perception/traffic_light_arbiter/include/traffic_light_arbiter/signal_match_validator.hpp

+28-2
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,41 @@ class SignalMatchValidator
6363
TrafficSignalArray validateSignals(
6464
const TrafficSignalArray & perception_signals, const TrafficSignalArray & external_signals);
6565

66+
/**
67+
* @brief Sets the pedestrian signals to be considered during validation.
68+
*
69+
* This method allows the specification of pedestrian signals, which are then
70+
* used to adjust the validation logic, acknowledging their unique characteristics
71+
* in traffic signal datasets.
72+
*
73+
* @param pedestrian_signals A vector of pedestrian signal pointers.
74+
*/
6675
void setPedestrianSignals(const std::vector<TrafficLightConstPtr> & pedestrian_signals);
6776

77+
/**
78+
* @brief Sets the priority flag for using external signal data over perception data.
79+
*
80+
* When set to true, this flag indicates that in cases of discrepancy between
81+
* perception and external signal data, the external data should be prioritized.
82+
*
83+
* @param external_priority The priority flag for external signal data.
84+
*/
6885
void setExternalPriority(const bool external_priority);
6986

70-
bool isPedestrianSignal(const lanelet::Id & signal_id);
71-
7287
private:
7388
bool external_priority_;
7489
std::unordered_set<lanelet::Id> map_pedestrian_signal_regulatory_elements_set_;
90+
91+
/**
92+
* @brief Checks if a given signal ID corresponds to a pedestrian signal.
93+
*
94+
* This method determines whether a signal, identified by its ID, is classified
95+
* as a pedestrian signal.
96+
*
97+
* @param signal_id The ID of the signal to check.
98+
* @return True if the signal is a pedestrian signal, false otherwise.
99+
*/
100+
bool isPedestrianSignal(const lanelet::Id & signal_id);
75101
};
76102

77103
#endif

perception/traffic_light_arbiter/src/signal_match_validator.cpp

+28-22
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,23 @@ namespace util
1919
using TrafficSignalArray = autoware_perception_msgs::msg::TrafficSignalArray;
2020
using TrafficSignal = autoware_perception_msgs::msg::TrafficSignal;
2121
using Element = autoware_perception_msgs::msg::TrafficSignalElement;
22+
using Time = builtin_interfaces::msg::Time;
2223

24+
// Finds a signal by its ID within a TrafficSignalArray.
2325
std::optional<TrafficSignal> find_signal_by_id(
2426
const TrafficSignalArray & signals, int64_t signal_id)
2527
{
2628
auto it = std::find_if(
2729
signals.signals.begin(), signals.signals.end(),
2830
[signal_id](const TrafficSignal & signal) { return signal.traffic_signal_id == signal_id; });
2931
if (it != signals.signals.end()) {
30-
return *it;
32+
return *it; // Return the found signal.
3133
} else {
32-
return std::nullopt;
34+
return std::nullopt; // Return an empty optional if not found.
3335
}
3436
}
3537

38+
// Creates a TrafficSignalElement with specified attributes.
3639
Element create_element(
3740
const Element::_color_type & color, const Element::_shape_type & shape,
3841
const Element::_status_type & status, const Element::_confidence_type & confidence)
@@ -46,7 +49,7 @@ Element create_element(
4649
return signal_element;
4750
}
4851

49-
// Create unknown elements for each shape
52+
// Creates unknown elements for each unique shape from two element vectors.
5053
std::vector<Element> create_unknown_elements(
5154
const std::vector<Element> & elements1, const std::vector<Element> & elements2)
5255
{
@@ -60,66 +63,68 @@ std::vector<Element> create_unknown_elements(
6063

6164
std::vector<Element> unknown_elements;
6265
for (const auto & shape : shape_set) {
63-
// Confidence doesn't matter because this is the unknown signal
66+
// Confidence is set to a default value as it is not relevant for unknown signals.
6467
unknown_elements.emplace_back(
65-
util::create_element(Element::UNKNOWN, shape, Element::UNKNOWN, /* confidence */ 1.0));
68+
create_element(Element::UNKNOWN, shape, Element::UNKNOWN, /* confidence */ 1.0));
6669
}
6770

6871
return unknown_elements;
6972
}
7073

74+
// Creates a 'unknown' signal with elements matching the shapes of a given signal's elements.
7175
TrafficSignal create_unknown_signal(const TrafficSignal & signal)
7276
{
7377
TrafficSignal unknown_signal;
7478
unknown_signal.traffic_signal_id = signal.traffic_signal_id;
7579
for (const auto & element : signal.elements) {
76-
// Confidence doesn't matter because this is the unknown signal
80+
// Confidence is set to a default value as it is not relevant for unknown signals.
7781
const auto unknown_element =
78-
util::create_element(Element::UNKNOWN, element.shape, Element::UNKNOWN, /* confidence */ 1.0);
82+
create_element(Element::UNKNOWN, element.shape, Element::UNKNOWN, /* confidence */ 1.0);
7983
unknown_signal.elements.emplace_back(unknown_element);
8084
}
8185

8286
return unknown_signal;
8387
}
8488

89+
// Creates an 'unknown' signal by combining unique shapes from two signals' elements.
8590
TrafficSignal create_unknown_signal(const TrafficSignal & signal1, const TrafficSignal & signal2)
8691
{
8792
TrafficSignal unknown_signal;
8893

89-
// Assume that the both ids are same
94+
// Assumes that both signals have the same traffic_signal_id.
9095
unknown_signal.traffic_signal_id = signal1.traffic_signal_id;
9196

92-
const auto unknown_elements = util::create_unknown_elements(signal1.elements, signal2.elements);
97+
const auto unknown_elements = create_unknown_elements(signal1.elements, signal2.elements);
9398
for (const auto & element : unknown_elements) {
9499
unknown_signal.elements.emplace_back(element);
95100
}
96101

97102
return unknown_signal;
98103
}
99104

105+
// Checks if all elements in two vectors are equivalent
100106
bool are_all_elements_equivalent(
101107
const std::vector<Element> & signal1, const std::vector<Element> & signal2)
102108
{
103-
// Check if the vectors have the same size
109+
// Returns false if vectors have different sizes.
104110
if (signal1.size() != signal2.size()) {
105111
return false;
106112
}
107113

108-
// Create copies of the vectors
114+
// Sorts copies of the vectors by shape for comparison.
109115
std::vector<Element> sorted_signal1 = signal1;
110116
std::vector<Element> sorted_signal2 = signal2;
111-
112-
// Sort based on the shape to ensure that they are same order
113117
auto compare_by_shape = [](const Element & a, const Element & b) { return a.shape < b.shape; };
114118
std::sort(sorted_signal1.begin(), sorted_signal1.end(), compare_by_shape);
115119
std::sort(sorted_signal2.begin(), sorted_signal2.end(), compare_by_shape);
116120

117-
// Compare the sorted vectors and return true if they have all the same elements
121+
// Returns true if sorted vectors are equal.
118122
return std::equal(
119123
sorted_signal1.begin(), sorted_signal1.end(), sorted_signal2.begin(), sorted_signal2.end(),
120124
[](const Element & a, const Element & b) { return a.color == b.color && a.shape == b.shape; });
121125
}
122126

127+
// Creates a set of unique signal IDs from two vectors of TrafficSignals.
123128
std::unordered_set<lanelet::Id> create_signal_id_set(
124129
const std::vector<TrafficSignal> & signals1, const std::vector<TrafficSignal> & signals2)
125130
{
@@ -134,24 +139,25 @@ std::unordered_set<lanelet::Id> create_signal_id_set(
134139
return signal_id_set;
135140
}
136141

142+
// Returns the signal with the highest confidence elements, considering a external priority.
137143
TrafficSignal get_highest_confidence_signal(
138144
const std::optional<TrafficSignal> & perception_signal,
139145
const std::optional<TrafficSignal> & external_signal, const bool external_priority)
140146
{
141-
// If the either of the signal doesn't exist, return the signal that exists
147+
// Returns the existing signal if only one of them exists.
142148
if (!perception_signal) {
143149
return *external_signal;
144150
}
145151
if (!external_signal) {
146152
return *perception_signal;
147153
}
148154

149-
// If the external_priority is true, use the external results
155+
// Gives priority to the external signal if external_priority is true.
150156
if (external_priority) {
151157
return *external_signal;
152158
}
153159

154-
// Create map using shape as key
160+
// Compiles elements into a map by shape, to compare their confidences.
155161
using Key = Element::_shape_type;
156162
std::map<Key, std::vector<Element>> shape_element_map;
157163
for (const auto & element : perception_signal->elements) {
@@ -163,12 +169,11 @@ TrafficSignal get_highest_confidence_signal(
163169

164170
TrafficSignal highest_confidence_signal;
165171

166-
// Assume that the both ids are same
172+
// Assumes that both signals have the same traffic_signal_id.
167173
highest_confidence_signal.traffic_signal_id = perception_signal->traffic_signal_id;
168174

169-
// Find the highest confidence element and push it
170-
for (const auto & shape_and_elements : shape_element_map) {
171-
const auto & elements = shape_and_elements.second;
175+
// For each shape, finds the element with the highest confidence and adds it to the signal.
176+
for (const auto & [shape, elements] : shape_element_map) {
172177
const auto highest_confidence_element = std::max_element(
173178
elements.begin(), elements.end(),
174179
[](const Element & a, const Element & b) { return a.confidence < b.confidence; });
@@ -178,9 +183,10 @@ TrafficSignal get_highest_confidence_signal(
178183
return highest_confidence_signal;
179184
}
180185

181-
using Time = builtin_interfaces::msg::Time;
186+
// Determines the newer of two Time stamps.
182187
Time get_newer_stamp(const Time & stamp1, const Time & stamp2)
183188
{
189+
// Returns stamp1 if it is newer than stamp2, otherwise returns stamp2.
184190
if (stamp1.sec > stamp2.sec || (stamp1.sec == stamp2.sec && stamp1.nanosec > stamp2.nanosec)) {
185191
return stamp1;
186192
} else {

0 commit comments

Comments
 (0)