Easy Navigation
Loading...
Searching...
No Matches
IMUPerception.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__IMUPERCEPTION_HPP_
24#define EASYNAV_SENSORS__TYPES__IMUPERCEPTION_HPP_
25
26#include <mutex>
27#include <string>
28#include <vector>
29#include <memory>
30
31#include "sensor_msgs/msg/imu.hpp"
32#include "rclcpp_lifecycle/lifecycle_node.hpp"
34
35namespace easynav
36{
37
43{
44public:
46 static constexpr std::string_view default_group_ = "imu";
47
51 static inline bool supports_msg_type(std::string_view t)
52 {
53 return t == "sensor_msgs/msg/Imu";
54 }
55
57 {
58 [[maybe_unused]] static const bool _ = [] {
60 [](const IMUPerception & perception) {
61 std::ostringstream ret;
62 ret << "{ " << perception.stamp.seconds()
63 << " } IMUPerception linear acc = ("
64 << perception.data.linear_acceleration.x << ", "
65 << perception.data.linear_acceleration.y << ", "
66 << perception.data.linear_acceleration.z
67 << ") in frame [" << perception.frame_id
68 << "] with ts " << perception.stamp.seconds() << "\n";
69 return ret.str();
70 });
71 return true;
72 }();
73 }
74
76 {
77 std::lock_guard<std::mutex> lock(other.mutex_);
78 stamp = other.stamp;
79 frame_id = other.frame_id;
80 valid = other.valid;
81 new_data = other.new_data;
82 data = other.data;
83 }
84
86 {
87 if (this == &other) {
88 return *this;
89 }
90
91 std::scoped_lock lock(mutex_, other.mutex_);
92 stamp = other.stamp;
93 frame_id = other.frame_id;
94 valid = other.valid;
95 new_data = other.new_data;
96 data = other.data;
97
98 return *this;
99 }
100
102 sensor_msgs::msg::Imu data;
103
109 const sensor_msgs::msg::Imu & msg, const rclcpp::Time & msg_stamp,
110 const std::string & msg_frame_id)
111 {
112 std::lock_guard<std::mutex> lock(mutex_);
113 stamp = msg_stamp;
114 frame_id = msg_frame_id;
115 new_data = true;
116 data = msg;
117 valid = true;
118 }
119
123 {
124 std::lock_guard<std::mutex> lock(mutex_);
125 const bool had_new_data = new_data;
126 new_data = false;
127 return had_new_data;
128 }
129
130protected:
131 mutable std::mutex mutex_;
132};
133
141{
142public:
146 void on_initialize() override;
147
155 bool cycle_rt([[maybe_unused]] std::shared_ptr<NavState> nav_state) override;
156
158 std::shared_ptr<PerceptionBase> get_perception() const override {return perception_data_;}
159
160private:
162 std::shared_ptr<IMUPerception> perception_data_ {nullptr};
163
165 rclcpp::SubscriptionBase::SharedPtr perception_sub_;
166};
167
175 std::vector<std::shared_ptr<IMUPerception>>;
176
181rclcpp::Time get_latest_imu_perceptions_stamp(const IMUPerceptions & perceptions);
182
183} // namespace easynav
184
185#endif // EASYNAV_SENSORS__TYPES__IMUPERCEPTION_HPP_
Defines data structures and utilities for representing and processing sensor perceptions.
Handles the creation and updating of IMUPerception instances from sensor_msgs::msg::Imu messages.
Definition IMUPerception.hpp:141
bool cycle_rt(std::shared_ptr< NavState > nav_state) override
Run one real-time sensor processing cycle.
Definition IMUPerception.cpp:65
void on_initialize() override
Optional post-initialization hook for subclasses.
Definition IMUPerception.cpp:30
std::shared_ptr< PerceptionBase > get_perception() const override
The perception this handler keeps up to date.
Definition IMUPerception.hpp:158
bool consume_new_data()
Atomically reads and clears new_data.
Definition IMUPerception.hpp:122
std::mutex mutex_
Definition IMUPerception.hpp:131
IMUPerception & operator=(const IMUPerception &other)
Definition IMUPerception.hpp:85
void set_data(const sensor_msgs::msg::Imu &msg, const rclcpp::Time &msg_stamp, const std::string &msg_frame_id)
Atomically overwrites stamp/frame_id/data/valid from the subscription callback.
Definition IMUPerception.hpp:108
static bool supports_msg_type(std::string_view t)
Returns whether the given ROS 2 type name is supported by this perception.
Definition IMUPerception.hpp:51
static constexpr std::string_view default_group_
Group identifier for IMU perceptions.
Definition IMUPerception.hpp:46
sensor_msgs::msg::Imu data
IMU data received from the sensor.
Definition IMUPerception.hpp:102
IMUPerception(const IMUPerception &other)
Definition IMUPerception.hpp:75
IMUPerception()
Definition IMUPerception.hpp:56
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< IMUPerception > > IMUPerceptions
Alias for a vector of shared pointers to IMUPerception objects.
Definition IMUPerception.hpp:174
rclcpp::Time get_latest_imu_perceptions_stamp(const IMUPerceptions &perceptions)
Retrieves the latest timestamp among a set of IMU perceptions.
Definition IMUPerception.cpp:73