SMACC2
Loading...
Searching...
No Matches
cp_pose.hpp
Go to the documentation of this file.
1// Copyright 2025 Robosoft Inc.
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
15/*****************************************************************************************************************
16 *-2020
17 * Authors: Pablo Inigo Blasco, Brett Aldrich
18 *
19 ******************************************************************************************************************/
20#pragma once
21
22#include <functional>
23#include <mutex>
24
25#include <geometry_msgs/msg/pose_stamped.h>
26#include <tf2/transform_datatypes.h>
27#include <tf2/utils.h>
28#include <tf2_ros/buffer.h>
29#include <tf2_ros/transform_listener.h>
30#include <geometry_msgs/msg/quaternion_stamped.hpp>
31#include <tf2_geometry_msgs/tf2_geometry_msgs.hpp>
32
33#include <smacc2/component.hpp>
35
36namespace cl_nav2z
37{
39{
40 Map,
42};
43
45{
46public:
47 CpPose(std::string poseFrameName = "base_link", std::string referenceFrame = "odom");
48
49 CpPose(StandardReferenceFrames referenceFrame);
50
51 void onInitialize() override;
52
53 void update() override;
54
55 // synchronously waits a transform in the current thread
56 // Blocks until the pose is available; returns false if cancelled() became true first.
58 rclcpp::Rate r = rclcpp::Rate(20), const std::function<bool()> & cancelled = {});
59
60 inline geometry_msgs::msg::Pose toPoseMsg()
61 {
62 std::lock_guard<std::mutex> guard(m_mutex_);
63 return this->pose_.pose;
64 }
65
66 inline geometry_msgs::msg::PoseStamped toPoseStampedMsg()
67 {
68 RCLCPP_INFO_STREAM(getLogger(), "[Pose] ToPoseMsg ");
69 std::lock_guard<std::mutex> guard(m_mutex_);
70 return this->pose_;
71 }
72
73 // get yaw in radians
74 float getYaw();
75
76 float getX();
77 float getY();
78 float getZ();
79
80 inline void setReferenceFrame(std::string referenceFrame)
81 {
82 referenceFrame_ = std::move(referenceFrame);
83 }
84
85 inline const std::string & getReferenceFrame() const { return referenceFrame_; }
86
87 inline const std::string & getFrameId() const { return poseFrameName_; }
88
90
91 std::optional<rclcpp::Time> frozenReferenceFrameTime;
93 {
94 frozenReferenceFrameTime = getNode()->now() - rclcpp::Duration::from_seconds(1);
95 }
96
98
99private:
100 geometry_msgs::msg::PoseStamped pose_;
101
102 // One TF buffer and listener per process, shared by every CpPose instance and owned by them:
103 // each instance holds a strong reference, the statics hold only weak ones. The last CpPose
104 // to be destroyed, inside the state machine's destruction in run<>, releases the listener
105 // while the rclcpp context is still alive. Held as static shared_ptrs, they were released by
106 // the library's static destructors after main() returned, after the context had finalized
107 // the DDS participant, and the listener's own node crashed in Fast DDS on every shutdown.
108 // Member order matters: the listener references the buffer, so it is declared after it and
109 // destroyed before it.
110 std::shared_ptr<tf2_ros::Buffer> tfBuffer_;
111 std::shared_ptr<tf2_ros::TransformListener> tfListener_;
112
113 static std::weak_ptr<tf2_ros::Buffer> sharedTfBuffer_;
114 static std::weak_ptr<tf2_ros::TransformListener> sharedTfListener_;
115 static std::mutex listenerMutex_;
116
117 std::string poseFrameName_;
118 std::string referenceFrame_;
119
120 std::mutex m_mutex_;
121};
122} // namespace cl_nav2z
geometry_msgs::msg::Pose toPoseMsg()
Definition cp_pose.hpp:60
std::mutex m_mutex_
Definition cp_pose.hpp:120
bool waitTransformUpdate(rclcpp::Rate r=rclcpp::Rate(20), const std::function< bool()> &cancelled={})
Definition cp_pose.cpp:81
const std::string & getFrameId() const
Definition cp_pose.hpp:87
std::string referenceFrame_
Definition cp_pose.hpp:118
static std::mutex listenerMutex_
Definition cp_pose.hpp:115
CpPose(std::string poseFrameName="base_link", std::string referenceFrame="odom")
Definition cp_pose.cpp:35
static std::weak_ptr< tf2_ros::Buffer > sharedTfBuffer_
Definition cp_pose.hpp:113
geometry_msgs::msg::PoseStamped toPoseStampedMsg()
Definition cp_pose.hpp:66
std::shared_ptr< tf2_ros::TransformListener > tfListener_
Definition cp_pose.hpp:111
void setReferenceFrame(std::string referenceFrame)
Definition cp_pose.hpp:80
void unfreezeReferenceFrame()
Definition cp_pose.hpp:97
const std::string & getReferenceFrame() const
Definition cp_pose.hpp:85
std::optional< rclcpp::Time > frozenReferenceFrameTime
Definition cp_pose.hpp:91
void onInitialize() override
Definition cp_pose.cpp:59
std::shared_ptr< tf2_ros::Buffer > tfBuffer_
Definition cp_pose.hpp:110
geometry_msgs::msg::PoseStamped pose_
Definition cp_pose.hpp:100
std::string poseFrameName_
Definition cp_pose.hpp:117
static std::weak_ptr< tf2_ros::TransformListener > sharedTfListener_
Definition cp_pose.hpp:114
void update() override
Definition cp_pose.cpp:132
void freezeReferenceFrame()
Definition cp_pose.hpp:92
rclcpp::Logger getLogger() const
rclcpp::Node::SharedPtr getNode()
StandardReferenceFrames
Definition cp_pose.hpp:39