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