ViSP  2.9.0
sonarPioneerReader.cpp
1 /****************************************************************************
2  *
3  * $Id: sonarPioneerReader.cpp 4604 2014-01-21 14:15:23Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Example that shows how to control a Pioneer mobile robot in ViSP.
36  *
37  * Authors:
38  * Fabien Spindler
39  *
40  *****************************************************************************/
41 
42 #include <iostream>
43 
44 #include <visp/vpRobotPioneer.h> // Include before vpDisplayX to avoid build issues
45 #include <visp/vpConfig.h>
46 #include <visp/vpDisplay.h>
47 #include <visp/vpDisplayGDI.h>
48 #include <visp/vpDisplayX.h>
49 #include <visp/vpImage.h>
50 #include <visp/vpIoTools.h>
51 #include <visp/vpImageIo.h>
52 #include <visp/vpTime.h>
53 
54 #ifndef VISP_HAVE_PIONEER
55 int main()
56 {
57  std::cout << "\nThis example requires Aria 3rd party library. You should install it.\n"
58  << std::endl;
59  return 0;
60 }
61 
62 #else
63 
64 ArSonarDevice sonar;
65 vpRobotPioneer *robot;
66 #if defined(VISP_HAVE_X11)
67 vpDisplayX *d;
68 #elif defined (VISP_HAVE_GDI)
69 vpDisplayGDI *d;
70 #endif
72 static int isInitialized = false;
73 static int half_size = 256*2;
74 
75 void sonarPrinter(void)
76 {
77  fprintf(stdout, "in sonarPrinter()\n"); fflush(stdout);
78  double scale = (double)half_size / (double)sonar.getMaxRange();
79 
80  /*
81  ArSonarDevice *sd;
82 
83  std::list<ArPoseWithTime *>::iterator it;
84  std::list<ArPoseWithTime *> *readings;
85  ArPose *pose;
86 
87  sd = (ArSonarDevice *)robot->findRangeDevice("sonar");
88  if (sd != NULL)
89  {
90  sd->lockDevice();
91  readings = sd->getCurrentBuffer();
92  if (readings != NULL)
93  {
94  for (it = readings->begin(); it != readings->end(); ++it)
95  {
96  pose = (*it);
97  //pose->log();
98  }
99  }
100  sd->unlockDevice();
101  }
102 */
103  double range;
104  double angle;
105 
106  /*
107  * example to show how to find closest readings within polar sections
108  */
109  printf("Closest readings within polar sections:\n");
110 
111  double start_angle = -45;
112  double end_angle = 45;
113  range = sonar.currentReadingPolar(start_angle, end_angle, &angle);
114  printf(" front quadrant: %5.0f ", range);
115  if (range != sonar.getMaxRange())
116  printf("%3.0f ", angle);
117  printf("\n");
118 #if defined(VISP_HAVE_X11) || defined (VISP_HAVE_GDI)
119  if (isInitialized && range != sonar.getMaxRange())
120  {
121  double x = range * cos(vpMath::rad(angle)); // position of the obstacle in the sensor frame
122  double y = range * sin(vpMath::rad(angle));
123 
124  // Conversion in pixels so that the robot frame is in the middle of the image
125  double j = -y * scale + half_size; // obstacle position
126  double i = -x * scale + half_size;
127 
129  vpDisplay::displayLine(I, half_size, half_size, 0, 0, vpColor::red, 5);
130  vpDisplay::displayLine(I, half_size, half_size, 0, 2*half_size-1, vpColor::red, 5);
131  vpDisplay::displayLine(I, half_size, half_size, i, j, vpColor::green, 3);
133  }
134 #endif
135 
136  range = sonar.currentReadingPolar(-135, -45, &angle);
137  printf(" right quadrant: %5.0f ", range);
138  if (range != sonar.getMaxRange())
139  printf("%3.0f ", angle);
140  printf("\n");
141 
142  range = sonar.currentReadingPolar(45, 135, &angle);
143  printf(" left quadrant: %5.0f ", range);
144  if (range != sonar.getMaxRange())
145  printf("%3.0f ", angle);
146  printf("\n");
147 
148  range = sonar.currentReadingPolar(-135, 135, &angle);
149  printf(" back quadrant: %5.0f ", range);
150  if (range != sonar.getMaxRange())
151  printf("%3.0f ", angle);
152  printf("\n");
153 
154  /*
155  * example to show how get all sonar sensor data
156  */
157  ArSensorReading *reading;
158  for (int sensor = 0; sensor < robot->getNumSonar(); sensor++)
159  {
160  reading = robot->getSonarReading(sensor);
161  if (reading != NULL)
162  {
163  angle = reading->getSensorTh();
164  range = reading->getRange();
165  double sx = reading->getSensorX(); // position of the sensor in the robot frame
166  double sy = reading->getSensorY();
167  double ox = range * cos(vpMath::rad(angle)); // position of the obstacle in the sensor frame
168  double oy = range * sin(vpMath::rad(angle));
169  double x = sx + ox; // position of the obstacle in the robot frame
170  double y = sy + oy;
171 
172  // Conversion in pixels so that the robot frame is in the middle of the image
173  double sj = -sy * scale + half_size; // sensor position
174  double si = -sx * scale + half_size;
175  double j = -y * scale + half_size; // obstacle position
176  double i = -x * scale + half_size;
177 
178  // printf("%d x: %.1f y: %.1f th: %.1f d: %d\n", sensor, reading->getSensorX(),
179  // reading->getSensorY(), reading->getSensorTh(), reading->getRange());
180 
181 #if defined(VISP_HAVE_X11) || defined (VISP_HAVE_GDI)
182  if (isInitialized && range != sonar.getMaxRange())
183  {
184  vpDisplay::displayLine(I, si, sj, i, j, vpColor::blue, 2);
185  vpDisplay::displayCross(I, si, sj, 7, vpColor::blue);
186  char legend[15];
187  sprintf(legend, "%d: %1.2fm", sensor, float(range)/1000);
188  vpDisplay::displayCharString(I, i-7, j+7, legend, vpColor::blue);
189  }
190 #endif
191  }
192 
193  }
194 
195 #if defined(VISP_HAVE_X11) || defined (VISP_HAVE_GDI)
196  if (isInitialized)
197  vpDisplay::flush(I);
198 #endif
199 
200  fflush(stdout);
201 }
202 
208 int main(int argc, char **argv)
209 {
210  try {
211  ArArgumentParser parser(&argc, argv);
212  parser.loadDefaultArguments();
213 
214  robot = new vpRobotPioneer;
215 
216  // ArRobotConnector connects to the robot, get some initial data from it such as type and name,
217  // and then loads parameter files for this robot.
218  ArRobotConnector robotConnector(&parser, robot);
219  if(!robotConnector.connectRobot())
220  {
221  ArLog::log(ArLog::Terse, "Could not connect to the robot");
222  if(parser.checkHelpAndWarnUnparsed())
223  {
224  Aria::logOptions();
225  Aria::exit(1);
226  }
227  }
228  if (!Aria::parseArgs())
229  {
230  Aria::logOptions();
231  Aria::shutdown();
232  return false;
233  }
234 
235  std::cout << "Robot connected" << std::endl;
236 
237 #if defined(VISP_HAVE_X11) || defined (VISP_HAVE_GDI)
238  // Create a display to show sensor data
239  if (isInitialized == false)
240  {
241  I.resize(half_size*2, half_size*2);
242  I = 255;
243 
244 #if defined(VISP_HAVE_X11)
245  d = new vpDisplayX;
246 #elif defined (VISP_HAVE_GDI)
247  d = new vpDisplayGDI;
248 #endif
249  d->init(I, -1, -1, "Sonar range data");
250  isInitialized = true;
251  }
252 #endif
253 
254  // Activates the sonar
255  ArGlobalFunctor sonarPrinterCB(&sonarPrinter);
256  robot->addRangeDevice(&sonar);
257  robot->addUserTask("Sonar printer", 50, &sonarPrinterCB);
258 
259  robot->useSonar(true); // activates the sonar device usage
260 
261  // Robot velocities
262  vpColVector v_mes(2);
263 
264  for (int i=0; i < 1000; i++)
265  {
266  double t = vpTime::measureTimeMs();
267 
268  v_mes = robot->getVelocity(vpRobot::REFERENCE_FRAME);
269  std::cout << "Trans. vel= " << v_mes[0] << " m/s, Rot. vel=" << vpMath::deg(v_mes[1]) << " deg/s" << std::endl;
270  v_mes = robot->getVelocity(vpRobot::ARTICULAR_FRAME);
271  std::cout << "Left wheel vel= " << v_mes[0] << " m/s, Right wheel vel=" << v_mes[1] << " m/s" << std::endl;
272  std::cout << "Battery=" << robot->getBatteryVoltage() << std::endl;
273 
274 #if defined(VISP_HAVE_X11) || defined (VISP_HAVE_GDI)
275  if (isInitialized) {
276  // A mouse click to exit
277  // Before exiting save the last sonar image
278  if (vpDisplay::getClick(I, false) == true) {
279  {
280  // Set the default output path
281  std::string opath;
282 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
283  opath = "/tmp";
284 #elif defined(_WIN32)
285  opath = "C:\\temp";
286 #endif
287  std::string username = vpIoTools::getUserName();
288 
289  // Append to the output path string, the login name of the user
290  opath += vpIoTools::path("/") + username;
291 
292  // Test if the output path exist. If no try to create it
293  if (vpIoTools::checkDirectory(opath) == false) {
294  try {
295  // Create the dirname
297  }
298  catch (...) {
299  std::cerr << std::endl
300  << "ERROR:" << std::endl;
301  std::cerr << " Cannot create " << opath << std::endl;
302  exit(-1);
303  }
304  }
305  std::string filename = opath + "/sonar.png";
306  vpImage<vpRGBa> C;
307  vpDisplay::getImage(I, C);
308  vpImageIo::write(C, filename);
309  }
310  break;
311  }
312  }
313 #endif
314 
315  vpTime::wait(t, 40);
316  }
317 
318  ArLog::log(ArLog::Normal, "simpleMotionCommands: Stopping.");
319  robot->lock();
320  robot->stop();
321  robot->unlock();
322  ArUtil::sleep(1000);
323 
324  robot->lock();
325  ArLog::log(ArLog::Normal, "simpleMotionCommands: Pose=(%.2f,%.2f,%.2f), Trans. Vel=%.2f, Rot. Vel=%.2f, Battery=%.2fV",
326  robot->getX(), robot->getY(), robot->getTh(), robot->getVel(), robot->getRotVel(), robot->getBatteryVoltage());
327  robot->unlock();
328 
329  std::cout << "Ending robot thread..." << std::endl;
330  robot->stopRunning();
331 
332 #if defined(VISP_HAVE_X11) || defined (VISP_HAVE_GDI)
333  if (isInitialized) {
334  if (d != NULL)
335  delete d;
336  }
337 #endif
338 
339  // wait for the thread to stop
340  robot->waitForRunExit();
341 
342  delete robot;
343 
344  // exit
345  ArLog::log(ArLog::Normal, "simpleMotionCommands: Exiting.");
346  return 0;
347  }
348  catch(vpException e) {
349  std::cout << "Catch an exception: " << e << std::endl;
350  return 1;
351  }
352 }
353 
354 #endif
355 
static void write(const vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:452
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:335
void useSonar(bool usage)
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:132
Define the X11 console to display images.
Definition: vpDisplayX.h:152
error that can be emited by ViSP classes.
Definition: vpException.h:76
static std::string path(const char *pathname)
Definition: vpIoTools.cpp:715
Interface for Pioneer mobile robots based on Aria 3rd party library.
static double measureTimeMs()
Definition: vpTime.cpp:86
static int wait(double t0, double t)
Definition: vpTime.cpp:149
static const vpColor green
Definition: vpColor.h:170
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1994
static const vpColor red
Definition: vpColor.h:167
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:404
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
virtual void displayCross(const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)=0
static std::string getUserName()
Definition: vpIoTools.cpp:140
void resize(const unsigned int h, const unsigned int w)
set the size of the image
Definition: vpImage.h:532
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
static void getImage(const vpImage< unsigned char > &Is, vpImage< vpRGBa > &Id)
Definition: vpDisplay.cpp:324
static double rad(double deg)
Definition: vpMath.h:100
static double deg(double rad)
Definition: vpMath.h:93
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
virtual void displayCharString(const vpImagePoint &ip, const char *text, const vpColor &color=vpColor::green)=0
virtual bool getClick(bool blocking=true)=0
virtual void displayLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)=0
void getVelocity(const vpRobot::vpControlFrameType frame, vpColVector &velocity)
static const vpColor blue
Definition: vpColor.h:173