scenario_simulator_v2 C++ API
traffic_lights_base.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_BASE_HPP_
16 #define TRAFFIC_SIMULATOR__TRAFFIC_LIGHTS__TRAFFIC_LIGHTS_BASE_HPP_
17 
18 #include <simulation_interface/simulation_api_schema.pb.h>
19 
20 #include <functional>
21 #include <memory>
22 #include <rclcpp/rclcpp.hpp>
23 #include <string>
28 #include <unordered_map>
29 #include <vector>
30 
31 namespace traffic_simulator
32 {
33 /*
34  TrafficLightsBase class is designed in such a way that while trying to perform an operation
35  on a TrafficLight (add, set, etc.) that is not added to traffic_light_map_,
36  it adds the traffic light first and then performs the operation, so that the methods
37  here cannot be tagged with the "const" specifier
38 */
40 {
41 public:
42  // State change callback types
43  enum class StateChangeType {
44  SET, // setTrafficLightsState()
45  CLEAR, // clearTrafficLightsState()
46  ADD, // addTrafficLightsState()
47  };
48 
49  using StateChangeCallback = std::function<void(
50  lanelet::Id lanelet_id, const std::string & state, StateChangeType change_type)>;
51 
52  template <typename NodeTypePointer>
54  const NodeTypePointer & node_ptr, const std::shared_ptr<hdmap_utils::HdMapUtils> & hdmap_utils)
56  clock_ptr_(node_ptr->get_clock()),
57  marker_publisher_ptr_(std::make_unique<TrafficLightMarkerPublisher>(node_ptr)),
58  rate_updater_(node_ptr, [this]() { update(); })
59  {
60  }
61 
62  virtual ~TrafficLightsBase() = default;
63 
64  // update
65  auto startUpdate(const double update_rate) -> void;
66 
67  auto resetUpdate(const double update_rate) -> void;
68 
69  // checks, setters, getters
70  auto isAnyTrafficLightChanged() const -> bool;
71 
72  auto isRequiredStopTrafficLightState(const lanelet::Id traffic_light_id) -> bool;
73 
74  auto compareTrafficLightsState(const lanelet::Id lanelet_id, const std::string & state) -> bool;
75 
77  const lanelet::Id lanelet_id, const traffic_simulator::TrafficLight::Color & color) -> void;
78 
79  auto setTrafficLightsState(const lanelet::Id lanelet_id, const std::string & state) -> void;
80 
81  auto clearTrafficLightsState(const lanelet::Id lanelet_id) -> void;
82 
83  auto addTrafficLightsState(const lanelet::Id lanelet_id, const std::string & state) -> void;
84 
85  auto setTrafficLightsConfidence(const lanelet::Id lanelet_id, const double confidence) -> void;
86 
87  auto getTrafficLightsComposedState(const lanelet::Id lanelet_id) -> std::string;
88 
90  -> simulation_api_schema::UpdateTrafficLightsRequest;
91 
92  auto getTrafficLight(const lanelet::Id traffic_light_id) -> TrafficLight &;
93 
94  auto registerStateChangeCallback(StateChangeCallback callback) -> void;
95 
96 protected:
97  virtual auto update() const -> void = 0;
98 
99  auto notifyStateChange(
100  const lanelet::Id lanelet_id, const std::string & state, StateChangeType change_type) -> void;
101 
102  auto isTrafficLightAdded(const lanelet::Id traffic_light_id) const -> bool;
103 
104  auto addTrafficLight(const lanelet::Id traffic_light_id) -> void;
105 
106  auto getTrafficLights(const lanelet::Id lanelet_id)
107  -> std::vector<std::reference_wrapper<TrafficLight>>;
108 
109  const std::shared_ptr<hdmap_utils::HdMapUtils> hdmap_utils_;
110  const rclcpp::Clock::SharedPtr clock_ptr_;
111 
112  std::unordered_map<lanelet::Id, TrafficLight> traffic_lights_map_;
115 
117 };
118 } // namespace traffic_simulator
119 #endif // TRAFFIC_SIMULATOR__TRAFFIC_LIGHTS__TRAFFIC_LIGHTS_BASE_HPP_
Definition: configurable_rate_updater.hpp:24
Definition: traffic_light_marker_publisher.hpp:24
Definition: traffic_lights_base.hpp:40
StateChangeType
Definition: traffic_lights_base.hpp:43
TrafficLightsBase(const NodeTypePointer &node_ptr, const std::shared_ptr< hdmap_utils::HdMapUtils > &hdmap_utils)
Definition: traffic_lights_base.hpp:53
auto resetUpdate(const double update_rate) -> void
Definition: traffic_lights_base.cpp:24
auto setTrafficLightsState(const lanelet::Id lanelet_id, const std::string &state) -> void
Definition: traffic_lights_base.cpp:68
auto isAnyTrafficLightChanged() const -> bool
Definition: traffic_lights_base.cpp:30
auto generateUpdateTrafficLightsRequest() const -> simulation_api_schema::UpdateTrafficLightsRequest
Definition: traffic_lights_base.cpp:112
auto addTrafficLight(const lanelet::Id traffic_light_id) -> void
Definition: traffic_lights_base.cpp:132
ConfigurableRateUpdater rate_updater_
Definition: traffic_lights_base.hpp:114
virtual auto update() const -> void=0
auto isRequiredStopTrafficLightState(const lanelet::Id traffic_light_id) -> bool
Definition: traffic_lights_base.cpp:32
const rclcpp::Clock::SharedPtr clock_ptr_
Definition: traffic_lights_base.hpp:110
std::vector< StateChangeCallback > state_change_callbacks_
Definition: traffic_lights_base.hpp:116
auto clearTrafficLightsState(const lanelet::Id lanelet_id) -> void
Definition: traffic_lights_base.cpp:76
auto registerStateChangeCallback(StateChangeCallback callback) -> void
Definition: traffic_lights_base.cpp:148
auto setTrafficLightsConfidence(const lanelet::Id lanelet_id, const double confidence) -> void
Definition: traffic_lights_base.cpp:93
auto notifyStateChange(const lanelet::Id lanelet_id, const std::string &state, StateChangeType change_type) -> void
Definition: traffic_lights_base.cpp:153
auto startUpdate(const double update_rate) -> void
Definition: traffic_lights_base.cpp:19
auto addTrafficLightsState(const lanelet::Id lanelet_id, const std::string &state) -> void
Definition: traffic_lights_base.cpp:84
const std::unique_ptr< TrafficLightMarkerPublisher > marker_publisher_ptr_
Definition: traffic_lights_base.hpp:113
const std::shared_ptr< hdmap_utils::HdMapUtils > hdmap_utils_
Definition: traffic_lights_base.hpp:109
auto isTrafficLightAdded(const lanelet::Id traffic_light_id) const -> bool
Definition: traffic_lights_base.cpp:127
auto getTrafficLight(const lanelet::Id traffic_light_id) -> TrafficLight &
Definition: traffic_lights_base.cpp:140
auto setTrafficLightsColor(const lanelet::Id lanelet_id, const traffic_simulator::TrafficLight::Color &color) -> void
Definition: traffic_lights_base.cpp:60
auto getTrafficLightsComposedState(const lanelet::Id lanelet_id) -> std::string
Definition: traffic_lights_base.cpp:101
std::function< void(lanelet::Id lanelet_id, const std::string &state, StateChangeType change_type)> StateChangeCallback
Definition: traffic_lights_base.hpp:50
auto getTrafficLights(const lanelet::Id lanelet_id) -> std::vector< std::reference_wrapper< TrafficLight >>
Definition: traffic_lights_base.cpp:163
auto compareTrafficLightsState(const lanelet::Id lanelet_id, const std::string &state) -> bool
Definition: traffic_lights_base.cpp:43
std::unordered_map< lanelet::Id, TrafficLight > traffic_lights_map_
Definition: traffic_lights_base.hpp:112
Definition: cache.hpp:28
Definition: lanelet_wrapper.hpp:40
Definition: api.hpp:32
std::string string
Definition: junit5.hpp:26
Definition: traffic_light.hpp:44
Definition: traffic_light.hpp:42