YARP
Yet Another Robot Platform
Localization2DClient.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include "Localization2DClient.h"
21 #include <yarp/os/Log.h>
22 #include <yarp/os/LogComponent.h>
23 #include <yarp/os/LogStream.h>
24 #include <mutex>
25 
28 using namespace yarp::dev;
29 using namespace yarp::dev::Nav2D;
30 using namespace yarp::os;
31 using namespace yarp::sig;
32 
33 namespace {
34 YARP_LOG_COMPONENT(LOCALIZATION2DCLIENT, "yarp.device.localization2DClient")
35 }
36 
37 //------------------------------------------------------------------------------------------------------------------------------
38 
40 {
41  m_local_name.clear();
42  m_remote_name.clear();
43 
44  m_local_name = config.find("local").asString();
45  m_remote_name = config.find("remote").asString();
46 
47  if (m_local_name == "")
48  {
49  yCError(LOCALIZATION2DCLIENT, "open() error you have to provide a valid 'local' param");
50  return false;
51  }
52 
53  if (m_remote_name == "")
54  {
55  yCError(LOCALIZATION2DCLIENT, "open() error you have to provide valid 'remote' param");
56  return false;
57  }
58 
59  std::string
60  local_rpc,
61  remote_rpc,
62  remote_streaming_name,
63  local_streaming_name;
64 
65  local_rpc = m_local_name + "/localization/rpc";
66  remote_rpc = m_remote_name + "/rpc";
67  remote_streaming_name = m_remote_name + "/stream:o";
68  local_streaming_name = m_local_name + "/stream:i";
69 
70  if (!m_rpc_port_localization_server.open(local_rpc))
71  {
72  yCError(LOCALIZATION2DCLIENT, "open() error could not open rpc port %s, check network", local_rpc.c_str());
73  return false;
74  }
75 
76  /*
77  //currently unused
78  bool ok=Network::connect(remote_streaming_name.c_str(), local_streaming_name.c_str(), "tcp");
79  if (!ok)
80  {
81  yCError(LOCALIZATION2DCLIENT, "open() error could not connect to %s", remote_streaming_name.c_str());
82  return false;
83  }*/
84 
85  bool ok = true;
86 
87  ok = Network::connect(local_rpc, remote_rpc);
88  if (!ok)
89  {
90  yCError(LOCALIZATION2DCLIENT, "open() error could not connect to %s", remote_rpc.c_str());
91  return false;
92  }
93 
94  return true;
95 }
96 
98 {
100  yarp::os::Bottle resp;
101 
104  b.addString(loc.map_id);
105  b.addFloat64(loc.x);
106  b.addFloat64(loc.y);
107  b.addFloat64(loc.theta);
108 
109  bool ret = m_rpc_port_localization_server.write(b, resp);
110  if (ret)
111  {
112  if (resp.get(0).asVocab() != VOCAB_OK)
113  {
114  yCError(LOCALIZATION2DCLIENT) << "setInitialPose() received error from localization server";
115  return false;
116  }
117  }
118  else
119  {
120  yCError(LOCALIZATION2DCLIENT) << "setInitialPose() error on writing on rpc port";
121  return false;
122  }
123  return true;
124 }
125 
127 {
128  if (cov.rows() != 3 || cov.cols() != 3)
129  {
130  yCError(LOCALIZATION2DCLIENT) << "Covariance matrix is expected to have size (3,3)";
131  return false;
132  }
134  yarp::os::Bottle resp;
135 
138  b.addString(loc.map_id);
139  b.addFloat64(loc.x);
140  b.addFloat64(loc.y);
141  b.addFloat64(loc.theta);
142  yarp::os::Bottle& mc = b.addList();
143  for (size_t i = 0; i < 3; i++) {for (size_t j = 0; j < 3; j++) { mc.addFloat64(cov[i][j]); }}
144 
145  bool ret = m_rpc_port_localization_server.write(b, resp);
146  if (ret)
147  {
148  if (resp.get(0).asVocab() != VOCAB_OK)
149  {
150  yCError(LOCALIZATION2DCLIENT) << "setInitialPose() received error from localization server";
151  return false;
152  }
153  }
154  else
155  {
156  yCError(LOCALIZATION2DCLIENT) << "setInitialPose() error on writing on rpc port";
157  return false;
158  }
159  return true;
160 }
161 
163 {
165  yarp::os::Bottle resp;
166 
169 
170  bool ret = m_rpc_port_localization_server.write(b, resp);
171  if (ret)
172  {
173  if (resp.get(0).asVocab() != VOCAB_OK || resp.size() != 10)
174  {
175  yCError(LOCALIZATION2DCLIENT) << "getEstimatedOdometry() received error from localization server";
176  return false;
177  }
178  else
179  {
180  odom.odom_x = resp.get(1).asFloat64();
181  odom.odom_y = resp.get(2).asFloat64();
182  odom.odom_theta = resp.get(3).asFloat64();
183  odom.base_vel_x = resp.get(4).asFloat64();
184  odom.base_vel_y = resp.get(5).asFloat64();
185  odom.base_vel_theta = resp.get(6).asFloat64();
186  odom.odom_vel_x = resp.get(7).asFloat64();
187  odom.odom_vel_y = resp.get(8).asFloat64();
188  odom.odom_vel_theta = resp.get(9).asFloat64();
189  return true;
190  }
191  }
192  else
193  {
194  yCError(LOCALIZATION2DCLIENT) << "getEstimatedOdometry() error on writing on rpc port";
195  return false;
196  }
197  return true;
198 }
199 
201 {
203  yarp::os::Bottle resp;
204 
207 
208  bool ret = m_rpc_port_localization_server.write(b, resp);
209  if (ret)
210  {
211  if (resp.get(0).asVocab() != VOCAB_OK || resp.size() != 5)
212  {
213  yCError(LOCALIZATION2DCLIENT) << "getCurrentPosition() received error from localization server";
214  return false;
215  }
216  else
217  {
218  loc.map_id = resp.get(1).asString();
219  loc.x = resp.get(2).asFloat64();
220  loc.y = resp.get(3).asFloat64();
221  loc.theta = resp.get(4).asFloat64();
222  return true;
223  }
224  }
225  else
226  {
227  yCError(LOCALIZATION2DCLIENT) << "getCurrentPosition() error on writing on rpc port";
228  return false;
229  }
230  return true;
231 }
232 
234 {
236  yarp::os::Bottle resp;
237 
240 
241  bool ret = m_rpc_port_localization_server.write(b, resp);
242  if (ret)
243  {
244  if (resp.get(0).asVocab() != VOCAB_OK || resp.size() != 6)
245  {
246  yCError(LOCALIZATION2DCLIENT) << "getCurrentPosition() received error from localization server";
247  return false;
248  }
249  else
250  {
251  if (cov.rows() != 3 || cov.cols() != 3)
252  {
253  yCDebug(LOCALIZATION2DCLIENT) << "Performance warning: covariance matrix is not (3,3), resizing...";
254  cov.resize(3, 3);
255  }
256  loc.map_id = resp.get(1).asString();
257  loc.x = resp.get(2).asFloat64();
258  loc.y = resp.get(3).asFloat64();
259  loc.theta = resp.get(4).asFloat64();
260  Bottle* mc = resp.get(5).asList();
261  if (mc == nullptr) return false;
262  for (size_t i = 0; i < 3; i++) { for (size_t j = 0; j < 3; j++) { cov[i][j] = mc->get(i*3+j).asFloat64(); } }
263  return true;
264  }
265  }
266  else
267  {
268  yCError(LOCALIZATION2DCLIENT) << "getCurrentPosition() error on writing on rpc port";
269  return false;
270  }
271  return true;
272 }
273 
274 bool Localization2DClient::getEstimatedPoses(std::vector<Map2DLocation>& poses)
275 {
277  yarp::os::Bottle resp;
278 
281 
282  bool ret = m_rpc_port_localization_server.write(b, resp);
283  if (ret)
284  {
285  if (resp.get(0).asVocab() != VOCAB_OK)
286  {
287  yCError(LOCALIZATION2DCLIENT) << "getEstimatedPoses() received error from localization server";
288  return false;
289  }
290  else
291  {
292  int nposes = resp.get(1).asInt32();
293  poses.clear();
294  for (int i = 0; i < nposes; i++)
295  {
296  Map2DLocation loc;
297  Bottle* b = resp.get(2 + i).asList();
298  if (b)
299  {
300  loc.map_id = b->get(0).asString();
301  loc.x = b->get(1).asFloat64();
302  loc.y = b->get(2).asFloat64();
303  loc.theta = b->get(3).asFloat64();
304  }
305  else
306  {
307  poses.clear();
308  yCError(LOCALIZATION2DCLIENT) << "getEstimatedPoses() parsing error";
309  return false;
310  }
311  poses.push_back(loc);
312  }
313  return true;
314  }
315  }
316  else
317  {
318  yCError(LOCALIZATION2DCLIENT) << "getEstimatedPoses() error on writing on rpc port";
319  return false;
320  }
321  return true;
322 }
323 
325 {
327  yarp::os::Bottle resp;
328 
331 
332  bool ret = m_rpc_port_localization_server.write(b, resp);
333  if (ret)
334  {
335  if (resp.get(0).asVocab() != VOCAB_OK || resp.size() != 2)
336  {
337  yCError(LOCALIZATION2DCLIENT) << "getLocalizationStatus() received error from localization server";
338  return false;
339  }
340  else
341  {
343  return true;
344  }
345  }
346  else
347  {
348  yCError(LOCALIZATION2DCLIENT) << "getLocalizationStatus() error on writing on rpc port";
349  return false;
350  }
351  return true;
352 }
353 
355 {
357  yarp::os::Bottle resp;
358 
361 
362  bool ret = m_rpc_port_localization_server.write(b, resp);
363  if (ret)
364  {
365  if (resp.get(0).asVocab() != VOCAB_OK)
366  {
367  yCError(LOCALIZATION2DCLIENT) << "startLocalizationService() received error from navigation server";
368  return false;
369  }
370  }
371  else
372  {
373  yCError(LOCALIZATION2DCLIENT) << "startLocalizationService() error on writing on rpc port";
374  return false;
375  }
376  return true;
377 }
378 
380 {
382  yarp::os::Bottle resp;
383 
386 
387  bool ret = m_rpc_port_localization_server.write(b, resp);
388  if (ret)
389  {
390  if (resp.get(0).asVocab() != VOCAB_OK)
391  {
392  yCError(LOCALIZATION2DCLIENT) << "stopLocalizationService() received error from navigation server";
393  return false;
394  }
395  }
396  else
397  {
398  yCError(LOCALIZATION2DCLIENT) << "stopLocalizationService() error on writing on rpc port";
399  return false;
400  }
401  return true;
402 }
403 
405 {
406  m_rpc_port_localization_server.close();
407  return true;
408 }
constexpr yarp::conf::vocab32_t VOCAB_OK
Definition: GenericVocabs.h:18
constexpr yarp::conf::vocab32_t VOCAB_NAV_GET_LOCALIZER_POSES
constexpr yarp::conf::vocab32_t VOCAB_INAVIGATION
constexpr yarp::conf::vocab32_t VOCAB_NAV_LOCALIZATION_STOP
constexpr yarp::conf::vocab32_t VOCAB_NAV_SET_INITIAL_POSCOV
constexpr yarp::conf::vocab32_t VOCAB_NAV_GET_CURRENT_POS
constexpr yarp::conf::vocab32_t VOCAB_NAV_GET_LOCALIZER_STATUS
constexpr yarp::conf::vocab32_t VOCAB_NAV_GET_ESTIMATED_ODOM
constexpr yarp::conf::vocab32_t VOCAB_NAV_SET_INITIAL_POS
constexpr yarp::conf::vocab32_t VOCAB_NAV_GET_CURRENT_POSCOV
constexpr yarp::conf::vocab32_t VOCAB_NAV_LOCALIZATION_START
bool ret
bool getEstimatedOdometry(yarp::dev::OdometryData &odom) override
Gets the estimated odometry the robot, including its velocity expressed in the world and in the local...
bool startLocalizationService() override
Starts the localization service.
bool getLocalizationStatus(yarp::dev::Nav2D::LocalizationStatusEnum &status) override
Gets the current status of the localization task.
bool close() override
Close the DeviceDriver.
bool getCurrentPosition(yarp::dev::Nav2D::Map2DLocation &loc) override
Gets the current position of the robot w.r.t world reference frame.
bool setInitialPose(const yarp::dev::Nav2D::Map2DLocation &loc) override
Sets the initial pose for the localization algorithm which estimates the current position of the robo...
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
bool stopLocalizationService() override
Stops the localization service.
bool getEstimatedPoses(std::vector< yarp::dev::Nav2D::Map2DLocation > &poses) override
Gets a set of pose estimates computed by the localization algorithm.
double base_vel_x
velocity of the robot [m/s] expressed in the robot reference frame
Definition: OdometryData.h:45
double odom_vel_y
velocity of the robot [m/s] expressed in the world reference frame
Definition: OdometryData.h:61
double base_vel_theta
angular velocity of the robot [deg/s] expressed in the robot reference frame
Definition: OdometryData.h:53
double odom_vel_theta
angular velocity of the robot [deg/s] expressed in the world reference frame
Definition: OdometryData.h:65
double base_vel_y
velocity of the robot [m/s] expressed in the robot reference frame
Definition: OdometryData.h:49
double odom_x
position of the robot [m], expressed in the world reference frame
Definition: OdometryData.h:33
double odom_y
position of the robot [m], expressed in the world reference frame
Definition: OdometryData.h:37
double odom_theta
orientation the robot [deg], expressed in the world reference frame
Definition: OdometryData.h:41
double odom_vel_x
velocity of the robot [m/s] expressed in the world reference frame
Definition: OdometryData.h:57
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:73
Bottle & addList()
Places an empty nested list in the bottle, at the end of the list.
Definition: Bottle.cpp:185
size_type size() const
Gets the number of elements in the bottle.
Definition: Bottle.cpp:254
void addFloat64(yarp::conf::float64_t x)
Places a 64-bit floating point number in the bottle, at the end of the list.
Definition: Bottle.cpp:161
void addVocab(int x)
Places a vocabulary item in the bottle, at the end of the list.
Definition: Bottle.cpp:167
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition: Bottle.cpp:249
void addString(const char *str)
Places a string in the bottle, at the end of the list.
Definition: Bottle.cpp:173
A base class for nested structures that can be searched.
Definition: Searchable.h:69
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
virtual yarp::conf::float64_t asFloat64() const
Get 64-bit floating point value.
Definition: Value.cpp:225
virtual std::int32_t asInt32() const
Get 32-bit integer value.
Definition: Value.cpp:207
virtual Bottle * asList() const
Get list value.
Definition: Value.cpp:243
virtual std::int32_t asVocab() const
Get vocabulary identifier as an integer.
Definition: Value.cpp:231
virtual std::string asString() const
Get string value.
Definition: Value.cpp:237
A class for a Matrix.
Definition: Matrix.h:46
void resize(size_t r, size_t c)
Resize the matrix, if matrix is not empty preserve old content.
Definition: Matrix.cpp:251
size_t cols() const
Return number of columns.
Definition: Matrix.h:101
size_t rows() const
Return number of rows.
Definition: Matrix.h:95
#define yCError(component,...)
Definition: LogComponent.h:157
#define yCDebug(component,...)
Definition: LogComponent.h:112
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:80
An interface for the device drivers.
An interface to the operating system, including Port based communication.
Signal processing.
Definition: Image.h:25