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
24
25#ifndef EASYNAV_SENSORS__TYPES__PERCEPTIONS_HPP_
26#define EASYNAV_SENSORS__TYPES__PERCEPTIONS_HPP_
27
28#include <string>
29#include <memory>
30#include <utility>
31#include <vector>
32
33#include "rclcpp/time.hpp"
34#include "rclcpp_lifecycle/lifecycle_node.hpp"
36
37namespace easynav
38{
39
40// Forward declaration needed by PerceptionPtr.
42
48{
49public:
50 virtual ~PerceptionBase() = default;
51
53 rclcpp::Time stamp;
54
56 std::string frame_id;
57
60 bool valid = false;
61
63 bool new_data = false;
64};
65
68using PerceptionBasePtr = std::shared_ptr<PerceptionBase>;
69
70
84template<typename T = PerceptionBase>
85inline std::vector<std::shared_ptr<T>>
86get_perceptions(const std::vector<PerceptionBasePtr> & src)
87{
88 static_assert(
89 std::is_base_of_v<PerceptionBase, T>,
90 "T must inherit from PerceptionBase");
91
92 std::vector<std::shared_ptr<T>> out;
93 out.reserve(src.size());
94
95 for (const auto & perception : src) {
96 if (!perception) {continue;}
97 // Homogeneous by derived type: include only successful casts
98 if (auto p = std::dynamic_pointer_cast<T>(perception)) {
99 out.push_back(std::move(p));
100 }
101 }
102 return out;
103}
104
105
117{
118public:
119 virtual ~PerceptionHandler() = default;
120
129 const std::shared_ptr<rclcpp_lifecycle::LifecycleNode> parent_node,
130 const rclcpp::CallbackGroup::SharedPtr realtime_cbg,
131 const std::string & sensor_name)
132 {
133 parent_node_ = parent_node;
134 realtime_cbg_ = realtime_cbg;
135 sensor_name_ = sensor_name;
137 }
138
142 virtual void on_initialize() {}
143
151 virtual bool cycle_rt([[maybe_unused]] std::shared_ptr<NavState> nav_state) {return false;}
152
153
155 const std::string & get_sensor_name() const {return sensor_name_;}
156
159 virtual std::shared_ptr<PerceptionBase> get_perception() const {return nullptr;}
160
161protected:
163 std::shared_ptr<rclcpp_lifecycle::LifecycleNode> get_node() const {return parent_node_.lock();}
164
166 rclcpp::CallbackGroup::SharedPtr get_realtime_cbg() const {return realtime_cbg_;}
167
169 std::weak_ptr<rclcpp_lifecycle::LifecycleNode> parent_node_;
170
172 rclcpp::CallbackGroup::SharedPtr realtime_cbg_;
173
175 std::string sensor_name_;
176};
177
178} // namespace easynav
179
180#endif // EASYNAV_SENSORS__TYPES__PERCEPTIONS_HPP_
A blackboard-like structure to hold the current state of the navigation system.
Abstract base class for representing a single sensor perception.
Definition Perceptions.hpp:48
virtual ~PerceptionBase()=default
bool valid
Whether the perception holds usable data: set with new data, cleared by SensorsNode once it is older ...
Definition Perceptions.hpp:60
rclcpp::Time stamp
Timestamp of the perception (ROS time).
Definition Perceptions.hpp:53
bool new_data
Whether the data has changed since the last observation.
Definition Perceptions.hpp:63
std::string frame_id
Coordinate frame associated with the perception.
Definition Perceptions.hpp:56
Abstract base class for pluginlib-based sensor perception handlers.
Definition Perceptions.hpp:117
void initialize(const std::shared_ptr< rclcpp_lifecycle::LifecycleNode > parent_node, const rclcpp::CallbackGroup::SharedPtr realtime_cbg, const std::string &sensor_name)
Initializes the handler with the parent node and sensor name.
Definition Perceptions.hpp:128
virtual bool cycle_rt(std::shared_ptr< NavState > nav_state)
Run one real-time sensor processing cycle.
Definition Perceptions.hpp:151
std::string sensor_name_
Name of the sensor (used as YAML parameter namespace prefix).
Definition Perceptions.hpp:175
std::weak_ptr< rclcpp_lifecycle::LifecycleNode > parent_node_
Shared pointer to the parent lifecycle node.
Definition Perceptions.hpp:169
rclcpp::CallbackGroup::SharedPtr realtime_cbg_
Callback group for real-time operations.
Definition Perceptions.hpp:172
const std::string & get_sensor_name() const
Returns the sensor name provided during initialize.
Definition Perceptions.hpp:155
virtual ~PerceptionHandler()=default
virtual void on_initialize()
Optional post-initialization hook for subclasses.
Definition Perceptions.hpp:142
std::shared_ptr< rclcpp_lifecycle::LifecycleNode > get_node() const
Returns the parent lifecycle node.
Definition Perceptions.hpp:163
rclcpp::CallbackGroup::SharedPtr get_realtime_cbg() const
Returns the parent lifecycle node.
Definition Perceptions.hpp:166
virtual std::shared_ptr< PerceptionBase > get_perception() const
The perception this handler keeps up to date, or nullptr.
Definition Perceptions.hpp:159
Definition CircularBuffer.hpp:24
std::shared_ptr< PerceptionBase > PerceptionBasePtr
Shared pointer alias to PerceptionBase.
Definition Perceptions.hpp:68
std::vector< std::shared_ptr< T > > get_perceptions(const std::vector< PerceptionBasePtr > &src)
Extracts a homogeneous collection of perceptions of type T from a heterogeneous vector.
Definition Perceptions.hpp:86