Easy Navigation
Loading...
Searching...
No Matches
Perceptions.hpp
Go to the documentation of this file.
1// Copyright 2025 Intelligent Robotics Lab
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
23
24#ifndef EASYNAV_COMMON_TYPES__PERCEPTIONS_HPP_
25#define EASYNAV_COMMON_TYPES__PERCEPTIONS_HPP_
26
27#include <string>
28
29#include "rclcpp/time.hpp"
30#include "rclcpp_lifecycle/lifecycle_node.hpp"
31
32namespace easynav
33{
34
40{
41public:
42 virtual ~PerceptionBase() = default;
43
45 rclcpp::Time stamp;
46
48 std::string frame_id;
49
51 bool valid = false;
52
54 bool new_data = false;
55};
56
59using PerceptionBasePtr = std::shared_ptr<PerceptionBase>;
60
67{
70
72 rclcpp::SubscriptionBase::SharedPtr subscription;
73};
74
84template<typename T = PerceptionBase>
85inline std::vector<std::shared_ptr<T>>
86get_perceptions(const std::vector<PerceptionPtr> & src)
87{
88 static_assert(std::is_base_of_v<PerceptionBase, T>,
89 "T must inherit from PerceptionBase");
90
91 std::vector<std::shared_ptr<T>> out;
92 out.reserve(src.size());
93
94 for (const auto & h : src) {
95 if (!h.perception) {continue;}
96
97 if constexpr (std::is_same_v<T, PerceptionBase>) {
98 // Heterogeneous: no cast needed
99 out.push_back(h.perception);
100 } else {
101 // Homogeneous by derived type: include only successful casts
102 if (auto p = std::dynamic_pointer_cast<T>(h.perception)) {
103 out.push_back(std::move(p));
104 }
105 }
106 }
107 return out;
108}
109
116{
117public:
118 virtual ~PerceptionHandler() = default;
119
123 virtual std::shared_ptr<PerceptionBase> create(const std::string & sensor_id) = 0;
124
136 virtual rclcpp::SubscriptionBase::SharedPtr create_subscription(
137 rclcpp_lifecycle::LifecycleNode & node,
138 const std::string & topic,
139 const std::string & type,
140 std::shared_ptr<PerceptionBase> target,
141 rclcpp::CallbackGroup::SharedPtr cb_group) = 0;
142
149 virtual std::string group() const = 0;
150};
151
152} // namespace easynav
153
154#endif // EASYNAV_COMMON_TYPES__PERCEPTIONS_HPP_
Abstract base class for representing a single sensor perception.
Definition Perceptions.hpp:40
virtual ~PerceptionBase()=default
bool valid
Whether the perception contains valid data.
Definition Perceptions.hpp:51
rclcpp::Time stamp
Timestamp of the perception (ROS time).
Definition Perceptions.hpp:45
bool new_data
Whether the data has changed since the last observation.
Definition Perceptions.hpp:54
std::string frame_id
Coordinate frame associated with the perception.
Definition Perceptions.hpp:48
Abstract base interface for group-specific perception handlers.
Definition Perceptions.hpp:116
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:23
std::shared_ptr< PerceptionBase > PerceptionBasePtr
Shared pointer alias to PerceptionBase.
Definition Perceptions.hpp:59
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:86
Represents a perception entry with its state and ROS subscription.
Definition Perceptions.hpp:67
rclcpp::SubscriptionBase::SharedPtr subscription
ROS 2 subscription to the sensor topic that provides data.
Definition Perceptions.hpp:72
PerceptionBasePtr perception
Shared pointer to the current perception object.
Definition Perceptions.hpp:69