YARP
Yet Another Robot Platform
FrameTransformGet_nws_yarp.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/LogComponent.h>
9 #include <yarp/os/LogStream.h>
10 
11 
12 YARP_LOG_COMPONENT(FRAMETRANSFORMGETNWSYARP, "yarp.devices.FrameTransformGet_nws_yarp")
13 
15 PeriodicThread (0.010),
16 m_thrift_rpcPort_Name("/frameTransformGet/rpc")
17 {
18 }
19 
20 
23 {
25  yCError(FRAMETRANSFORMGETNWSYARP,"Error! YARP Network is not initialized");
26  return false;
27  }
28  std::string prefix;
29  //checking default config param
30  bool default_config = true;
31  if(config.check("default-config")) {
32  default_config = config.find("default-config").asString() == "true";
33  }
34  // configuration
35  if (config.check("nws_thrift_port_prefix")){
36  prefix = config.find("nws_thrift_port_prefix").asString() + (default_config ? m_defaultConfigPrefix : "");
37  if(prefix[0] != '/') {prefix = "/"+prefix;}
38  m_thrift_rpcPort_Name = prefix + "/" + m_deviceName + "/thrift";
39  }
40  else {
41  prefix = default_config ? m_defaultConfigPrefix : "";
42  m_thrift_rpcPort_Name = prefix + "/" + m_deviceName + "/thrift";
43  yCWarning(FRAMETRANSFORMGETNWSYARP) << "no nws_thrift_port_prefix param found. The resulting port name will be: " << m_thrift_rpcPort_Name;
44  }
45  if(config.check("streaming_enabled")) {
46  m_streaming_port_enabled = config.find("streaming_enabled").asString() == "true";
47  }
48  if (config.check("period")) {
49  double period = config.find("period").asFloat64();
50  this->setPeriod(period);
51  }
52 
53  // rpc initialization
54  if(!m_thrift_rpcPort.open(m_thrift_rpcPort_Name))
55  {
56  yCError(FRAMETRANSFORMGETNWSYARP,"Could not open \"%s\" port",m_thrift_rpcPort_Name.c_str());
57  return false;
58  }
59  if(!this->yarp().attachAsServer(m_thrift_rpcPort))
60  {
61  yCError(FRAMETRANSFORMGETNWSYARP,"Error! Cannot attach the port as a server");
62  return false;
63  }
64  if(m_verbose <= 2) {
65  yCTrace(FRAMETRANSFORMGETNWSYARP) << "\nParameters are: \n" << config.toString();
66  }
67 
68  if (m_streaming_port_enabled)
69  {
70  if (config.check("output_streaming_port_prefix")){
71  prefix = config.find("output_streaming_port_prefix").asString() + (default_config ? m_defaultConfigPrefix : "");
72  if(prefix[0] != '/') {prefix = "/"+prefix;}
73  m_streaming_port_name = prefix + "/" + m_deviceName + "/tf:o";
74  }
75  else {
76  prefix = default_config ? m_defaultConfigPrefix : "";
77  m_streaming_port_name = prefix + "/" + m_deviceName + "/tf:o";
78  yCWarning(FRAMETRANSFORMGETNWSYARP) << "no output_streaming_port_prefix param found. The resulting port name will be: " << m_streaming_port_name;
79  }
80  yCInfo(FRAMETRANSFORMGETNWSYARP) << "Streaming transforms on Yarp port enabled";
81  this->start();
82  }
83  else
84  {
85  yCInfo(FRAMETRANSFORMGETNWSYARP) << "Streaming transforms on Yarp port NOT enabled";
86  }
87 
88  return true;
89 }
90 
91 
93 {
94  if (m_streaming_port_enabled)
95  {
96  this->stop();
97  }
98 
100  detach();
101  // Closing port
102  m_thrift_rpcPort.interrupt();
103  m_thrift_rpcPort.close();
104  return true;
105 }
106 
107 
109 {
110  m_iFrameTransformStorageGet = nullptr;
111  return true;
112 }
113 
114 
116 {
117  deviceToAttach->view(m_iFrameTransformStorageGet);
118 
119  if ( m_iFrameTransformStorageGet==nullptr){
120  yCError(FRAMETRANSFORMGETNWSYARP) << "could not attach to the device";
121  return false;
122  }
123  return true;
124 }
125 
126 
128 {
129  if (m_iFrameTransformStorageGet != nullptr) {
130  std::vector<yarp::math::FrameTransform> localTransform;
131  if (m_iFrameTransformStorageGet->getTransforms(localTransform)) {
132  return return_getAllTransforms(true, localTransform);
133  }
134  }
135  yCError(FRAMETRANSFORMGETNWSYARP) << "error getting transform from interface";
136  return return_getAllTransforms(false, std::vector<yarp::math::FrameTransform>());;
137 
138 }
139 
141 {
142  if (!m_streaming_port.open(m_streaming_port_name))
143  {
144  yCError(FRAMETRANSFORMGETNWSYARP, "Could not open \"%s\" port", m_streaming_port_name.c_str());
145  return false;
146  }
147  return true;
148 }
149 
151 {
152  m_streaming_port.interrupt();
153  m_streaming_port.close();
154 }
155 
157 {
158  if (m_iFrameTransformStorageGet != nullptr)
159  {
160  std::vector<yarp::math::FrameTransform> localTransform;
161  if (m_iFrameTransformStorageGet->getTransforms(localTransform))
162  {
163  return_getAllTransforms rgt(true, localTransform);
164  m_streaming_port.write(rgt);
165  }
166  }
167 }
const yarp::os::LogComponent & FRAMETRANSFORMGETNWSYARP()
static bool rpc(const Contact &c, const char *carrier, Bottle &writer, Bottle &reader)
Definition: RosLookup.cpp:19
frameTransformGet_nws_yarp: A network wrapper client which converts the input retrieved from an IFram...
bool threadInit() override
Initialization method.
void run() override
Loop function.
bool detach() override
Detach the object (you must have first called attach).
return_getAllTransforms getTransformsRPC() override
void threadRelease() override
Release method.
bool attach(yarp::dev::PolyDriver *deviceToAttach) override
Attach to another object.
bool close() override
Close the DeviceDriver.
bool open(yarp::os::Searchable &params) override
Device driver interface.
bool view(T *&x)
Get an interface to the device driver.
Definition: DeviceDriver.h:74
virtual bool getTransforms(std::vector< yarp::math::FrameTransform > &transforms) const =0
Obtains all frame transforms saved in a storage.
A container for a device driver.
Definition: PolyDriver.h:24
static bool checkNetwork()
Check if the YARP Network is up and running.
Definition: Network.cpp:1377
bool setPeriod(double period)
Set the (new) period of the thread.
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...
bool write(const PortWriter &writer, const PortWriter *callback=nullptr) const override
Write an object to the port.
Definition: Port.cpp:427
void interrupt() override
Interrupt any current reads or writes attached to the port.
Definition: Port.cpp:374
void close() override
Stop port activity.
Definition: Port.cpp:354
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition: Port.cpp:79
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 std::string toString() const =0
Return a standard text representation of the content of the object.
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
yarp::os::WireLink & yarp()
Get YARP state associated with this object.
Definition: Wire.h:29
#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