on_motion.hpp
Go to the documentation of this file.
1 // Copyright 2024 Ekumen, 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 
15 #ifndef BELUGA_POLICIES_ON_MOTION_HPP
16 #define BELUGA_POLICIES_ON_MOTION_HPP
17 
19 #include <sophus/se2.hpp>
20 
26 namespace beluga::policies {
27 
28 namespace detail {
29 
31 
34 template <class Pose>
36 
38 
41 template <class Scalar>
42 struct on_motion_policy_base<Sophus::SE2<Scalar>> {
43  public:
45 
49  constexpr on_motion_policy_base(Scalar min_distance, Scalar min_angle)
50  : min_distance_(min_distance), min_angle_(min_angle) {}
51 
53 
61  constexpr bool operator()(const Sophus::SE2<Scalar>& prev, const Sophus::SE2<Scalar>& current) {
62  const auto delta = prev.inverse() * current;
63  return std::abs(delta.translation().x()) > min_distance_ || //
64  std::abs(delta.translation().y()) > min_distance_ || //
65  std::abs(delta.so2().log()) > min_angle_;
66  }
67 
68  private:
69  Scalar min_distance_{0.0};
70  Scalar min_angle_{0.0};
71 };
72 
74 
77 template <class Pose>
78 struct on_motion_policy : public on_motion_policy_base<Pose> {
79  public:
82 
84  constexpr bool operator()(const Pose& pose) {
85  if (!latest_pose_) {
86  latest_pose_ = pose;
87  return true;
88  }
89 
90  const bool moved = (*this)(*latest_pose_, pose);
91  if (moved) {
92  latest_pose_ = pose;
93  }
94 
95  return moved;
96  }
97 
98  private:
99  std::optional<Pose> latest_pose_;
100 };
101 
103 struct on_motion_fn {
105 
108  template <class Scalar>
109  constexpr auto operator()(Scalar min_distance, Scalar min_angle) const {
110  return beluga::make_policy(on_motion_policy<Sophus::SE2<Scalar>>{min_distance, min_angle});
111  }
112 };
113 
114 } // namespace detail
115 
117 
122 
123 } // namespace beluga::policies
124 
125 #endif
beluga::policies
Definition: every_n.hpp:25
beluga::make_policy
constexpr detail::make_policy_fn make_policy
Make policy function objects.
Definition: policy.hpp:42
beluga::policies::detail::on_motion_policy::operator()
constexpr bool operator()(const Pose &pose)
Return true if motion has been detected.
Definition: on_motion.hpp:84
beluga::policies::detail::on_motion_policy_base< Sophus::SE2< Scalar > >::on_motion_policy_base
constexpr on_motion_policy_base(Scalar min_distance, Scalar min_angle)
Constructor.
Definition: on_motion.hpp:49
se2.hpp
Sophus
beluga::policies::detail::on_motion_fn::operator()
constexpr auto operator()(Scalar min_distance, Scalar min_angle) const
Overload that creates the policy closure in SE2 space.
Definition: on_motion.hpp:109
beluga::policies::detail::on_motion_policy_base
Primary template for the on_motion_policy_base class.
Definition: on_motion.hpp:35
policy.hpp
beluga::policies::detail::on_motion_policy_base< Sophus::SE2< Scalar > >::operator()
constexpr bool operator()(const Sophus::SE2< Scalar > &prev, const Sophus::SE2< Scalar > &current)
Return true if motion has been detected.
Definition: on_motion.hpp:61
Sophus::SE2
beluga::policies::detail::on_motion_policy::latest_pose_
std::optional< Pose > latest_pose_
The latest pose for motion comparison.
Definition: on_motion.hpp:99
beluga::policies::detail::on_motion_policy
Base implementation for the on_motion_policy algorithm.
Definition: on_motion.hpp:78
beluga::policies::detail::on_motion_fn
Implementation detail for the on_motion_fn object.
Definition: on_motion.hpp:103
beluga::policies::on_motion
constexpr detail::on_motion_fn on_motion
Policy that triggers an action based on motion.
Definition: on_motion.hpp:121


beluga
Author(s):
autogenerated on Tue Jul 16 2024 02:59:53