ViSP  2.8.0
simulateCircle2DCamVelocity.cpp
1 /****************************************************************************
2  *
3  * $Id: simulateCircle2DCamVelocity.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 
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 
193 
194  vpServo task ;
195  vpRobotCamera robot ;
196 
197  float sampling_time = 0.040f; // Sampling period in second
198  robot.setSamplingTime(sampling_time);
199 
200  /* std::cout << std::endl ;
201  std::cout << "-----------------------" << std::endl ;
202  std::cout << " Test program for vpServo " <<std::endl ;
203  std::cout << " Simulation " << std::endl ;
204  std::cout << " task : servo a circle " << std::endl ;
205  std::cout << "-----------------------" << std::endl ;
206  std::cout << std::endl ;
207 */
208 
209  vpTRACE("sets the initial camera location " ) ;
210 
211 
212  robot.setPosition(cMo) ;
213  simu->setCameraPosition(cMo) ;
214 
215 
216  if (pos==1) cMod[2][3] = 0.32 ;
217 
218  vpTRACE("sets the circle coordinates in the world frame " ) ;
219  vpCircle circle ;
220  circle.setWorldCoordinates(0,0,1,0,0,0,0.1) ;
221 
222  vpTRACE("sets the desired position of the visual feature ") ;
223  vpFeatureEllipse pd ;
224  circle.track(cMod) ;
225  vpFeatureBuilder::create(pd,circle) ;
226 
227  vpTRACE("project : computes the circle coordinates in the camera frame and its 2D coordinates" ) ;
228 
229  vpTRACE("sets the current position of the visual feature ") ;
230  vpFeatureEllipse p ;
231  circle.track(cMo) ;
232  vpFeatureBuilder::create(p,circle) ;
233 
234  vpTRACE("define the task") ;
235  vpTRACE("\t we want an eye-in-hand control law") ;
236  vpTRACE("\t robot is controlled in the camera frame") ;
239 
240  vpTRACE("\t we want to see a circle on a circle..") ;
241  std::cout << std::endl ;
242  task.addFeature(p,pd) ;
243 
244  vpTRACE("\t set the gain") ;
245 
246  task.setLambda(1.0) ;
247  // if (pos==2)
248  // task.setLambda(0.0251) ;
249  // else
250  // task.setLambda(0.0251) ;
251 
252 
253  vpTRACE("Display task information " ) ;
254  task.print() ;
255 
256  vpTime::wait(1000); // Sleep 1s
257 
258  std::cout << "\nEnter a character to continue... " <<std::endl ;
259  { int a ; std::cin >> a ; }
260 
261 
262  unsigned int iter=0 ;
263  vpTRACE("\t loop") ;
264  unsigned int itermax ;
265  if (pos==2) itermax = 75 ; else itermax = 100 ;
266  char name[FILENAME_MAX] ;
267  while(iter++<itermax)
268  {
269  double t = vpTime::measureTimeMs();
270  std::cout << "---------------------------------------------"
271  << iter <<std::endl ;
272  vpColVector v ;
273 
274  if (iter==1) vpTRACE("\t\t get the robot position ") ;
275  robot.getPosition(cMo) ;
276  if (iter==1) vpTRACE("\t\t new circle position ") ;
277  //retrieve x,y and Z of the vpCircle structure
278 
279  circle.track(cMo) ;
280  vpFeatureBuilder::create(p,circle);
281 
282  if (iter==1) vpTRACE("\t\t compute the control law ") ;
283  v = task.computeControlLaw() ;
284  // vpTRACE("computeControlLaw" ) ;
285  std::cout << "Task rank: " << task.getTaskRank() <<std::endl ;
286  if (iter==1)
287  vpTRACE("\t\t send the camera velocity to the controller ") ;
289 
290  simu->setCameraPosition(cMo) ;
291 
292  if(SAVE==1)
293  {
294  sprintf(name,"/tmp/image.%04d.external.png",it) ;
295  std::cout << "Save " << name << std::endl ;
296  simu->write(name) ;
297  sprintf(name,"/tmp/image.%04d.internal.png",iter) ;
298  std::cout << "Save " << name << std::endl ;
299  simu->write(name) ;
300  it++ ;
301  }
302  // vpTRACE("\t\t || s - s* || ") ;
303  // std::cout << ( task.getError() ).sumSquare() <<std::endl ; ;
304  vpTime::wait(t, sampling_time * 1000); // Wait 40 ms
305 
306  }
307  pos-- ;
308  task.kill();
309 
310  }
311 
312 
313  simu->closeMainApplication() ;
314 
315  void *a=NULL ;
316  return a ;
317  // return (void *);
318 }
319 
320 
321 int
322 main(int argc, const char ** argv)
323 {
324  std::string env_ipath;
325  std::string opt_ipath;
326  std::string ipath;
327  std::string filename;
328  std::string username;
329  bool opt_display = true;
330 
331  // Get the VISP_IMAGE_PATH environment variable value
332  char *ptenv = getenv("VISP_INPUT_IMAGE_PATH");
333  if (ptenv != NULL)
334  env_ipath = ptenv;
335 
336  // Set the default input path
337  if (! env_ipath.empty())
338  ipath = env_ipath;
339 
340  // Read the command line options
341  if (getOptions(argc, argv, opt_ipath, opt_display) == false) {
342  exit (-1);
343  }
344 
345  // Get the option values
346  if (!opt_ipath.empty())
347  ipath = opt_ipath;
348 
349  // Compare ipath and env_ipath. If they differ, we take into account
350  // the input path comming from the command line option
351  if (!opt_ipath.empty() && !env_ipath.empty()) {
352  if (ipath != env_ipath) {
353  std::cout << std::endl
354  << "WARNING: " << std::endl;
355  std::cout << " Since -i <visp image path=" << ipath << "> "
356  << " is different from VISP_INPUT_IMAGE_PATH=" << env_ipath << std::endl
357  << " we skip the environment variable." << std::endl;
358  }
359  }
360 
361  // Test if an input path is set
362  if (opt_ipath.empty() && env_ipath.empty()){
363  usage(argv[0], NULL, ipath);
364  std::cerr << std::endl
365  << "ERROR:" << std::endl;
366  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
367  << std::endl
368  << " environment variable to specify the location of the " << std::endl
369  << " image path where test images are located." << std::endl << std::endl;
370  exit(-1);
371  }
372 
373  vpCameraParameters cam ;
374  vpHomogeneousMatrix fMo ; fMo[2][3] = 0 ;
375 
376  if (opt_display) {
377 
378  vpSimulator simu ;
379  simu.initInternalViewer(300, 300) ;
380  simu.initExternalViewer(300, 300) ;
381 
382  vpTime::wait(1000) ;
383  simu.setZoomFactor(1.0f) ;
384  simu.addAbsoluteFrame() ;
385 
386  // Load the cad model
387  filename = ipath + vpIoTools::path("/ViSP-images/iv/circle.iv");
388  simu.load(filename.c_str(),fMo) ;
389 
390  simu.setInternalCameraParameters(cam) ;
391 
392  simu.initApplication(&mainLoop) ;
393  simu.mainLoop() ;
394 
395  }
396 }
397 
398 
399 #else
400 int
401 main()
402 { vpTRACE("You should install Coin3D and SoQT or SoWin or SoXt") ;
403 
404 }
405 #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: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
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()
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
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 load(const char *file_name)
load an iv file
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
The pose is a complete representation of every rigid motion in the euclidian space.
Definition: vpPoseVector.h:92
double getTaskRank() const
Definition: vpServo.h:389
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:258
Class that defines 2D ellipse visual feature.
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
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 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 vpColVector &oP)
Definition: vpCircle.cpp:66