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_api_schema.pb.h>
19 
20 #include <memory>
21 #include <rclcpp/rclcpp.hpp>
22 #include <string>
27 #include <unordered_map>
28 #include <vector>
29 
30 namespace traffic_simulator
31 {
32 /*
33  TrafficLightsBase class is designed in such a way that while trying to perform an operation
34  on a TrafficLight (add, set, etc.) that is not added to traffic_light_map_,
35  it adds the traffic light first and then performs the operation, so that the methods
36  here cannot be tagged with the "const" specifier
37 */
39 {
40 public:
41  template <typename NodeTypePointer>
43  const NodeTypePointer & node_ptr, const std::shared_ptr<hdmap_utils::HdMapUtils> & hdmap_utils)
45  clock_ptr_(node_ptr->get_clock()),
46  marker_publisher_ptr_(std::make_unique<TrafficLightMarkerPublisher>(node_ptr)),
47  rate_updater_(node_ptr, [this]() { update(); })
48  {
49  }
50 
51  virtual ~TrafficLightsBase() = default;
52 
53  // update
54  auto startUpdate(const double update_rate) -> void;
55 
56  auto resetUpdate(const double update_rate) -> void;
57 
58  // checks, setters, getters
59  auto isAnyTrafficLightChanged() const -> bool;
60 
61  auto isRequiredStopTrafficLightState(const lanelet::Id traffic_light_id) -> bool;
62 
63  auto compareTrafficLightsState(const lanelet::Id lanelet_id, const std::string & state) -> bool;
64 
66  const lanelet::Id lanelet_id, const traffic_simulator::TrafficLight::Color & color) -> void;
67 
68  auto setTrafficLightsState(const lanelet::Id lanelet_id, const std::string & state) -> void;
69 
70  auto setTrafficLightsConfidence(const lanelet::Id lanelet_id, const double confidence) -> void;
71 
72  auto getTrafficLightsComposedState(const lanelet::Id lanelet_id) -> std::string;
73 
75  -> simulation_api_schema::UpdateTrafficLightsRequest;
76 
77 protected:
78  virtual auto update() const -> void = 0;
79 
80  auto isTrafficLightAdded(const lanelet::Id traffic_light_id) const -> bool;
81 
82  auto addTrafficLight(const lanelet::Id traffic_light_id) -> void;
83 
84  auto getTrafficLight(const lanelet::Id traffic_light_id) -> TrafficLight &;
85 
86  auto getTrafficLights(const lanelet::Id lanelet_id)
87  -> std::vector<std::reference_wrapper<TrafficLight>>;
88 
89  const std::shared_ptr<hdmap_utils::HdMapUtils> hdmap_utils_;
90  const rclcpp::Clock::SharedPtr clock_ptr_;
91 
92  std::unordered_map<lanelet::Id, TrafficLight> traffic_lights_map_;
95 };
96 } // namespace traffic_simulator
97 #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:39
TrafficLightsBase(const NodeTypePointer &node_ptr, const std::shared_ptr< hdmap_utils::HdMapUtils > &hdmap_utils)
Definition: traffic_lights_base.hpp:42
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:96
auto addTrafficLight(const lanelet::Id traffic_light_id) -> void
Definition: traffic_lights_base.cpp:116
ConfigurableRateUpdater rate_updater_
Definition: traffic_lights_base.hpp:94
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:90
auto setTrafficLightsConfidence(const lanelet::Id lanelet_id, const double confidence) -> void
Definition: traffic_lights_base.cpp:77
auto startUpdate(const double update_rate) -> void
Definition: traffic_lights_base.cpp:19
const std::unique_ptr< TrafficLightMarkerPublisher > marker_publisher_ptr_
Definition: traffic_lights_base.hpp:93
const std::shared_ptr< hdmap_utils::HdMapUtils > hdmap_utils_
Definition: traffic_lights_base.hpp:89
auto isTrafficLightAdded(const lanelet::Id traffic_light_id) const -> bool
Definition: traffic_lights_base.cpp:111
auto getTrafficLight(const lanelet::Id traffic_light_id) -> TrafficLight &
Definition: traffic_lights_base.cpp:124
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:85
auto getTrafficLights(const lanelet::Id lanelet_id) -> std::vector< std::reference_wrapper< TrafficLight >>
Definition: traffic_lights_base.cpp:130
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:92
Definition: cache.hpp:46
Definition: cache.hpp:27
Definition: api.hpp:49
Definition: traffic_light.hpp:44
Definition: traffic_light.hpp:42