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