ViSP  2.8.0
simulateFourPoints2DPolarCamVelocity.cpp
1 /****************************************************************************
2  *
3  * $Id: simulateFourPoints2DPolarCamVelocity.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  * Simulation of a visual servoing with visualization.
36  *
37  * Authors:
38  * Eric Marchand
39  * Fabien Spindler
40  *
41  *****************************************************************************/
42 
53 #include <visp/vpConfig.h>
54 #include <visp/vpDebug.h>
55 
56 
57 #ifdef VISP_HAVE_COIN_AND_GUI
58 
59 #include <visp/vpImage.h>
60 #include <visp/vpCameraParameters.h>
61 #include <visp/vpTime.h>
62 #include <visp/vpSimulator.h>
63 
64 
65 #include <visp/vpMath.h>
66 #include <visp/vpHomogeneousMatrix.h>
67 #include <visp/vpFeaturePointPolar.h>
68 #include <visp/vpServo.h>
69 #include <visp/vpRobotCamera.h>
70 #include <visp/vpFeatureBuilder.h>
71 #include <visp/vpParseArgv.h>
72 #include <visp/vpIoTools.h>
73 
74 #define GETOPTARGS "di:h"
75 #define SAVE 0
76 
86 void usage(const char *name, const char *badparam, std::string ipath)
87 {
88  fprintf(stdout, "\n\
89 Simulation Servo 4points.\n\
90  \n\
91 SYNOPSIS\n\
92  %s [-i <input image path>] [-d] [-h]\n", name);
93 
94 fprintf(stdout, "\n\
95 OPTIONS: Default\n\
96  -i <input image path> %s\n\
97  Set image input path.\n\
98  From this path read \"ViSP-images/iv/4points.iv\"\n\
99  cad model.\n\
100  Setting the VISP_INPUT_IMAGE_PATH environment\n\
101  variable produces the same behaviour than using\n\
102  this option.\n\
103  \n\
104  -d \n\
105  Disable the image display. This can be useful \n\
106  for automatic tests using crontab under Unix or \n\
107  using the task manager under Windows.\n\
108  \n\
109  -h\n\
110  Print the help.\n\n", ipath.c_str());
111 
112  if (badparam)
113  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
114 }
115 
131 bool getOptions(int argc, const char **argv, std::string &ipath, bool &display)
132 {
133  const char *optarg;
134  int c;
135  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
136 
137  switch (c) {
138  case 'i': ipath = optarg; break;
139  case 'd': display = false; break;
140  case 'h': usage(argv[0], NULL, ipath); return false; break;
141 
142  default:
143  usage(argv[0], optarg, ipath); return false; break;
144  }
145  }
146 
147  if ((c == 1) || (c == -1)) {
148  // standalone param or error
149  usage(argv[0], NULL, ipath);
150  std::cerr << "ERROR: " << std::endl;
151  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
152  return false;
153  }
154 
155  return true;
156 }
157 
158 static
159 void *mainLoop (void *_simu)
160 {
161  vpSimulator *simu = (vpSimulator *)_simu ;
162  simu->initMainApplication() ;
163 
164  for ( ; ; ) {
165  vpServo task ;
166  vpRobotCamera robot ;
167 
168  float sampling_time = 0.040f; // Sampling period in second
169  robot.setSamplingTime(sampling_time);
170 
171  std::cout << std::endl ;
172  std::cout << "-------------------------------------------------------" << std::endl ;
173  std::cout << " Test program for vpServo " <<std::endl ;
174  std::cout << " Eye-in-hand task control, articular velocity are computed" << std::endl ;
175  std::cout << " Simulation " << std::endl ;
176  std::cout << " task : servo 4 points " << std::endl ;
177  std::cout << "-------------------------------------------------------" << std::endl ;
178  std::cout << std::endl ;
179 
180 
181  vpTRACE("sets the initial camera location " ) ;
182  vpPoseVector vcMo ;
183 
184  vcMo[0] = 0. ;
185  vcMo[1] = 0. ;
186  vcMo[2] = 3 ;
187  vcMo[3] = 0 ;
188  vcMo[4] = vpMath::rad(0) ;
189  vcMo[5] = vpMath::rad(90) ;
190 
191  vpHomogeneousMatrix cMo(vcMo) ;
192  robot.setPosition(cMo) ;
193  simu->setCameraPosition(cMo) ;
194 
195  simu->getCameraPosition(cMo) ;
196  robot.setPosition(cMo) ;
197 
198  vpCameraParameters cam ;
199 
200  vpTRACE("sets the point coordinates in the world frame " ) ;
201  vpPoint point[4] ;
202  point[0].setWorldCoordinates(-0.1,-0.1,0) ;
203  point[1].setWorldCoordinates(0.1,-0.1,0) ;
204  point[2].setWorldCoordinates(0.1,0.1,0) ;
205  point[3].setWorldCoordinates(-0.1,0.1,0) ;
206 
207  vpTRACE("project : computes the point coordinates in the camera frame and its 2D coordinates" ) ;
208  for (int i = 0 ; i < 4 ; i++) {
209  point[i].changeFrame(cMo); // Compute point coordinates in the camera frame
210  point[i].project(); // Compute desired point doordinates in the camera frame
211  }
212 
213  vpTRACE("sets the desired position of the point ") ;
214  vpFeaturePointPolar p[4] ;
215  for (int i = 0 ; i < 4 ; i++)
216  vpFeatureBuilder::create(p[i], point[i]) ; //retrieve x,y and Z of the vpPoint structure to build the polar coordinates
217 
218  std::cout << "s: \n";
219  for (int i=0; i < 4; i ++) {
220  printf("[%d] rho %f theta %f Z %f\n",
221  i, p[i].get_rho(), p[i].get_theta(), p[i].get_Z());
222  }
223 
224  vpTRACE("sets the desired position of the point ") ;
225  vcMo[0] = 0 ;
226  vcMo[1] = 0 ;
227  vcMo[2] = 1 ;
228  vcMo[3] = vpMath::rad(0);
229  vcMo[4] = vpMath::rad(0);
230  vcMo[5] = vpMath::rad(0);
231 
232  vpHomogeneousMatrix cMod(vcMo);
233 
234  vpFeaturePointPolar pd[4] ;
235  vpPoint pointd[4]; // Desired position of the points
236  pointd[0].setWorldCoordinates(-0.1,-0.1,0) ;
237  pointd[1].setWorldCoordinates(0.1,-0.1,0) ;
238  pointd[2].setWorldCoordinates(0.1,0.1,0) ;
239  pointd[3].setWorldCoordinates(-0.1,0.1,0) ;
240  for (int i=0; i < 4; i ++) {
241  pointd[i].changeFrame(cMod); // Compute desired point doordinates in the camera frame
242  pointd[i].project(); // Compute desired point doordinates in the camera frame
243 
244  vpFeatureBuilder::create(pd[i], pointd[i]) ; //retrieve x,y and Z of the vpPoint structure to build the polar coordinates
245  }
246  std::cout << "s*: \n";
247  for (int i=0; i < 4; i ++) {
248  printf("[%d] rho %f theta %f Z %f\n",
249  i, pd[i].get_rho(), pd[i].get_theta(), pd[i].get_Z());
250  }
251 
252  vpTRACE("define the task") ;
253  vpTRACE("\t we want an eye-in-hand control law") ;
254  vpTRACE("\t articular velocity are computed") ;
257 
258  vpTRACE("Set the position of the camera in the end-effector frame ") ;
259  vpHomogeneousMatrix cMe ;
260  vpVelocityTwistMatrix cVe(cMe) ;
261  task.set_cVe(cVe) ;
262 
263  vpTRACE("Set the Jacobian (expressed in the end-effector frame)") ;
264  vpMatrix eJe ;
265  robot.get_eJe(eJe) ;
266  task.set_eJe(eJe) ;
267 
268  vpTRACE("\t we want to see a point on a point..") ;
269  for (int i = 0 ; i < 4 ; i++)
270  task.addFeature(p[i],pd[i]) ;
271 
272  vpTRACE("\t set the gain") ;
273  task.setLambda(1.0) ;
274 
275 
276  vpTRACE("Display task information " ) ;
277  task.print() ;
278 
279  vpTime::wait(1000); // Sleep 1s
280  std::cout << "\nEnter a character to continue or CTRL-C to quit... "
281  << std::endl ;
282  { char a ; std::cin >> a ; }
283 
284 
285  char name[FILENAME_MAX];
286  unsigned int iter=0 ;
287  vpTRACE("\t loop") ;
288  while(iter++ < 300) {
289  double t = vpTime::measureTimeMs();
290 
291  vpColVector v ;
292 
293  robot.get_eJe(eJe) ;
294  task.set_eJe(eJe) ;
295 
296  robot.getPosition(cMo) ;
297  for (int i = 0 ; i < 4 ; i++)
298  {
299  point[i].track(cMo) ;
300  vpFeatureBuilder::create(p[i],point[i]) ;
301  }
302 
303  v = task.computeControlLaw() ;
305 
306  //vpTime::wait(100) ;
307 
308 
309  simu->setCameraPosition(cMo) ;
310 
311 
312  if(SAVE==1)
313  {
314  sprintf(name,"/tmp/image.%04d.external.png",iter) ;
315  std::cout << name << std::endl ;
316  simu->write(name) ;
317  sprintf(name,"/tmp/image.%04d.internal.png",iter) ;
318  simu->write(name) ;
319  }
320 
321  vpTime::wait(t, sampling_time * 1000); // Wait 40 ms
322 
323  }
324  vpTRACE("Display task information " ) ;
325  task.print() ;
326  task.kill() ;
327 
328  std::cout << "cMo:\n" << cMo << std::endl;
329  vpPoseVector pose(cMo);
330  std::cout << "final pose:\n" << pose.t() << std::endl;
331 
332  std::cout << "\nEnter a character to continue..." <<std::endl ;
333  { char a ; std::cin >> a ; }
334  }
335 
336  simu->closeMainApplication() ;
337 
338 
339  void *a=NULL ;
340  return a ;
341  // return (void *);
342 }
343 
344 
345 int
346 main(int argc, const char ** argv)
347 {
348  std::string env_ipath;
349  std::string opt_ipath;
350  std::string ipath;
351  std::string filename;
352  std::string username;
353  bool opt_display = true;
354 
355  // Get the VISP_IMAGE_PATH environment variable value
356  char *ptenv = getenv("VISP_INPUT_IMAGE_PATH");
357  if (ptenv != NULL)
358  env_ipath = ptenv;
359 
360  // Set the default input path
361  if (! env_ipath.empty())
362  ipath = env_ipath;
363 
364  // Read the command line options
365  if (getOptions(argc, argv, opt_ipath, opt_display) == false) {
366  exit (-1);
367  }
368 
369  // Get the option values
370  if (!opt_ipath.empty())
371  ipath = opt_ipath;
372 
373  // Compare ipath and env_ipath. If they differ, we take into account
374  // the input path comming from the command line option
375  if (!opt_ipath.empty() && !env_ipath.empty()) {
376  if (ipath != env_ipath) {
377  std::cout << std::endl
378  << "WARNING: " << std::endl;
379  std::cout << " Since -i <visp image path=" << ipath << "> "
380  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
381  << " we skip the environment variable." << std::endl;
382  }
383  }
384 
385  // Test if an input path is set
386  if (opt_ipath.empty() && env_ipath.empty()){
387  usage(argv[0], NULL, ipath);
388  std::cerr << std::endl
389  << "ERROR:" << std::endl;
390  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
391  << std::endl
392  << " environment variable to specify the location of the " << std::endl
393  << " image path where test images are located." << std::endl << std::endl;
394  exit(-1);
395  }
396 
397  vpCameraParameters cam ;
398  vpHomogeneousMatrix fMo ; fMo[2][3] = 0 ;
399 
400 
401  if (opt_display) {
402  vpSimulator simu ;
403  simu.initInternalViewer(300, 300) ;
404  simu.initExternalViewer(300, 300) ;
405 
406  vpTime::wait(1000) ;
407  simu.setZoomFactor(1.0f) ;
408 
409  // Load the cad model
410  filename = ipath + vpIoTools::path("/ViSP-images/iv/4points.iv");
411  simu.load(filename.c_str()) ;
412 
413  simu.setInternalCameraParameters(cam) ;
414  simu.setExternalCameraParameters(cam) ;
415  simu.initApplication(&mainLoop) ;
416 
417  simu.mainLoop() ;
418  }
419 }
420 
421 #else
422 int
423 main()
424 { vpTRACE("You should install Coin3D and SoQT or SoWin or SoXt") ;
425 
426 }
427 #endif
Definition of the vpMatrix class.
Definition: vpMatrix.h:96
virtual void initInternalViewer(const unsigned int nlig, const unsigned int ncol)
initialize the camera view
void write(const char *fileName)
void setExternalCameraParameters(vpCameraParameters &cam)
set external camera parameters
void setCameraPosition(vpHomogeneousMatrix &cMf)
set the camera position (from an homogeneous matrix)
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
#define vpTRACE
Definition: vpDebug.h:401
void setPosition(const vpHomogeneousMatrix &cMw)
Implementation of a simulator based on Coin3d (www.coin3d.org).
Definition: vpSimulator.h:102
void closeMainApplication()
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, const unsigned int select=vpBasicFeature::FEATURE_ALL)
create a new ste of two visual features
Definition: vpServo.cpp:444
void setLambda(double _lambda)
set the gain lambda
Definition: vpServo.h:253
static std::string path(const char *pathname)
Definition: vpIoTools.cpp:715
void track(const vpHomogeneousMatrix &cMo)
static double measureTimeMs()
Definition: vpTime.cpp:86
virtual void mainLoop()
activate the mainloop
static int wait(double t0, double t)
Definition: vpTime.cpp:149
void set_cVe(vpVelocityTwistMatrix &_cVe)
Definition: vpServo.h:230
Class that defines 2D image point visual feature with polar coordinates described in ...
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:79
Class that defines what is a point.
Definition: vpPoint.h:65
virtual void setSamplingTime(const double &delta_t)
void kill()
destruction (memory deallocation if required)
Definition: vpServo.cpp:177
void initApplication(void *(*start_routine)(void *))
begin the main program
vpColVector computeControlLaw()
compute the desired control law
Definition: vpServo.cpp:883
void getCameraPosition(vpHomogeneousMatrix &_cMf)
get the camera position (from an homogeneous matrix)
Definition: vpSimulator.h:256
Class that defines the simplest robot: a free flying camera.
void setInternalCameraParameters(vpCameraParameters &cam)
set internal camera parameters
void set_eJe(vpMatrix &_eJe)
Definition: vpServo.h:238
Generic class defining intrinsic camera parameters.
void load(const char *file_name)
load an iv file
Class that consider the particular case of twist transformation matrix that allows to transform a vel...
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Set the type of the interaction matrix (current, mean, desired, user).
Definition: vpServo.cpp:509
static double rad(double deg)
Definition: vpMath.h:100
void getPosition(vpHomogeneousMatrix &cMw) const
void initMainApplication()
perform some initialization in the main program thread
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
void get_eJe(vpMatrix &eJe)
The pose is a complete representation of every rigid motion in the euclidian space.
Definition: vpPoseVector.h:92
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:258
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
void changeFrame(const vpHomogeneousMatrix &cMo, vpColVector &_cP)
Definition: vpPoint.cpp:150
Class required to compute the visual servoing control law descbribed in and .
Definition: vpServo.h:153
void initExternalViewer(const unsigned int nlig, const unsigned int ncol)
initialize the external view
void setZoomFactor(const float zoom)
set the size of the camera/frame
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &v)
void setServo(vpServoType _servo_type)
Choice of the visual servoing control law.
Definition: vpServo.cpp:214
void setWorldCoordinates(const double ox, const double oy, const double oz)
Set the point world coordinates. We mean here the coordinates of the point in the object frame...
Definition: vpPoint.cpp:74