YARP
Yet Another Robot Platform
Map2D_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: LGPL-2.1-or-later
4 */
5
6#include "Map2D_nws_yarp.h"
7
8#include <yarp/os/Log.h>
10#include <yarp/os/LogStream.h>
11
13#include <yarp/dev/IMap2D.h>
15
16#include <yarp/math/Math.h>
17
18#include <cstdlib>
19#include <fstream>
20#include <limits>
21#include <mutex>
22#include <sstream>
23
24using namespace yarp::sig;
25using namespace yarp::dev;
26using namespace yarp::dev::Nav2D;
27using namespace yarp::os;
28
29namespace {
30YARP_LOG_COMPONENT(MAP2D_NWS_YARP, "yarp.device.map2D_nws_yarp")
31}
32
38{
39}
40
42
43bool Map2D_nws_yarp::read(yarp::os::ConnectionReader& connection)
44{
45 bool b = m_RPC.read(connection);
46 if (b)
47 {
48 return true;
49 }
50 yCDebug(MAP2D_NWS_YARP) << "read() Command failed";
51 return false;
52}
53
55{
56 Property params;
57 params.fromString(config.toString());
58
59 if (!config.check("name"))
60 {
61 m_rpcPortName = "/map2D_nws_yarp/rpc";
62 }
63 else
64 {
65 m_rpcPortName = config.find("name").asString();
66 }
67
68 //open rpc port
69 if (!m_rpcPort.open(m_rpcPortName))
70 {
71 yCError(MAP2D_NWS_YARP, "Failed to open port %s", m_rpcPortName.c_str());
72 return false;
73 }
74 m_rpcPort.setReader(*this);
75
76 //subdevice handling
77 if (config.check("subdevice"))
78 {
79 Property p;
80 p.fromString(config.toString(), false);
81 p.put("device", config.find("subdevice").asString());
82
83 if (!m_drv.open(p) || !m_drv.isValid())
84 {
85 yCError(MAP2D_NWS_YARP) << "Failed to open subdevice.. check params";
86 return false;
87 }
88
89 if (!attach(&m_drv))
90 {
91 yCError(MAP2D_NWS_YARP) << "Failed to open subdevice.. check params";
92 return false;
93 }
94 }
95 else
96 {
97 yCInfo(MAP2D_NWS_YARP) << "Waiting for device to attach";
98 }
99
100 return true;
101}
102
104{
105 yCTrace(MAP2D_NWS_YARP, "Close");
106 return true;
107}
108
110{
111 m_iMap2D = nullptr;
112 return true;
113}
114
116{
117 if (driver->isValid())
118 {
119 driver->view(m_iMap2D);
120 m_RPC.setInterface(m_iMap2D);
121 }
122
123 if (nullptr == m_iMap2D)
124 {
125 yCError(MAP2D_NWS_YARP, "Subdevice passed to attach method is invalid");
126 return false;
127 }
128
129 return true;
130}
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
void setInterface(yarp::dev::Nav2D::IMap2D *_imap)
Map2D_nws_yarp()
Map2D_nws_yarp.
bool detach() override
Detach the object (you must have first called attach).
bool attach(yarp::dev::PolyDriver *driver) override
Attach to another object.
bool open(yarp::os::Searchable &params) override
Open the DeviceDriver.
bool close() override
Close the DeviceDriver.
bool view(T *&x)
Get an interface to the device driver.
Definition: DeviceDriver.h:88
A container for a device driver.
Definition: PolyDriver.h:23
bool isValid() const
Check if device is valid.
Definition: PolyDriver.cpp:196
bool open(const std::string &txt)
Construct and configure a device by its common name.
Definition: PolyDriver.cpp:140
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
void setReader(PortReader &reader) override
Set an external reader for port data.
An interface for reading from a network connection.
A class for storing options and configuration information.
Definition: Property.h:33
void fromString(const std::string &txt, bool wipe=true)
Interprets a string as a list of properties.
Definition: Property.cpp:1063
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
Definition: Property.cpp:1015
A base class for nested structures that can be searched.
Definition: Searchable.h:63
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.
virtual std::string asString() const
Get string value.
Definition: Value.cpp:234
#define yCInfo(component,...)
Definition: LogComponent.h:171
#define yCError(component,...)
Definition: LogComponent.h:213
#define yCTrace(component,...)
Definition: LogComponent.h:84
#define yCDebug(component,...)
Definition: LogComponent.h:128
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:76
For streams capable of holding different kinds of content, check what they actually have.
An interface to the operating system, including Port based communication.