YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
FrameTransformGet_nwc_ros.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2023 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
7
8#include <yarp/os/Log.h>
10#include <yarp/os/LogStream.h>
11
12using namespace yarp::dev;
13using namespace yarp::os;
14using namespace yarp::sig;
15using namespace yarp::math;
16
17namespace {
18YARP_LOG_COMPONENT(FRAMETRANSFORGETNWCROS, "yarp.device.frameTransformGet_nwc_ros")
19}
20
21//------------------------------------------------------------------------------------------------------------------------------
27
29{
31 yCError(FRAMETRANSFORGETNWCROS,"Error! YARP Network is not initialized");
32 return false;
33 }
34
35 bool okGeneral = config.check("GENERAL");
36 if(okGeneral)
37 {
39 if (general_config.check("period"))
40 {
41 m_period = general_config.find("period").asFloat64();
42 setPeriod(m_period);
43 }
44 if (general_config.check("refresh_interval")) {m_refreshInterval = general_config.find("refresh_interval").asFloat64();}
45 }
46 m_ftContainer.m_timeout = m_refreshInterval;
47
48 //ROS configuration
49 if (config.check("ROS"))
50 {
51 yCInfo(FRAMETRANSFORGETNWCROS, "Configuring ROS params");
52 Bottle ROS_config = config.findGroup("ROS");
53 if (ROS_config.check("ft_topic")) {
54 m_topic = ROS_config.find("ft_topic").asString();
55 }
56 if (ROS_config.check("ft_topic_static")) {
57 m_topic_static = ROS_config.find("ft_topic_static").asString();
58 }
59 if (ROS_config.check("ft_node")) {
60 m_nodeName = ROS_config.find("ft_node").asString();
61 }
62
63 //open ros publisher
64 if (m_rosNode == nullptr)
65 {
66 m_rosNode = new yarp::os::Node(m_nodeName);
67 }
68 if (!m_rosSubscriberPort_tf_timed.topic(m_topic))
69 {
70 yCError(FRAMETRANSFORGETNWCROS) << "Unable to publish data on " << m_topic << " topic, check your yarp-ROS network configuration";
71 return false;
72 }
73 m_rosSubscriberPort_tf_timed.setStrict();
74 if (!m_rosSubscriberPort_tf_static.topic(m_topic_static))
75 {
76 yCError(FRAMETRANSFORGETNWCROS) << "Unable to publish data on " << m_topic_static << " topic, check your yarp-ROS network configuration";
77 return false;
78 }
79 m_rosSubscriberPort_tf_static.setStrict();
80 }
81 else
82 {
83 //no ROS options
84 yCWarning(FRAMETRANSFORGETNWCROS) << "ROS Group not configured";
85 return false;
86 }
87
88 bool resp = start();
89
90 return resp;
91}
92
94{
97 {
99 }
100 m_rosSubscriberPort_tf_timed.close();
101 m_rosSubscriberPort_tf_static.close();
102 m_rosNode = nullptr;
103 return true;
104}
105
107{
108 std::lock_guard <std::mutex> lg(m_trf_mutex);
109 if(!m_ftContainer.checkAndRemoveExpired())
110 {
111 yCError(FRAMETRANSFORGETNWCROS,"Unable to remove expired transforms");
112 return;
113 }
115}
116
117bool FrameTransformGet_nwc_ros::getTransforms(std::vector<yarp::math::FrameTransform>& transforms) const
118{
119 std::lock_guard<std::mutex> lock(m_trf_mutex);
120 if(!m_ftContainer.checkAndRemoveExpired())
121 {
122 yCError(FRAMETRANSFORGETNWCROS,"Unable to remove expired transforms");
123 return false;
124 }
125 if(!m_ftContainer.getTransforms(transforms))
126 {
127 yCError(FRAMETRANSFORGETNWCROS,"Unable to retrieve transforms");
128 return false;
129 }
130 return true;
131}
132
133double FrameTransformGet_nwc_ros::yarpStampFromROS(const yarp::rosmsg::TickTime& rosTime)
134{
135 double yarpTime;
136 double sec_part;
137 double nsec_part;
138 sec_part = (double)rosTime.sec;
139 nsec_part = ((double)rosTime.nsec)/1000000000.0;
141
142 return yarpTime;
143}
144
145void FrameTransformGet_nwc_ros::rosTransformToYARPTransform(const yarp::rosmsg::geometry_msgs::TransformStamped &input, yarp::math::FrameTransform &output, bool isStatic)
146{
147 output.dst_frame_id = input.child_frame_id;
148 output.src_frame_id = input.header.frame_id;
149 output.timestamp = yarpStampFromROS(input.header.stamp);
150 output.rotation.x() = input.transform.rotation.x;
151 output.rotation.y() = input.transform.rotation.y;
152 output.rotation.z() = input.transform.rotation.z;
153 output.rotation.w() = input.transform.rotation.w;
154 output.translation.tX = input.transform.translation.x;
155 output.translation.tY = input.transform.translation.y;
156 output.translation.tZ = input.transform.translation.z;
157 output.isStatic = isStatic;
158}
159
161{
162 yarp::rosmsg::tf2_msgs::TFMessage* rosInData_timed = nullptr;
163 do
164 {
165 rosInData_timed = m_rosSubscriberPort_tf_timed.read(false);
166 if (rosInData_timed != nullptr)
167 {
168 std::vector <yarp::rosmsg::geometry_msgs::TransformStamped> tfs = rosInData_timed->transforms;
169 for (auto& tf : tfs)
170 {
173 m_ftContainer.setTransform(t);
174 }
175 }
176 } while (rosInData_timed != nullptr);
177 yarp::rosmsg::tf2_msgs::TFMessage* rosInData_static = nullptr;
178 do
179 {
180 rosInData_static = m_rosSubscriberPort_tf_static.read(false);
181 if (rosInData_static != nullptr)
182 {
183 std::vector <yarp::rosmsg::geometry_msgs::TransformStamped> tfs = rosInData_static->transforms;
184 for (auto& tf : tfs)
185 {
188 m_ftContainer.setTransform(t);
189 }
190 }
191 } while (rosInData_static != nullptr);
192}
bool getTransforms(std::vector< yarp::math::FrameTransform > &transforms) const override
Obtains all frame transforms saved in a storage.
void rosTransformToYARPTransform(const yarp::rosmsg::geometry_msgs::TransformStamped &input, yarp::math::FrameTransform &output, bool isStatic)
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
bool close() override
Close the DeviceDriver.
double yarpStampFromROS(const yarp::rosmsg::TickTime &rosTime)
FrameTransformGet_nwc_ros(double tperiod=0.010)
void run() override
Loop function.
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.
struct yarp::math::FrameTransform::Translation_t translation
A simple collection of objects that can be described and transmitted in a portable way.
Definition Bottle.h:64
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
The Node class.
Definition Node.h:23
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
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
virtual Bottle & findGroup(const std::string &key) const =0
Gets a list corresponding to a given keyword.
T * read(bool shouldWait=true)
Read a message from the port.
Definition Subscriber.h:113
void close() override
Stop port activity.
Definition Subscriber.h:83
void setStrict(bool strict=true)
Definition Subscriber.h:150
bool topic(const std::string &name)
Set topic to subscribe to.
Definition Subscriber.h:62
#define yCInfo(component,...)
#define yCError(component,...)
#define yCTrace(component,...)
#define yCWarning(component,...)
#define YARP_LOG_COMPONENT(name,...)
For streams capable of holding different kinds of content, check what they actually have.
An interface to the operating system, including Port based communication.