scenario_simulator_v2 C++ API
test_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__TEST__TEST_LIDAR_SENSOR_HPP_
16 #define SIMPLE_SENSOR_SIMULATOR__TEST__TEST_LIDAR_SENSOR_HPP_
17 
18 #include <gtest/gtest.h>
19 
20 #include <agnocast_wrapper/agnocast_wrapper.hpp>
21 #include <rclcpp/rclcpp.hpp>
22 #include <sensor_msgs/msg/point_cloud2.hpp>
23 #include <set>
25 #include <string>
26 #include <vector>
27 
28 #include "../../utils/helper_functions.hpp"
29 
30 using namespace simple_sensor_simulator;
31 
32 class LidarSensorTest : public ::testing::Test
33 {
34 protected:
35  static void SetUpTestSuite() { rclcpp::init(0, nullptr); }
36 
37  static void TearDownTestSuite() { rclcpp::shutdown(); }
38 
40  : config_(utils::constructLidarConfiguration("ego", "awf/universe/20240605", 0.0, 0.5))
41  {
42  // Note: Executor must be created after rclcpp::init. If created before, it causes context is null error.
43  executor_ = std::make_shared<rclcpp::executors::SingleThreadedExecutor>();
44  node_ = std::make_shared<rclcpp::Node>("lidar_sensor_test_node");
45  executor_->add_node(node_);
46  makeRosInterface();
47  initializeEntityStatuses();
48 
49  lidar_ = std::make_unique<LidarSensor<sensor_msgs::msg::PointCloud2>>(0.0, config_, publisher_);
50  }
51 
52  ~LidarSensorTest() = default;
53 
54  rclcpp::Node::SharedPtr node_;
55  rclcpp::executors::SingleThreadedExecutor::SharedPtr executor_;
56  agnocast_wrapper::PublisherPtr<sensor_msgs::msg::PointCloud2> publisher_;
57  agnocast_wrapper::SubscriptionPtr<sensor_msgs::msg::PointCloud2> subscription_;
58 
59  std::vector<EntityStatus> status_;
60 
61  std::unique_ptr<LidarSensorBase> lidar_;
62  simulation_api_schema::LidarConfiguration config_;
63  agnocast_wrapper::MessagePtr<sensor_msgs::msg::PointCloud2> received_msg_;
64 
65  double current_simulation_time_{1.0};
66  rclcpp::Time current_ros_time_{1};
67 
68 private:
69  auto initializeEntityStatuses() -> void
70  {
71  const auto dimensions = utils::makeDimensions(4.5, 2.0, 1.5);
72 
73  const auto ego_status = utils::makeEntity(
74  "ego", EntityType::EGO, utils::makePose(5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0), dimensions);
75  const auto other1_status = utils::makeEntity(
76  "other1", EntityType::VEHICLE, utils::makePose(-3.0, -3.0, 0.0, 0.0, 0.0, 0.0, 1.0),
77  dimensions);
78  const auto other2_status = utils::makeEntity(
79  "other2", EntityType::VEHICLE, utils::makePose(5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0),
80  dimensions);
81 
82  status_ = {ego_status, other1_status, other2_status};
83  }
84 
85  auto makeRosInterface() -> void
86  {
87  publisher_ =
88  agnocast_wrapper::create_publisher<sensor_msgs::msg::PointCloud2>(node_, "lidar_output", 10);
89  subscription_ = agnocast_wrapper::create_subscription<sensor_msgs::msg::PointCloud2>(
90  node_, "lidar_output", 10,
91  [this](const agnocast_wrapper::MessagePtr<sensor_msgs::msg::PointCloud2> msg) {
92  received_msg_ = msg;
93  });
94  }
95 };
96 #endif // SIMPLE_SENSOR_SIMULATOR__TEST__TEST_LIDAR_SENSOR_HPP_
Definition: test_lidar_sensor.hpp:33
agnocast_wrapper::MessagePtr< sensor_msgs::msg::PointCloud2 > received_msg_
Definition: test_lidar_sensor.hpp:63
~LidarSensorTest()=default
agnocast_wrapper::SubscriptionPtr< sensor_msgs::msg::PointCloud2 > subscription_
Definition: test_lidar_sensor.hpp:57
static void TearDownTestSuite()
Definition: test_lidar_sensor.hpp:37
std::unique_ptr< LidarSensorBase > lidar_
Definition: test_lidar_sensor.hpp:61
rclcpp::executors::SingleThreadedExecutor::SharedPtr executor_
Definition: test_lidar_sensor.hpp:55
std::vector< EntityStatus > status_
Definition: test_lidar_sensor.hpp:59
agnocast_wrapper::PublisherPtr< sensor_msgs::msg::PointCloud2 > publisher_
Definition: test_lidar_sensor.hpp:56
rclcpp::Node::SharedPtr node_
Definition: test_lidar_sensor.hpp:54
LidarSensorTest()
Definition: test_lidar_sensor.hpp:39
static void SetUpTestSuite()
Definition: test_lidar_sensor.hpp:35
simulation_api_schema::LidarConfiguration config_
Definition: test_lidar_sensor.hpp:62
Definition: constants.hpp:19
Definition: helper_functions.hpp:35
auto makePose(const double px, const double py, const double pz, const double ox, const double oy, const double oz, const double ow) -> geometry_msgs::msg::Pose
Definition: helper_functions.hpp:45
auto constructLidarConfiguration(const std::string &entity, const std::string &architecture_type, const double lidar_sensor_delay, const double horizontal_resolution) -> const simulation_api_schema::LidarConfiguration
Definition: helper_functions.hpp:60
auto makeEntity(const std::string &name, const EntityType::Enum type, const EntitySubtype::Enum subtype, const geometry_msgs::msg::Pose &pose, const geometry_msgs::msg::Vector3 &dimensions) -> EntityStatus
Definition: helper_functions.hpp:125
auto makeDimensions(const double x, const double y, const double z) -> geometry_msgs::msg::Vector3
Definition: helper_functions.hpp:54