15 #ifndef TRAFFIC_SIMULATOR__TRAFFIC_LIGHTS__TRAFFIC_LIGHTS_HPP_
16 #define TRAFFIC_SIMULATOR__TRAFFIC_LIGHTS__TRAFFIC_LIGHTS_HPP_
19 #if __has_include(<autoware_perception_msgs/msg/traffic_signal_array.hpp>)
20 #include <autoware_perception_msgs/msg/traffic_signal_array.hpp>
23 #if __has_include(<autoware_perception_msgs/msg/traffic_light_group_array.hpp>)
24 #include <autoware_perception_msgs/msg/traffic_light_group_array.hpp>
38 template <
typename NodeTypePo
inter>
41 backward_compatible_publisher_ptr_(
43 node_ptr,
"/simulation/traffic_lights"))
50 auto update() const ->
void override
52 backward_compatible_publisher_ptr_->publish(
60 const std::unique_ptr<TrafficLightPublisherBase> backward_compatible_publisher_ptr_;
74 auto [iter, inserted] = detected_traffic_lights_.try_emplace(lanelet_id, lanelet_id);
75 iter->second.set(state);
80 return detected_traffic_lights_.erase(lanelet_id) > 0;
83 auto empty() const ->
bool {
return detected_traffic_lights_.empty(); }
85 auto apply(simulation_api_schema::UpdateTrafficLightsRequest & request)
const ->
void
87 for (
const auto & [lanelet_id, detected_light] : detected_traffic_lights_) {
88 if (
auto matched_state = std::find_if(
89 request.mutable_states()->begin(), request.mutable_states()->end(),
90 [lanelet_id](
const auto & state) { return state.id() == lanelet_id; });
91 matched_state != request.mutable_states()->end()) {
93 auto detected_signal =
static_cast<simulation_api_schema::TrafficSignal
>(detected_light);
94 matched_state->clear_traffic_light_status();
95 for (
const auto & status : detected_signal.traffic_light_status()) {
96 *matched_state->add_traffic_light_status() = status;
100 *request.add_states() =
static_cast<simulation_api_schema::TrafficSignal
>(detected_light);
106 std::map<lanelet::Id, TrafficLight> detected_traffic_lights_;
112 template <
typename NodeTypePo
inter>
115 publisher_ptr_(makePublisher(
116 node_ptr, architecture_type,
117 "/perception/traffic_light_recognition/external/traffic_signals")),
118 legacy_topic_publisher_ptr_(makePublisher(node_ptr, architecture_type,
"/v2x/traffic_signals"))
126 detected_ = detected;
130 const lanelet::Id lanelet_id,
const std::string & state,
double time_ahead_seconds) -> void;
135 auto update() const ->
void override
140 detected_->apply(request);
142 publisher_ptr_->publish(now, request, &predictions_);
143 legacy_topic_publisher_ptr_->publish(now, request, &predictions_);
150 template <
typename NodeTypePo
inter>
152 const NodeTypePointer & node_ptr,
const std::string & architecture_type,
153 const std::string & topic_name) -> std::unique_ptr<TrafficLightPublisherBase>
163 if (architecture_type ==
"awf/universe") {
165 "This version of scenario_simulator_v2 does not support ", std::quoted(architecture_type),
166 " as ", std::quoted(
"architecture_type"),
". Please use older version.");
167 #if __has_include(<autoware_perception_msgs/msg/traffic_signal_array.hpp>)
168 }
else if (architecture_type <=
"awf/universe/20230906") {
169 return std::make_unique<
170 TrafficLightPublisher<autoware_perception_msgs::msg::TrafficSignalArray>>(
171 node_ptr, topic_name);
173 #if __has_include(<autoware_perception_msgs/msg/traffic_light_group_array.hpp>)
174 }
else if (architecture_type >=
"awf/universe/20240605") {
175 return std::make_unique<
176 TrafficLightPublisher<autoware_perception_msgs::msg::TrafficLightGroupArray>>(
177 node_ptr, topic_name);
181 "Unexpected architecture_type ", std::quoted(architecture_type),
182 " given for V2I traffic lights simulation.");
186 const std::unique_ptr<TrafficLightPublisherBase> publisher_ptr_;
188 const std::unique_ptr<TrafficLightPublisherBase> legacy_topic_publisher_ptr_;
190 std::shared_ptr<DetectedTrafficLights> detected_;
195 template <
typename GroundTruthType>
199 template <
typename NodeTypePointer,
typename... Args>
201 : ground_truth_(
std::make_shared<GroundTruthType>(node_ptr,
std::forward<Args>(args)...)),
214 auto request = ground_truth_->generateUpdateTrafficLightsRequest();
215 detected_->apply(request);
220 std::shared_ptr<GroundTruthType> ground_truth_;
222 std::shared_ptr<DetectedTrafficLights> detected_;
228 template <
typename NodeTypePo
inter>
230 : conventional_channel_(node_ptr), v2i_channel_(node_ptr, architecture_type)
232 v2i_channel_.getGroundTruth()->setDetectedTrafficLights(v2i_channel_.getDetected());
234 conventional_channel_.getGroundTruth()->registerStateChangeCallback(
235 [
this, v2i = v2i_channel_.getGroundTruth()](
238 if (v2i_enabled_traffic_lights_.count(lanelet_id) > 0) {
239 switch (change_type) {
240 case TrafficLightsBase::StateChangeType::SET:
241 v2i->setTrafficLightsState(lanelet_id, state);
243 case TrafficLightsBase::StateChangeType::CLEAR:
244 v2i->clearTrafficLightsState(lanelet_id);
246 case TrafficLightsBase::StateChangeType::ADD:
247 v2i->addTrafficLightsState(lanelet_id, state);
254 auto setV2IFeature(
const lanelet::Id lanelet_id,
const bool enabled) ->
void
257 for (
const auto & traffic_light_way_id :
259 setV2IFeature(traffic_light_way_id, enabled);
264 v2i_enabled_traffic_lights_.insert(lanelet_id);
266 v2i_enabled_traffic_lights_.erase(lanelet_id);
271 auto isAnyTrafficLightChanged() -> bool;
273 auto startTrafficLightsUpdate(
274 const double conventional_traffic_light_update_rate,
275 const double v2i_traffic_lights_update_rate) -> void;
285 auto generateConventionalUpdateRequest() const
286 -> simulation_api_schema::UpdateTrafficLightsRequest;
288 auto isV2ITrafficLightEnabled(const lanelet::Id lanelet_id) const ->
bool;
295 std::set<lanelet::Id> v2i_enabled_traffic_lights_;
Definition: traffic_lights.hpp:36
~ConventionalTrafficLights() override=default
ConventionalTrafficLights(const NodeTypePointer &node_ptr)
Definition: traffic_lights.hpp:39
Definition: traffic_lights.hpp:64
auto setState(const lanelet::Id lanelet_id, const std::string &state) -> void
Definition: traffic_lights.hpp:66
auto clearState(const lanelet::Id lanelet_id) -> bool
Definition: traffic_lights.hpp:78
auto addState(const lanelet::Id lanelet_id, const std::string &state) -> void
Definition: traffic_lights.hpp:72
auto empty() const -> bool
Definition: traffic_lights.hpp:83
auto apply(simulation_api_schema::UpdateTrafficLightsRequest &request) const -> void
Definition: traffic_lights.hpp:85
Definition: traffic_light_publisher.hpp:39
Definition: traffic_lights_base.hpp:44
StateChangeType
Definition: traffic_lights_base.hpp:47
auto isAnyTrafficLightChanged() const -> bool
Definition: traffic_lights_base.cpp:32
auto generateUpdateTrafficLightsRequest() const -> simulation_api_schema::UpdateTrafficLightsRequest
Definition: traffic_lights_base.cpp:114
const rclcpp::Clock::SharedPtr clock_ptr_
Definition: traffic_lights_base.hpp:115
const std::unique_ptr< TrafficLightMarkerPublisher > marker_publisher_ptr_
Definition: traffic_lights_base.hpp:118
std::unordered_map< lanelet::Id, TrafficLight > traffic_lights_map_
Definition: traffic_lights_base.hpp:117
Definition: traffic_lights.hpp:197
auto getGroundTruth() const -> std::shared_ptr< GroundTruthType >
Definition: traffic_lights.hpp:206
TrafficLightsChannel(const NodeTypePointer &node_ptr, Args &&... args)
Definition: traffic_lights.hpp:200
auto getDetected() const -> std::shared_ptr< DetectedTrafficLights >
Definition: traffic_lights.hpp:208
auto generateUpdateRequest() const -> simulation_api_schema::UpdateTrafficLightsRequest
Definition: traffic_lights.hpp:212
auto hasDetectedChanges() const -> bool
Definition: traffic_lights.hpp:210
Definition: traffic_lights.hpp:226
TrafficLights(const NodeTypePointer &node_ptr, const std::string &architecture_type)
Definition: traffic_lights.hpp:229
auto setV2IFeature(const lanelet::Id lanelet_id, const bool enabled) -> void
Definition: traffic_lights.hpp:254
Definition: traffic_lights.hpp:110
V2ITrafficLights(const NodeTypePointer &node_ptr, const std::string &architecture_type)
Definition: traffic_lights.hpp:113
auto addTrafficLightsStatePrediction(const lanelet::Id lanelet_id, const std::string &state, double time_ahead_seconds) -> void
Definition: traffic_lights.cpp:67
auto clearTrafficLightsStatePredictions() -> void
Definition: traffic_lights.cpp:106
~V2ITrafficLights() override=default
auto setDetectedTrafficLights(std::shared_ptr< DetectedTrafficLights > detected) -> void
Definition: traffic_lights.hpp:124
Definition: lanelet_wrapper.hpp:43
auto isTrafficLightRegulatoryElement(const lanelet::Id lanelet_id) -> bool
Definition: traffic_lights.cpp:34
auto isTrafficLight(const lanelet::Id lanelet_id) -> bool
Definition: traffic_lights.cpp:23
auto wayIds(const lanelet::Id lanelet_id) -> lanelet::Ids
Definition: traffic_lights.cpp:21
Definition: operators.hpp:25
std::unordered_map< lanelet::Id, std::vector< std::pair< rclcpp::Time, std::vector< simulation_api_schema::TrafficLight > >> > TrafficLightStatePredictions
Definition: traffic_lights_base.hpp:35
std::string string
Definition: junit5.hpp:26
Although there were no syntactic errors in the description of the scenario, differences in meaning an...
Definition: exception.hpp:44