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 // This message will be deleted in the future
19 #if __has_include(<autoware_perception_msgs/msg/traffic_signal_array.hpp>)
20 #include <autoware_perception_msgs/msg/traffic_signal_array.hpp>
21 #endif
22 
23 #if __has_include(<autoware_perception_msgs/msg/traffic_light_group_array.hpp>)
24 #include <autoware_perception_msgs/msg/traffic_light_group_array.hpp>
25 #endif
26 
29 
30 namespace traffic_simulator
31 {
33 {
34 public:
35  template <typename NodeTypePointer>
37  const NodeTypePointer & node_ptr, const std::shared_ptr<hdmap_utils::HdMapUtils> & hdmap_utils)
38  : TrafficLightsBase(node_ptr, hdmap_utils),
39  backward_compatible_publisher_ptr_(
40  std::make_unique<TrafficLightPublisher<traffic_simulator_msgs::msg::TrafficLightArrayV1>>(
41  node_ptr, "/simulation/traffic_lights"))
42  {
43  }
44 
45  ~ConventionalTrafficLights() override = default;
46 
47 private:
48  auto update() const -> void
49  {
50  backward_compatible_publisher_ptr_->publish(
53  marker_publisher_ptr_->deleteMarkers();
54  }
56  }
57 
58  const std::unique_ptr<TrafficLightPublisherBase> backward_compatible_publisher_ptr_;
59 };
60 
62 {
63 public:
64  template <typename NodeTypePointer>
65  explicit V2ITrafficLights(
66  const NodeTypePointer & node_ptr, const std::shared_ptr<hdmap_utils::HdMapUtils> & hdmap_utils,
67  const std::string & architecture_type)
68  : TrafficLightsBase(node_ptr, hdmap_utils),
69  publisher_ptr_(makePublisher(
70  node_ptr, architecture_type,
71  "/perception/traffic_light_recognition/external/traffic_signals")),
72  legacy_topic_publisher_ptr_(makePublisher(node_ptr, architecture_type, "/v2x/traffic_signals"))
73  {
74  }
75 
76  ~V2ITrafficLights() override = default;
77 
78 private:
79  auto update() const -> void override
80  {
81  const auto now = clock_ptr_->now();
82  const auto request = generateUpdateTrafficLightsRequest();
83  publisher_ptr_->publish(now, request);
84  legacy_topic_publisher_ptr_->publish(now, request);
86  marker_publisher_ptr_->deleteMarkers();
87  }
89  }
90 
91  template <typename NodeTypePointer>
92  auto makePublisher(
93  const NodeTypePointer & node_ptr, const std::string & architecture_type,
94  const std::string & topic_name) -> std::unique_ptr<TrafficLightPublisherBase>
95  {
96  /*
97  V2ITrafficLights in TrafficSimulator publishes using architecture-independent topics ("awf/universe..."):
98  "/v2x/traffic_signals" and "/perception/traffic_light_recognition/external/traffic_signals"
99 
100  TrafficLightsDetector in SimpleSensorSimulator publishes using architecture-dependent topics:
101  "/perception/traffic_light_recognition/internal/traffic_signals" for >= "awf/universe/20230906"
102  "/perception/traffic_light_recognition/traffic_signals" for "awf/universe"
103  */
104  if (architecture_type == "awf/universe") {
105  throw common::SemanticError(
106  "This version of scenario_simulator_v2 does not support ", std::quoted(architecture_type),
107  " as ", std::quoted("architecture_type"), ". Please use older version.");
108 #if __has_include(<autoware_perception_msgs/msg/traffic_signal_array.hpp>)
109  } else if (architecture_type <= "awf/universe/20230906") {
110  return std::make_unique<
111  TrafficLightPublisher<autoware_perception_msgs::msg::TrafficSignalArray>>(
112  node_ptr, topic_name);
113 #endif
114 #if __has_include(<autoware_perception_msgs/msg/traffic_light_group_array.hpp>)
115  } else if (architecture_type >= "awf/universe/20240605") {
116  return std::make_unique<
117  TrafficLightPublisher<autoware_perception_msgs::msg::TrafficLightGroupArray>>(
118  node_ptr, topic_name);
119 #endif
120  } else {
121  throw common::SemanticError(
122  "Unexpected architecture_type ", std::quoted(architecture_type),
123  " given for V2I traffic lights simulation.");
124  }
125  }
126 
127  const std::unique_ptr<TrafficLightPublisherBase> publisher_ptr_;
128  const std::unique_ptr<TrafficLightPublisherBase> legacy_topic_publisher_ptr_;
129 };
130 
132 {
133 public:
134  template <typename NodeTypePointer>
135  explicit TrafficLights(
136  const NodeTypePointer & node_ptr, const std::shared_ptr<hdmap_utils::HdMapUtils> & hdmap_utils,
137  const std::string & architecture_type)
138  : conventional_traffic_lights_(
139  std::make_shared<ConventionalTrafficLights>(node_ptr, hdmap_utils)),
140  v2i_traffic_lights_(
141  std::make_shared<V2ITrafficLights>(node_ptr, hdmap_utils, architecture_type))
142  {
143  }
144 
145  auto isAnyTrafficLightChanged() -> bool;
146 
148  const double conventional_traffic_light_update_rate,
149  const double v2i_traffic_lights_update_rate) -> void;
150 
151  auto getConventionalTrafficLights() const -> std::shared_ptr<ConventionalTrafficLights>;
152 
153  auto getV2ITrafficLights() const -> std::shared_ptr<V2ITrafficLights>;
154 
155 private:
156  const std::shared_ptr<ConventionalTrafficLights> conventional_traffic_lights_;
157  const std::shared_ptr<V2ITrafficLights> v2i_traffic_lights_;
158 };
159 } // namespace traffic_simulator
160 #endif // TRAFFIC_SIMULATOR__TRAFFIC_LIGHTS__TRAFFIC_LIGHTS_HPP_
Definition: traffic_lights.hpp:33
ConventionalTrafficLights(const NodeTypePointer &node_ptr, const std::shared_ptr< hdmap_utils::HdMapUtils > &hdmap_utils)
Definition: traffic_lights.hpp:36
Definition: traffic_light_publisher.hpp:39
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:132
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:135
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:62
V2ITrafficLights(const NodeTypePointer &node_ptr, const std::shared_ptr< hdmap_utils::HdMapUtils > &hdmap_utils, const std::string &architecture_type)
Definition: traffic_lights.hpp:65
~V2ITrafficLights() override=default
Definition: cache.hpp:46
Definition: cache.hpp:27
Definition: api.hpp:48
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