scenario_simulator_v2 C++ API
lidar_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__LIDAR__LIDAR_SENSOR_HPP_
16 #define SIMPLE_SENSOR_SIMULATOR__SENSOR_SIMULATION__LIDAR__LIDAR_SENSOR_HPP_
17 
18 #include <simulation_interface/simulation_api_schema.pb.h>
19 
20 #include <agnocast_wrapper/agnocast_wrapper.hpp>
21 #include <get_parameter/get_parameter.hpp>
22 #include <memory>
23 #include <queue>
24 #include <rclcpp/rclcpp.hpp>
26 #include <sensor_msgs/msg/point_cloud2.hpp>
29 #include <string>
30 #include <vector>
31 
33 {
35 {
36 protected:
38 
39  simulation_api_schema::LidarConfiguration configuration_;
40 
42  std::set<std::string> detected_objects_;
43 
44  explicit LidarSensorBase(
45  const double current_simulation_time,
46  const simulation_api_schema::LidarConfiguration & configuration)
47  : previous_simulation_time_(current_simulation_time), configuration_(configuration)
48  {
49  }
50 
51 public:
52  virtual ~LidarSensorBase() = default;
53 
54  virtual auto update(
55  const double current_simulation_time, const std::vector<traffic_simulator_msgs::EntityStatus> &,
56  const rclcpp::Time & current_ros_time) -> void = 0;
57 
58  auto getDetectedObjects() const -> const std::set<std::string> & { return detected_objects_; }
59 };
60 
61 template <typename T>
63 {
64  const agnocast_wrapper::PublisherPtr<T> publisher_ptr_;
65 
66  std::queue<std::pair<sensor_msgs::msg::PointCloud2, double>> queue_pointcloud_;
67 
68  std::unique_ptr<LidarNoiseModelV1> noise_model_v1_ = nullptr;
69 
70  auto raycast(const std::vector<traffic_simulator_msgs::EntityStatus> &, const rclcpp::Time &)
71  -> T;
72 
73 public:
74  explicit LidarSensor(
75  const double current_simulation_time,
76  const simulation_api_schema::LidarConfiguration & configuration,
77  const agnocast_wrapper::PublisherPtr<T> & publisher_ptr)
78  : LidarSensorBase(current_simulation_time, configuration), publisher_ptr_(publisher_ptr)
79  {
80  raycaster_.setDirection(configuration);
81  const std::string topic_name = publisher_ptr_->get_topic_name();
82  const std::string version_parameter_name = topic_name + ".noise.model.version";
83 
84  if (auto & parameter_node = common::getParameterNode();
85  parameter_node.has_parameter(version_parameter_name)) {
86  const auto version = common::getParameter<int>(version_parameter_name);
87  if (version == 1) {
88  const auto seed = common::getParameter<int>(topic_name + ".seed");
89  noise_model_v1_ = std::make_unique<LidarNoiseModelV1>(topic_name, seed);
90  } else {
91  throw common::Error(
92  "Unexpected noise model version for LiDAR sensor: ", version,
93  ". Expected version is 1 for now.");
94  }
95  } else {
96  // If parameter doesn't exist, no noise model is used
97  }
98  }
99 
100  auto update(
101  const double current_simulation_time,
102  const std::vector<traffic_simulator_msgs::EntityStatus> & status,
103  const rclcpp::Time & current_ros_time) -> void override
104  {
105  if (
106  current_simulation_time - previous_simulation_time_ - configuration_.scan_duration() >=
107  -0.002) {
108  previous_simulation_time_ = current_simulation_time;
109  queue_pointcloud_.push(
110  std::make_pair(raycast(status, current_ros_time), current_simulation_time));
111  } else {
112  detected_objects_.clear();
113  }
114 
115  if (
116  not queue_pointcloud_.empty() and
117  current_simulation_time - queue_pointcloud_.front().second >=
118  configuration_.lidar_sensor_delay()) {
119  auto pointcloud = agnocast_wrapper::create_message(publisher_ptr_);
120  *pointcloud = queue_pointcloud_.front().first;
121  queue_pointcloud_.pop();
122  publisher_ptr_->publish(std::move(pointcloud));
123  }
124  }
125 
126 private:
127  // Override
128  Raycaster raycaster_;
129 };
130 
131 template <>
132 auto LidarSensor<sensor_msgs::msg::PointCloud2>::raycast(
133  const std::vector<traffic_simulator_msgs::EntityStatus> &, const rclcpp::Time &)
134  -> sensor_msgs::msg::PointCloud2;
135 } // namespace simple_sensor_simulator
136 
137 #endif // SIMPLE_SENSOR_SIMULATOR__SENSOR_SIMULATION__LIDAR__LIDAR_SENSOR_HPP_
Definition: lidar_sensor.hpp:35
LidarSensorBase(const double current_simulation_time, const simulation_api_schema::LidarConfiguration &configuration)
Definition: lidar_sensor.hpp:44
std::set< std::string > detected_objects_
Definition: lidar_sensor.hpp:42
Raycaster raycaster_
Definition: lidar_sensor.hpp:41
virtual auto update(const double current_simulation_time, const std::vector< traffic_simulator_msgs::EntityStatus > &, const rclcpp::Time &current_ros_time) -> void=0
simulation_api_schema::LidarConfiguration configuration_
Definition: lidar_sensor.hpp:39
auto getDetectedObjects() const -> const std::set< std::string > &
Definition: lidar_sensor.hpp:58
double previous_simulation_time_
Definition: lidar_sensor.hpp:37
Definition: lidar_sensor.hpp:63
auto update(const double current_simulation_time, const std::vector< traffic_simulator_msgs::EntityStatus > &status, const rclcpp::Time &current_ros_time) -> void override
Definition: lidar_sensor.hpp:100
LidarSensor(const double current_simulation_time, const simulation_api_schema::LidarConfiguration &configuration, const agnocast_wrapper::PublisherPtr< T > &publisher_ptr)
Definition: lidar_sensor.hpp:74
Definition: raycaster.hpp:36
void setDirection(const simulation_api_schema::LidarConfiguration &configuration, double horizontal_angle_start=0, double horizontal_angle_end=2 *M_PI)
Definition: raycaster.cpp:60
Definition: constants.hpp:19
Definition: lanelet_wrapper.hpp:40
std::string string
Definition: junit5.hpp:26
Definition: exception.hpp:27