YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
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
9#include <yarp/os/LogStream.h>
11
12using namespace yarp::dev;
13
14YARP_LOG_COMPONENT(FRAMETRANSFORMGETNWSYARP, "yarp.devices.FrameTransformGet_nws_yarp")
15
17PeriodicThread (0.010),
18m_thrift_rpcPort_Name("/frameTransformGet/rpc")
19{
20}
21
22
25{
27 yCError(FRAMETRANSFORMGETNWSYARP,"Error! YARP Network is not initialized");
28 return false;
29 }
30 std::string prefix;
31 //checking default config param
32 bool default_config = true;
33 if(config.check("default-config")) {
34 default_config = config.find("default-config").asString() == "true";
35 }
36 // configuration
37 if (config.check("nws_thrift_port_prefix")){
38 prefix = config.find("nws_thrift_port_prefix").asString() + (default_config ? m_defaultConfigPrefix : "");
39 if(prefix[0] != '/') {prefix = "/"+prefix;}
40 m_thrift_rpcPort_Name = prefix + "/" + m_deviceName + "/thrift";
41 }
42 else {
43 prefix = default_config ? m_defaultConfigPrefix : "";
44 m_thrift_rpcPort_Name = prefix + "/" + m_deviceName + "/thrift";
45 yCWarning(FRAMETRANSFORMGETNWSYARP) << "no nws_thrift_port_prefix param found. The resulting port name will be: " << m_thrift_rpcPort_Name;
46 }
47 if(config.check("streaming_enabled")) {
48 m_streaming_port_enabled = config.find("streaming_enabled").asString() == "true";
49 }
50 if (config.check("period")) {
51 double period = config.find("period").asFloat64();
52 this->setPeriod(period);
53 }
54
55 // rpc initialization
56 if(!m_thrift_rpcPort.open(m_thrift_rpcPort_Name))
57 {
58 yCError(FRAMETRANSFORMGETNWSYARP,"Could not open \"%s\" port",m_thrift_rpcPort_Name.c_str());
59 return false;
60 }
61 if(!this->yarp().attachAsServer(m_thrift_rpcPort))
62 {
63 yCError(FRAMETRANSFORMGETNWSYARP,"Error! Cannot attach the port as a server");
64 return false;
65 }
66 if(m_verbose <= 2) {
67 yCTrace(FRAMETRANSFORMGETNWSYARP) << "\nParameters are: \n" << config.toString();
68 }
69
70 if (m_streaming_port_enabled)
71 {
72 if (config.check("output_streaming_port_prefix")){
73 prefix = config.find("output_streaming_port_prefix").asString() + (default_config ? m_defaultConfigPrefix : "");
74 if(prefix[0] != '/') {prefix = "/"+prefix;}
75 m_streaming_port_name = prefix + "/" + m_deviceName + "/tf:o";
76 }
77 else {
78 prefix = default_config ? m_defaultConfigPrefix : "";
79 m_streaming_port_name = prefix + "/" + m_deviceName + "/tf:o";
80 yCWarning(FRAMETRANSFORMGETNWSYARP) << "no output_streaming_port_prefix param found. The resulting port name will be: " << m_streaming_port_name;
81 }
82 yCInfo(FRAMETRANSFORMGETNWSYARP) << "Streaming transforms on Yarp port enabled";
83 if (!m_streaming_port.open(m_streaming_port_name))
84 {
85 yCError(FRAMETRANSFORMGETNWSYARP, "Could not open \"%s\" port", m_streaming_port_name.c_str());
86 return false;
87 }
88
89 this->start();
90 }
91 else
92 {
93 yCInfo(FRAMETRANSFORMGETNWSYARP) << "Streaming transforms on Yarp port NOT enabled";
94 }
95
96 return true;
97}
98
103
105{
106 // Stop the thread
107 if (this->isRunning())
108 {
109 this->stop();
110 }
111
112 // Detaching
113 detach();
114
115 // Closing port
116 m_thrift_rpcPort.interrupt();
117 m_thrift_rpcPort.close();
118 m_streaming_port.interrupt();
119 m_streaming_port.close();
120 return true;
121}
122
123
125{
126 std::lock_guard<std::mutex> m_lock(m_mutex);
127 m_iFrameTransformStorageGet = nullptr;
128 return true;
129}
130
131
133{
134 std::lock_guard<std::mutex> m_lock(m_mutex);
135 deviceToAttach->view(m_iFrameTransformStorageGet);
136
137 if ( m_iFrameTransformStorageGet==nullptr){
138 yCError(FRAMETRANSFORMGETNWSYARP) << "could not attach to the device";
139 return false;
140 }
141 return true;
142}
143
144
146{
147 std::lock_guard<std::mutex> m_lock(m_mutex);
148
150 if (m_iFrameTransformStorageGet != nullptr)
151 {
152 std::vector<yarp::math::FrameTransform> localTransform;
153 if (m_iFrameTransformStorageGet->getTransforms(localTransform))
154 {
155 ret.transforms_list = localTransform;
156 ret.retvalue = ReturnValue_ok;
157 return ret;
158 }
159 }
160 yCError(FRAMETRANSFORMGETNWSYARP) << "error getting transform from interface";
161 ret.transforms_list = std::vector<yarp::math::FrameTransform>();
162 ret.retvalue = ReturnValue::return_code::return_value_error_method_failed;
163 return ret;
164
165}
166
168{
169 std::lock_guard<std::mutex> m_lock(m_mutex);
170
171 if (m_iFrameTransformStorageGet != nullptr)
172 {
173 std::vector<yarp::math::FrameTransform> localTransform;
174 if (m_iFrameTransformStorageGet->getTransforms(localTransform))
175 {
178 rgt.transforms_list = localTransform;
179 m_streaming_port.write(rgt);
180 }
181 }
182}
const yarp::os::LogComponent & FRAMETRANSFORMGETNWSYARP()
bool ret
#define ReturnValue_ok
Definition ReturnValue.h:77
frameTransformGet_nws_yarp: A network wrapper client which converts the input retrieved from an IFram...
void run() override
Loop function.
bool detach() override
Detach the object (you must have first called attach).
return_getAllTransforms getTransformsRPC() override
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.
std::vector< yarp::math::FrameTransform > transforms_list
yarp::dev::ReturnValue retvalue
bool view(T *&x)
Get an interface to the device driver.
virtual yarp::dev::ReturnValue 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:23
static bool checkNetwork()
Check if the YARP Network is up and running.
Definition Network.cpp:1370
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...
bool write(const PortWriter &writer, const PortWriter *callback=nullptr) const override
Write an object to the port.
Definition Port.cpp:436
void interrupt() override
Interrupt any current reads or writes attached to the port.
Definition Port.cpp:383
void close() override
Stop port activity.
Definition Port.cpp:363
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:31
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 Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
yarp::os::WireLink & yarp()
Get YARP state associated with this object.
Definition Wire.h:28
#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.