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>
37 template <
typename NodeTypePo
inter>
39 const NodeTypePointer & node_ptr,
const std::shared_ptr<hdmap_utils::HdMapUtils> &
hdmap_utils)
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_;
79 auto [iter, inserted] =
80 detected_traffic_lights_.try_emplace(lanelet_id, lanelet_id, *hdmap_utils_);
81 iter->second.set(state);
86 return detected_traffic_lights_.erase(lanelet_id) > 0;
89 auto empty() const ->
bool {
return detected_traffic_lights_.empty(); }
91 auto apply(simulation_api_schema::UpdateTrafficLightsRequest & request)
const ->
void
93 for (
const auto & [lanelet_id, detected_light] : detected_traffic_lights_) {
94 if (
auto matched_state = std::find_if(
95 request.mutable_states()->begin(), request.mutable_states()->end(),
96 [lanelet_id](
const auto & state) { return state.id() == lanelet_id; });
97 matched_state != request.mutable_states()->end()) {
99 auto detected_signal =
static_cast<simulation_api_schema::TrafficSignal
>(detected_light);
100 matched_state->clear_traffic_light_status();
101 for (
const auto & status : detected_signal.traffic_light_status()) {
102 *matched_state->add_traffic_light_status() = status;
106 *request.add_states() =
static_cast<simulation_api_schema::TrafficSignal
>(detected_light);
112 std::map<lanelet::Id, TrafficLight> detected_traffic_lights_;
114 std::shared_ptr<hdmap_utils::HdMapUtils> hdmap_utils_;
120 template <
typename NodeTypePo
inter>
122 const NodeTypePointer & node_ptr,
const std::shared_ptr<hdmap_utils::HdMapUtils> &
hdmap_utils,
125 publisher_ptr_(makePublisher(
126 node_ptr, architecture_type,
127 "/perception/traffic_light_recognition/external/traffic_signals")),
128 legacy_topic_publisher_ptr_(makePublisher(node_ptr, architecture_type,
"/v2x/traffic_signals"))
136 detected_ = detected;
140 const lanelet::Id lanelet_id,
const std::string & state,
double time_ahead_seconds) -> void;
145 auto update() const ->
void override
150 detected_->apply(request);
152 publisher_ptr_->publish(now, request, &predictions_);
153 legacy_topic_publisher_ptr_->publish(now, request, &predictions_);
160 template <
typename NodeTypePo
inter>
162 const NodeTypePointer & node_ptr,
const std::string & architecture_type,
163 const std::string & topic_name) -> std::unique_ptr<TrafficLightPublisherBase>
173 if (architecture_type ==
"awf/universe") {
175 "This version of scenario_simulator_v2 does not support ", std::quoted(architecture_type),
176 " as ", std::quoted(
"architecture_type"),
". Please use older version.");
177 #if __has_include(<autoware_perception_msgs/msg/traffic_signal_array.hpp>)
178 }
else if (architecture_type <=
"awf/universe/20230906") {
179 return std::make_unique<
180 TrafficLightPublisher<autoware_perception_msgs::msg::TrafficSignalArray>>(
181 node_ptr, topic_name);
183 #if __has_include(<autoware_perception_msgs/msg/traffic_light_group_array.hpp>)
184 }
else if (architecture_type >=
"awf/universe/20240605") {
185 return std::make_unique<
186 TrafficLightPublisher<autoware_perception_msgs::msg::TrafficLightGroupArray>>(
187 node_ptr, topic_name);
191 "Unexpected architecture_type ", std::quoted(architecture_type),
192 " given for V2I traffic lights simulation.");
196 const std::unique_ptr<TrafficLightPublisherBase> publisher_ptr_;
198 const std::unique_ptr<TrafficLightPublisherBase> legacy_topic_publisher_ptr_;
200 std::shared_ptr<DetectedTrafficLights> detected_;
205 template <
typename GroundTruthType>
209 template <
typename NodeTypePointer,
typename... Args>
211 const NodeTypePointer & node_ptr,
const std::shared_ptr<hdmap_utils::HdMapUtils> &
hdmap_utils,
214 std::make_shared<GroundTruthType>(node_ptr,
hdmap_utils,
std::forward<Args>(args)...)),
227 auto request = ground_truth_->generateUpdateTrafficLightsRequest();
228 detected_->apply(request);
233 std::shared_ptr<GroundTruthType> ground_truth_;
235 std::shared_ptr<DetectedTrafficLights> detected_;
241 template <
typename NodeTypePo
inter>
243 const NodeTypePointer & node_ptr,
const std::shared_ptr<hdmap_utils::HdMapUtils> &
hdmap_utils,
246 v2i_channel_(node_ptr,
hdmap_utils, architecture_type),
249 v2i_channel_.getGroundTruth()->setDetectedTrafficLights(v2i_channel_.getDetected());
251 conventional_channel_.getGroundTruth()->registerStateChangeCallback(
252 [
this, v2i = v2i_channel_.getGroundTruth()](
255 if (v2i_enabled_traffic_lights_.count(lanelet_id) > 0) {
256 switch (change_type) {
257 case TrafficLightsBase::StateChangeType::SET:
258 v2i->setTrafficLightsState(lanelet_id, state);
260 case TrafficLightsBase::StateChangeType::CLEAR:
261 v2i->clearTrafficLightsState(lanelet_id);
263 case TrafficLightsBase::StateChangeType::ADD:
264 v2i->addTrafficLightsState(lanelet_id, state);
271 auto setV2IFeature(
const lanelet::Id lanelet_id,
const bool enabled) ->
void
273 if (hdmap_utils_->isTrafficLightRegulatoryElement(lanelet_id)) {
275 const auto regulatory_element = hdmap_utils_->getTrafficLightRegulatoryElement(lanelet_id);
276 for (
const auto & ref_member :
277 regulatory_element->getParameters<lanelet::ConstLineString3d>(
"refers")) {
278 setV2IFeature(ref_member.id(), enabled);
280 }
else if (hdmap_utils_->isTrafficLight(lanelet_id)) {
283 v2i_enabled_traffic_lights_.insert(lanelet_id);
285 v2i_enabled_traffic_lights_.erase(lanelet_id);
290 auto isAnyTrafficLightChanged() -> bool;
292 auto startTrafficLightsUpdate(
293 const double conventional_traffic_light_update_rate,
294 const double v2i_traffic_lights_update_rate) -> void;
304 auto generateConventionalUpdateRequest() const
305 -> simulation_api_schema::UpdateTrafficLightsRequest;
307 auto isV2ITrafficLightEnabled(const lanelet::Id lanelet_id) const ->
bool;
314 std::set<lanelet::Id> v2i_enabled_traffic_lights_;
Definition: traffic_lights.hpp:35
ConventionalTrafficLights(const NodeTypePointer &node_ptr, const std::shared_ptr< hdmap_utils::HdMapUtils > &hdmap_utils)
Definition: traffic_lights.hpp:38
~ConventionalTrafficLights() override=default
Definition: traffic_lights.hpp:64
auto setState(const lanelet::Id lanelet_id, const std::string &state) -> void
Definition: traffic_lights.hpp:71
auto clearState(const lanelet::Id lanelet_id) -> bool
Definition: traffic_lights.hpp:84
auto addState(const lanelet::Id lanelet_id, const std::string &state) -> void
Definition: traffic_lights.hpp:77
auto empty() const -> bool
Definition: traffic_lights.hpp:89
auto apply(simulation_api_schema::UpdateTrafficLightsRequest &request) const -> void
Definition: traffic_lights.hpp:91
DetectedTrafficLights(const std::shared_ptr< hdmap_utils::HdMapUtils > &hdmap_utils)
Definition: traffic_lights.hpp:66
Definition: traffic_light_publisher.hpp:40
Definition: traffic_lights_base.hpp:44
StateChangeType
Definition: traffic_lights_base.hpp:47
auto isAnyTrafficLightChanged() const -> bool
Definition: traffic_lights_base.cpp:30
auto generateUpdateTrafficLightsRequest() const -> simulation_api_schema::UpdateTrafficLightsRequest
Definition: traffic_lights_base.cpp:112
const rclcpp::Clock::SharedPtr clock_ptr_
Definition: traffic_lights_base.hpp:114
const std::unique_ptr< TrafficLightMarkerPublisher > marker_publisher_ptr_
Definition: traffic_lights_base.hpp:117
std::unordered_map< lanelet::Id, TrafficLight > traffic_lights_map_
Definition: traffic_lights_base.hpp:116
Definition: traffic_lights.hpp:207
auto getGroundTruth() const -> std::shared_ptr< GroundTruthType >
Definition: traffic_lights.hpp:219
TrafficLightsChannel(const NodeTypePointer &node_ptr, const std::shared_ptr< hdmap_utils::HdMapUtils > &hdmap_utils, Args &&... args)
Definition: traffic_lights.hpp:210
auto getDetected() const -> std::shared_ptr< DetectedTrafficLights >
Definition: traffic_lights.hpp:221
auto generateUpdateRequest() const -> simulation_api_schema::UpdateTrafficLightsRequest
Definition: traffic_lights.hpp:225
auto hasDetectedChanges() const -> bool
Definition: traffic_lights.hpp:223
Definition: traffic_lights.hpp:239
TrafficLights(const NodeTypePointer &node_ptr, const std::shared_ptr< hdmap_utils::HdMapUtils > &hdmap_utils, const std::string &architecture_type)
Definition: traffic_lights.hpp:242
auto setV2IFeature(const lanelet::Id lanelet_id, const bool enabled) -> void
Definition: traffic_lights.hpp:271
Definition: traffic_lights.hpp:118
auto addTrafficLightsStatePrediction(const lanelet::Id lanelet_id, const std::string &state, double time_ahead_seconds) -> void
Definition: traffic_lights.cpp:67
V2ITrafficLights(const NodeTypePointer &node_ptr, const std::shared_ptr< hdmap_utils::HdMapUtils > &hdmap_utils, const std::string &architecture_type)
Definition: traffic_lights.hpp:121
auto clearTrafficLightsStatePredictions() -> void
Definition: traffic_lights.cpp:107
~V2ITrafficLights() override=default
auto setDetectedTrafficLights(std::shared_ptr< DetectedTrafficLights > detected) -> void
Definition: traffic_lights.hpp:134
Definition: lanelet_wrapper.hpp:40
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