Easy Navigation
Loading...
Searching...
No Matches
Perceptions.hpp
Go to the documentation of this file.
1// Copyright 2025 Intelligent Robotics Lab
2//
3// This file is part of the project Easy Navigation (EasyNav in short)
4// licensed under the GNU General Public License v3.0.
5// See <http://www.gnu.org/licenses/> for details.
6//
7// Easy Navigation program is free software: you can redistribute it and/or modify
8// it under the terms of the GNU General Public License as published by
9// the Free Software Foundation, either version 3 of the License, or
10// (at your option) any later version.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with this program. If not, see <http://www.gnu.org/licenses/>.
19
28
29#ifndef EASYNAV_COMMON_TYPES__PERCEPTIONS_HPP_
30#define EASYNAV_COMMON_TYPES__PERCEPTIONS_HPP_
31
32#include <string>
33
34#include "rclcpp/time.hpp"
35#include "rclcpp_lifecycle/lifecycle_node.hpp"
36
37namespace easynav
38{
39
45{
46public:
47 virtual ~PerceptionBase() = default;
48
50 rclcpp::Time stamp;
51
53 std::string frame_id;
54
56 bool valid = false;
57
59 bool new_data = false;
60};
61
64using PerceptionBasePtr = std::shared_ptr<PerceptionBase>;
65
72{
75
77 rclcpp::SubscriptionBase::SharedPtr subscription;
78};
79
89template<typename T = PerceptionBase>
90inline std::vector<std::shared_ptr<T>>
91get_perceptions(const std::vector<PerceptionPtr> & src)
92{
93 static_assert(std::is_base_of_v<PerceptionBase, T>,
94 "T must inherit from PerceptionBase");
95
96 std::vector<std::shared_ptr<T>> out;
97 out.reserve(src.size());
98
99 for (const auto & h : src) {
100 if (!h.perception) {continue;}
101
102 if constexpr (std::is_same_v<T, PerceptionBase>) {
103 // Heterogeneous: no cast needed
104 out.push_back(h.perception);
105 } else {
106 // Homogeneous by derived type: include only successful casts
107 if (auto p = std::dynamic_pointer_cast<T>(h.perception)) {
108 out.push_back(std::move(p));
109 }
110 }
111 }
112 return out;
113}
114
121{
122public:
123 virtual ~PerceptionHandler() = default;
124
128 virtual std::shared_ptr<PerceptionBase> create(const std::string & sensor_id) = 0;
129
141 virtual rclcpp::SubscriptionBase::SharedPtr create_subscription(
142 rclcpp_lifecycle::LifecycleNode & node,
143 const std::string & topic,
144 const std::string & type,
145 std::shared_ptr<PerceptionBase> target,
146 rclcpp::CallbackGroup::SharedPtr cb_group) = 0;
147
154 virtual std::string group() const = 0;
155};
156
157} // namespace easynav
158
159#endif // EASYNAV_COMMON_TYPES__PERCEPTIONS_HPP_
Abstract base class for representing a single sensor perception.
Definition Perceptions.hpp:45
virtual ~PerceptionBase()=default
bool valid
Whether the perception contains valid data.
Definition Perceptions.hpp:56
rclcpp::Time stamp
Timestamp of the perception (ROS time).
Definition Perceptions.hpp:50
bool new_data
Whether the data has changed since the last observation.
Definition Perceptions.hpp:59
std::string frame_id
Coordinate frame associated with the perception.
Definition Perceptions.hpp:53
Abstract base interface for group-specific perception handlers.
Definition Perceptions.hpp:121
virtual std::shared_ptr< PerceptionBase > create(const std::string &sensor_id)=0
Creates a new instance of a perception object managed by this handler.
virtual ~PerceptionHandler()=default
virtual rclcpp::SubscriptionBase::SharedPtr create_subscription(rclcpp_lifecycle::LifecycleNode &node, const std::string &topic, const std::string &type, std::shared_ptr< PerceptionBase > target, rclcpp::CallbackGroup::SharedPtr cb_group)=0
Creates a subscription that processes messages into PerceptionBase instances.
virtual std::string group() const =0
Returns the group identifier associated with this handler.
Definition CircularBuffer.hpp:28
std::shared_ptr< PerceptionBase > PerceptionBasePtr
Shared pointer alias to PerceptionBase.
Definition Perceptions.hpp:64
std::vector< std::shared_ptr< T > > get_perceptions(const std::vector< PerceptionPtr > &src)
Extracts a homogeneous collection of perceptions of type T from a heterogeneous vector.
Definition Perceptions.hpp:91
Represents a perception entry with its state and ROS subscription.
Definition Perceptions.hpp:72
rclcpp::SubscriptionBase::SharedPtr subscription
ROS 2 subscription to the sensor topic that provides data.
Definition Perceptions.hpp:77
PerceptionBasePtr perception
Shared pointer to the current perception object.
Definition Perceptions.hpp:74