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