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