ViSP  2.6.2
servoPioneerPoint2DDepthWithoutVpServo.cpp
1 /****************************************************************************
2  *
3  * $Id: servoPioneerPoint2DDepthWithoutVpServo.cpp 3820 2012-06-27 13:13:29Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2012 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 
88 #ifdef TEST_COULD_BE_ACHIEVED
89 int main(int argc, char **argv)
90 {
91  vpImage<unsigned char> I; // Create a gray level image container
92  double depth = 1.;
93  double lambda = 0.6;
94  double coef = 1./6.77; // Scale parameter used to estimate the depth Z of the blob from its surface
95 
96  vpRobotPioneer robot;
97  ArArgumentParser parser(&argc, argv);
98  parser.loadDefaultArguments();
99 
100  // ArRobotConnector connects to the robot, get some initial data from it such as type and name,
101  // and then loads parameter files for this robot.
102  ArRobotConnector robotConnector(&parser, &robot);
103  if(!robotConnector.connectRobot())
104  {
105  ArLog::log(ArLog::Terse, "Could not connect to the robot.");
106  if(parser.checkHelpAndWarnUnparsed())
107  {
108  Aria::logOptions();
109  Aria::exit(1);
110  }
111  }
112  if (!Aria::parseArgs())
113  {
114  Aria::logOptions();
115  Aria::shutdown();
116  return false;
117  }
118 
119  std::cout << "Robot connected" << std::endl;
120 
121  // Create the camera framegrabber
122 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020100)
123  int device = 1;
124  std::cout << "Use device: " << device << std::endl;
125  cv::VideoCapture g(device); // open the default camera
126  g.set(CV_CAP_PROP_FRAME_WIDTH, 640);
127  g.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
128  if(!g.isOpened()) // check if we succeeded
129  return -1;
130  cv::Mat frame;
131  g >> frame; // get a new frame from camera
132  vpImageConvert::convert(frame, I);
133 #elif defined(VISP_HAVE_V4L2)
134  // Create a grabber based on v4l2 third party lib (for usb cameras under Linux)
135  vpV4l2Grabber g;
136  g.setScale(1);
137  g.setInput(0);
138  g.setDevice("/dev/video1");
139  g.open(I);
140 #elif defined(VISP_HAVE_DC1394_2)
141  // Create a grabber based on libdc1394-2.x third party lib (for firewire cameras under Linux)
142  vp1394TwoGrabber g(false);
145 #elif defined(VISP_HAVE_CMU1394)
146  // Create a grabber based on CMU 1394 third party lib (for firewire cameras under windows)
148  g.setVideoMode(0, 5); // 640x480 MONO8
149  g.setFramerate(4); // 30 Hz
150  g.open(I);
151 #endif
152 
153  // Acquire an image from the grabber
154 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020100)
155  g >> frame; // get a new frame from camera
156  vpImageConvert::convert(frame, I);
157 #else
158  g.acquire(I);
159 #endif
160 
161  // Create an image viewer
162 #if defined(VISP_HAVE_X11)
163  vpDisplayX d(I, 10, 10, "Current frame");
164 #elif defined(VISP_HAVE_GDI)
165  vpDisplayGDI d(I, 10, 10, "Current frame");
166 #endif
168  vpDisplay::flush(I);
169 
170  // Create a blob tracker
171  vpDot2 dot;
172  dot.setGraphics(true);
173  dot.setComputeMoments(true);
174  dot.setEllipsoidShapePrecision(0.); // to track a blob without any constraint on the shape
175  dot.setGrayLevelPrecision(0.9); // to set the blob gray level bounds for binarisation
176  dot.setEllipsoidBadPointsPercentage(0.5); // to be accept 50% of bad inner and outside points with bad gray level
177  dot.initTracking(I);
178  vpDisplay::flush(I);
179 
180  // Camera parameters. In this experiment we don't need a precise calibration of the camera
181  vpCameraParameters cam;
182  cam.initPersProjWithoutDistortion(800, 800, I.getWidth() / 2., I.getHeight() / 2.);
183 
184  // Current and desired visual feature associated to the x coordinate of the point
185  vpFeaturePoint s_x, s_xd;
186 
187  // Create the current x visual feature
188  vpFeatureBuilder::create(s_x, cam, dot);
189 
190  // Create the desired x* visual feature
191  s_xd.buildFrom(0, 0, depth);
193 
194  // Create the current log(Z/Z*) visual feature
195  vpFeatureDepth s_Z;
196  // Surface of the blob estimated from the image moment m00 and converted in meters
197  double surface = 1./sqrt(dot.m00/(cam.get_px()*cam.get_py()));
198  double Z, Zd;
199  // Initial depth of the blob in from of the camera
200  Z = coef * surface ;
201  // Desired depth Z* of the blob. This depth is learned and equal to the initial depth
202  Zd = Z;
203  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
204  vpMatrix L_Z = s_Z.interaction();
205 
206  vpVelocityTwistMatrix cVe = robot.get_cVe();
207  vpMatrix eJe; // pioneer jacobian
208  robot.get_eJe(eJe);
209 
210  vpMatrix L; // Interaction matrix
211  L.stackMatrices(L_x); // constant since build with the desired feature
212  L.stackMatrices(L_Z); // not constant since it corresponds to log(Z/Z*) that evolves at each iteration
213 
214  vpColVector v; // vz, wx
215 
216  vpFeatureDepth s_Zd;
217  s_Zd.buildFrom(0, 0, 1, 0); // The value of s* is 0 with Z=1 meter.
218 
219  try
220  {
221  while(1)
222  {
223  // Acquire a new image
224 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020100)
225  g >> frame; // get a new frame from camera
226  vpImageConvert::convert(frame, I);
227 #else
228  g.acquire(I);
229 #endif
230  // Set the image as background of the viewer
232 
233  // Does the blob tracking
234  dot.track(I);
235  // Update the current x feature
236  vpFeatureBuilder::create(s_x, cam, dot);
237 
238  // Update log(Z/Z*) feature. Since the depth Z change, we need to update the intection matrix
239  surface = 1./sqrt(dot.m00/(cam.get_px()*cam.get_py()));
240  Z = coef * surface ;
241  s_Z.buildFrom(s_x.get_x(), s_x.get_y(), Z, log(Z/Zd)) ;
242  L_Z = s_Z.interaction();
243 
244  // Update the global interaction matrix
245  vpMatrix L;
246  L.stackMatrices(L_x); // constant since build with the desired feature
247  L.stackMatrices(L_Z); // not constant since it corresponds to log(Z/Z*) that evolves at each iteration
248 
249  // Update the global error s-s*
250  vpColVector error;
251  error.stack( s_x.error( s_xd, vpFeaturePoint::selectX() ) );
252  error.stack( s_Z.error( s_Zd ) );
253 
254  // Compute the control law. Velocities are computed in the mobile robot reference frame
255  v = -lambda * (L * cVe * eJe).pseudoInverse() * error;
256 
257  std::cout << "v: " << v.t() << std::endl;
258 
259  // Send the velocity to the robot
261 
262  // Draw a vertical line which corresponds to the desired x coordinate of the dot cog
263  vpDisplay::displayLine(I, 0, 320, 479, 320, vpColor::red);
264  vpDisplay::flush(I);
265 
266  // A click in the viewer to exit
267  if ( vpDisplay::getClick(I, false) )
268  break;
269  }
270  }
271  catch(...)
272  {
273  }
274 
275  std::cout << "Ending robot thread..." << std::endl;
276  robot.stopRunning();
277 
278  // wait for the thread to stop
279  robot.waitForRunExit();
280 }
281 #else
282 int main()
283 {
284  std::cout << "You don't have the right 3rd party libraries to run this example..." << std::endl;
285 }
286 #endif
void stack(const vpColVector &B)
Definition of the vpMatrix class.
Definition: vpMatrix.h:96
vpMatrix interaction(const unsigned int select=FEATURE_ALL)
void setVideoMode(unsigned long format, unsigned long mode)
void open(vpImage< unsigned char > &I)
unsigned int getWidth() const
Definition: vpImage.h:154
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
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:157
vpVelocityTwistMatrix get_cVe() const
Definition: vpUnicycle.h:89
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
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...
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:114
void track(const vpImage< unsigned char > &I)
Definition: vpDot2.cpp:439
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1964
static const vpColor red
Definition: vpColor.h:165
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:763
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:186
vpRowVector t() const
transpose of Vector
Generic class defining intrinsic camera parameters.
void setComputeMoments(const bool activate)
Definition: vpDot2.h:143
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:838
void setInput(unsigned input=vpV4l2Grabber::DEFAULT_INPUT)
static vpMatrix stackMatrices(const vpMatrix &A, const vpMatrix &B)
Stack two Matrices C = [ A B ]^T.
Definition: vpMatrix.cpp:2261
void buildFrom(const double x, const double y, const double Z)
Class for the Video4Linux2 video device.
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:240
unsigned int getHeight() const
Definition: vpImage.h:145
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:336
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:178
void setFramerate(vpV4l2FramerateType framerate)