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_api_schema.pb.h>
19 
20 #include <agnocast_wrapper/agnocast_wrapper.hpp>
22 #include <memory>
23 #include <queue>
24 #include <rclcpp/rclcpp.hpp>
25 #include <sensor_msgs/msg/point_cloud2.hpp>
27 #include <string>
28 #include <vector>
29 
31 {
33 {
34 protected:
36 
37  simulation_api_schema::LidarConfiguration configuration_;
38 
40  std::vector<std::string> detected_objects_;
41 
42  explicit LidarSensorBase(
43  const double current_simulation_time,
44  const simulation_api_schema::LidarConfiguration & configuration)
45  : previous_simulation_time_(current_simulation_time), configuration_(configuration)
46  {
47  }
48 
49 public:
50  virtual ~LidarSensorBase() = default;
51 
52  virtual auto update(
53  const double current_simulation_time, const std::vector<traffic_simulator_msgs::EntityStatus> &,
54  const rclcpp::Time & current_ros_time) -> void = 0;
55 
56  auto getDetectedObjects() const -> const std::vector<std::string> & { return detected_objects_; }
57 };
58 
59 template <typename T>
61 {
62  const agnocast_wrapper::PublisherPtr<T> publisher_ptr_;
63 
64  std::queue<std::pair<sensor_msgs::msg::PointCloud2, double>> queue_pointcloud_;
65 
66  auto raycast(const std::vector<traffic_simulator_msgs::EntityStatus> &, const rclcpp::Time &)
67  -> T;
68 
69 public:
70  explicit LidarSensor(
71  const double current_simulation_time,
72  const simulation_api_schema::LidarConfiguration & configuration,
73  const agnocast_wrapper::PublisherPtr<T> & publisher_ptr)
74  : LidarSensorBase(current_simulation_time, configuration), publisher_ptr_(publisher_ptr)
75  {
76  raycaster_.setDirection(configuration);
77  }
78 
79  auto update(
80  const double current_simulation_time,
81  const std::vector<traffic_simulator_msgs::EntityStatus> & status,
82  const rclcpp::Time & current_ros_time) -> void override
83  {
84  if (
85  current_simulation_time - previous_simulation_time_ - configuration_.scan_duration() >=
86  -0.002) {
87  previous_simulation_time_ = current_simulation_time;
88  queue_pointcloud_.push(
89  std::make_pair(raycast(status, current_ros_time), current_simulation_time));
90  } else {
91  detected_objects_.clear();
92  }
93 
94  if (
95  not queue_pointcloud_.empty() and
96  current_simulation_time - queue_pointcloud_.front().second >=
97  configuration_.lidar_sensor_delay()) {
98  auto pointcloud = agnocast_wrapper::create_message(publisher_ptr_);
99  *pointcloud = queue_pointcloud_.front().first;
100  queue_pointcloud_.pop();
101  publisher_ptr_->publish(std::move(pointcloud));
102  }
103  }
104 
105 private:
106  // Override
107  Raycaster raycaster_;
108 };
109 
110 template <>
111 auto LidarSensor<sensor_msgs::msg::PointCloud2>::raycast(
112  const std::vector<traffic_simulator_msgs::EntityStatus> &, const rclcpp::Time &)
113  -> sensor_msgs::msg::PointCloud2;
114 } // namespace simple_sensor_simulator
115 
116 #endif // SIMPLE_SENSOR_SIMULATOR__SENSOR_SIMULATION__LIDAR__LIDAR_SENSOR_HPP_
Definition: lidar_sensor.hpp:33
LidarSensorBase(const double current_simulation_time, const simulation_api_schema::LidarConfiguration &configuration)
Definition: lidar_sensor.hpp:42
std::vector< std::string > detected_objects_
Definition: lidar_sensor.hpp:40
Raycaster raycaster_
Definition: lidar_sensor.hpp:39
virtual auto update(const double current_simulation_time, const std::vector< traffic_simulator_msgs::EntityStatus > &, const rclcpp::Time &current_ros_time) -> void=0
auto getDetectedObjects() const -> const std::vector< std::string > &
Definition: lidar_sensor.hpp:56
simulation_api_schema::LidarConfiguration configuration_
Definition: lidar_sensor.hpp:37
double previous_simulation_time_
Definition: lidar_sensor.hpp:35
Definition: lidar_sensor.hpp:61
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:79
LidarSensor(const double current_simulation_time, const simulation_api_schema::LidarConfiguration &configuration, const agnocast_wrapper::PublisherPtr< T > &publisher_ptr)
Definition: lidar_sensor.hpp:70
Definition: raycaster.hpp:38
void setDirection(const simulation_api_schema::LidarConfiguration &configuration, double horizontal_angle_start=0, double horizontal_angle_end=2 *M_PI)
Definition: raycaster.cpp:48
Definition: constants.hpp:19
Definition: lanelet_wrapper.hpp:40