Easy Navigation
Loading...
Searching...
No Matches
GNSSPerception.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__GNSSPERCEPTION_HPP_
24#define EASYNAV_SENSORS__TYPES__GNSSPERCEPTION_HPP_
25
26#include <mutex>
27#include <string>
28#include <vector>
29#include <memory>
30
31#include "sensor_msgs/msg/nav_sat_fix.hpp"
32#include "rclcpp_lifecycle/lifecycle_node.hpp"
34
35namespace easynav
36{
37
43{
44public:
46 static constexpr std::string_view default_group_ = "gnss";
47
51 static inline bool supports_msg_type(std::string_view t)
52 {
53 return t == "sensor_msgs/msg/NavSatFix";
54 }
55
57 {
58 [[maybe_unused]] static const bool _ = [] {
60 [](const GNSSPerception & perception) {
61 std::ostringstream ret;
62 const auto & fix = perception.data;
63 ret << "{ " << perception.stamp.seconds()
64 << " } GNSSPerception lat = " << fix.latitude
65 << ", lon = " << fix.longitude
66 << ", alt = " << fix.altitude
67 << " (status: " << static_cast<int>(fix.status.status)
68 << ", service: " << fix.status.service << ")"
69 << " in frame [" << perception.frame_id
70 << "] with ts " << perception.stamp.seconds() << "\n";
71 return ret.str();
72 });
73 return true;
74 }();
75 }
76
78 {
79 std::lock_guard<std::mutex> lock(other.mutex_);
80 stamp = other.stamp;
81 frame_id = other.frame_id;
82 valid = other.valid;
83 new_data = other.new_data;
84 data = other.data;
85 }
86
88 {
89 if (this == &other) {
90 return *this;
91 }
92
93 std::scoped_lock lock(mutex_, other.mutex_);
94 stamp = other.stamp;
95 frame_id = other.frame_id;
96 valid = other.valid;
97 new_data = other.new_data;
98 data = other.data;
99
100 return *this;
101 }
102
104 sensor_msgs::msg::NavSatFix data;
105
111 const sensor_msgs::msg::NavSatFix & msg, const rclcpp::Time & msg_stamp,
112 const std::string & msg_frame_id)
113 {
114 std::lock_guard<std::mutex> lock(mutex_);
115 stamp = msg_stamp;
116 frame_id = msg_frame_id;
117 new_data = true;
118 data = msg;
119 valid = true;
120 }
121
125 {
126 std::lock_guard<std::mutex> lock(mutex_);
127 const bool had_new_data = new_data;
128 new_data = false;
129 return had_new_data;
130 }
131
132protected:
133 mutable std::mutex mutex_;
134};
135
143{
144public:
148 void on_initialize() override;
149
157 bool cycle_rt([[maybe_unused]] std::shared_ptr<NavState> nav_state) override;
158
160 std::shared_ptr<PerceptionBase> get_perception() const override {return perception_data_;}
161
162private:
164 std::shared_ptr<GNSSPerception> perception_data_ {nullptr};
165
167 rclcpp::SubscriptionBase::SharedPtr perception_sub_;
168};
169
177 std::vector<std::shared_ptr<GNSSPerception>>;
178
183rclcpp::Time get_latest_gnss_perceptions_stamp(const GNSSPerceptions & perceptions);
184
185} // namespace easynav
186
187#endif // EASYNAV_SENSORS__TYPES__GNSSPERCEPTION_HPP_
Defines data structures and utilities for representing and processing sensor perceptions.
Handles the creation and updating of GNSSPerception instances from sensor_msgs::msg::NavSatFix messag...
Definition GNSSPerception.hpp:143
bool cycle_rt(std::shared_ptr< NavState > nav_state) override
Run one real-time sensor processing cycle.
Definition GNSSPerception.cpp:63
void on_initialize() override
Optional post-initialization hook for subclasses.
Definition GNSSPerception.cpp:28
std::shared_ptr< PerceptionBase > get_perception() const override
The perception this handler keeps up to date.
Definition GNSSPerception.hpp:160
void set_data(const sensor_msgs::msg::NavSatFix &msg, const rclcpp::Time &msg_stamp, const std::string &msg_frame_id)
Atomically overwrites stamp/frame_id/data/valid from the subscription callback.
Definition GNSSPerception.hpp:110
bool consume_new_data()
Atomically reads and clears new_data.
Definition GNSSPerception.hpp:124
std::mutex mutex_
Definition GNSSPerception.hpp:133
sensor_msgs::msg::NavSatFix data
GNSS data received from the sensor.
Definition GNSSPerception.hpp:104
static bool supports_msg_type(std::string_view t)
Returns whether the given ROS 2 type name is supported by this perception.
Definition GNSSPerception.hpp:51
static constexpr std::string_view default_group_
Group identifier for GNSS perceptions.
Definition GNSSPerception.hpp:46
GNSSPerception & operator=(const GNSSPerception &other)
Definition GNSSPerception.hpp:87
GNSSPerception()
Definition GNSSPerception.hpp:56
GNSSPerception(const GNSSPerception &other)
Definition GNSSPerception.hpp:77
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< GNSSPerception > > GNSSPerceptions
Alias for a vector of shared pointers to GNSSPerception objects.
Definition GNSSPerception.hpp:176
rclcpp::Time get_latest_gnss_perceptions_stamp(const GNSSPerceptions &perceptions)
Retrieves the latest timestamp among a set of GNSS perceptions.
Definition GNSSPerception.cpp:71