Easy Navigation
Loading...
Searching...
No Matches
ImagePerception.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__IMAGEPERCEPTION_HPP_
24#define EASYNAV_SENSORS__TYPES__IMAGEPERCEPTION_HPP_
25
26#include <mutex>
27#include <string>
28#include <utility>
29#include <vector>
30#include <memory>
31
32#if __has_include("cv_bridge/cv_bridge.hpp")
33#include "cv_bridge/cv_bridge.hpp"
34#else
35#include "cv_bridge/cv_bridge.h" // Humble: no .hpp header yet
36#endif
37
38#include "rclcpp_lifecycle/lifecycle_node.hpp"
39
41
42namespace easynav
43{
44
51{
52public:
54 static constexpr std::string_view default_group_ = "image";
55
59 static inline bool supports_msg_type(std::string_view t)
60 {
61 return t == "sensor_msgs/msg/Image";
62 }
63
65 {
66 [[maybe_unused]] static const bool _ = [] {
68 [](const ImagePerception & perception) {
69 std::ostringstream ret;
70 ret << "{ " << perception.stamp.seconds()
71 << " } ImagePerception ("
72 << perception.data.cols << " x " << perception.data.rows
73 << ") in frame [" << perception.frame_id
74 << "] with ts " << perception.stamp.seconds() << "\n";
75 return ret.str();
76 });
77 return true;
78 }();
79 }
80
82 {
83 std::lock_guard<std::mutex> lock(other.mutex_);
84 stamp = other.stamp;
85 frame_id = other.frame_id;
86 valid = other.valid;
87 new_data = other.new_data;
88 data = other.data;
89 }
90
92 {
93 if (this == &other) {
94 return *this;
95 }
96
97 std::scoped_lock lock(mutex_, other.mutex_);
98 stamp = other.stamp;
99 frame_id = other.frame_id;
100 valid = other.valid;
101 new_data = other.new_data;
102 data = other.data;
103
104 return *this;
105 }
106
111 cv::Mat data;
112
119 cv::Mat && image, const rclcpp::Time & msg_stamp,
120 const std::string & msg_frame_id)
121 {
122 std::lock_guard<std::mutex> lock(mutex_);
123 stamp = msg_stamp;
124 frame_id = msg_frame_id;
125 new_data = true;
126 data = std::move(image);
127 valid = true;
128 }
129
132 void mark_invalid(const rclcpp::Time & msg_stamp, const std::string & msg_frame_id)
133 {
134 std::lock_guard<std::mutex> lock(mutex_);
135 stamp = msg_stamp;
136 frame_id = msg_frame_id;
137 new_data = true;
138 valid = false;
139 }
140
144 {
145 std::lock_guard<std::mutex> lock(mutex_);
146 const bool had_new_data = new_data;
147 new_data = false;
148 return had_new_data;
149 }
150
151protected:
152 mutable std::mutex mutex_;
153};
154
162{
163public:
167 void on_initialize() override;
168
176 bool cycle_rt([[maybe_unused]] std::shared_ptr<NavState> nav_state) override;
177
179 std::shared_ptr<PerceptionBase> get_perception() const override {return perception_data_;}
180
181private:
183 std::shared_ptr<ImagePerception> perception_data_ {nullptr};
184
186 rclcpp::SubscriptionBase::SharedPtr perception_sub_;
187};
188
196 std::vector<std::shared_ptr<ImagePerception>>;
197
202rclcpp::Time get_latest_image_perceptions_stamp(const ImagePerceptions & perceptions);
203
204} // namespace easynav
205
206#endif // EASYNAV_SENSORS__TYPES__IMAGEPERCEPTION_HPP_
Defines data structures and utilities for representing and processing sensor perceptions.
Handles the creation and updating of ImagePerception instances from sensor_msgs::msg::Image messages.
Definition ImagePerception.hpp:162
bool cycle_rt(std::shared_ptr< NavState > nav_state) override
Run one real-time sensor processing cycle.
Definition ImagePerception.cpp:76
void on_initialize() override
Optional post-initialization hook for subclasses.
Definition ImagePerception.cpp:30
std::shared_ptr< PerceptionBase > get_perception() const override
The perception this handler keeps up to date.
Definition ImagePerception.hpp:179
bool consume_new_data()
Atomically reads and clears new_data.
Definition ImagePerception.hpp:143
std::mutex mutex_
Definition ImagePerception.hpp:152
ImagePerception & operator=(const ImagePerception &other)
Definition ImagePerception.hpp:91
static bool supports_msg_type(std::string_view t)
Returns whether the given ROS 2 type name is supported by this perception.
Definition ImagePerception.hpp:59
static constexpr std::string_view default_group_
Group identifier for image perceptions.
Definition ImagePerception.hpp:54
ImagePerception()
Definition ImagePerception.hpp:64
cv::Mat data
Image data received from the sensor.
Definition ImagePerception.hpp:111
void mark_invalid(const rclcpp::Time &msg_stamp, const std::string &msg_frame_id)
Atomically records a failed decode: updates stamp/frame_id, marks invalid, and leaves data untouched ...
Definition ImagePerception.hpp:132
void set_data(cv::Mat &&image, const rclcpp::Time &msg_stamp, const std::string &msg_frame_id)
Atomically overwrites stamp/frame_id/data with a successfully decoded image and marks the perception ...
Definition ImagePerception.hpp:118
ImagePerception(const ImagePerception &other)
Definition ImagePerception.hpp:81
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< ImagePerception > > ImagePerceptions
Alias for a vector of shared pointers to ImagePerception objects.
Definition ImagePerception.hpp:195
rclcpp::Time get_latest_image_perceptions_stamp(const ImagePerceptions &perceptions)
Retrieves the latest timestamp among a set of image-based perceptions.
Definition ImagePerception.cpp:84