Easy Navigation
Loading...
Searching...
No Matches
DetectionsPerception.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
22
23#ifndef EASYNAV_SENSORS__TYPES__DETECTIONSPERCEPTION_HPP_
24#define EASYNAV_SENSORS__TYPES__DETECTIONSPERCEPTION_HPP_
25
26#include <mutex>
27#include <string>
28#include <vector>
29#include <memory>
30
31#include "vision_msgs/msg/detection3_d_array.hpp"
32#include "rclcpp_lifecycle/lifecycle_node.hpp"
34
35namespace easynav
36{
37
44{
45public:
47 static constexpr std::string_view default_group_ = "detections";
48
52 static inline bool supports_msg_type(std::string_view t)
53 {
54 return t == "vision_msgs/msg/Detection3DArray";
55 }
56
58 {
59 [[maybe_unused]] static const bool _ = [] {
61 [](const DetectionsPerception & perception) {
62 std::ostringstream ret;
63 ret << "{ " << perception.stamp.seconds()
64 << " } DetectionsPerception with "
65 << perception.data.detections.size()
66 << " detections in frame [" << perception.frame_id
67 << "] with ts " << perception.stamp.seconds() << "\n";
68 return ret.str();
69 });
70 return true;
71 }();
72 }
73
75 {
76 std::lock_guard<std::mutex> lock(other.mutex_);
77 stamp = other.stamp;
78 frame_id = other.frame_id;
79 valid = other.valid;
80 new_data = other.new_data;
81 data = other.data;
82 }
83
85 {
86 if (this == &other) {
87 return *this;
88 }
89
90 std::scoped_lock lock(mutex_, other.mutex_);
91 stamp = other.stamp;
92 frame_id = other.frame_id;
93 valid = other.valid;
94 new_data = other.new_data;
95 data = other.data;
96
97 return *this;
98 }
99
101 vision_msgs::msg::Detection3DArray data;
102
108 const vision_msgs::msg::Detection3DArray & msg, const rclcpp::Time & msg_stamp,
109 const std::string & msg_frame_id)
110 {
111 std::lock_guard<std::mutex> lock(mutex_);
112 stamp = msg_stamp;
113 frame_id = msg_frame_id;
114 new_data = true;
115 data = msg;
116 valid = true;
117 }
118
122 {
123 std::lock_guard<std::mutex> lock(mutex_);
124 const bool had_new_data = new_data;
125 new_data = false;
126 return had_new_data;
127 }
128
129protected:
130 mutable std::mutex mutex_;
131};
132
140{
141public:
145 void on_initialize() override;
146
154 bool cycle_rt([[maybe_unused]] std::shared_ptr<NavState> nav_state) override;
155
157 std::shared_ptr<PerceptionBase> get_perception() const override {return perception_data_;}
158
159private:
161 std::shared_ptr<DetectionsPerception> perception_data_ {nullptr};
162
164 rclcpp::SubscriptionBase::SharedPtr perception_sub_;
165};
166
174 std::vector<std::shared_ptr<DetectionsPerception>>;
175
180rclcpp::Time get_latest_detections_perceptions_stamp(const DetectionsPerceptions & perceptions);
181
182} // namespace easynav
183
184#endif // EASYNAV_SENSORS__TYPES__DETECTIONSPERCEPTION_HPP_
Defines data structures and utilities for representing and processing sensor perceptions.
bool consume_new_data()
Atomically reads and clears new_data.
Definition DetectionsPerception.hpp:121
std::mutex mutex_
Definition DetectionsPerception.hpp:130
static bool supports_msg_type(std::string_view t)
Returns whether the given ROS 2 type name is supported by this perception.
Definition DetectionsPerception.hpp:52
DetectionsPerception()
Definition DetectionsPerception.hpp:57
static constexpr std::string_view default_group_
Group identifier for image perceptions.
Definition DetectionsPerception.hpp:47
DetectionsPerception(const DetectionsPerception &other)
Definition DetectionsPerception.hpp:74
DetectionsPerception & operator=(const DetectionsPerception &other)
Definition DetectionsPerception.hpp:84
vision_msgs::msg::Detection3DArray data
Detection3DArray data received from the an external processing system.
Definition DetectionsPerception.hpp:101
void set_data(const vision_msgs::msg::Detection3DArray &msg, const rclcpp::Time &msg_stamp, const std::string &msg_frame_id)
Atomically overwrites stamp/frame_id/data/valid from the subscription callback.
Definition DetectionsPerception.hpp:107
Handles the creation and updating of DetectionsPerceptions instances from sensor_msgs::msg::Image mes...
Definition DetectionsPerception.hpp:140
bool cycle_rt(std::shared_ptr< NavState > nav_state) override
Run one real-time sensor processing cycle.
Definition DetectionsPerception.cpp:66
void on_initialize() override
Optional post-initialization hook for subclasses.
Definition DetectionsPerception.cpp:29
std::shared_ptr< PerceptionBase > get_perception() const override
The perception this handler keeps up to date.
Definition DetectionsPerception.hpp:157
static void register_printer(std::function< std::string(const T &)> printer)
Registers a pretty-printer for type T used by debug_string().
Definition NavState.hpp:458
Abstract base class for representing a single sensor perception.
Definition Perceptions.hpp:48
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
Definition CircularBuffer.hpp:24
std::vector< std::shared_ptr< DetectionsPerception > > DetectionsPerceptions
Definition DetectionsPerception.hpp:173
rclcpp::Time get_latest_detections_perceptions_stamp(const DetectionsPerceptions &perceptions)
Retrieves the latest timestamp among a set of detection-based perceptions.
Definition DetectionsPerception.cpp:74