scenario_simulator_v2 C++ API
occupancy_grid_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__OCCUPANCY_GRID__OCCUPANCY_GRID_SENSOR_HPP_
16 #define SIMPLE_SENSOR_SIMULATOR__SENSOR_SIMULATION__OCCUPANCY_GRID__OCCUPANCY_GRID_SENSOR_HPP_
17 
18 #include <simulation_api_schema.pb.h>
19 
20 #include <memory>
21 #include <nav_msgs/msg/occupancy_grid.hpp>
22 #include <rclcpp/rclcpp.hpp>
24 #include <string>
25 #include <vector>
26 
28 {
33 {
34 protected:
36 
37  simulation_api_schema::OccupancyGridSensorConfiguration configuration_;
38 
39  std::vector<std::string> detected_objects_;
40 
42  const double current_simulation_time,
43  const simulation_api_schema::OccupancyGridSensorConfiguration & configuration)
44  : previous_simulation_time_(current_simulation_time), configuration_(configuration)
45  {
46  }
47 
48 public:
49  virtual ~OccupancyGridSensorBase() = default;
50 
54  virtual void update(
55  const double current_simulation_time, const std::vector<traffic_simulator_msgs::EntityStatus> &,
56  const rclcpp::Time & current_ros_time,
57  const std::vector<std::string> & lidar_detected_entities) = 0;
58 
64  const std::vector<std::string> getDetectedObjects(
65  const std::vector<traffic_simulator_msgs::EntityStatus> & status,
66  const std::vector<std::string> & lidar_detected_entities) const;
67 
74  geometry_msgs::Pose getSensorPose(
75  const std::vector<traffic_simulator_msgs::EntityStatus> &) const;
76 };
77 
81 template <typename T>
83 {
84  const typename rclcpp::Publisher<T>::SharedPtr publisher_ptr_;
85 
90  auto getOccupancyGrid(
91  const std::vector<traffic_simulator_msgs::EntityStatus> &, const rclcpp::Time &,
92  const std::vector<std::string> &) -> T;
93 
94 public:
96  const double current_simulation_time,
97  const simulation_api_schema::OccupancyGridSensorConfiguration & configuration,
98  const typename rclcpp::Publisher<T>::SharedPtr & publisher_ptr)
99  : OccupancyGridSensorBase(current_simulation_time, configuration),
100  publisher_ptr_(publisher_ptr),
101  builder_(configuration.resolution(), configuration.height(), configuration.width())
102  {
103  }
104 
105  auto update(
106  const double current_simulation_time,
107  const std::vector<traffic_simulator_msgs::EntityStatus> & entities,
108  const rclcpp::Time & current_ros_time, const std::vector<std::string> & lidar_detected_entities)
109  -> void override
110  {
111  if (
112  current_simulation_time - previous_simulation_time_ - configuration_.update_duration() >=
113  -0.002) {
114  previous_simulation_time_ = current_simulation_time;
115  publisher_ptr_->publish(
116  getOccupancyGrid(entities, current_ros_time, lidar_detected_entities));
117  } else {
118  detected_objects_ = {};
119  }
120  }
121 
122 private:
123  mutable OccupancyGridBuilder builder_;
124 };
125 
126 template <>
127 auto OccupancyGridSensor<nav_msgs::msg::OccupancyGrid>::getOccupancyGrid(
128  const std::vector<traffic_simulator_msgs::EntityStatus> & status, const rclcpp::Time & stamp,
129  const std::vector<std::string> & lidar_detected_entities) -> nav_msgs::msg::OccupancyGrid;
130 } // namespace simple_sensor_simulator
131 
132 #endif // SIMPLE_SENSOR_SIMULATOR__SENSOR_SIMULATION__OCCUPANCY_GRID__OCCUPANCY_GRID_SENSOR_HPP_
grid
Definition: occupancy_grid_builder.hpp:30
Base class of occupancy grid sensor simulator.
Definition: occupancy_grid_sensor.hpp:33
geometry_msgs::Pose getSensorPose(const std::vector< traffic_simulator_msgs::EntityStatus > &) const
Extract sensor pose from entity statuses.
Definition: occupancy_grid_sensor.cpp:28
std::vector< std::string > detected_objects_
Definition: occupancy_grid_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
Update sensor status.
OccupancyGridSensorBase(const double current_simulation_time, const simulation_api_schema::OccupancyGridSensorConfiguration &configuration)
Definition: occupancy_grid_sensor.hpp:41
simulation_api_schema::OccupancyGridSensorConfiguration configuration_
Definition: occupancy_grid_sensor.hpp:37
double previous_simulation_time_
Definition: occupancy_grid_sensor.hpp:35
const std::vector< std::string > getDetectedObjects(const std::vector< traffic_simulator_msgs::EntityStatus > &status, const std::vector< std::string > &lidar_detected_entities) const
List all objects in range of sensor sight.
Definition: occupancy_grid_sensor.cpp:41
occupancy grid sensor implementation
Definition: occupancy_grid_sensor.hpp:83
auto update(const double current_simulation_time, const std::vector< traffic_simulator_msgs::EntityStatus > &entities, const rclcpp::Time &current_ros_time, const std::vector< std::string > &lidar_detected_entities) -> void override
Update sensor status.
Definition: occupancy_grid_sensor.hpp:105
OccupancyGridSensor(const double current_simulation_time, const simulation_api_schema::OccupancyGridSensorConfiguration &configuration, const typename rclcpp::Publisher< T >::SharedPtr &publisher_ptr)
Definition: occupancy_grid_sensor.hpp:95
Definition: constants.hpp:19