ViSP  2.10.0
simulateCircle2DCamVelocity.cpp
1 /****************************************************************************
2  *
3  * $Id: simulateCircle2DCamVelocity.cpp 5263 2015-02-04 13:43:25Z 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 #include <visp/vpMath.h>
67 #include <visp/vpHomogeneousMatrix.h>
68 #include <visp/vpFeatureEllipse.h>
69 #include <visp/vpCircle.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 "cdi: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 Circle\n\
92  \n\
93 SYNOPSIS\n\
94  %s [-i <input image path>] [-d] [-h]\n", name);
95 
96 
97  fprintf(stdout, "\n\
98 OPTIONS: Default\n\
99  -i <input image path> %s\n\
100  Set image input path.\n\
101  From this path read \"ViSP-images/iv/4points.iv\"\n\
102  cad model.\n\
103  Setting the VISP_INPUT_IMAGE_PATH environment\n\
104  variable produces the same behaviour than using\n\
105  this option.\n\
106  \n\
107  -d \n\
108  Disable the image display. This can be useful \n\
109  for automatic tests using crontab under Unix or \n\
110  using the task manager under Windows.\n\
111  \n\
112  -h\n\
113  Print the help.\n\n",
114  ipath.c_str());
115 
116  if (badparam)
117  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
118 }
119 
135 bool getOptions(int argc, const char **argv, std::string &ipath, bool &display)
136 {
137  const char *optarg;
138  int c;
139  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
140 
141  switch (c) {
142  case 'i': ipath = optarg; break;
143  case 'd': display = false; break;
144  case 'h': usage(argv[0], NULL, ipath); return false; break;
145 
146  default:
147  usage(argv[0], optarg, ipath); return false; break;
148  }
149  }
150 
151  if ((c == 1) || (c == -1)) {
152  // standalone param or error
153  usage(argv[0], NULL, ipath);
154  std::cerr << "ERROR: " << std::endl;
155  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
156  return false;
157  }
158 
159  return true;
160 }
161 
162 static
163 void *mainLoop (void *_simu)
164 {
165  vpSimulator *simu = (vpSimulator *)_simu ;
166  simu->initMainApplication() ;
167 
168  vpPoseVector vcMo ;
169 
170  vcMo[0] = 0.3 ;
171  vcMo[1] = 0.2 ;
172  vcMo[2] = 3 ;
173  vcMo[3] = 0 ;
174  vcMo[4] = vpMath::rad(45) ;
175  vcMo[5] = vpMath::rad(40) ;
176  vpHomogeneousMatrix cMo(vcMo) ; ;
177 
178  vpHomogeneousMatrix cMod ;
179  cMod[0][3] = 0 ;
180  cMod[1][3] = 0 ;
181  cMod[2][3] = 1 ;
182 
183  int it =0 ;
184  unsigned int pos = 2 ;
185  while (pos!=0)
186  {
187  vpServo task ;
188  vpRobotCamera robot ;
189 
190  float sampling_time = 0.040f; // Sampling period in second
191  robot.setSamplingTime(sampling_time);
192  robot.setMaxTranslationVelocity(4.);
193 
194  // Sets the initial camera location
195  robot.setPosition(cMo) ;
196  simu->setCameraPosition(cMo) ;
197 
198  if (pos==1) cMod[2][3] = 0.32 ;
199 
200  // Sets the circle coordinates in the world frame
201  vpCircle circle ;
202  circle.setWorldCoordinates(0,0,1,0,0,0,0.1) ;
203 
204  // Sets the desired position of the visual feature
205  vpFeatureEllipse pd ;
206  circle.track(cMod) ;
207  vpFeatureBuilder::create(pd,circle) ;
208 
209  // Project : computes the circle coordinates in the camera frame and its 2D coordinates
210  // Sets the current position of the visual feature
211  vpFeatureEllipse p ;
212  circle.track(cMo) ;
213  vpFeatureBuilder::create(p,circle) ;
214 
215  // Define the task
216  // We want an eye-in-hand control law
217  // Robot is controlled in the camera frame
220 
221  // We want to see a circle on a circle
222  std::cout << std::endl ;
223  task.addFeature(p,pd) ;
224 
225  // Set the gain
226  task.setLambda(1.0) ;
227 
228  // Display task information
229  task.print() ;
230 
231  vpTime::wait(1000); // Sleep 1s
232 
233  unsigned int iter=0 ;
234  // Visual servoing loop
235  unsigned int itermax ;
236  if (pos==2) itermax = 75 ; else itermax = 100 ;
237  while(iter++ < itermax)
238  {
239  double t = vpTime::measureTimeMs();
240 
241  if (iter==1) std::cout << "get the robot position" << std::endl;
242  robot.getPosition(cMo) ;
243  if (iter==1) std::cout << "new circle position" << std::endl;
244  //retrieve x,y and Z of the vpCircle structure
245 
246  circle.track(cMo) ;
247  vpFeatureBuilder::create(p,circle);
248 
249  if (iter==1) std::cout << "compute the control law" << std::endl;
250  vpColVector v = task.computeControlLaw() ;
251  if (iter==1) {
252  std::cout << "Task rank: " << task.getTaskRank() <<std::endl ;
253  std::cout << "send the camera velocity to the controller" << std::endl;
254  }
256 
257  simu->setCameraPosition(cMo) ;
258 
259  if(SAVE==1)
260  {
261  char name[FILENAME_MAX] ;
262  sprintf(name,"/tmp/image.%04d.external.png",it) ;
263  std::cout << "Save " << name << std::endl ;
264  simu->write(name) ;
265  sprintf(name,"/tmp/image.%04d.internal.png",iter) ;
266  std::cout << "Save " << name << std::endl ;
267  simu->write(name) ;
268  it++ ;
269  }
270  // std::cout << "\t\t || s - s* || "
271  // std::cout << ( task.getError() ).sumSquare() <<std::endl ; ;
272  vpTime::wait(t, sampling_time * 1000); // Wait 40 ms
273 
274  }
275  pos-- ;
276  task.kill();
277  }
278 
279  simu->closeMainApplication() ;
280 
281  void *a=NULL ;
282  return a ;
283 }
284 
285 
286 int
287 main(int argc, const char ** argv)
288 {
289  try {
290  std::string env_ipath;
291  std::string opt_ipath;
292  std::string ipath;
293  std::string filename;
294  std::string username;
295  bool opt_display = true;
296 
297  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH environment variable value
298  env_ipath = vpIoTools::getViSPImagesDataPath();
299 
300  // Set the default input path
301  if (! env_ipath.empty())
302  ipath = env_ipath;
303 
304  // Read the command line options
305  if (getOptions(argc, argv, opt_ipath, opt_display) == false) {
306  exit (-1);
307  }
308 
309  // Get the option values
310  if (!opt_ipath.empty())
311  ipath = opt_ipath;
312 
313  // Compare ipath and env_ipath. If they differ, we take into account
314  // the input path comming from the command line option
315  if (!opt_ipath.empty() && !env_ipath.empty()) {
316  if (ipath != env_ipath) {
317  std::cout << std::endl
318  << "WARNING: " << std::endl;
319  std::cout << " Since -i <visp image path=" << ipath << "> "
320  << " is different from VISP_INPUT_IMAGE_PATH=" << env_ipath << std::endl
321  << " we skip the environment variable." << std::endl;
322  }
323  }
324 
325  // Test if an input path is set
326  if (opt_ipath.empty() && env_ipath.empty()){
327  usage(argv[0], NULL, ipath);
328  std::cerr << std::endl
329  << "ERROR:" << std::endl;
330  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
331  << std::endl
332  << " environment variable to specify the location of the " << std::endl
333  << " image path where test images are located." << std::endl << std::endl;
334  exit(-1);
335  }
336 
337  vpCameraParameters cam ;
338  vpHomogeneousMatrix fMo ; fMo[2][3] = 0 ;
339 
340  if (opt_display) {
341 
342  vpSimulator simu ;
343  simu.initInternalViewer(300, 300) ;
344  simu.initExternalViewer(300, 300) ;
345 
346  vpTime::wait(1000) ;
347  simu.setZoomFactor(1.0f) ;
348  simu.addAbsoluteFrame() ;
349 
350  // Load the cad model
351  filename = vpIoTools::createFilePath(ipath, "ViSP-images/iv/circle.iv");
352  simu.load(filename.c_str(),fMo) ;
353 
354  simu.setInternalCameraParameters(cam) ;
355 
356  simu.initApplication(&mainLoop) ;
357  simu.mainLoop() ;
358 
359  }
360  return 0;
361  }
362  catch(vpException e) {
363  std::cout << "Catch an exception: " << e << std::endl;
364  return 1;
365  }
366 }
367 
368 
369 #else
370 int
371 main()
372 { vpTRACE("You should install Coin3D and SoQT or SoWin or SoXt") ;
373 
374 }
375 #endif
virtual void initInternalViewer(const unsigned int nlig, const unsigned int ncol)
initialize the camera view
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1071
void write(const char *fileName)
void setMaxTranslationVelocity(const double maxVt)
Definition: vpRobot.cpp:242
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.
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:80
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.
static std::string createFilePath(const std::string &parent, const std::string child)
Definition: vpIoTools.cpp:1245
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:1572
void setWorldCoordinates(const vpColVector &oP)
Definition: vpCircle.cpp:66