YARP
Yet Another Robot Platform
FrameTransformGet_nwc_ros.cpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
7 
8 #include <yarp/os/Log.h>
9 #include <yarp/os/LogComponent.h>
10 #include <yarp/os/LogStream.h>
11 
12 using namespace yarp::dev;
13 using namespace yarp::os;
14 using namespace yarp::sig;
15 using namespace yarp::math;
16 
17 namespace {
18 YARP_LOG_COMPONENT(FRAMETRANSFORGETNWCROS, "yarp.device.frameTransformGet_nwc_ros")
19 }
20 
21 //------------------------------------------------------------------------------------------------------------------------------
23 PeriodicThread(tperiod),
24 m_period(tperiod)
25 {
26 }
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  {
38  yarp::os::Searchable& general_config = config.findGroup("GENERAL");
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 {
95  yCTrace(FRAMETRANSFORGETNWCROS, "Close");
96  if (PeriodicThread::isRunning())
97  {
98  PeriodicThread::stop();
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 
117 bool 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 
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;
140  yarpTime = sec_part+nsec_part;
141 
142  return yarpTime;
143 }
144 
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 }
float t
bool setTransform(const yarp::math::FrameTransform &transform) override
Save a frame transform in a storage.
bool getTransforms(std::vector< yarp::math::FrameTransform > &transforms) const override
Obtains all frame transforms saved in a storage.
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.
struct yarp::math::FrameTransform::Translation_t translation
double x() const
Definition: Quaternion.cpp:81
double z() const
Definition: Quaternion.cpp:91
double w() const
Definition: Quaternion.cpp:76
double y() const
Definition: Quaternion.cpp:86
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:74
bool check(const std::string &key) const override
Check if there exists a property of the given name.
Definition: Bottle.cpp:277
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
Definition: Bottle.cpp:287
static bool checkNetwork()
Check if the YARP Network is up and running.
Definition: Network.cpp:1377
The Node class.
Definition: Node.h:24
An abstraction for a periodic thread.
bool setPeriod(double period)
Set the (new) period of the thread.
bool start()
Call this to start the thread.
A base class for nested structures that can be searched.
Definition: Searchable.h:66
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
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.
void close() override
Stop port activity.
Definition: Subscriber.h:84
T * read(bool shouldWait=true)
Read a message from the port.
Definition: Subscriber.h:114
void setStrict(bool strict=true)
Definition: Subscriber.h:151
bool topic(const std::string &name)
Set topic to subscribe to.
Definition: Subscriber.h:63
virtual yarp::conf::float64_t asFloat64() const
Get 64-bit floating point value.
Definition: Value.cpp:222
virtual std::string asString() const
Get string value.
Definition: Value.cpp:234
std::uint32_t nsec
Definition: TickTime.h:30
std::uint32_t sec
Definition: TickTime.h:29
yarp::rosmsg::geometry_msgs::Transform transform
yarp::rosmsg::geometry_msgs::Quaternion rotation
Definition: Transform.h:35
yarp::rosmsg::geometry_msgs::Vector3 translation
Definition: Transform.h:34
yarp::conf::float64_t y
Definition: Vector3.h:38
yarp::conf::float64_t z
Definition: Vector3.h:39
yarp::conf::float64_t x
Definition: Vector3.h:37
yarp::rosmsg::TickTime stamp
Definition: Header.h:45
std::vector< yarp::rosmsg::geometry_msgs::TransformStamped > transforms
Definition: TFMessage.h:30
#define yCInfo(component,...)
Definition: LogComponent.h:132
#define yCError(component,...)
Definition: LogComponent.h:154
#define yCTrace(component,...)
Definition: LogComponent.h:85
#define yCWarning(component,...)
Definition: LogComponent.h:143
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:77
Definition: FrameGraph.h:19
An interface for the device drivers.
An interface to the operating system, including Port based communication.
Signal processing.
Definition: Image.h:22