YARP
Yet Another Robot Platform
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 <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(NAVIGATION2D_NWC_YARP, "yarp.device.navigation2D_nwc_yarp")
23}
24
25//------------------------------------------------------------------------------------------------------------------------------
26
28{
29 m_local_name.clear();
33
34 m_local_name = config.find("local").asString();
35 m_navigation_server_name = config.find("navigation_server").asString();
36 m_map_locations_server_name = config.find("map_locations_server").asString();
37 m_localization_server_name = config.find("localization_server").asString();
38
39 if (m_local_name == "")
40 {
41 yCError(NAVIGATION2D_NWC_YARP, "open() error you have to provide a valid 'local' param");
42 return false;
43 }
44
46 {
47 yCError(NAVIGATION2D_NWC_YARP, "open() error you have to provide a valid 'navigation_server' param");
48 return false;
49 }
50
52 {
53 yCError(NAVIGATION2D_NWC_YARP, "open() error you have to provide valid 'map_locations_server' param");
54 return false;
55 }
56
58 {
59 yCError(NAVIGATION2D_NWC_YARP, "open() error you have to provide valid 'localization_server' param");
60 return false;
61 }
62
63 if (config.check("period"))
64 {
65 m_period = config.find("period").asInt32();
66 }
67 else
68 {
69 m_period = 10;
70 yCWarning(NAVIGATION2D_NWC_YARP, "Using default period of %d ms" , m_period);
71 }
72
73 std::string
74 local_rpc_1,
75 local_rpc_2,
76 local_rpc_3,
77 remote_rpc_1,
78 remote_rpc_2,
79 remote_rpc_3,
80 remote_streaming_name,
81 local_streaming_name;
82
83 local_rpc_1 = m_local_name + "/navigation/rpc";
84 local_rpc_2 = m_local_name + "/locations/rpc";
85 local_rpc_3 = m_local_name + "/localization/rpc";
86 remote_rpc_1 = m_navigation_server_name + "/rpc";
87 remote_rpc_2 = m_map_locations_server_name + "/rpc";
88 remote_rpc_3 = m_localization_server_name + "/rpc";
89 remote_streaming_name = m_localization_server_name + "/stream:o";
90 local_streaming_name = m_local_name + "/stream:i";
91
92 if (!m_rpc_port_to_navigation_server.open(local_rpc_1))
93 {
94 yCError(NAVIGATION2D_NWC_YARP, "open() error could not open rpc port %s, check network", local_rpc_1.c_str());
95 return false;
96 }
97
98 if (!m_rpc_port_to_Map2DServer.open(local_rpc_2))
99 {
100 yCError(NAVIGATION2D_NWC_YARP, "open() error could not open rpc port %s, check network", local_rpc_2.c_str());
101 return false;
102 }
103
104 if (!m_rpc_port_to_localization_server.open(local_rpc_3))
105 {
106 yCError(NAVIGATION2D_NWC_YARP, "open() error could not open rpc port %s, check network", local_rpc_3.c_str());
107 return false;
108 }
109
110 bool ok = true;
111
112 ok = Network::connect(local_rpc_1, remote_rpc_1);
113 if (!ok)
114 {
115 yCError(NAVIGATION2D_NWC_YARP, "open() error could not connect to %s", remote_rpc_1.c_str());
116 return false;
117 }
118
119 ok = Network::connect(local_rpc_2, remote_rpc_2);
120 if (!ok)
121 {
122 yCError(NAVIGATION2D_NWC_YARP, "open() error could not connect to %s", remote_rpc_2.c_str());
123 return false;
124 }
125
126 ok = Network::connect(local_rpc_3, remote_rpc_3);
127 if (!ok)
128 {
129 yCError(NAVIGATION2D_NWC_YARP, "open() error could not connect to %s", remote_rpc_3.c_str());
130 return false;
131 }
132
134 {
135 yCError(NAVIGATION2D_NWC_YARP, "Error! Cannot attach the m_rpc_port_to_Map2DServer port as a client");
136 return false;
137 }
138
140 {
141 yCError(NAVIGATION2D_NWC_YARP, "Error! Cannot attach the m_rpc_port_localization_server port as a client");
142 return false;
143 }
144
146 {
147 yCError(NAVIGATION2D_NWC_YARP, "Error! Cannot attach the m_rpc_port_navigation_server port as a client");
148 return false;
149 }
150
151 return true;
152}
153
155{
159 return true;
160}
161
163{
164 yCError(NAVIGATION2D_NWC_YARP, "Should not enter here");
165 return true;
166}
167
168bool Navigation2D_nwc_yarp::checkNearToLocation(Map2DLocation loc, double linear_tolerance, double angular_tolerance)
169{
170 Map2DLocation curr_loc;
171 if (getCurrentPosition(curr_loc) == false)
172 {
173 yCError(NAVIGATION2D_NWC_YARP) << "checkInsideArea() unable to get robot position";
174 return false;
175 }
176
177 return curr_loc.is_near_to(loc, linear_tolerance, angular_tolerance);
178}
179
180bool Navigation2D_nwc_yarp::checkNearToLocation(std::string location_name, double linear_tolerance, double angular_tolerance)
181{
182 Map2DLocation loc;
183 Map2DLocation curr_loc;
184 if (this->getLocation(location_name, loc) == false)
185 {
186 yCError(NAVIGATION2D_NWC_YARP) << "Location" << location_name << "not found";
187 return false;
188 }
189
190 if (getCurrentPosition(curr_loc) == false)
191 {
192 yCError(NAVIGATION2D_NWC_YARP) << "checkInsideArea() unable to get robot position";
193 return false;
194 }
195
196 return curr_loc.is_near_to(loc, linear_tolerance, angular_tolerance);
197}
198
200{
201 Map2DLocation curr_loc;
202 if (getCurrentPosition(curr_loc) == false)
203 {
204 yCError(NAVIGATION2D_NWC_YARP) << "checkInsideArea() unable to get robot position";
205 return false;
206 }
207
208 if (area.checkLocationInsideArea(curr_loc) == false)
209 {
210 //yCDebug(NAVIGATION2D_NWC) << "Not inside Area";
211 return false;
212 }
213
214 return true;
215}
216
217bool Navigation2D_nwc_yarp::checkInsideArea(std::string area_name)
218{
219 Map2DLocation curr_loc;
220 Map2DArea area;
221 if (this->getArea(area_name, area) == false)
222 {
223 yCError(NAVIGATION2D_NWC_YARP) << "Area" << area_name << "not found";
224 return false;
225 }
226
227 if (getCurrentPosition(curr_loc) == false)
228 {
229 yCError(NAVIGATION2D_NWC_YARP) << "checkInsideArea() unable to get robot position";
230 return false;
231 }
232
233 if (area.checkLocationInsideArea(curr_loc) == false)
234 {
235 //yCDebug(NAVIGATION2D_NWC) << "Not inside Area";
236 return false;
237 }
238
239 return true;
240}
241
243{
244 Map2DLocation loc;
245 Map2DArea area;
246
247 //first of all, ask to the location server if location_name exists as a location_name...
248 bool found = this->getLocation(location_name, loc);
249
250 //...if found, ok...otherwise check if location_name is an area name instead...
251 if (found == false)
252 {
253 found = this->getArea(location_name, area);
254 if (found)
255 {
256 area.getRandomLocation(loc);
257 }
258 }
259
260 //...if it is neither a location, nor an area then quit...
261 if (found == false)
262 {
263 yCError(NAVIGATION2D_NWC_YARP) << "Location not found, stopping navigation";
265 return false;
266 }
267
268 //...otherwise we can go to the found/computed location!
269 std::lock_guard <std::mutex> lg(m_mutex);
271}
272
273
274bool Navigation2D_nwc_yarp::getNameOfCurrentTarget(std::string& location_name)
275{
276 std::lock_guard <std::mutex> lg(m_mutex);
278 if (!ret.ret)
279 {
280 yCError(NAVIGATION2D_NWC_YARP, "Unable to get_name_of_current_target_RPC");
281 return false;
282 }
283 location_name = ret.name;
284 return true;
285}
286
287bool Navigation2D_nwc_yarp::storeCurrentPosition(std::string location_name)
288{
289 Map2DLocation loc;
290 bool b = this->getCurrentPosition(loc);
291 if (!b) {return false;}
292 this->storeLocation(location_name,loc);
293 if (!b) {return false;}
294 return true;
295}
bool ret
virtual bool stop_navigation_RPC()
virtual return_get_name_of_current_target get_name_of_current_target_RPC()
virtual bool goto_target_by_absolute_location_and_set_name_RPC(const yarp::dev::Nav2D::Map2DLocation &loc, const std::string &name)
bool getLocation(std::string location_name, yarp::dev::Nav2D::Map2DLocation &loc) override
Retrieves a location specified by the user in the world reference frame.
INavigation2DMsgs m_nav_RPC
std::string m_map_locations_server_name
bool storeLocation(std::string location_name, yarp::dev::Nav2D::Map2DLocation loc) override
Store a location specified by the user in the world reference frame.
ILocalization2DMsgs m_loc_RPC
bool gotoTargetByLocationName(std::string location_name) override
Ask the robot to reach a previously stored location/area.
yarp::os::Port m_rpc_port_to_localization_server
bool checkInsideArea(yarp::dev::Nav2D::Map2DArea area) override
Check if the robot is currently inside the specified area.
bool close() override
Close the DeviceDriver.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
bool getNameOfCurrentTarget(std::string &location_name) override
Gets the name of the current target, if available (set by gotoTargetByLocationName)
bool getCurrentPosition(yarp::dev::Nav2D::Map2DLocation &loc) override
Gets the current position of the robot w.r.t world reference frame.
std::string m_localization_server_name
virtual bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
bool storeCurrentPosition(std::string location_name) override
Store the current location of the robot.
bool checkNearToLocation(yarp::dev::Nav2D::Map2DLocation loc, double linear_tolerance, double angular_tolerance=std::numeric_limits< double >::infinity()) override
Check if the robot is currently near to the specified area.
yarp::os::Port m_rpc_port_to_Map2DServer
bool getArea(std::string location_name, yarp::dev::Nav2D::Map2DArea &area) override
Retrieves an area.
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
Definition: Map2DArea.cpp:169
bool checkLocationInsideArea(yarp::dev::Nav2D::Map2DLocation loc)
Check if a Map2DLocation is inside a Map2DArea.
Definition: Map2DArea.cpp:87
An interface for reading from a network connection.
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:63
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
virtual std::int32_t asInt32() const
Get 32-bit integer value.
Definition: Value.cpp:204
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,...)
Definition: LogComponent.h:213
#define yCWarning(component,...)
Definition: LogComponent.h:192
#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.
bool is_near_to(const Map2DLocation &other_loc, double linear_tolerance, double angular_tolerance) const
Compares two Map2DLocations.