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_SENSORS_TYPES__PERCEPTIONS_HPP_
25#define EASYNAV_SENSORS_TYPES__PERCEPTIONS_HPP_
26
27#include <string>
28
29#include "rclcpp/time.hpp"
30#include "rclcpp_lifecycle/lifecycle_node.hpp"
32
33namespace easynav
34{
35
36// Forward declaration needed by PerceptionPtr.
38
44{
45public:
46 virtual ~PerceptionBase() = default;
47
49 rclcpp::Time stamp;
50
52 std::string frame_id;
53
55 // TODO: Not in use
56 bool valid = false;
57
59 bool new_data = false;
60};
61
64using PerceptionBasePtr = std::shared_ptr<PerceptionBase>;
65
66
75template<typename T = PerceptionBase>
76inline std::vector<std::shared_ptr<T>>
77get_perceptions(const std::vector<PerceptionBasePtr> & src)
78{
79 static_assert(std::is_base_of_v<PerceptionBase, T>,
80 "T must inherit from PerceptionBase");
81
82 std::vector<std::shared_ptr<T>> out;
83 out.reserve(src.size());
84
85 for (const auto & perception : src) {
86 if (!perception) {continue;}
87 // Homogeneous by derived type: include only successful casts
88 if (auto p = std::dynamic_pointer_cast<T>(perception)) {
89 out.push_back(std::move(p));
90 }
91 }
92 return out;
93}
94
95
108{
109public:
110 virtual ~PerceptionHandler() = default;
111
120 const std::shared_ptr<rclcpp_lifecycle::LifecycleNode> parent_node,
121 const rclcpp::CallbackGroup::SharedPtr realtime_cbg,
122 const std::string & sensor_name)
123 {
124 parent_node_ = parent_node;
125 realtime_cbg_ = realtime_cbg;
126 sensor_name_ = sensor_name;
128 }
129
133 virtual void on_initialize() {}
134
142 virtual bool cycle_rt([[maybe_unused]] std::shared_ptr<NavState> nav_state) {return false;}
143
144
146 const std::string & get_sensor_name() const {return sensor_name_;}
147
148protected:
150 std::shared_ptr<rclcpp_lifecycle::LifecycleNode> get_node() const {return parent_node_.lock();}
151
153 rclcpp::CallbackGroup::SharedPtr get_realtime_cbg() const {return realtime_cbg_;}
154
156 std::weak_ptr<rclcpp_lifecycle::LifecycleNode> parent_node_;
157
159 rclcpp::CallbackGroup::SharedPtr realtime_cbg_;
160
162 std::string sensor_name_;
163};
164
165} // namespace easynav
166
167#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:44
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:49
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:52
Abstract base class for pluginlib-based sensor perception handlers.
Definition Perceptions.hpp:108
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:119
virtual bool cycle_rt(std::shared_ptr< NavState > nav_state)
Run one real-time sensor processing cycle.
Definition Perceptions.hpp:142
std::string sensor_name_
Name of the sensor (used as YAML parameter namespace prefix).
Definition Perceptions.hpp:162
std::weak_ptr< rclcpp_lifecycle::LifecycleNode > parent_node_
Shared pointer to the parent lifecycle node.
Definition Perceptions.hpp:156
rclcpp::CallbackGroup::SharedPtr realtime_cbg_
Callback group for real-time operations.
Definition Perceptions.hpp:159
const std::string & get_sensor_name() const
Returns the sensor name provided during initialize.
Definition Perceptions.hpp:146
virtual ~PerceptionHandler()=default
virtual void on_initialize()
Optional post-initialization hook for subclasses.
Definition Perceptions.hpp:133
std::shared_ptr< rclcpp_lifecycle::LifecycleNode > get_node() const
Returns the parent lifecycle node.
Definition Perceptions.hpp:150
rclcpp::CallbackGroup::SharedPtr get_realtime_cbg() const
Returns the parent lifecycle node.
Definition Perceptions.hpp:153
Definition CircularBuffer.hpp:23
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< PerceptionBasePtr > &src)
Extracts a homogeneous collection of perceptions of type T from a heterogeneous vector.
Definition Perceptions.hpp:77