ViSP  2.8.0
simulateFourPoints2DCartesianCamVelocity.cpp
1 /****************************************************************************
2  *
3  * $Id: simulateFourPoints2DCartesianCamVelocity.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 
55 #include <visp/vpConfig.h>
56 #include <visp/vpDebug.h>
57 
58 
59 #ifdef VISP_HAVE_COIN_AND_GUI
60 
61 #include <visp/vpImage.h>
62 #include <visp/vpCameraParameters.h>
63 #include <visp/vpTime.h>
64 #include <visp/vpSimulator.h>
65 
66 
67 #include <visp/vpMath.h>
68 #include <visp/vpHomogeneousMatrix.h>
69 #include <visp/vpFeaturePoint.h>
70 #include <visp/vpServo.h>
71 #include <visp/vpRobotCamera.h>
72 #include <visp/vpFeatureBuilder.h>
73 #include <visp/vpParseArgv.h>
74 #include <visp/vpIoTools.h>
75 
76 #define GETOPTARGS "di:h"
77 #define SAVE 0
78 
88 void usage(const char *name, const char *badparam, std::string ipath)
89 {
90  fprintf(stdout, "\n\
91 Simulation Servo 4points.\n\
92  \n\
93 SYNOPSIS\n\
94  %s [-i <input image path>] [-d] [-h]\n", name);
95 
96  fprintf(stdout, "\n\
97 OPTIONS: Default\n\
98  -i <input image path> %s\n\
99  Set image input path.\n\
100  From this path read \"ViSP-images/iv/4points.iv\"\n\
101  cad model.\n\
102  Setting the VISP_INPUT_IMAGE_PATH environment\n\
103  variable produces the same behaviour than using\n\
104  this option.\n\
105  \n\
106  -d \n\
107  Disable the image display. This can be useful \n\
108  for automatic tests using crontab under Unix or \n\
109  using the task manager under Windows.\n\
110  \n\
111  -h\n\
112  Print the help.\n\n", ipath.c_str());
113 
114  if (badparam)
115  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
116 }
117 
133 bool getOptions(int argc, const char **argv, std::string &ipath, bool &display)
134 {
135  const char *optarg;
136  int c;
137  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
138 
139  switch (c) {
140  case 'i': ipath = optarg; break;
141  case 'd': display = false; break;
142  case 'h': usage(argv[0], NULL, ipath); return false; break;
143 
144  default:
145  usage(argv[0], optarg, ipath); return false; break;
146  }
147  }
148 
149  if ((c == 1) || (c == -1)) {
150  // standalone param or error
151  usage(argv[0], NULL, ipath);
152  std::cerr << "ERROR: " << std::endl;
153  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
154  return false;
155  }
156 
157  return true;
158 }
159 
160 static
161 void *mainLoop (void *_simu)
162 {
163  vpSimulator *simu = (vpSimulator *)_simu ;
164  simu->initMainApplication() ;
165 
166  for ( ; ; ) {
167  int i ;
168 
169  vpServo task ;
170  vpRobotCamera robot ;
171 
172  float sampling_time = 0.040f; // Sampling period in second
173  robot.setSamplingTime(sampling_time);
174 
175  std::cout << std::endl ;
176  std::cout << "-------------------------------------------------------" << std::endl ;
177  std::cout << " Test program for vpServo " <<std::endl ;
178  std::cout << " Eye-in-hand task control, articular velocity are computed" << std::endl ;
179  std::cout << " Simulation " << std::endl ;
180  std::cout << " task : servo 4 points " << std::endl ;
181  std::cout << "-------------------------------------------------------" << std::endl ;
182  std::cout << std::endl ;
183 
184 
185  vpTRACE("sets the initial camera location " ) ;
186  vpPoseVector vcMo ;
187 
188  vcMo[0] = 0.3 ;
189  vcMo[1] = 0.2 ;
190  vcMo[2] = 3 ;
191  vcMo[3] = 0 ;
192  vcMo[4] = vpMath::rad(0) ;
193  vcMo[5] = vpMath::rad(40) ;
194 
195  vpHomogeneousMatrix cMo(vcMo) ;
196  robot.setPosition(cMo) ;
197  simu->setCameraPosition(cMo) ;
198 
199  simu->getCameraPosition(cMo) ;
200  robot.setPosition(cMo) ;
201 
202  vpCameraParameters cam ;
203 
204  vpTRACE("sets the point coordinates in the world frame " ) ;
205  vpPoint point[4] ;
206  point[0].setWorldCoordinates(-0.1,-0.1,0) ;
207  point[1].setWorldCoordinates(0.1,-0.1,0) ;
208  point[2].setWorldCoordinates(0.1,0.1,0) ;
209  point[3].setWorldCoordinates(-0.1,0.1,0) ;
210 
211  vpTRACE("project : computes the point coordinates in the camera frame and its 2D coordinates" ) ;
212  for (i = 0 ; i < 4 ; i++)
213  point[i].track(cMo) ;
214 
215  vpTRACE("sets the desired position of the point ") ;
216  vpFeaturePoint p[4] ;
217  for (i = 0 ; i < 4 ; i++)
218  vpFeatureBuilder::create(p[i], point[i]) ; //retrieve x,y and Z of the vpPoint structure
219 
220 
221  vpTRACE("sets the desired position of the point ") ;
222  vpFeaturePoint pd[4] ;
223 
224  pd[0].buildFrom(-0.1,-0.1,1) ;
225  pd[1].buildFrom(0.1,-0.1,1) ;
226  pd[2].buildFrom(0.1,0.1,1) ;
227  pd[3].buildFrom(-0.1,0.1,1) ;
228 
229  vpTRACE("define the task") ;
230  vpTRACE("\t we want an eye-in-hand control law") ;
231  vpTRACE("\t articular velocity are computed") ;
234 
235 
236  vpTRACE("Set the position of the camera in the end-effector frame ") ;
237  vpHomogeneousMatrix cMe ;
238  vpVelocityTwistMatrix cVe(cMe) ;
239  task.set_cVe(cVe) ;
240 
241  vpTRACE("Set the Jacobian (expressed in the end-effector frame)") ;
242  vpMatrix eJe ;
243  robot.get_eJe(eJe) ;
244  task.set_eJe(eJe) ;
245 
246  vpTRACE("\t we want to see a point on a point..") ;
247  for (i = 0 ; i < 4 ; i++)
248  task.addFeature(p[i],pd[i]) ;
249 
250  vpTRACE("\t set the gain") ;
251  task.setLambda(1.0) ;
252 
253 
254  vpTRACE("Display task information " ) ;
255  task.print() ;
256 
257  vpTime::wait(1000); // Sleep 1s
258  std::cout << "\nEnter a character to continue or CTRL-C to quit... "
259  << std::endl ;
260  { char a ; std::cin >> a ; }
261 
262 
263  char name[FILENAME_MAX];
264  unsigned int iter=0 ;
265  vpTRACE("\t loop") ;
266  while(iter++ < 100) {
267  double t = vpTime::measureTimeMs();
268 
269  vpColVector v ;
270 
271  robot.get_eJe(eJe) ;
272  task.set_eJe(eJe) ;
273 
274  robot.getPosition(cMo) ;
275  for (i = 0 ; i < 4 ; i++)
276  {
277  point[i].track(cMo) ;
278  vpFeatureBuilder::create(p[i],point[i]) ;
279  }
280 
281  v = task.computeControlLaw() ;
283 
284  //vpTime::wait(100) ;
285 
286 
287  simu->setCameraPosition(cMo) ;
288 
289 
290  if(SAVE==1)
291  {
292  sprintf(name,"/tmp/image.%04d.external.png",iter) ;
293  std::cout << name << std::endl ;
294  simu->write(name) ;
295  sprintf(name,"/tmp/image.%04d.internal.png",iter) ;
296  simu->write(name) ;
297  }
298 
299  vpTime::wait(t, sampling_time * 1000); // Wait 40 ms
300 
301  }
302  vpTRACE("Display task information " ) ;
303  task.print() ;
304  task.kill() ;
305  std::cout << "\nEnter a character to continue..." <<std::endl ;
306  { char a ; std::cin >> a ; }
307  }
308 
309  simu->closeMainApplication() ;
310 
311 
312  void *a=NULL ;
313  return a ;
314  // return (void *);
315 }
316 
317 
318 int
319 main(int argc, const char ** argv)
320 {
321  std::string env_ipath;
322  std::string opt_ipath;
323  std::string ipath;
324  std::string filename;
325  std::string username;
326  bool opt_display = true;
327 
328  // Get the VISP_IMAGE_PATH environment variable value
329  char *ptenv = getenv("VISP_INPUT_IMAGE_PATH");
330  if (ptenv != NULL)
331  env_ipath = ptenv;
332 
333  // Set the default input path
334  if (! env_ipath.empty())
335  ipath = env_ipath;
336 
337  // Read the command line options
338  if (getOptions(argc, argv, opt_ipath, opt_display) == false) {
339  exit (-1);
340  }
341 
342  // Get the option values
343  if (!opt_ipath.empty())
344  ipath = opt_ipath;
345 
346  // Compare ipath and env_ipath. If they differ, we take into account
347  // the input path comming from the command line option
348  if (!opt_ipath.empty() && !env_ipath.empty()) {
349  if (ipath != env_ipath) {
350  std::cout << std::endl
351  << "WARNING: " << std::endl;
352  std::cout << " Since -i <visp image path=" << ipath << "> "
353  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
354  << " we skip the environment variable." << std::endl;
355  }
356  }
357 
358  // Test if an input path is set
359  if (opt_ipath.empty() && env_ipath.empty()){
360  usage(argv[0], NULL, ipath);
361  std::cerr << std::endl
362  << "ERROR:" << std::endl;
363  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
364  << std::endl
365  << " environment variable to specify the location of the " << std::endl
366  << " image path where test images are located." << std::endl << std::endl;
367  exit(-1);
368  }
369 
370  vpCameraParameters cam ;
371  vpHomogeneousMatrix fMo ; fMo[2][3] = 0 ;
372 
373 
374  if (opt_display) {
375  vpSimulator simu ;
376  simu.initInternalViewer(300, 300) ;
377  simu.initExternalViewer(300, 300) ;
378 
379  vpTime::wait(1000) ;
380  simu.setZoomFactor(1.0f) ;
381 
382  // Load the cad model
383  filename = ipath + vpIoTools::path("/ViSP-images/iv/4points.iv");
384  simu.load(filename.c_str()) ;
385 
386  simu.setInternalCameraParameters(cam) ;
387  simu.setExternalCameraParameters(cam) ;
388  simu.initApplication(&mainLoop) ;
389 
390  simu.mainLoop() ;
391  }
392 }
393 
394 #else
395 int
396 main()
397 { vpTRACE("You should install Coin3D and SoQT or SoWin or SoXt") ;
398 
399 }
400 #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)
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
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
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 buildFrom(const double x, const double y, const double Z)
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)
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