scenario_simulator_v2 C++ API
publisher.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 CONCEALER__PUBLISHER_HPP_
16 #define CONCEALER__PUBLISHER_HPP_
17 
19 #include <nav_msgs/msg/odometry.hpp>
20 #include <random>
21 #include <rclcpp/rclcpp.hpp>
22 
23 namespace concealer
24 {
25 template <typename>
26 struct Identity
27 {
28  explicit constexpr Identity(
29  const rclcpp::node_interfaces::NodeParametersInterface::SharedPtr &, const std::string &)
30  {
31  }
32 
33  template <typename T>
34  constexpr auto operator()(T && x) const -> decltype(auto)
35  {
36  return std::forward<decltype(x)>(x);
37  }
38 };
39 
40 template <typename>
42 
43 template <>
44 struct NormalDistribution<nav_msgs::msg::Odometry>
45 {
46  std::random_device::result_type seed;
47 
48  std::random_device device;
49 
50  std::mt19937_64 engine;
51 
52  struct Error
53  {
54  std::normal_distribution<double> additive, multiplicative;
55 
56  explicit Error(
57  const rclcpp::node_interfaces::NodeParametersInterface::SharedPtr & node,
58  const std::string & prefix)
59  : additive(
60  getParameter<double>(node, prefix + ".additive.mean"),
61  getParameter<double>(node, prefix + ".additive.standard_deviation")),
62  multiplicative(
63  getParameter<double>(node, prefix + ".multiplicative.mean"),
64  getParameter<double>(node, prefix + ".multiplicative.standard_deviation"))
65  {
66  }
67 
68  auto apply(std::mt19937_64 & engine, double value) -> decltype(auto)
69  {
70  return value * (multiplicative(engine) + 1.0) + additive(engine);
71  }
72  };
73 
74  // clang-format off
87  // clang-format on
88 
89  explicit NormalDistribution(
90  const rclcpp::node_interfaces::NodeParametersInterface::SharedPtr &, const std::string &);
91 
92  auto operator()(nav_msgs::msg::Odometry odometry) -> nav_msgs::msg::Odometry;
93 };
94 
95 template <typename Message, template <typename> typename Randomizer = Identity>
96 class Publisher
97 {
98  typename rclcpp::Publisher<Message>::SharedPtr publisher;
99 
100  Randomizer<Message> randomize;
101 
102 public:
103  template <typename Node>
104  explicit Publisher(const std::string & topic, Node & node)
105  : publisher(node.template create_publisher<Message>(topic, rclcpp::QoS(1).reliable())),
106  randomize(node.get_node_parameters_interface(), topic)
107  {
108  }
109 
110  template <typename... Ts>
111  auto operator()(Ts &&... xs) -> decltype(auto)
112  {
113  return publisher->publish(randomize(std::forward<decltype(xs)>(xs)...));
114  }
115 };
116 } // namespace concealer
117 
118 #endif // CONCEALER__PUBLISHER_HPP_
Definition: publisher.hpp:97
auto operator()(Ts &&... xs) -> decltype(auto)
Definition: publisher.hpp:111
Publisher(const std::string &topic, Node &node)
Definition: publisher.hpp:104
Definition: autoware_universe.hpp:40
auto getParameter(const rclcpp::node_interfaces::NodeParametersInterface::SharedPtr &node, const std::string &name, T value={})
Definition: get_parameter.hpp:25
Definition: junit5.hpp:25
std::string string
Definition: junit5.hpp:26
Definition: publisher.hpp:27
constexpr Identity(const rclcpp::node_interfaces::NodeParametersInterface::SharedPtr &, const std::string &)
Definition: publisher.hpp:28
constexpr auto operator()(T &&x) const -> decltype(auto)
Definition: publisher.hpp:34
std::normal_distribution< double > additive
Definition: publisher.hpp:54
Error(const rclcpp::node_interfaces::NodeParametersInterface::SharedPtr &node, const std::string &prefix)
Definition: publisher.hpp:56
auto apply(std::mt19937_64 &engine, double value) -> decltype(auto)
Definition: publisher.hpp:68
std::mt19937_64 engine
Definition: publisher.hpp:50
Error linear_z_error
Definition: publisher.hpp:83
Error angular_x_error
Definition: publisher.hpp:84
std::random_device device
Definition: publisher.hpp:48
Error orientation_r_error
Definition: publisher.hpp:78
Error angular_z_error
Definition: publisher.hpp:86
Error linear_y_error
Definition: publisher.hpp:82
Error position_local_x_error
Definition: publisher.hpp:75
std::random_device::result_type seed
Definition: publisher.hpp:46
Error position_local_z_error
Definition: publisher.hpp:77
Error angular_y_error
Definition: publisher.hpp:85
Error orientation_y_error
Definition: publisher.hpp:80
Error orientation_p_error
Definition: publisher.hpp:79
Error linear_x_error
Definition: publisher.hpp:81
Error position_local_y_error
Definition: publisher.hpp:76
Definition: publisher.hpp:41