ViSP  2.9.0
servoPioneerPoint2DDepth.cpp
1 /****************************************************************************
2  *
3  * $Id: servoPioneerPoint2DDepth.cpp 4574 2014-01-09 08:48:51Z 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  * IBVS on Pioneer P3DX mobile platform
36  *
37  * Authors:
38  * Fabien Spindler
39  *
40  *****************************************************************************/
41 #include <iostream>
42 
43 #include <visp/vpConfig.h>
44 
45 #include <visp/vpRobotPioneer.h>
46 #include <visp/vpCameraParameters.h>
47 #include <visp/vpDisplayGDI.h>
48 #include <visp/vpDisplayX.h>
49 #include <visp/vpDot2.h>
50 #include <visp/vpFeatureBuilder.h>
51 #include <visp/vpFeatureDepth.h>
52 #include <visp/vpFeaturePoint.h>
53 #include <visp/vpHomogeneousMatrix.h>
54 #include <visp/vpImage.h>
55 #include <visp/vpImageConvert.h>
56 #include <visp/vp1394TwoGrabber.h>
57 #include <visp/vp1394CMUGrabber.h>
58 #include <visp/vpV4l2Grabber.h>
59 #include <visp/vpOpenCVGrabber.h>
60 #include <visp/vpServo.h>
61 #include <visp/vpVelocityTwistMatrix.h>
62 
63 #if defined(VISP_HAVE_DC1394_2) || defined(VISP_HAVE_V4L2) || defined(VISP_HAVE_CMU1394) || defined(VISP_HAVE_OPENCV)
64 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI)
65 #if defined(VISP_HAVE_PIONEER)
66 # define TEST_COULD_BE_ACHIEVED
67 #endif
68 #endif
69 #endif
70 
71 #undef VISP_HAVE_OPENCV // To use a firewire camera
72 #undef VISP_HAVE_V4L2 // To use a firewire camera
73 
93 #ifdef TEST_COULD_BE_ACHIEVED
94 int main(int argc, char **argv)
95 {
96  try {
97  vpImage<unsigned char> I; // Create a gray level image container
98  double depth = 1.;
99  double lambda = 0.6;
100  double coef = 1./6.77; // Scale parameter used to estimate the depth Z of the blob from its surface
101 
102  vpRobotPioneer robot;
103  ArArgumentParser parser(&argc, argv);
104  parser.loadDefaultArguments();
105 
106  // ArRobotConnector connects to the robot, get some initial data from it such as type and name,
107  // and then loads parameter files for this robot.
108  ArRobotConnector robotConnector(&parser, &robot);
109  if(!robotConnector.connectRobot())
110  {
111  ArLog::log(ArLog::Terse, "Could not connect to the robot.");
112  if(parser.checkHelpAndWarnUnparsed())
113  {
114  Aria::logOptions();
115  Aria::exit(1);
116  }
117  }
118  if (!Aria::parseArgs())
119  {
120  Aria::logOptions();
121  Aria::shutdown();
122  return false;
123  }
124 
125  // Wait 3 sec to be sure that the low level Aria thread used to control
126  // the robot is started. Without this delay we experienced a delay (arround 2.2 sec)
127  // between the velocity send to the robot and the velocity that is really applied
128  // to the wheels.
129  vpTime::sleepMs(3000);
130 
131  std::cout << "Robot connected" << std::endl;
132 
133  // Camera parameters. In this experiment we don't need a precise calibration of the camera
134  vpCameraParameters cam;
135 
136  // Create the camera framegrabber
137 #if defined(VISP_HAVE_OPENCV)
138  int device = 1;
139  std::cout << "Use device: " << device << std::endl;
140  cv::VideoCapture g(device); // open the default camera
141  g.set(CV_CAP_PROP_FRAME_WIDTH, 640);
142  g.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
143  if(!g.isOpened()) // check if we succeeded
144  return -1;
145  cv::Mat frame;
146  g >> frame; // get a new frame from camera
147  vpImageConvert::convert(frame, I);
148 
149  // Logitec sphere parameters
150  cam.initPersProjWithoutDistortion(558, 555, 312, 210);
151 #elif defined(VISP_HAVE_V4L2)
152  // Create a grabber based on v4l2 third party lib (for usb cameras under Linux)
153  vpV4l2Grabber g;
154  g.setScale(1);
155  g.setInput(0);
156  g.setDevice("/dev/video1");
157  g.open(I);
158  // Logitec sphere parameters
159  cam.initPersProjWithoutDistortion(558, 555, 312, 210);
160 #elif defined(VISP_HAVE_DC1394_2)
161  // Create a grabber based on libdc1394-2.x third party lib (for firewire cameras under Linux)
162  vp1394TwoGrabber g(false);
165  // AVT Pike 032C parameters
166  cam.initPersProjWithoutDistortion(800, 795, 320, 216);
167 #elif defined(VISP_HAVE_CMU1394)
168  // Create a grabber based on CMU 1394 third party lib (for firewire cameras under windows)
170  g.setVideoMode(0, 5); // 640x480 MONO8
171  g.setFramerate(4); // 30 Hz
172  g.open(I);
173  // AVT Pike 032C parameters
174  cam.initPersProjWithoutDistortion(800, 795, 320, 216);
175 #endif
176 
177  // Acquire an image from the grabber
178 #if defined(VISP_HAVE_OPENCV)
179  g >> frame; // get a new frame from camera
180  vpImageConvert::convert(frame, I);
181 #else
182  g.acquire(I);
183 #endif
184 
185  // Create an image viewer
186 #if defined(VISP_HAVE_X11)
187  vpDisplayX d(I, 10, 10, "Current frame");
188 #elif defined(VISP_HAVE_GDI)
189  vpDisplayGDI d(I, 10, 10, "Current frame");
190 #endif
192  vpDisplay::flush(I);
193 
194  // Create a blob tracker
195  vpDot2 dot;
196  dot.setGraphics(true);
197  dot.setComputeMoments(true);
198  dot.setEllipsoidShapePrecision(0.); // to track a blob without any constraint on the shape
199  dot.setGrayLevelPrecision(0.9); // to set the blob gray level bounds for binarisation
200  dot.setEllipsoidBadPointsPercentage(0.5); // to be accept 50% of bad inner and outside points with bad gray level
201  dot.initTracking(I);
202  vpDisplay::flush(I);
203 
204  vpServo task;
207  task.setLambda(lambda) ;
209  cVe = robot.get_cVe() ;
210  task.set_cVe(cVe) ;
211 
212  std::cout << "cVe: \n" << cVe << std::endl;
213 
214  vpMatrix eJe;
215  robot.get_eJe(eJe) ;
216  task.set_eJe(eJe) ;
217  std::cout << "eJe: \n" << eJe << std::endl;
218 
219  // Current and desired visual feature associated to the x coordinate of the point
220  vpFeaturePoint s_x, s_xd;
221 
222  // Create the current x visual feature
223  vpFeatureBuilder::create(s_x, cam, dot);
224 
225  // Create the desired x* visual feature
226  s_xd.buildFrom(0, 0, depth);
227 
228  // Add the feature
229  task.addFeature(s_x, s_xd) ;
230 
231  // Create the current log(Z/Z*) visual feature
232  vpFeatureDepth s_Z, s_Zd;
233  // Surface of the blob estimated from the image moment m00 and converted in meters
234  double surface = 1./sqrt(dot.m00/(cam.get_px()*cam.get_py()));
235  double Z, Zd;
236  // Initial depth of the blob in from of the camera
237  Z = coef * surface ;
238  // Desired depth Z* of the blob. This depth is learned and equal to the initial depth
239  Zd = Z;
240 
241  std::cout << "Z " << Z << std::endl;
242  s_Z.buildFrom(s_x.get_x(), s_x.get_y(), Z , 0); // log(Z/Z*) = 0 that's why the last parameter is 0
243  s_Zd.buildFrom(s_x.get_x(), s_x.get_y(), Zd , 0); // log(Z/Z*) = 0 that's why the last parameter is 0
244 
245  // Add the feature
246  task.addFeature(s_Z, s_Zd) ;
247 
248  vpColVector v; // vz, wx
249 
250  while(1)
251  {
252  // Acquire a new image
253 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020100)
254  g >> frame; // get a new frame from camera
255  vpImageConvert::convert(frame, I);
256 #else
257  g.acquire(I);
258 #endif
259  // Set the image as background of the viewer
261 
262  // Does the blob tracking
263  dot.track(I);
264  // Update the current x feature
265  vpFeatureBuilder::create(s_x, cam, dot);
266 
267  // Update log(Z/Z*) feature. Since the depth Z change, we need to update the intection matrix
268  surface = 1./sqrt(dot.m00/(cam.get_px()*cam.get_py()));
269  Z = coef * surface ;
270  s_Z.buildFrom(s_x.get_x(), s_x.get_y(), Z, log(Z/Zd)) ;
271 
272  robot.get_cVe(cVe) ;
273  task.set_cVe(cVe) ;
274 
275  robot.get_eJe(eJe) ;
276  task.set_eJe(eJe) ;
277 
278  // Compute the control law. Velocities are computed in the mobile robot reference frame
279  v = task.computeControlLaw() ;
280 
281  std::cout << "Send velocity to the pionner: " << v[0] << " m/s "
282  << vpMath::deg(v[1]) << " deg/s" << std::endl;
283 
284  // Send the velocity to the robot
286 
287  // Draw a vertical line which corresponds to the desired x coordinate of the dot cog
288  vpDisplay::displayLine(I, 0, 320, 479, 320, vpColor::red);
289  vpDisplay::flush(I);
290 
291  // A click in the viewer to exit
292  if ( vpDisplay::getClick(I, false) )
293  break;
294  }
295 
296  std::cout << "Ending robot thread..." << std::endl;
297  robot.stopRunning();
298 
299  // wait for the thread to stop
300  robot.waitForRunExit();
301 
302  // Kill the servo task
303  task.print() ;
304  task.kill();
305  }
306  catch(vpException e) {
307  std::cout << "Catch an exception: " << e << std::endl;
308  return 1;
309  }
310 }
311 #else
312 int main()
313 {
314  std::cout << "You don't have the right 3rd party libraries to run this example..." << std::endl;
315 }
316 #endif
Definition of the vpMatrix class.
Definition: vpMatrix.h:98
void setVideoMode(unsigned long format, unsigned long mode)
void open(vpImage< unsigned char > &I)
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
void get_eJe(vpMatrix &eJe)
void open(vpImage< unsigned char > &I)
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:132
void setEllipsoidBadPointsPercentage(const double &percentage=0.0)
Definition: vpDot2.h:288
void set_eJe(const vpMatrix &eJe_)
Definition: vpServo.h:439
vpVelocityTwistMatrix get_cVe() const
Definition: vpUnicycle.h:89
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
static void sleepMs(double t)
Definition: vpTime.cpp:236
void buildFrom(const double x, const double y, const double Z, const double LogZoverZstar)
Define the X11 console to display images.
Definition: vpDisplayX.h:152
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, const unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:449
void setDevice(const char *devname)
Class that defines a 3D point visual feature which is composed by one parameters that is that defin...
error that can be emited by ViSP classes.
Definition: vpException.h:76
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
Interface for Pioneer mobile robots based on Aria 3rd party library.
double get_py() const
This tracker is meant to track a blob (connex pixels with same gray level) on a vpImage.
Definition: vpDot2.h:127
void track(const vpImage< unsigned char > &I)
Definition: vpDot2.cpp:465
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1994
static const vpColor red
Definition: vpColor.h:167
void initPersProjWithoutDistortion(const double px, const double py, const double u0, const double v0)
void setGrayLevelPrecision(const double &grayLevelPrecision)
Definition: vpDot2.cpp:788
void kill()
Definition: vpServo.cpp:189
Firewire cameras video capture based on CMU 1394 Digital Camera SDK.
void setFramerate(unsigned long fps)
vpColVector computeControlLaw()
Definition: vpServo.cpp:902
void acquire(vpImage< unsigned char > &I)
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
Generic class defining intrinsic camera parameters.
void setLambda(double c)
Definition: vpServo.h:370
void setComputeMoments(const bool activate)
Definition: vpDot2.h:274
Class that consider the particular case of twist transformation matrix that allows to transform a vel...
void setScale(unsigned scale=vpV4l2Grabber::DEFAULT_SCALE)
double get_px() const
void setEllipsoidShapePrecision(const double &ellipsoidShapePrecision)
Definition: vpDot2.cpp:863
void setInput(unsigned input=vpV4l2Grabber::DEFAULT_INPUT)
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:522
void buildFrom(const double x, const double y, const double Z)
Class for the Video4Linux2 video device.
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
double get_y() const
double get_x() const
void set_cVe(const vpVelocityTwistMatrix &cVe_)
Definition: vpServo.h:414
void initTracking(const vpImage< unsigned char > &I, unsigned int size=0)
Definition: vpDot2.cpp:266
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:251
Class for firewire ieee1394 video devices using libdc1394-2.x api.
virtual bool getClick(bool blocking=true)=0
virtual void displayLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)=0
double m00
Definition: vpDot2.h:374
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:220
void setGraphics(const bool activate)
Definition: vpDot2.h:312
void setFramerate(vpV4l2FramerateType framerate)