ViSP  2.9.0
simulateCircle2DCamVelocity.cpp
1 /****************************************************************************
2  *
3  * $Id: simulateCircle2DCamVelocity.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  * Simulation of a visual servoing with visualization.
36  *
37  * Authors:
38  * Eric Marchand
39  * Fabien Spindler
40  *
41  *****************************************************************************/
42 
57 #include <visp/vpConfig.h>
58 #include <visp/vpDebug.h>
59 
60 
61 #ifdef VISP_HAVE_COIN_AND_GUI
62 #include <visp/vpImage.h>
63 #include <visp/vpCameraParameters.h>
64 #include <visp/vpTime.h>
65 #include <visp/vpSimulator.h>
66 
67 
68 
69 #include <visp/vpMath.h>
70 #include <visp/vpHomogeneousMatrix.h>
71 #include <visp/vpFeatureEllipse.h>
72 #include <visp/vpCircle.h>
73 #include <visp/vpServo.h>
74 #include <visp/vpRobotCamera.h>
75 #include <visp/vpFeatureBuilder.h>
76 #include <visp/vpParseArgv.h>
77 #include <visp/vpIoTools.h>
78 
79 
80 #define GETOPTARGS "cdi:h"
81 #define SAVE 0
82 
83 
93 void usage(const char *name, const char *badparam, std::string ipath)
94 {
95  fprintf(stdout, "\n\
96 Simulation Servo Circle\n\
97  \n\
98 SYNOPSIS\n\
99  %s [-i <input image path>] [-d] [-h]\n", name);
100 
101 
102  fprintf(stdout, "\n\
103 OPTIONS: Default\n\
104  -i <input image path> %s\n\
105  Set image input path.\n\
106  From this path read \"ViSP-images/iv/4points.iv\"\n\
107  cad model.\n\
108  Setting the VISP_INPUT_IMAGE_PATH environment\n\
109  variable produces the same behaviour than using\n\
110  this option.\n\
111  \n\
112  -d \n\
113  Disable the image display. This can be useful \n\
114  for automatic tests using crontab under Unix or \n\
115  using the task manager under Windows.\n\
116  \n\
117  -h\n\
118  Print the help.\n\n",
119  ipath.c_str());
120 
121  if (badparam)
122  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
123 }
124 
140 bool getOptions(int argc, const char **argv, std::string &ipath, bool &display)
141 {
142  const char *optarg;
143  int c;
144  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
145 
146  switch (c) {
147  case 'i': ipath = optarg; break;
148  case 'd': display = false; break;
149  case 'h': usage(argv[0], NULL, ipath); return false; break;
150 
151  default:
152  usage(argv[0], optarg, ipath); return false; break;
153  }
154  }
155 
156  if ((c == 1) || (c == -1)) {
157  // standalone param or error
158  usage(argv[0], NULL, ipath);
159  std::cerr << "ERROR: " << std::endl;
160  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
161  return false;
162  }
163 
164  return true;
165 }
166 
167 static
168 void *mainLoop (void *_simu)
169 {
170  vpSimulator *simu = (vpSimulator *)_simu ;
171  simu->initMainApplication() ;
172 
173  vpPoseVector vcMo ;
174 
175  vcMo[0] = 0.3 ;
176  vcMo[1] = 0.2 ;
177  vcMo[2] = 3 ;
178  vcMo[3] = 0 ;
179  vcMo[4] = vpMath::rad(45) ;
180  vcMo[5] = vpMath::rad(40) ;
181  vpHomogeneousMatrix cMo(vcMo) ; ;
182 
183  vpHomogeneousMatrix cMod ;
184  cMod[0][3] = 0 ;
185  cMod[1][3] = 0 ;
186  cMod[2][3] = 1 ;
187 
188  int it =0 ;
189  unsigned int pos = 2 ;
190  while (pos!=0)
191  {
192  vpServo task ;
193  vpRobotCamera robot ;
194 
195  float sampling_time = 0.040f; // Sampling period in second
196  robot.setSamplingTime(sampling_time);
197 
198  /* std::cout << std::endl ;
199  std::cout << "-----------------------" << std::endl ;
200  std::cout << " Test program for vpServo " <<std::endl ;
201  std::cout << " Simulation " << std::endl ;
202  std::cout << " task : servo a circle " << std::endl ;
203  std::cout << "-----------------------" << std::endl ;
204  std::cout << std::endl ;
205 */
206 
207  vpTRACE("sets the initial camera location " ) ;
208 
209 
210  robot.setPosition(cMo) ;
211  simu->setCameraPosition(cMo) ;
212 
213 
214  if (pos==1) cMod[2][3] = 0.32 ;
215 
216  vpTRACE("sets the circle coordinates in the world frame " ) ;
217  vpCircle circle ;
218  circle.setWorldCoordinates(0,0,1,0,0,0,0.1) ;
219 
220  vpTRACE("sets the desired position of the visual feature ") ;
221  vpFeatureEllipse pd ;
222  circle.track(cMod) ;
223  vpFeatureBuilder::create(pd,circle) ;
224 
225  vpTRACE("project : computes the circle coordinates in the camera frame and its 2D coordinates" ) ;
226 
227  vpTRACE("sets the current position of the visual feature ") ;
228  vpFeatureEllipse p ;
229  circle.track(cMo) ;
230  vpFeatureBuilder::create(p,circle) ;
231 
232  vpTRACE("define the task") ;
233  vpTRACE("\t we want an eye-in-hand control law") ;
234  vpTRACE("\t robot is controlled in the camera frame") ;
237 
238  vpTRACE("\t we want to see a circle on a circle..") ;
239  std::cout << std::endl ;
240  task.addFeature(p,pd) ;
241 
242  vpTRACE("\t set the gain") ;
243 
244  task.setLambda(1.0) ;
245  // if (pos==2)
246  // task.setLambda(0.0251) ;
247  // else
248  // task.setLambda(0.0251) ;
249 
250 
251  vpTRACE("Display task information " ) ;
252  task.print() ;
253 
254  vpTime::wait(1000); // Sleep 1s
255 
256  std::cout << "\nEnter a character to continue... " <<std::endl ;
257  { int a ; std::cin >> a ; }
258 
259 
260  unsigned int iter=0 ;
261  vpTRACE("\t loop") ;
262  unsigned int itermax ;
263  if (pos==2) itermax = 75 ; else itermax = 100 ;
264  char name[FILENAME_MAX] ;
265  while(iter++<itermax)
266  {
267  double t = vpTime::measureTimeMs();
268  std::cout << "---------------------------------------------"
269  << iter <<std::endl ;
270  vpColVector v ;
271 
272  if (iter==1) vpTRACE("\t\t get the robot position ") ;
273  robot.getPosition(cMo) ;
274  if (iter==1) vpTRACE("\t\t new circle position ") ;
275  //retrieve x,y and Z of the vpCircle structure
276 
277  circle.track(cMo) ;
278  vpFeatureBuilder::create(p,circle);
279 
280  if (iter==1) vpTRACE("\t\t compute the control law ") ;
281  v = task.computeControlLaw() ;
282  // vpTRACE("computeControlLaw" ) ;
283  std::cout << "Task rank: " << task.getTaskRank() <<std::endl ;
284  if (iter==1)
285  vpTRACE("\t\t send the camera velocity to the controller ") ;
287 
288  simu->setCameraPosition(cMo) ;
289 
290  if(SAVE==1)
291  {
292  sprintf(name,"/tmp/image.%04d.external.png",it) ;
293  std::cout << "Save " << name << std::endl ;
294  simu->write(name) ;
295  sprintf(name,"/tmp/image.%04d.internal.png",iter) ;
296  std::cout << "Save " << name << std::endl ;
297  simu->write(name) ;
298  it++ ;
299  }
300  // vpTRACE("\t\t || s - s* || ") ;
301  // std::cout << ( task.getError() ).sumSquare() <<std::endl ; ;
302  vpTime::wait(t, sampling_time * 1000); // Wait 40 ms
303 
304  }
305  pos-- ;
306  task.kill();
307 
308  }
309 
310 
311  simu->closeMainApplication() ;
312 
313  void *a=NULL ;
314  return a ;
315  // return (void *);
316 }
317 
318 
319 int
320 main(int argc, const char ** argv)
321 {
322  try {
323  std::string env_ipath;
324  std::string opt_ipath;
325  std::string ipath;
326  std::string filename;
327  std::string username;
328  bool opt_display = true;
329 
330  // Get the VISP_IMAGE_PATH environment variable value
331  char *ptenv = getenv("VISP_INPUT_IMAGE_PATH");
332  if (ptenv != NULL)
333  env_ipath = ptenv;
334 
335  // Set the default input path
336  if (! env_ipath.empty())
337  ipath = env_ipath;
338 
339  // Read the command line options
340  if (getOptions(argc, argv, opt_ipath, opt_display) == false) {
341  exit (-1);
342  }
343 
344  // Get the option values
345  if (!opt_ipath.empty())
346  ipath = opt_ipath;
347 
348  // Compare ipath and env_ipath. If they differ, we take into account
349  // the input path comming from the command line option
350  if (!opt_ipath.empty() && !env_ipath.empty()) {
351  if (ipath != env_ipath) {
352  std::cout << std::endl
353  << "WARNING: " << std::endl;
354  std::cout << " Since -i <visp image path=" << ipath << "> "
355  << " is different from VISP_INPUT_IMAGE_PATH=" << env_ipath << std::endl
356  << " we skip the environment variable." << std::endl;
357  }
358  }
359 
360  // Test if an input path is set
361  if (opt_ipath.empty() && env_ipath.empty()){
362  usage(argv[0], NULL, ipath);
363  std::cerr << std::endl
364  << "ERROR:" << std::endl;
365  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
366  << std::endl
367  << " environment variable to specify the location of the " << std::endl
368  << " image path where test images are located." << std::endl << std::endl;
369  exit(-1);
370  }
371 
372  vpCameraParameters cam ;
373  vpHomogeneousMatrix fMo ; fMo[2][3] = 0 ;
374 
375  if (opt_display) {
376 
377  vpSimulator simu ;
378  simu.initInternalViewer(300, 300) ;
379  simu.initExternalViewer(300, 300) ;
380 
381  vpTime::wait(1000) ;
382  simu.setZoomFactor(1.0f) ;
383  simu.addAbsoluteFrame() ;
384 
385  // Load the cad model
386  filename = ipath + vpIoTools::path("/ViSP-images/iv/circle.iv");
387  simu.load(filename.c_str(),fMo) ;
388 
389  simu.setInternalCameraParameters(cam) ;
390 
391  simu.initApplication(&mainLoop) ;
392  simu.mainLoop() ;
393 
394  }
395  return 0;
396  }
397  catch(vpException e) {
398  std::cout << "Catch an exception: " << e << std::endl;
399  return 1;
400  }
401 }
402 
403 
404 #else
405 int
406 main()
407 { vpTRACE("You should install Coin3D and SoQT or SoWin or SoXt") ;
408 
409 }
410 #endif
virtual void initInternalViewer(const unsigned int nlig, const unsigned int ncol)
initialize the camera view
void write(const char *fileName)
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:418
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)
Definition: vpServo.cpp:449
error that can be emited by ViSP classes.
Definition: vpException.h:76
void addAbsoluteFrame(float zoom=1)
Add the representation of the absolute frame.
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
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:79
virtual void setSamplingTime(const double &delta_t)
void kill()
Definition: vpServo.cpp:189
void initApplication(void *(*start_routine)(void *))
begin the main program
vpColVector computeControlLaw()
Definition: vpServo.cpp:902
Class that defines the simplest robot: a free flying camera.
void setInternalCameraParameters(vpCameraParameters &cam)
set internal camera parameters
Generic class defining intrinsic camera parameters.
void setLambda(double c)
Definition: vpServo.h:370
void load(const char *file_name)
load an iv file
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:522
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
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:251
Class that defines 2D ellipse visual feature.
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
Class that defines what is a circle.
Definition: vpCircle.h:61
void initExternalViewer(const unsigned int nlig, const unsigned int ncol)
initialize the external view
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:220
void setZoomFactor(const float zoom)
set the size of the camera/frame
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &v)
unsigned int getTaskRank() const
Definition: vpServo.cpp:1567
void setWorldCoordinates(const vpColVector &oP)
Definition: vpCircle.cpp:66