YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
Navigation2D_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: LGPL-2.1-or-later
4 */
5
8#include <yarp/os/Log.h>
10#include <yarp/os/LogStream.h>
11#include "yarp/dev/Map2DPath.h"
12#include <mutex>
13#include <cmath>
14
17using namespace yarp::dev;
18using namespace yarp::dev::Nav2D;
19using namespace yarp::os;
20using namespace yarp::sig;
21
22namespace {
23YARP_LOG_COMPONENT(NAVIGATION2D_NWC_YARP, "yarp.device.navigation2D_nwc_yarp")
24}
25
26//------------------------------------------------------------------------------------------------------------------------------
27
29{
30 if (!parseParams(config)) { return false; }
31
32 std::string local_name = m_local;
36
37 std::string
47
48 local_rpc_1 = local_name + "/navigation/rpc";
49 local_rpc_2 = local_name + "/locations/rpc";
50 local_rpc_3 = local_name + "/localization/rpc";
55 local_streaming_name = local_name + "/stream:i";
56 rpc_port_user_commandsName = local_name + "/user_commands/rpc";
57
59 {
60 yCError(NAVIGATION2D_NWC_YARP, "open() error could not open rpc port %s, check network", local_rpc_1.c_str());
61 return false;
62 }
63
65 {
66 yCError(NAVIGATION2D_NWC_YARP, "open() error could not open rpc port %s, check network", local_rpc_2.c_str());
67 return false;
68 }
69
71 {
72 yCError(NAVIGATION2D_NWC_YARP, "open() error could not open rpc port %s, check network", local_rpc_3.c_str());
73 return false;
74 }
75
76 bool ok = true;
77
78 ok = Network::connect(local_rpc_1, remote_rpc_1);
79 if (!ok)
80 {
81 yCError(NAVIGATION2D_NWC_YARP, "open() error could not connect to %s", remote_rpc_1.c_str());
82 return false;
83 }
84
85 ok = Network::connect(local_rpc_2, remote_rpc_2);
86 if (!ok)
87 {
88 yCError(NAVIGATION2D_NWC_YARP, "open() error could not connect to %s", remote_rpc_2.c_str());
89 return false;
90 }
91
92 ok = Network::connect(local_rpc_3, remote_rpc_3);
93 if (!ok)
94 {
95 yCError(NAVIGATION2D_NWC_YARP, "open() error could not connect to %s", remote_rpc_3.c_str());
96 return false;
97 }
98
100 {
101 yCError(NAVIGATION2D_NWC_YARP, "Error! Cannot attach the m_rpc_port_to_Map2DServer port as a client");
102 return false;
103 }
104
106 {
107 yCError(NAVIGATION2D_NWC_YARP, "Error! Cannot attach the m_rpc_port_localization_server port as a client");
108 return false;
109 }
110
112 {
113 yCError(NAVIGATION2D_NWC_YARP, "Error! Cannot attach the m_rpc_port_navigation_server port as a client");
114 return false;
115 }
116
118 {
119 yCError(NAVIGATION2D_NWC_YARP, "Failed to open port %s", rpc_port_user_commandsName.c_str());
120 return false;
121 }
122
124 return true;
125}
126
135
137{
138 yarp::os::Bottle command;
139 yarp::os::Bottle reply;
140 bool ok = command.read(connection);
141 if (!ok) {
142 return false;
143 }
144 reply.clear();
145
146 if (command.get(0).asString() == "gotoLocation")
147 {
148 std::string loc= command.get(1).asString();
149 this->gotoTargetByLocationName(loc);
150 reply.addVocab32(VOCAB_OK);
151 }
152 else if (command.get(0).asString() == "listLocations")
153 {
154 std::vector<std::string> locations;
155 this->getLocationsList(locations);
156 reply.addVocab32("many");
157 for (auto it=locations.begin(); it!=locations.end(); it++)
158 {
159 std::string loc = *it;
160 reply.addString(loc);
161 }
162 }
163 else if (command.get(0).asString() == "help")
164 {
165 reply.addVocab32("many");
166 reply.addString("gotoLocation <locationName>");
167 reply.addString("listLocations");
168 }
169 else
170 {
171 yCError(NAVIGATION2D_NWC_YARP) << "Invalid command";
172 reply.addVocab32(VOCAB_ERR);
173 }
174
176 if (returnToSender != nullptr)
177 {
178 reply.write(*returnToSender);
179 }
180 return true;
181}
182
184{
187 if (!ret)
188 {
189 yCError(NAVIGATION2D_NWC_YARP) << "checkInsideArea() unable to get robot position";
190 return ret;
191 }
192
194
195 return ReturnValue_ok;
196}
197
199{
200 Map2DLocation loc;
202 auto ret1 = getLocation(location_name, loc);
203 if (!ret1)
204 {
205 yCError(NAVIGATION2D_NWC_YARP) << "Location" << location_name << "not found";
206 return ret1;
207 }
208
210 if (!ret2)
211 {
212 yCError(NAVIGATION2D_NWC_YARP) << "checkInsideArea() unable to get robot position";
213 return ret2;
214 }
215
217
218 return ReturnValue_ok;
219}
220
222{
225 if (!ret1)
226 {
227 yCError(NAVIGATION2D_NWC_YARP) << "checkInsideArea() unable to get robot position";
228 return ret1;
229 }
230
232
233 return ReturnValue_ok;
234}
235
237{
239 Map2DArea area;
240 auto ret1 = getArea(area_name, area);
241 if (!ret1)
242 {
243 yCError(NAVIGATION2D_NWC_YARP) << "Area" << area_name << "not found";
244 return ret1;
245 }
246
248 if (!ret2)
249 {
250 yCError(NAVIGATION2D_NWC_YARP) << "checkInsideArea() unable to get robot position";
251 return ret2;
252 }
253
255
256 return ReturnValue_ok;
257}
258
260{
261 Map2DLocation loc;
262 Map2DArea area;
263 Map2DPath path;
264
265 //first of all, ask to the location server if location_name exists as a location_name...
266 bool found = this->getLocation(location_name, loc);
267
268 //...if found, ok...otherwise check if location_name is an area name instead...
269 if (found == false)
270 {
271 found = this->getArea(location_name, area);
272 if (found)
273 {
274 area.getRandomLocation(loc);
275 }
276 }
277
278 //...if it is neither a location, nor an area then check if it is a path...
279 if (found == false)
280 {
281 if(this->getPath(location_name, path))
282 {
283 // We still need to handle path navigation differently
284 std::lock_guard <std::mutex> lg(m_mutex);
285 return m_nav_RPC.follow_path_RPC(path);
286 }
287 }
288
289 //...if it is neither a location, nor an area, nor a path then quit...
290 if (found == false)
291 {
292 yCError(NAVIGATION2D_NWC_YARP) << "Location not found, stopping navigation";
294 return ReturnValue::return_code::return_value_error_method_failed;
295 }
296
297 //...otherwise we can go to the found/computed location!
298 std::lock_guard <std::mutex> lg(m_mutex);
300}
301
302
304{
305 std::lock_guard <std::mutex> lg(m_mutex);
307 if (!ret.ret)
308 {
309 yCError(NAVIGATION2D_NWC_YARP, "Unable to get_name_of_current_target_RPC");
310 return ret.ret;
311 }
312 location_name = ret.name;
313 return ret.ret;
314}
315
317{
318 Map2DLocation loc;
319 auto ret = this->getCurrentPosition(loc);
320 if (!ret) {return ret;}
321 auto ret2 = this->storeLocation(location_name,loc);
322 if (!ret2) {return ret2;}
323 return ReturnValue_ok;
324}
constexpr yarp::conf::vocab32_t VOCAB_OK
constexpr yarp::conf::vocab32_t VOCAB_ERR
bool ret
contains the definition of a Map2DPath type
#define ReturnValue_ok
Definition ReturnValue.h:77
virtual yarp::dev::ReturnValue goto_target_by_absolute_location_and_set_name_RPC(const yarp::dev::Nav2D::Map2DLocation &loc, const std::string &name)
virtual yarp::dev::ReturnValue follow_path_RPC(const yarp::dev::Nav2D::Map2DPath &path)
virtual yarp::dev::ReturnValue stop_navigation_RPC()
virtual return_get_name_of_current_target get_name_of_current_target_RPC()
bool parseParams(const yarp::os::Searchable &config) override
Parse the DeviceDriver parameters.
yarp::os::Port m_rpc_port_user_commands
yarp::dev::ReturnValue storeCurrentPosition(std::string location_name) override
Store the current location of the robot.
yarp::dev::ReturnValue storeLocation(std::string location_name, yarp::dev::Nav2D::Map2DLocation loc) override
Store a location specified by the user in the world reference frame.
yarp::dev::ReturnValue getLocation(std::string location_name, yarp::dev::Nav2D::Map2DLocation &loc) override
Retrieves a location specified by the user in the world reference frame.
ILocalization2DMsgs m_loc_RPC
yarp::dev::ReturnValue getNameOfCurrentTarget(std::string &location_name) override
Gets the name of the current target, if available (set by gotoTargetByLocationName)
yarp::dev::ReturnValue getPath(std::string path_name, yarp::dev::Nav2D::Map2DPath &path) override
Retrieves a path.
yarp::os::Port m_rpc_port_to_localization_server
yarp::dev::ReturnValue gotoTargetByLocationName(std::string location_name) override
Ask the robot to reach a previously stored location/area.
yarp::dev::ReturnValue getArea(std::string location_name, yarp::dev::Nav2D::Map2DArea &area) override
Retrieves an area.
yarp::dev::ReturnValue getLocationsList(std::vector< std::string > &locations) override
Get a list of the names of all stored locations.
yarp::dev::ReturnValue checkNearToLocation(yarp::dev::Nav2D::Map2DLocation loc, bool &is_near, double linear_tolerance, double angular_tolerance=std::numeric_limits< double >::infinity()) override
Check if the robot is currently near to the specified area.
bool close() override
Close the DeviceDriver.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
yarp::dev::ReturnValue getCurrentPosition(yarp::dev::Nav2D::Map2DLocation &loc) override
Gets the current position of the robot w.r.t world reference frame.
yarp::dev::ReturnValue checkInsideArea(yarp::dev::Nav2D::Map2DArea area, bool &is_inside) override
Check if the robot is currently inside the specified area.
virtual bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
yarp::os::Port m_rpc_port_to_Map2DServer
yarp::os::Port m_rpc_port_to_navigation_server
bool getRandomLocation(yarp::dev::Nav2D::Map2DLocation &loc)
get a random Map2DLocation inside the Map2DArea @loc the computed Map2DLocation
bool checkLocationInsideArea(yarp::dev::Nav2D::Map2DLocation loc)
Check if a Map2DLocation is inside a Map2DArea.
Definition Map2DArea.cpp:87
A simple collection of objects that can be described and transmitted in a portable way.
Definition Bottle.h:64
void addVocab32(yarp::conf::vocab32_t x)
Places a vocabulary item in the bottle, at the end of the list.
Definition Bottle.cpp:164
bool read(ConnectionReader &reader) override
Set the bottle's value based on input from a network connection.
Definition Bottle.cpp:240
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition Bottle.cpp:246
void clear()
Empties the bottle of any objects it contains.
Definition Bottle.cpp:121
bool write(ConnectionWriter &writer) const override
Output a representation of the bottle to a network connection.
Definition Bottle.cpp:230
void addString(const char *str)
Places a string in the bottle, at the end of the list.
Definition Bottle.cpp:170
A mini-server for performing network communication in the background.
An interface for reading from a network connection.
virtual ConnectionWriter * getWriter()=0
Gets a way to reply to the message, if possible.
An interface for writing to a network connection.
void setReader(PortReader &reader) override
Set an external reader for port data.
Definition Port.cpp:511
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 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 yCError(component,...)
#define YARP_LOG_COMPONENT(name,...)
For streams capable of holding different kinds of content, check what they actually have.
An interface to the operating system, including Port based communication.