scenario_simulator_v2 C++ API
detection_sensor.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 SIMPLE_SENSOR_SIMULATOR__SENSOR_SIMULATION__DETECTION_SENSOR__DETECTION_SENSOR_HPP_
16 #define SIMPLE_SENSOR_SIMULATOR__SENSOR_SIMULATION__DETECTION_SENSOR__DETECTION_SENSOR_HPP_
17 
18 #include <simulation_interface/simulation_api_schema.pb.h>
19 
20 #include <autoware_perception_msgs/msg/tracked_objects.hpp>
21 #include <geometry/plane.hpp>
22 #include <geometry_msgs/msg/pose.hpp>
23 #include <get_parameter/get_parameter.hpp>
24 #include <memory>
25 #include <optional>
26 #include <queue>
27 #include <random>
28 #include <rclcpp/rclcpp.hpp>
30 #include <string>
31 #include <unordered_map>
32 #include <utility>
33 #include <vector>
34 
36 {
38 {
39 protected:
41 
42  simulation_api_schema::DetectionSensorConfiguration configuration_;
43 
45  const double current_simulation_time,
46  const simulation_api_schema::DetectionSensorConfiguration & configuration)
47  : previous_simulation_time_(current_simulation_time), configuration_(configuration)
48  {
49  }
50 
52  const traffic_simulator_msgs::EntityStatus &) const -> bool;
53 
55  const std::vector<traffic_simulator_msgs::EntityStatus> &) const
56  -> std::vector<traffic_simulator_msgs::EntityStatus>::const_iterator;
57 
59  const geometry_msgs::Pose & npc_pose, const geometry_msgs::Pose & ego_pose) -> bool;
60 
61 public:
62  virtual ~DetectionSensorBase() = default;
63 
64  virtual void update(
65  const double current_simulation_time, const std::vector<traffic_simulator_msgs::EntityStatus> &,
66  const rclcpp::Time & current_ros_time,
67  const std::vector<std::string> & lidar_detected_entities) = 0;
68 
69 private:
70  std::optional<math::geometry::Plane> ego_plane_opt_{std::nullopt};
71  std::optional<geometry_msgs::msg::Pose> ego_plane_pose_opt_{std::nullopt};
72 };
73 
74 template <typename T, typename U = autoware_perception_msgs::msg::TrackedObjects>
76 {
77  const typename rclcpp::Publisher<T>::SharedPtr detected_objects_publisher;
78 
79  const typename rclcpp::Publisher<U>::SharedPtr ground_truth_objects_publisher;
80 
81  int noise_model_version;
82 
83  std::default_random_engine random_engine_;
84 
85  std::queue<std::pair<std::vector<traffic_simulator_msgs::EntityStatus>, double>>
86  unpublished_detected_entities, unpublished_ground_truth_entities;
87 
88  struct NoiseOutput
89  {
90  double simulation_time, distance_noise, yaw_noise;
91 
92  bool true_positive, flip;
93 
94  explicit NoiseOutput(double simulation_time = 0.0)
95  : simulation_time(simulation_time),
96  distance_noise(0.0),
97  yaw_noise(0.0),
98  true_positive(true),
99  flip(false)
100  {
101  }
102  };
103 
104  std::unordered_map<std::string, NoiseOutput> noise_outputs;
105 
106  template <typename Message, typename std::enable_if_t<std::is_same_v<Message, T>, int> = 0>
107  auto delay() const
108  {
109  static const auto override_legacy_configuration = common::getParameter<bool>(
110  detected_objects_publisher->get_topic_name() + std::string(".override_legacy_configuration"));
111  if (override_legacy_configuration) {
112  static const auto delay = common::getParameter<double>(
113  detected_objects_publisher->get_topic_name() + std::string(".delay"));
114  return delay;
115  } else {
116  return configuration_.object_recognition_delay();
117  }
118  }
119 
120  template <typename Message, typename std::enable_if_t<std::is_same_v<Message, U>, int> = 0>
121  auto delay() const
122  {
123  static const auto override_legacy_configuration = common::getParameter<bool>(
124  ground_truth_objects_publisher->get_topic_name() +
125  std::string(".override_legacy_configuration"));
126  if (override_legacy_configuration) {
127  static const auto delay = common::getParameter<double>(
128  ground_truth_objects_publisher->get_topic_name() + std::string(".delay"));
129  return delay;
130  } else {
131  return configuration_.object_recognition_ground_truth_delay();
132  }
133  }
134 
135  auto range() const
136  {
137  static const auto override_legacy_configuration = common::getParameter<bool>(
138  detected_objects_publisher->get_topic_name() + std::string(".override_legacy_configuration"));
139  if (override_legacy_configuration) {
140  static const auto range = common::getParameter<double>(
141  detected_objects_publisher->get_topic_name() + std::string(".range"), 300.0);
142  return range;
143  } else {
144  return configuration_.range();
145  }
146  }
147 
148  auto detect_all_objects_in_range() const
149  {
150  // cspell: ignore occlusionless
151  static const auto override_legacy_configuration = common::getParameter<bool>(
152  detected_objects_publisher->get_topic_name() + std::string(".override_legacy_configuration"));
153  if (override_legacy_configuration) {
154  static const auto occlusionless = common::getParameter<bool>(
155  detected_objects_publisher->get_topic_name() + std::string(".occlusionless"));
156  return occlusionless;
157  } else {
158  return configuration_.detect_all_objects_in_range();
159  }
160  }
161 
162 public:
163  explicit DetectionSensor(
164  const double current_simulation_time,
165  const simulation_api_schema::DetectionSensorConfiguration & configuration,
166  const typename rclcpp::Publisher<T>::SharedPtr & publisher,
167  const typename rclcpp::Publisher<U>::SharedPtr & ground_truth_publisher = nullptr)
168  : DetectionSensorBase(current_simulation_time, configuration),
169  detected_objects_publisher(publisher),
170  ground_truth_objects_publisher(ground_truth_publisher),
171  noise_model_version(common::getParameter<int>(
172  detected_objects_publisher->get_topic_name() + std::string(".noise.model.version"))),
173  random_engine_([this]() {
174  if (const auto seed = [this]() -> std::random_device::result_type {
175  switch (noise_model_version) {
176  default:
177  [[fallthrough]];
178  case 1:
179  return configuration_.random_seed();
180  case 2:
181  [[fallthrough]];
182  case 3:
183  return common::getParameter<int>(
184  detected_objects_publisher->get_topic_name() + std::string(".seed"));
185  }
186  }();
187  seed) {
188  if (std::random_device::min() <= seed and seed <= std::random_device::max()) {
189  return seed;
190  } else {
192  "The value of parameter ",
193  std::quoted(detected_objects_publisher->get_topic_name() + std::string(".seed")),
194  " must be greater than or equal to ", std::random_device::min(),
195  " and less than or equal to ", std::random_device::max());
196  }
197  } else {
198  return std::random_device()();
199  }
200  }())
201  {
202  }
203 
204  ~DetectionSensor() override = default;
205 
206  auto update(
207  const double, const std::vector<traffic_simulator_msgs::EntityStatus> &, const rclcpp::Time &,
208  const std::vector<std::string> & lidar_detected_entities) -> void override;
209 };
210 } // namespace simple_sensor_simulator
211 
212 #endif // SIMPLE_SENSOR_SIMULATOR__SENSOR_SIMULATION__DETECTION_SENSOR__DETECTION_SENSOR_HPP_
Definition: detection_sensor.hpp:38
auto isOnOrAboveEgoPlane(const geometry_msgs::Pose &npc_pose, const geometry_msgs::Pose &ego_pose) -> bool
Definition: detection_sensor.cpp:73
DetectionSensorBase(const double current_simulation_time, const simulation_api_schema::DetectionSensorConfiguration &configuration)
Definition: detection_sensor.hpp:44
simulation_api_schema::DetectionSensorConfiguration configuration_
Definition: detection_sensor.hpp:42
auto isEgoEntityStatusToWhichThisSensorIsAttached(const traffic_simulator_msgs::EntityStatus &) const -> bool
Definition: detection_sensor.cpp:50
double previous_simulation_time_
Definition: detection_sensor.hpp:40
virtual void update(const double current_simulation_time, const std::vector< traffic_simulator_msgs::EntityStatus > &, const rclcpp::Time &current_ros_time, const std::vector< std::string > &lidar_detected_entities)=0
auto findEgoEntityStatusToWhichThisSensorIsAttached(const std::vector< traffic_simulator_msgs::EntityStatus > &) const -> std::vector< traffic_simulator_msgs::EntityStatus >::const_iterator
Definition: detection_sensor.cpp:57
Definition: detection_sensor.hpp:76
DetectionSensor(const double current_simulation_time, const simulation_api_schema::DetectionSensorConfiguration &configuration, const typename rclcpp::Publisher< T >::SharedPtr &publisher, const typename rclcpp::Publisher< U >::SharedPtr &ground_truth_publisher=nullptr)
Definition: detection_sensor.hpp:163
auto update(const double, const std::vector< traffic_simulator_msgs::EntityStatus > &, const rclcpp::Time &, const std::vector< std::string > &lidar_detected_entities) -> void override
Definition: concatenate.hpp:24
Definition: constants.hpp:19
Definition: lanelet_wrapper.hpp:40
geometry_msgs::msg::Pose Pose
Definition: lanelet_wrapper.hpp:66
std::string string
Definition: junit5.hpp:26
traffic_simulator_msgs::EntityStatus EntityStatus
Definition: helper_functions.hpp:32