Autoware.Auto
state_estimation_node.hpp
Go to the documentation of this file.
1 // Copyright 2021 Apex.AI, Inc.
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 
17 
18 
19 #ifndef STATE_ESTIMATION_NODES__STATE_ESTIMATION_NODE_HPP_
20 #define STATE_ESTIMATION_NODES__STATE_ESTIMATION_NODE_HPP_
21 
22 
23 #include <common/types.hpp>
24 #include <geometry_msgs/msg/pose_with_covariance_stamped.hpp>
25 #include <geometry_msgs/msg/quaternion_stamped.hpp>
26 #include <geometry_msgs/msg/twist_with_covariance_stamped.hpp>
27 #include <kalman_filter/esrcf.hpp>
29 #include <nav_msgs/msg/odometry.hpp>
30 #include <rclcpp/publisher.hpp>
31 #include <rclcpp/rclcpp.hpp>
32 #include <rclcpp/subscription.hpp>
37 
38 #include <tf2/buffer_core.h>
39 #include <tf2_ros/transform_broadcaster.h>
40 #include <tf2_ros/transform_listener.h>
41 
42 #include <Eigen/Geometry>
43 
44 #include <chrono>
45 #include <memory>
46 #include <string>
47 #include <vector>
48 
49 namespace autoware
50 {
51 namespace prediction
52 {
53 namespace state_estimation_nodes
54 {
55 
59 class STATE_ESTIMATION_NODES_PUBLIC StateEstimationNode : public rclcpp::Node
60 {
61 public:
67  explicit StateEstimationNode(
68  const rclcpp::NodeOptions & node_options);
69 
70 private:
71  using OdomMsgT = nav_msgs::msg::Odometry;
72  using PoseMsgT = geometry_msgs::msg::PoseWithCovarianceStamped;
73  using TwistMsgT = geometry_msgs::msg::TwistWithCovarianceStamped;
74  using TfMsgT = tf2_msgs::msg::TFMessage;
75 
76  template<std::int32_t kDim>
77  using VectorT = Eigen::Matrix<float32_t, kDim, 1>;
78 
83  void STATE_ESTIMATION_NODES_LOCAL odom_callback(const OdomMsgT::SharedPtr msg);
84 
89  void STATE_ESTIMATION_NODES_LOCAL pose_callback(const PoseMsgT::SharedPtr msg);
90 
95  void STATE_ESTIMATION_NODES_LOCAL twist_callback(const TwistMsgT::SharedPtr msg);
96 
98  void STATE_ESTIMATION_NODES_LOCAL predict_and_publish_current_state();
99 
101  void STATE_ESTIMATION_NODES_LOCAL publish_current_state();
102 
104  void STATE_ESTIMATION_NODES_LOCAL update_latest_orientation_if_needed(
105  const geometry_msgs::msg::QuaternionStamped & rotation);
106 
108  geometry_msgs::msg::TransformStamped STATE_ESTIMATION_NODES_LOCAL get_transform(
109  const std_msgs::msg::Header & header);
110 
111  template<typename MessageT>
112  using CallbackFnT = void (StateEstimationNode::*)(const typename MessageT::SharedPtr);
113 
114  template<typename MessageT>
115  void create_subscriptions(
116  const std::vector<std::string> & input_topics,
117  std::vector<typename rclcpp::Subscription<MessageT>::SharedPtr> * subscribers,
118  CallbackFnT<MessageT> callback);
119 
120  template<std::int32_t kNumOfStates, std::int32_t kProcessNoiseDim>
121  static Eigen::Matrix<float32_t, kNumOfStates, kProcessNoiseDim> create_GQ_factor(
122  const std::chrono::nanoseconds & expected_delta_t,
123  const VectorT<kProcessNoiseDim> & process_noise_variances);
124 
125  std::vector<rclcpp::Subscription<OdomMsgT>::SharedPtr> m_odom_subscribers;
126  std::vector<rclcpp::Subscription<PoseMsgT>::SharedPtr> m_pose_subscribers;
127  std::vector<rclcpp::Subscription<TwistMsgT>::SharedPtr> m_twist_subscribers;
128 
129  std::shared_ptr<rclcpp::Publisher<OdomMsgT>> m_publisher{};
130  std::shared_ptr<rclcpp::Publisher<TfMsgT>> m_tf_publisher{};
131 
132  std::chrono::system_clock::duration m_init_timeout{};
133  std::chrono::system_clock::duration m_expected_time_between_publish_requests{};
134  std::chrono::system_clock::duration m_history_length{};
135  std::chrono::system_clock::time_point m_time_of_last_publish{};
136 
137  rclcpp::TimerBase::SharedPtr m_wall_timer{};
138 
139  common::types::bool8_t m_filter_initialized{};
140  common::types::bool8_t m_publish_data_driven{};
141  common::types::float64_t m_publish_frequency{};
142 
143  std::string m_frame_id{};
144  std::string m_child_frame_id{};
145 
146  // TODO(igor): we can replace the unique_ptr here with std::variant or alike at a later time to
147  // allow configuring which filter to use at runtime.
148  std::unique_ptr<ConstantAccelerationFilter> m_ekf{};
149 
150  tf2::BufferCore m_tf_buffer;
151  tf2_ros::TransformListener m_tf_listener;
152 
153  geometry_msgs::msg::QuaternionStamped m_latest_orientation{};
154  common::types::float64_t m_min_speed_to_use_speed_orientation{};
155 };
156 
157 } // namespace state_estimation_nodes
158 } // namespace prediction
159 } // namespace autoware
160 
161 #endif // STATE_ESTIMATION_NODES__STATE_ESTIMATION_NODE_HPP_
double float64_t
Definition: types.hpp:37
geometry_msgs::msg::TransformStamped TransformStamped
Definition: motion_testing_publisher.hpp:37
This file defines a class for extended square root covariance filter.
bool bool8_t
Definition: types.hpp:33
This file includes common type definition.
tf2_msgs::msg::TFMessage TFMessage
Definition: motion_testing_publisher.hpp:34
This file defines the constant velocity motion model.
TransformStamped get_transform(const std::string &input_frame_id, const std::string &output_frame_id, float64_t r_x, float64_t r_y, float64_t r_z, float64_t r_w, float64_t t_x, float64_t t_y, float64_t t_z)
Definition: point_cloud_filter_transform_node.cpp:44
autoware_auto_msgs::msg::VehicleOdometry Odometry
Definition: safety_state_machine.hpp:43
This file defines the lanelet2_map_provider_node class.
Definition: quick_sort.hpp:24