scenario_simulator_v2 C++ API
traffic_lights.hpp
Go to the documentation of this file.
1 // Copyright 2015 TIER IV, Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef TRAFFIC_SIMULATOR__TRAFFIC_LIGHTS__TRAFFIC_LIGHTS_HPP_
16 #define TRAFFIC_SIMULATOR__TRAFFIC_LIGHTS__TRAFFIC_LIGHTS_HPP_
17 
18 #include <autoware_auto_perception_msgs/msg/traffic_signal_array.hpp>
19 #include <autoware_perception_msgs/msg/traffic_signal_array.hpp>
20 
21 #if __has_include(<autoware_perception_msgs/msg/traffic_light_group_array.hpp>)
22 #include <autoware_perception_msgs/msg/traffic_light_group_array.hpp>
23 #endif
24 
27 
28 namespace traffic_simulator
29 {
31 {
32 public:
33  template <typename NodeTypePointer>
35  const NodeTypePointer & node_ptr, const std::shared_ptr<hdmap_utils::HdMapUtils> & hdmap_utils)
36  : TrafficLightsBase(node_ptr, hdmap_utils),
37  backward_compatible_publisher_ptr_(
38  std::make_unique<TrafficLightPublisher<traffic_simulator_msgs::msg::TrafficLightArrayV1>>(
39  node_ptr, "/simulation/traffic_lights"))
40  {
41  }
42 
43  ~ConventionalTrafficLights() override = default;
44 
45 private:
46  auto update() const -> void
47  {
48  backward_compatible_publisher_ptr_->publish(
51  marker_publisher_ptr_->deleteMarkers();
52  }
54  }
55 
56  const std::unique_ptr<TrafficLightPublisherBase> backward_compatible_publisher_ptr_;
57 };
58 
60 {
61 public:
62  template <typename NodeTypePointer>
63  explicit V2ITrafficLights(
64  const NodeTypePointer & node_ptr, const std::shared_ptr<hdmap_utils::HdMapUtils> & hdmap_utils,
65  const std::string & architecture_type)
66  : TrafficLightsBase(node_ptr, hdmap_utils),
67  publisher_ptr_(makePublisher(
68  node_ptr, architecture_type,
69  "/perception/traffic_light_recognition/external/traffic_signals")),
70  legacy_topic_publisher_ptr_(makePublisher(node_ptr, architecture_type, "/v2x/traffic_signals"))
71  {
72  }
73 
74  ~V2ITrafficLights() override = default;
75 
76 private:
77  auto update() const -> void override
78  {
79  const auto now = clock_ptr_->now();
80  const auto request = generateUpdateTrafficLightsRequest();
81  publisher_ptr_->publish(now, request);
82  legacy_topic_publisher_ptr_->publish(now, request);
84  marker_publisher_ptr_->deleteMarkers();
85  }
87  }
88 
89  template <typename NodeTypePointer>
90  auto makePublisher(
91  const NodeTypePointer & node_ptr, const std::string & architecture_type,
92  const std::string & topic_name) -> std::unique_ptr<TrafficLightPublisherBase>
93  {
94  /*
95  V2ITrafficLights in TrafficSimulator publishes publishes using architecture-independent topics ("awf/universe..."):
96  "/v2x/traffic_signals" and "/perception/traffic_light_recognition/external/traffic_signals"
97 
98  TrafficLightsDetector in SimpleSensorSimulator publishes using architecture-dependent topics:
99  "/perception/traffic_light_recognition/internal/traffic_signals" for >= "awf/universe/20230906"
100  "/perception/traffic_light_recognition/traffic_signals" for "awf/universe"
101  */
102  if (architecture_type <= "awf/universe/20230906") {
103  return std::make_unique<
105  node_ptr, topic_name);
106 #if __has_include(<autoware_perception_msgs/msg/traffic_light_group_array.hpp>)
107  } else if (architecture_type >= "awf/universe/20240605") {
108  return std::make_unique<
109  TrafficLightPublisher<autoware_perception_msgs::msg::TrafficLightGroupArray>>(
110  node_ptr, topic_name);
111 #endif
112  } else {
113  throw common::SemanticError(
114  "Unexpected architecture_type ", std::quoted(architecture_type),
115  " given for V2I traffic lights simulation.");
116  }
117  }
118 
119  const std::unique_ptr<TrafficLightPublisherBase> publisher_ptr_;
120  const std::unique_ptr<TrafficLightPublisherBase> legacy_topic_publisher_ptr_;
121 };
122 
124 {
125 public:
126  template <typename NodeTypePointer>
127  explicit TrafficLights(
128  const NodeTypePointer & node_ptr, const std::shared_ptr<hdmap_utils::HdMapUtils> & hdmap_utils,
129  const std::string & architecture_type)
130  : conventional_traffic_lights_(
131  std::make_shared<ConventionalTrafficLights>(node_ptr, hdmap_utils)),
132  v2i_traffic_lights_(
133  std::make_shared<V2ITrafficLights>(node_ptr, hdmap_utils, architecture_type))
134  {
135  }
136 
137  auto isAnyTrafficLightChanged() -> bool;
138 
140  const double conventional_traffic_light_update_rate,
141  const double v2i_traffic_lights_update_rate) -> void;
142 
143  auto getConventionalTrafficLights() const -> std::shared_ptr<ConventionalTrafficLights>;
144 
145  auto getV2ITrafficLights() const -> std::shared_ptr<V2ITrafficLights>;
146 
147 private:
148  const std::shared_ptr<ConventionalTrafficLights> conventional_traffic_lights_;
149  const std::shared_ptr<V2ITrafficLights> v2i_traffic_lights_;
150 };
151 } // namespace traffic_simulator
152 #endif // TRAFFIC_SIMULATOR__TRAFFIC_LIGHTS__TRAFFIC_LIGHTS_HPP_
Definition: traffic_lights.hpp:31
ConventionalTrafficLights(const NodeTypePointer &node_ptr, const std::shared_ptr< hdmap_utils::HdMapUtils > &hdmap_utils)
Definition: traffic_lights.hpp:34
Definition: traffic_light_publisher.hpp:38
Definition: traffic_lights_base.hpp:39
auto isAnyTrafficLightChanged() const -> bool
Definition: traffic_lights_base.cpp:30
auto generateUpdateTrafficLightsRequest() const -> simulation_api_schema::UpdateTrafficLightsRequest
Definition: traffic_lights_base.cpp:96
const rclcpp::Clock::SharedPtr clock_ptr_
Definition: traffic_lights_base.hpp:90
const std::unique_ptr< TrafficLightMarkerPublisher > marker_publisher_ptr_
Definition: traffic_lights_base.hpp:93
std::unordered_map< lanelet::Id, TrafficLight > traffic_lights_map_
Definition: traffic_lights_base.hpp:92
Definition: traffic_lights.hpp:124
auto getV2ITrafficLights() const -> std::shared_ptr< V2ITrafficLights >
Definition: traffic_lights.cpp:39
TrafficLights(const NodeTypePointer &node_ptr, const std::shared_ptr< hdmap_utils::HdMapUtils > &hdmap_utils, const std::string &architecture_type)
Definition: traffic_lights.hpp:127
auto startTrafficLightsUpdate(const double conventional_traffic_light_update_rate, const double v2i_traffic_lights_update_rate) -> void
Definition: traffic_lights.cpp:25
auto getConventionalTrafficLights() const -> std::shared_ptr< ConventionalTrafficLights >
Definition: traffic_lights.cpp:33
auto isAnyTrafficLightChanged() -> bool
Definition: traffic_lights.cpp:19
Definition: traffic_lights.hpp:60
V2ITrafficLights(const NodeTypePointer &node_ptr, const std::shared_ptr< hdmap_utils::HdMapUtils > &hdmap_utils, const std::string &architecture_type)
Definition: traffic_lights.hpp:63
~V2ITrafficLights() override=default
Definition: cache.hpp:46
Definition: cache.hpp:27
Definition: api.hpp:49
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