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