YARP
Yet Another Robot Platform
MobileBaseVelocityControl_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
8#include <yarp/os/Log.h>
10#include <yarp/os/LogStream.h>
11#include <mutex>
12#include <cmath>
13
16using namespace yarp::dev;
17using namespace yarp::dev::Nav2D;
18using namespace yarp::os;
19using namespace yarp::sig;
20
21namespace {
22YARP_LOG_COMPONENT(MOBVEL_NWS_YARP, "yarp.device.MobileBaseVelocityControl_nws_yarp")
23}
24
25//------------------------------------------------------------------------------------------------------------------------------
26
28{
29 if (m_iVel)
30 {
32 }
33 else
34 {
35 yCError(MOBVEL_NWS_YARP, "received data on streaming port, but no device attached yet");
36 }
37}
38
39
40//------------------------------------------------------------------------------------------------------------------------------
41
43{
44 //param configuration
45 m_local_name = config.find("local").asString();
46
47 if (m_local_name.empty())
48 {
49 yCError(MOBVEL_NWS_YARP, "open() error you have to provide a valid 'local' param");
50 return false;
51 }
52
53 //rpc block
54 {
55 std::string local_rpc_1;
56 local_rpc_1 = m_local_name + ":rpc";
57
58 if (!m_rpc_port_navigation_server.open(local_rpc_1))
59 {
60 yCError(MOBVEL_NWS_YARP, "open() error could not open rpc port %s, check network", local_rpc_1.c_str());
61 return false;
62 }
63
64 if (!this->yarp().attachAsServer(m_rpc_port_navigation_server))
65 {
66 yCError(MOBVEL_NWS_YARP, "Error! Cannot attach the port as a server");
67 return false;
68 }
69 }
70
71 //streaming input block
72 {
73 std::string local_stream_1;
74 local_stream_1 = m_local_name + ":i";
75
76 if (!m_StreamingInput.open(local_stream_1))
77 {
78 yCError(MOBVEL_NWS_YARP, "open() error could not open port %s, check network", local_stream_1.c_str());
79 return false;
80 }
83 }
84
85 //attach subdevice if required
86 if (config.check("subdevice"))
87 {
88 Property p;
89 p.fromString(config.toString(), false);
90 p.put("device", config.find("subdevice").asString());
91
92 if (!m_subdev.open(p) || !m_subdev.isValid())
93 {
94 yCError(MOBVEL_NWS_YARP) << "Failed to open subdevice.. check params";
95 return false;
96 }
97
98 if (!attach(&m_subdev))
99 {
100 yCError(MOBVEL_NWS_YARP) << "Failed to attach subdevice.. check params";
101 return false;
102 }
103 }
104 else
105 {
106 yCInfo(MOBVEL_NWS_YARP) << "Waiting for device to attach";
107 }
108
109 return true;
110}
111
113{
116 if (m_subdev.isValid()) { m_subdev.close(); }
117 return true;
118}
119
121{
122 m_iNavVel = nullptr;
123 return true;
124}
125
127{
128 if (driver->isValid())
129 {
130 driver->view(m_iNavVel);
131 }
132
133 if (nullptr == m_iNavVel)
134 {
135 yCError(MOBVEL_NWS_YARP, "Subdevice passed to attach method is invalid");
136 return false;
137 }
139
140 return true;
141}
142
143bool MobileBaseVelocityControl_nws_yarp::applyVelocityCommandRPC(const double x_vel, const double y_vel, const double theta_vel, const double timeout)
144{
145 std::lock_guard <std::mutex> lg(m_mutex);
146 if (nullptr == m_iNavVel)
147 {
148 yCError(MOBVEL_NWS_YARP, "Unable to applyVelocityCommandRPC");
149 return false;
150 }
151
152 bool b =m_iNavVel->applyVelocityCommand(x_vel, y_vel, theta_vel, timeout);
153
154 if (!b)
155 {
156 yCError(MOBVEL_NWS_YARP, "Unable to applyVelocityCommandRPC");
157 return false;
158 }
159 return true;
160}
161
163{
164 return_getLastVelocityCommand retrievedFromRPC;
165 std::lock_guard <std::mutex> lg(m_mutex);
166 if (nullptr == m_iNavVel)
167 {
168 yCError(MOBVEL_NWS_YARP, "Unable to getLastVelocityCommand");
169 retrievedFromRPC.retvalue = false;
170 return retrievedFromRPC;
171 }
172
173 double x_vel = 0;
174 double y_vel = 0;
175 double t_vel = 0;
176 bool b = m_iNavVel->getLastVelocityCommand(x_vel, y_vel, t_vel);
177
178 retrievedFromRPC.retvalue = b;
179 retrievedFromRPC.x_vel = x_vel;
180 retrievedFromRPC.y_vel = x_vel;
181 retrievedFromRPC.theta_vel = t_vel;
182 return retrievedFromRPC;
183}
bool detach() override
Detach the object (you must have first called attach).
bool close() override
Close the DeviceDriver.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
bool attach(yarp::dev::PolyDriver *driver) override
Attach to another object.
return_getLastVelocityCommand getLastVelocityCommandRPC() override
yarp::dev::Nav2D::INavigation2DVelocityActions * m_iNavVel
bool applyVelocityCommandRPC(const double x_vel, const double y_vel, const double theta_vel, const double timeout) override
void onRead(yarp::dev::MobileBaseVelocity &v) override
yarp::dev::Nav2D::INavigation2DVelocityActions * m_iVel
bool view(T *&x)
Get an interface to the device driver.
Definition: DeviceDriver.h:88
double vel_theta
angular velocity of the robot [deg/s]
double vel_y
velocity of the robot [m/s]
double vel_x
velocity of the robot [m/s]
virtual bool applyVelocityCommand(double x_vel, double y_vel, double theta_vel, double timeout=0.1)=0
Apply a velocity command.
virtual bool getLastVelocityCommand(double &x_vel, double &y_vel, double &theta_vel)=0
Returns the last applied velocity command.
A container for a device driver.
Definition: PolyDriver.h:23
bool close() override
Close the DeviceDriver.
Definition: PolyDriver.cpp:173
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
void close() override
Stop port activity.
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
void useCallback(TypedReaderCallback< T > &callback) override
Set an object whose onRead method will be called when data is available.
void setStrict(bool strict=true) override
Call this to strictly keep all messages, or allow old ones to be quietly dropped.
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 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
yarp::os::WireLink & yarp()
Get YARP state associated with this object.
Definition: Wire.h:28
#define yCInfo(component,...)
Definition: LogComponent.h:171
#define yCError(component,...)
Definition: LogComponent.h:213
#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.