ViSP  2.8.0
sonarPioneerReader.cpp
1 /****************************************************************************
2  *
3  * $Id: sonarPioneerReader.cpp 4056 2013-01-05 13:04:42Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 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  ArArgumentParser parser(&argc, argv);
211  parser.loadDefaultArguments();
212 
213  robot = new vpRobotPioneer;
214 
215  // ArRobotConnector connects to the robot, get some initial data from it such as type and name,
216  // and then loads parameter files for this robot.
217  ArRobotConnector robotConnector(&parser, robot);
218  if(!robotConnector.connectRobot())
219  {
220  ArLog::log(ArLog::Terse, "Could not connect to the robot");
221  if(parser.checkHelpAndWarnUnparsed())
222  {
223  Aria::logOptions();
224  Aria::exit(1);
225  }
226  }
227  if (!Aria::parseArgs())
228  {
229  Aria::logOptions();
230  Aria::shutdown();
231  return false;
232  }
233 
234  std::cout << "Robot connected" << std::endl;
235 
236 #if defined(VISP_HAVE_X11) || defined (VISP_HAVE_GDI)
237  // Create a display to show sensor data
238  if (isInitialized == false)
239  {
240  I.resize(half_size*2, half_size*2);
241  I = 255;
242 
243 #if defined(VISP_HAVE_X11)
244  d = new vpDisplayX;
245 #elif defined (VISP_HAVE_GDI)
246  d = new vpDisplayGDI;
247 #endif
248  d->init(I, -1, -1, "Sonar range data");
249  isInitialized = true;
250  }
251 #endif
252 
253  // Activates the sonar
254  ArGlobalFunctor sonarPrinterCB(&sonarPrinter);
255  robot->addRangeDevice(&sonar);
256  robot->addUserTask("Sonar printer", 50, &sonarPrinterCB);
257 
258  robot->useSonar(true); // activates the sonar device usage
259 
260  // Robot velocities
261  vpColVector v_mes(2);
262 
263  for (int i=0; i < 1000; i++)
264  {
265  double t = vpTime::measureTimeMs();
266 
267  v_mes = robot->getVelocity(vpRobot::REFERENCE_FRAME);
268  std::cout << "Trans. vel= " << v_mes[0] << " m/s, Rot. vel=" << vpMath::deg(v_mes[1]) << " deg/s" << std::endl;
269  v_mes = robot->getVelocity(vpRobot::ARTICULAR_FRAME);
270  std::cout << "Left wheel vel= " << v_mes[0] << " m/s, Right wheel vel=" << v_mes[1] << " m/s" << std::endl;
271  std::cout << "Battery=" << robot->getBatteryVoltage() << std::endl;
272 
273 #if defined(VISP_HAVE_X11) || defined (VISP_HAVE_GDI)
274  if (isInitialized) {
275  // A mouse click to exit
276  // Before exiting save the last sonar image
277  if (vpDisplay::getClick(I, false) == true) {
278  {
279  // Set the default output path
280  std::string opath;
281 #ifdef UNIX
282  opath = "/tmp";
283 #elif WIN32
284  opath = "C:\\temp";
285 #endif
286  std::string username = vpIoTools::getUserName();
287 
288  // Append to the output path string, the login name of the user
289  opath += vpIoTools::path("/") + username;
290 
291  // Test if the output path exist. If no try to create it
292  if (vpIoTools::checkDirectory(opath) == false) {
293  try {
294  // Create the dirname
296  }
297  catch (...) {
298  std::cerr << std::endl
299  << "ERROR:" << std::endl;
300  std::cerr << " Cannot create " << opath << std::endl;
301  exit(-1);
302  }
303  }
304  std::string filename = opath + "/sonar.png";
305  vpImage<vpRGBa> C;
306  vpDisplay::getImage(I, C);
307  vpImageIo::write(C, filename);
308  }
309  break;
310  }
311  }
312 #endif
313 
314  vpTime::wait(t, 40);
315  }
316 
317  ArLog::log(ArLog::Normal, "simpleMotionCommands: Stopping.");
318  robot->lock();
319  robot->stop();
320  robot->unlock();
321  ArUtil::sleep(1000);
322 
323  robot->lock();
324  ArLog::log(ArLog::Normal, "simpleMotionCommands: Pose=(%.2f,%.2f,%.2f), Trans. Vel=%.2f, Rot. Vel=%.2f, Battery=%.2fV",
325  robot->getX(), robot->getY(), robot->getTh(), robot->getVel(), robot->getRotVel(), robot->getBatteryVoltage());
326  robot->unlock();
327 
328  std::cout << "Ending robot thread..." << std::endl;
329  robot->stopRunning();
330 
331 #if defined(VISP_HAVE_X11) || defined (VISP_HAVE_GDI)
332  if (isInitialized) {
333  if (d != NULL)
334  delete d;
335  }
336 #endif
337 
338  // wait for the thread to stop
339  robot->waitForRunExit();
340 
341  delete robot;
342 
343  // exit
344  ArLog::log(ArLog::Normal, "simpleMotionCommands: Exiting.");
345  return 0;
346 }
347 
348 #endif
349 
static void write(const vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:442
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:133
void resize(const unsigned int height, const unsigned int width)
set the size of the image
Definition: vpImage.h:535
Define the X11 console to display images.
Definition: vpDisplayX.h:152
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:1991
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:203
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 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:321
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