YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
frameTransformSet_nwc_ros2.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2023 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
7#include <yarp/os/Log.h>
9#include <yarp/os/LogStream.h>
10
11using namespace std;
12using namespace yarp::dev;
13using namespace yarp::os;
14using namespace yarp::sig;
15using namespace yarp::math;
16
17namespace {
18YARP_LOG_COMPONENT(FRAMETRANSFORMSETNWCROS2, "yarp.device.frameTransformSet_nwc_ros2")
19}
20
21//------------------------------------------------------------------------------------------------------------------------------
22
27
29{
31 yCError(FRAMETRANSFORMSETNWCROS2,"Error! YARP Network is not initialized");
32 return false;
33 }
34
35 parseParams(config);
36
40 m_publisherFtTimed = m_node->create_publisher<tf2_msgs::msg::TFMessage>(m_ROS2_ft_topic, 10);
41 m_publisherFtStatic = m_node->create_publisher<tf2_msgs::msg::TFMessage>(m_ROS2_ft_topic_static, rclcpp::QoS(10).reliable().durability(RMW_QOS_POLICY_DURABILITY_TRANSIENT_LOCAL));
42
43 start();
44
45 return true;
46}
47
49{
52 {
54 }
55 return true;
56}
57
59{
60 std::lock_guard <std::mutex> lg(m_trf_mutex);
62 {
63 yCError(FRAMETRANSFORMSETNWCROS2,"Error while publishing transforms");
64 }
65 return;
66}
67
68yarp::dev::ReturnValue FrameTransformSet_nwc_ros2::setTransforms(const std::vector<yarp::math::FrameTransform>& transforms)
69{
70 std::lock_guard<std::mutex> lock(m_trf_mutex);
71 yarp::dev::ReturnValue ret = m_ftContainer.setTransforms(transforms);
72 if(!ret)
73 {
74 yCError(FRAMETRANSFORMSETNWCROS2,"Unable to set transforms. Error: %s",ret.toString().c_str());
75 return ret;
76 }
78 {
80 {
81 yCError(FRAMETRANSFORMSETNWCROS2,"Error while publishing transforms");
83 }
84 }
86}
87
89{
90 std::lock_guard<std::mutex> lock(m_trf_mutex);
92 if(!ret)
93 {
94 yCError(FRAMETRANSFORMSETNWCROS2,"Unable to set transform. Error: %s",ret.toString().c_str());
95 return ret;
96 }
98 {
100 {
101 yCError(FRAMETRANSFORMSETNWCROS2,"Error while publishing transforms");
103 }
104 }
106}
107
108void FrameTransformSet_nwc_ros2::ros2TimeFromYarp(double yarpTime, builtin_interfaces::msg::Time& ros2Time)
109{
110 uint64_t time;
113 sec_part = int(yarpTime); // (time / 1000000000UL);
114 nsec_part = (yarpTime - sec_part)*1000000000UL;
115 ros2Time.sec = sec_part;
116 ros2Time.nanosec = (uint32_t)nsec_part;
117}
118
119void FrameTransformSet_nwc_ros2::yarpTransformToROS2Transform(const yarp::math::FrameTransform &input, geometry_msgs::msg::TransformStamped& output)
120{
121 builtin_interfaces::msg::Time tempStamp;
122 std_msgs::msg::Header tempHeader;
123 geometry_msgs::msg::Transform tempTf;
124 geometry_msgs::msg::Vector3 tempTransl;
125 geometry_msgs::msg::Quaternion tempRotation;
126
127 tempTransl.x = input.translation.tX;
128 tempTransl.y = input.translation.tY;
129 tempTransl.z = input.translation.tZ;
130
131 tempRotation.w = input.rotation.w();
132 tempRotation.x = input.rotation.x();
133 tempRotation.y = input.rotation.y();
134 tempRotation.z = input.rotation.z();
135
136 tempTf.rotation = tempRotation;
137 tempTf.translation = tempTransl;
138
140 tempHeader.stamp = tempStamp;
141 tempHeader.frame_id = input.src_frame_id;
142
143 output.header = tempHeader;
144 output.child_frame_id = input.dst_frame_id;
145 output.transform = tempTf;
146}
147
149{
150 if(!m_ftContainer.checkAndRemoveExpired())
151 {
152 yCError(FRAMETRANSFORMSETNWCROS2,"Unable to remove expired transforms");
153 return false;
154 }
155 std::vector<yarp::math::FrameTransform> fromGet;
156 m_ftContainer.getTransforms(fromGet);
157 tf2_msgs::msg::TFMessage toPublish_timed;
158 tf2_msgs::msg::TFMessage toPublish_static;
159 std::vector<geometry_msgs::msg::TransformStamped> content_timed;
160 std::vector<geometry_msgs::msg::TransformStamped> content_static;
161 for(auto &ft : fromGet)
162 {
163 geometry_msgs::msg::TransformStamped tempTfs;
165 if(!ft.isStatic) {
166 content_timed.push_back(tempTfs);
167 }else {
168 content_static.push_back(tempTfs);
169 }
170 }
171
172 toPublish_timed.transforms = content_timed;
173 toPublish_static.transforms = content_static;
174 if (content_timed.size() > 0) {
175 m_publisherFtTimed->publish(toPublish_timed);
176 }
177 if (content_static.size() > 0) {
178 m_publisherFtStatic->publish(toPublish_static);
179 }
180
181 return true;
182}
183
185{
186 // Not yet implemented
187 yCError(FRAMETRANSFORMSETNWCROS2, "deleteTransform not yet implemented");
189}
190
float t
bool ret
bool parseParams(const yarp::os::Searchable &config) override
Parse the DeviceDriver parameters.
FrameTransformSet_nwc_ros2(double tperiod=0.010)
yarp::dev::ReturnValue clearAll() override
Delete all transforms in a storage.
void ros2TimeFromYarp(double yarpTime, builtin_interfaces::msg::Time &ros2Time)
yarp::dev::ReturnValue setTransform(const yarp::math::FrameTransform &transform) override
Save a frame transform in a storage.
yarp::dev::ReturnValue setTransforms(const std::vector< yarp::math::FrameTransform > &transforms) override
Save some frame transforms in a storage.
yarp::dev::ReturnValue deleteTransform(std::string t1, std::string t2) override
Delete a single transform in the storage.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
void run() override
Loop function.
bool close() override
Close the DeviceDriver.
void yarpTransformToROS2Transform(const yarp::math::FrameTransform &input, geometry_msgs::msg::TransformStamped &output)
static rclcpp::Node::SharedPtr createNode(std::string name)
Definition Ros2Utils.cpp:9
yarp::dev::ReturnValue getTransforms(std::vector< yarp::math::FrameTransform > &transforms) const override
Obtains all frame transforms saved in a storage.
yarp::dev::ReturnValue setTransform(const yarp::math::FrameTransform &transform) override
Save a frame transform in a storage.
yarp::dev::ReturnValue setTransforms(const std::vector< yarp::math::FrameTransform > &transforms) override
Save some frame transforms in a storage.
@ return_value_error_generic
Method was successfully executed.
struct yarp::math::FrameTransform::Translation_t translation
A mini-server for performing network communication in the background.
static bool checkNetwork()
Check if the YARP Network is up and running.
Definition Network.cpp:1370
An abstraction for a periodic thread.
bool setPeriod(double period)
Set the (new) period of the thread.
bool isRunning() const
Returns true when the thread is started, false otherwise.
bool start()
Call this to start the thread.
void stop()
Call this to stop the thread, this call blocks until the thread is terminated (and releaseThread() ca...
A base class for nested structures that can be searched.
Definition Searchable.h:31
#define yCError(component,...)
#define yCTrace(component,...)
#define YARP_LOG_COMPONENT(name,...)
STL namespace.
For streams capable of holding different kinds of content, check what they actually have.
Definition jointData.cpp:13
double now()
Return the current time in seconds, relative to an arbitrary starting point.
Definition Time.cpp:121
An interface to the operating system, including Port based communication.