YARP
Yet Another Robot Platform
FrameTransformSet_nwc_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/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(FRAMETRANSFORMSETNWCYARP, "yarp.device.frameTransformSet_nwc_yarp")
19 }
20 
21 //------------------------------------------------------------------------------------------------------------------------------
22 
24 {
25 
27  yCError(FRAMETRANSFORMSETNWCYARP,"Error! YARP Network is not initialized");
28  return false;
29  }
30  std::string prefix;
31  //checking default config params
32  bool default_config = true;
33  if(config.check("default-client")) {
34  default_config = config.find("default-client").asString() == "true";
35  }
36  bool default_server = true;
37  if(config.check("default-server")) {
38  default_server = config.find("default-server").asString() == "true";
39  }
40  // client port configuration
41  if (config.check("nwc_thrift_port_prefix")){
42  prefix = config.find("nwc_thrift_port_prefix").asString() + (default_config ? m_defaultConfigPrefix : "");
43  if(prefix[0] != '/') {prefix = "/"+prefix;}
44  m_thriftPortName = prefix + "/" + m_deviceName + "/thrift";
45  }
46  else {
47  prefix = default_config ? m_defaultConfigPrefix : "";
48  m_thriftPortName = prefix + "/" + m_deviceName + "/thrift";
49  yCWarning(FRAMETRANSFORMSETNWCYARP) << "no nwc_thrift_port_prefix param found. The resulting port name will be: " << m_thriftPortName;
50  }
51 
52  //server port configuration
53  if (config.check("nws_thrift_port_prefix")){
54  prefix = config.find("nws_thrift_port_prefix").asString() + (default_server ? m_defaultServerPrefix : "");
55  if(prefix[0] != '/') {prefix = "/"+prefix;}
56  m_thrift_server_rpcPort_Name = prefix + "/thrift";
57  }
58  else {
59  prefix = default_server ? m_defaultServerPrefix : "";
60  m_thrift_server_rpcPort_Name = prefix + "/thrift";
61  yCWarning(FRAMETRANSFORMSETNWCYARP) << "no nws_thrift_port_prefix param found. The resulting port name will be: " << m_thrift_server_rpcPort_Name;
62  }
63 
64  // rpc inizialisation
65  if(!m_thriftPort.open(m_thriftPortName))
66  {
67  yCError(FRAMETRANSFORMSETNWCYARP,"Could not open \"%s\" port",m_thriftPortName.c_str());
68  return false;
69  }
70  // connect to server
71  if (!yarp::os::NetworkBase::connect(m_thriftPortName,m_thrift_server_rpcPort_Name))
72  {
73  yCError(FRAMETRANSFORMSETNWCYARP,"Could not connect \"%s\" to \"%s\" port",m_thriftPortName.c_str(), m_thrift_server_rpcPort_Name.c_str());
74  return false;
75  }
76  if (!m_setRPC.yarp().attachAsClient(m_thriftPort))
77  {
78  yCError(FRAMETRANSFORMSETNWCYARP, "Error! Cannot attach the port as a client");
79  return false;
80  }
81 
82  return true;
83 }
84 
86 {
87  if(m_thriftPort.isOpen())
88  {
89  m_thriftPort.close();
90  }
91  return true;
92 }
93 
95 {
96  std::lock_guard <std::mutex> lg(m_pd_mutex);
97  if(!m_setRPC.setTransformRPC(transform))
98  {
99  yCError(FRAMETRANSFORMSETNWCYARP, "Unable to set transformation");
100  return false;
101  }
102  return true;
103 }
104 
105 bool FrameTransformSet_nwc_yarp::setTransforms(const std::vector<yarp::math::FrameTransform>& transforms)
106 {
107  std::lock_guard <std::mutex> lg(m_pd_mutex);
108  if(!m_setRPC.setTransformsRPC(transforms))
109  {
110  yCError(FRAMETRANSFORMSETNWCYARP, "Unable to set transformations");
111  return false;
112  }
113  return true;
114 }
115 
116 bool FrameTransformSet_nwc_yarp::deleteTransform(std::string t1, std::string t2)
117 {
118  std::lock_guard <std::mutex> lg(m_pd_mutex);
119  if (!m_setRPC.deleteTransformRPC(t1,t2))
120  {
121  yCError(FRAMETRANSFORMSETNWCYARP, "Unable to delete transformation");
122  return false;
123  }
124  return true;
125 }
126 
128 {
129  std::lock_guard <std::mutex> lg(m_pd_mutex);
130  if (!m_setRPC.clearAllRPC())
131  {
132  yCError(FRAMETRANSFORMSETNWCYARP, "Unable to clear all transformations");
133  return false;
134  }
135  return true;
136 }
bool setTransforms(const std::vector< yarp::math::FrameTransform > &transforms) override
Save some frame transforms in a storage.
bool clearAll() override
Delete all transforms in a storage.
bool deleteTransform(std::string t1, std::string t2) override
Delete a single transform in the storage.
bool setTransform(const yarp::math::FrameTransform &transform) override
Save a frame transform in a storage.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
bool close() override
Close the DeviceDriver.
static bool connect(const std::string &src, const std::string &dest, const std::string &carrier="", bool quiet=true)
Request that an output port connect to an input port.
Definition: Network.cpp:682
static bool checkNetwork()
Check if the YARP Network is up and running.
Definition: Network.cpp:1377
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 asString() const
Get string value.
Definition: Value.cpp:234
#define yCError(component,...)
Definition: LogComponent.h:154
#define yCWarning(component,...)
Definition: LogComponent.h:143
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:77
An interface for the device drivers.
An interface to the operating system, including Port based communication.
Signal processing.
Definition: Image.h:22