ViSP  2.9.0
servoPioneerPoint2DDepthWithoutVpServo.cpp
1 /****************************************************************************
2  *
3  * $Id: servoPioneerPoint2DDepthWithoutVpServo.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 #include <visp/vpRobotPioneer.h>
45 #include <visp/vpCameraParameters.h>
46 #include <visp/vpDisplayGDI.h>
47 #include <visp/vpDisplayX.h>
48 #include <visp/vpDot2.h>
49 #include <visp/vpFeatureBuilder.h>
50 #include <visp/vpFeatureDepth.h>
51 #include <visp/vpFeaturePoint.h>
52 #include <visp/vpHomogeneousMatrix.h>
53 #include <visp/vpImage.h>
54 #include <visp/vpImageConvert.h>
55 #include <visp/vp1394TwoGrabber.h>
56 #include <visp/vp1394CMUGrabber.h>
57 #include <visp/vpOpenCVGrabber.h>
58 #include <visp/vpV4l2Grabber.h>
59 #include <visp/vpVelocityTwistMatrix.h>
60 
61 #if defined(VISP_HAVE_DC1394_2) || defined(VISP_HAVE_V4L2) || defined(VISP_HAVE_CMU1394) || defined(VISP_HAVE_OPENCV)
62 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI)
63 #if defined(VISP_HAVE_PIONEER)
64 # define TEST_COULD_BE_ACHIEVED
65 #endif
66 #endif
67 #endif
68 
69 #undef VISP_HAVE_OPENCV // To use a firewire camera
70 #undef VISP_HAVE_V4L2 // To use a firewire camera
71 
91 #ifdef TEST_COULD_BE_ACHIEVED
92 int main(int argc, char **argv)
93 {
94  try {
95  vpImage<unsigned char> I; // Create a gray level image container
96  double depth = 1.;
97  double lambda = 0.6;
98  double coef = 1./6.77; // Scale parameter used to estimate the depth Z of the blob from its surface
99 
100  vpRobotPioneer robot;
101  ArArgumentParser parser(&argc, argv);
102  parser.loadDefaultArguments();
103 
104  // ArRobotConnector connects to the robot, get some initial data from it such as type and name,
105  // and then loads parameter files for this robot.
106  ArRobotConnector robotConnector(&parser, &robot);
107  if(!robotConnector.connectRobot())
108  {
109  ArLog::log(ArLog::Terse, "Could not connect to the robot.");
110  if(parser.checkHelpAndWarnUnparsed())
111  {
112  Aria::logOptions();
113  Aria::exit(1);
114  }
115  }
116  if (!Aria::parseArgs())
117  {
118  Aria::logOptions();
119  Aria::shutdown();
120  return false;
121  }
122 
123  // Wait 3 sec to be sure that the low level Aria thread used to control
124  // the robot is started. Without this delay we experienced a delay (arround 2.2 sec)
125  // between the velocity send to the robot and the velocity that is really applied
126  // to the wheels.
127  vpTime::sleepMs(3000);
128 
129  std::cout << "Robot connected" << std::endl;
130 
131  // Camera parameters. In this experiment we don't need a precise calibration of the camera
132  vpCameraParameters cam;
133 
134  // Create the camera framegrabber
135 #if defined(VISP_HAVE_OPENCV)
136  int device = 1;
137  std::cout << "Use device: " << device << std::endl;
138  cv::VideoCapture g(device); // open the default camera
139  g.set(CV_CAP_PROP_FRAME_WIDTH, 640);
140  g.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
141  if(!g.isOpened()) // check if we succeeded
142  return -1;
143  cv::Mat frame;
144  g >> frame; // get a new frame from camera
145  vpImageConvert::convert(frame, I);
146 
147  // Logitec sphere parameters
148  cam.initPersProjWithoutDistortion(558, 555, 312, 210);
149 #elif defined(VISP_HAVE_V4L2)
150  // Create a grabber based on v4l2 third party lib (for usb cameras under Linux)
151  vpV4l2Grabber g;
152  g.setScale(1);
153  g.setInput(0);
154  g.setDevice("/dev/video1");
155  g.open(I);
156  // Logitec sphere parameters
157  cam.initPersProjWithoutDistortion(558, 555, 312, 210);
158 #elif defined(VISP_HAVE_DC1394_2)
159  // Create a grabber based on libdc1394-2.x third party lib (for firewire cameras under Linux)
160  vp1394TwoGrabber g(false);
163  // AVT Pike 032C parameters
164  cam.initPersProjWithoutDistortion(800, 795, 320, 216);
165 #elif defined(VISP_HAVE_CMU1394)
166  // Create a grabber based on CMU 1394 third party lib (for firewire cameras under windows)
168  g.setVideoMode(0, 5); // 640x480 MONO8
169  g.setFramerate(4); // 30 Hz
170  g.open(I);
171  // AVT Pike 032C parameters
172  cam.initPersProjWithoutDistortion(800, 795, 320, 216);
173 #endif
174 
175  // Acquire an image from the grabber
176 #if defined(VISP_HAVE_OPENCV)
177  g >> frame; // get a new frame from camera
178  vpImageConvert::convert(frame, I);
179 #else
180  g.acquire(I);
181 #endif
182 
183  // Create an image viewer
184 #if defined(VISP_HAVE_X11)
185  vpDisplayX d(I, 10, 10, "Current frame");
186 #elif defined(VISP_HAVE_GDI)
187  vpDisplayGDI d(I, 10, 10, "Current frame");
188 #endif
190  vpDisplay::flush(I);
191 
192  // Create a blob tracker
193  vpDot2 dot;
194  dot.setGraphics(true);
195  dot.setComputeMoments(true);
196  dot.setEllipsoidShapePrecision(0.); // to track a blob without any constraint on the shape
197  dot.setGrayLevelPrecision(0.9); // to set the blob gray level bounds for binarisation
198  dot.setEllipsoidBadPointsPercentage(0.5); // to be accept 50% of bad inner and outside points with bad gray level
199  dot.initTracking(I);
200  vpDisplay::flush(I);
201 
202  // Current and desired visual feature associated to the x coordinate of the point
203  vpFeaturePoint s_x, s_xd;
204 
205  // Create the current x visual feature
206  vpFeatureBuilder::create(s_x, cam, dot);
207 
208  // Create the desired x* visual feature
209  s_xd.buildFrom(0, 0, depth);
211 
212  // Create the current log(Z/Z*) visual feature
213  vpFeatureDepth s_Z;
214  // Surface of the blob estimated from the image moment m00 and converted in meters
215  double surface = 1./sqrt(dot.m00/(cam.get_px()*cam.get_py()));
216  double Z, Zd;
217  // Initial depth of the blob in from of the camera
218  Z = coef * surface ;
219  // Desired depth Z* of the blob. This depth is learned and equal to the initial depth
220  Zd = Z;
221  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
222  vpMatrix L_Z = s_Z.interaction();
223 
224  vpVelocityTwistMatrix cVe = robot.get_cVe();
225  vpMatrix eJe; // pioneer jacobian
226  robot.get_eJe(eJe);
227 
228  vpMatrix L; // Interaction matrix
229  L.stackMatrices(L_x); // constant since build with the desired feature
230  L.stackMatrices(L_Z); // not constant since it corresponds to log(Z/Z*) that evolves at each iteration
231 
232  vpColVector v; // vz, wx
233 
234  vpFeatureDepth s_Zd;
235  s_Zd.buildFrom(0, 0, 1, 0); // The value of s* is 0 with Z=1 meter.
236 
237  while(1)
238  {
239  // Acquire a new image
240 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020100)
241  g >> frame; // get a new frame from camera
242  vpImageConvert::convert(frame, I);
243 #else
244  g.acquire(I);
245 #endif
246  // Set the image as background of the viewer
248 
249  // Does the blob tracking
250  dot.track(I);
251  // Update the current x feature
252  vpFeatureBuilder::create(s_x, cam, dot);
253 
254  // Update log(Z/Z*) feature. Since the depth Z change, we need to update the intection matrix
255  surface = 1./sqrt(dot.m00/(cam.get_px()*cam.get_py()));
256  Z = coef * surface ;
257  s_Z.buildFrom(s_x.get_x(), s_x.get_y(), Z, log(Z/Zd)) ;
258  L_Z = s_Z.interaction();
259 
260  // Update the global interaction matrix
261  vpMatrix L;
262  L.stackMatrices(L_x); // constant since build with the desired feature
263  L.stackMatrices(L_Z); // not constant since it corresponds to log(Z/Z*) that evolves at each iteration
264 
265  // Update the global error s-s*
266  vpColVector error;
267  error.stack( s_x.error( s_xd, vpFeaturePoint::selectX() ) );
268  error.stack( s_Z.error( s_Zd ) );
269 
270  // Compute the control law. Velocities are computed in the mobile robot reference frame
271  v = -lambda * (L * cVe * eJe).pseudoInverse() * error;
272 
273  std::cout << "Send velocity to the pionner: " << v[0] << " m/s "
274  << vpMath::deg(v[1]) << " deg/s" << std::endl;
275 
276  // Send the velocity to the robot
278 
279  // Draw a vertical line which corresponds to the desired x coordinate of the dot cog
280  vpDisplay::displayLine(I, 0, 320, 479, 320, vpColor::red);
281  vpDisplay::flush(I);
282 
283  // A click in the viewer to exit
284  if ( vpDisplay::getClick(I, false) )
285  break;
286  }
287 
288  std::cout << "Ending robot thread..." << std::endl;
289  robot.stopRunning();
290 
291  // wait for the thread to stop
292  robot.waitForRunExit();
293  }
294  catch(vpException e) {
295  std::cout << "Catch an exception: " << e << std::endl;
296  return 1;
297  }
298 }
299 #else
300 int main()
301 {
302  std::cout << "You don't have the right 3rd party libraries to run this example..." << std::endl;
303 }
304 #endif
Definition of the vpMatrix class.
Definition: vpMatrix.h:98
vpMatrix interaction(const unsigned int select=FEATURE_ALL)
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 stack(const double &b)
void get_eJe(vpMatrix &eJe)
void open(vpImage< unsigned char > &I)
vpColVector error(const vpBasicFeature &s_star, const unsigned int select=FEATURE_ALL)
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
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 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)
vpMatrix interaction(const unsigned int select=FEATURE_ALL)
void setGrayLevelPrecision(const double &grayLevelPrecision)
Definition: vpDot2.cpp:788
Firewire cameras video capture based on CMU 1394 Digital Camera SDK.
void setFramerate(unsigned long fps)
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 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)
void stackMatrices(const vpMatrix &A)
Definition: vpMatrix.cpp:3003
double get_px() const
void setEllipsoidShapePrecision(const double &ellipsoidShapePrecision)
Definition: vpDot2.cpp:863
void setInput(unsigned input=vpV4l2Grabber::DEFAULT_INPUT)
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 initTracking(const vpImage< unsigned char > &I, unsigned int size=0)
Definition: vpDot2.cpp:266
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
vpColVector error(const vpBasicFeature &s_star, const unsigned int select=FEATURE_ALL)
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
static unsigned int selectX()
void setGraphics(const bool activate)
Definition: vpDot2.h:312
void setFramerate(vpV4l2FramerateType framerate)