Nav2 Navigation Stack - kilted  kilted
ROS 2 Navigation Stack
pose_filter.cpp
1 // Copyright (c) 2024 Open Navigation LLC
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 #include "opennav_docking/pose_filter.hpp"
16 #include "rclcpp/rclcpp.hpp"
17 #include "tf2_geometry_msgs/tf2_geometry_msgs.hpp"
18 
19 namespace opennav_docking
20 {
21 
22 PoseFilter::PoseFilter(double coef, double timeout)
23 {
24  coef_ = coef;
25  timeout_ = timeout;
26  pose_.header.stamp = rclcpp::Time(0);
27 }
28 
29 geometry_msgs::msg::PoseStamped
30 PoseFilter::update(const geometry_msgs::msg::PoseStamped & measurement)
31 {
32  if (coef_ <= 0.0) {
33  // No filtering
34  return measurement;
35  }
36 
37  if ((rclcpp::Time(measurement.header.stamp) - pose_.header.stamp).seconds() > timeout_) {
38  pose_ = measurement;
39  } else if (pose_.header.frame_id != measurement.header.frame_id) {
40  pose_ = measurement;
41  } else {
42  // Copy header
43  pose_.header = measurement.header;
44 
45  // Filter position
46  filter(pose_.pose.position.x, measurement.pose.position.x);
47  filter(pose_.pose.position.y, measurement.pose.position.y);
48  filter(pose_.pose.position.z, measurement.pose.position.z);
49 
50  // Filter orientation
51  tf2::Quaternion f_quat, m_quat;
52  tf2::fromMsg(measurement.pose.orientation, m_quat);
53  tf2::fromMsg(pose_.pose.orientation, f_quat);
54  f_quat = f_quat.slerp(m_quat, coef_);
55  pose_.pose.orientation = tf2::toMsg(f_quat);
56  }
57 
58  return pose_;
59 }
60 
61 void PoseFilter::filter(double & filt, double meas)
62 {
63  filt = (1 - coef_) * filt + coef_ * meas;
64 }
65 
66 } // namespace opennav_docking
PoseFilter(double coef, double timeout)
Create a pose filter instance.
Definition: pose_filter.cpp:22
geometry_msgs::msg::PoseStamped update(const geometry_msgs::msg::PoseStamped &measurement)
Update the filter.
Definition: pose_filter.cpp:30