scenario_simulator_v2 C++ API
imu_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__IMU_SENSOR_HPP_
16 #define SIMPLE_SENSOR_SIMULATOR__SENSOR_SIMULATION__IMU_SENSOR_HPP_
17 
18 #include <simulation_api_schema.pb.h>
19 
20 #include <array>
21 #include <geometry_msgs/msg/accel.hpp>
22 #include <geometry_msgs/msg/twist.hpp>
23 #include <random>
24 #include <rclcpp/rclcpp.hpp>
25 #include <sensor_msgs/msg/imu.hpp>
27 #include <traffic_simulator_msgs/msg/entity_status.hpp>
28 
30 {
32 {
33 public:
34  explicit ImuSensorBase(const simulation_api_schema::ImuSensorConfiguration & configuration)
35  : add_gravity_(configuration.add_gravity()),
36  noise_standard_deviation_orientation_(configuration.noise_standard_deviation_orientation()),
37  noise_standard_deviation_twist_(configuration.noise_standard_deviation_twist()),
38  noise_standard_deviation_acceleration_(configuration.noise_standard_deviation_acceleration()),
39  random_generator_(configuration.use_seed() ? configuration.seed() : std::random_device{}()),
46  {
47  }
48 
49  virtual ~ImuSensorBase() = default;
50 
51  virtual auto update(
52  const rclcpp::Time & current_ros_time,
53  const std::vector<traffic_simulator_msgs::EntityStatus> & statuses) const -> bool = 0;
54 
55 protected:
56  const bool add_gravity_;
60  mutable std::mt19937 random_generator_;
61  mutable std::normal_distribution<> noise_distribution_orientation_;
62  mutable std::normal_distribution<> noise_distribution_twist_;
63  mutable std::normal_distribution<> noise_distribution_acceleration_;
64  const std::array<double, 9> orientation_covariance_;
65  const std::array<double, 9> angular_velocity_covariance_;
66  const std::array<double, 9> linear_acceleration_covariance_;
67 
68  auto calculateCovariance(const double stddev) const -> std::array<double, 9>
69  {
70  return {std::pow(stddev, 2), 0, 0, 0, std::pow(stddev, 2), 0, 0, 0, std::pow(stddev, 2)};
71  };
72 };
73 
74 template <typename MessageType>
75 class ImuSensor : public ImuSensorBase
76 {
77 public:
78  explicit ImuSensor(
79  const simulation_api_schema::ImuSensorConfiguration & configuration,
80  const typename rclcpp::Publisher<MessageType>::SharedPtr & publisher)
81  : ImuSensorBase(configuration),
82  entity_name_(configuration.entity()),
83  frame_id_(configuration.frame_id()),
84  publisher_(publisher)
85  {
86  }
87 
88  auto update(
89  const rclcpp::Time & current_ros_time,
90  const std::vector<traffic_simulator_msgs::EntityStatus> & statuses) const -> bool override
91  {
92  for (const auto & status : statuses) {
93  if (status.name() == entity_name_) {
95  simulation_interface::toMsg(status, status_msg);
96  publisher_->publish(generateMessage(current_ros_time, status_msg));
97  return true;
98  }
99  }
100  return false;
101  }
102 
103 private:
104  auto generateMessage(
105  const rclcpp::Time & current_ros_time,
106  const traffic_simulator_msgs::msg::EntityStatus & status) const -> const MessageType;
107 
108  const std::string entity_name_;
109  const std::string frame_id_;
110  const typename rclcpp::Publisher<MessageType>::SharedPtr publisher_;
111 };
112 } // namespace simple_sensor_simulator
113 #endif // SIMPLE_SENSOR_SIMULATOR__SENSOR_SIMULATION__IMU_SENSOR_HPP_
Definition: imu_sensor.hpp:32
std::mt19937 random_generator_
Definition: imu_sensor.hpp:60
const double noise_standard_deviation_orientation_
Definition: imu_sensor.hpp:57
std::normal_distribution noise_distribution_orientation_
Definition: imu_sensor.hpp:61
const std::array< double, 9 > linear_acceleration_covariance_
Definition: imu_sensor.hpp:66
std::normal_distribution noise_distribution_twist_
Definition: imu_sensor.hpp:62
const bool add_gravity_
Definition: imu_sensor.hpp:56
const std::array< double, 9 > angular_velocity_covariance_
Definition: imu_sensor.hpp:65
auto calculateCovariance(const double stddev) const -> std::array< double, 9 >
Definition: imu_sensor.hpp:68
virtual auto update(const rclcpp::Time &current_ros_time, const std::vector< traffic_simulator_msgs::EntityStatus > &statuses) const -> bool=0
ImuSensorBase(const simulation_api_schema::ImuSensorConfiguration &configuration)
Definition: imu_sensor.hpp:34
const double noise_standard_deviation_acceleration_
Definition: imu_sensor.hpp:59
const std::array< double, 9 > orientation_covariance_
Definition: imu_sensor.hpp:64
std::normal_distribution noise_distribution_acceleration_
Definition: imu_sensor.hpp:63
const double noise_standard_deviation_twist_
Definition: imu_sensor.hpp:58
Definition: imu_sensor.hpp:76
ImuSensor(const simulation_api_schema::ImuSensorConfiguration &configuration, const typename rclcpp::Publisher< MessageType >::SharedPtr &publisher)
Definition: imu_sensor.hpp:78
auto update(const rclcpp::Time &current_ros_time, const std::vector< traffic_simulator_msgs::EntityStatus > &statuses) const -> bool override
Definition: imu_sensor.hpp:88
Definition: constants.hpp:19
void toMsg(const geometry_msgs::Point &proto, geometry_msgs::msg::Point &p)
Definition: conversions.cpp:29
Definition: cache.hpp:27
std::string string
Definition: junit5.hpp:26
traffic_simulator_msgs::EntityStatus EntityStatus
Definition: helper_functions.hpp:32