ViSP  2.9.0
servoSimuFourPoints2DCamVelocity.cpp
1 /****************************************************************************
2  *
3  * $Id: servoSimuFourPoints2DCamVelocity.cpp 2503 2010-02-16 18:55:01Z 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 2D visual servoing using 4 points as visual feature.
36  *
37  * Authors:
38  * Eric Marchand
39  * Fabien Spindler
40  *
41  *****************************************************************************/
42 
57 #include <stdlib.h>
58 #include <stdio.h>
59 
60 #include <visp/vpConfig.h>
61 #include <visp/vpFeatureBuilder.h>
62 #include <visp/vpFeaturePoint.h>
63 #include <visp/vpHomogeneousMatrix.h>
64 #include <visp/vpMath.h>
65 #include <visp/vpParseArgv.h>
66 #include <visp/vpServo.h>
67 #include <visp/vpSimulatorCamera.h>
68 
69 // List of allowed command line options
70 #define GETOPTARGS "h"
71 
72 void usage(const char *name, const char *badparam);
73 bool getOptions(int argc, const char **argv);
74 
83 void usage(const char *name, const char *badparam)
84 {
85  fprintf(stdout, "\n\
86 Simulation of a 2D visual servoing:\n\
87 - servo on 4 points,\n\
88 - eye-in-hand control law,\n\
89 - articular velocity are computed,\n\
90 - without display.\n\
91  \n\
92 SYNOPSIS\n\
93  %s [-h]\n", name);
94 
95  fprintf(stdout, "\n\
96 OPTIONS: Default\n\
97  \n\
98  -h\n\
99  Print the help.\n");
100 
101  if (badparam) {
102  fprintf(stderr, "ERROR: \n" );
103  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
104  }
105 }
106 
117 bool getOptions(int argc, const char **argv)
118 {
119  const char *optarg_;
120  int c;
121  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
122 
123  switch (c) {
124  case 'h': usage(argv[0], NULL); return false; break;
125 
126  default:
127  usage(argv[0], optarg_);
128  return false; break;
129  }
130  }
131 
132  if ((c == 1) || (c == -1)) {
133  // standalone param or error
134  usage(argv[0], NULL);
135  std::cerr << "ERROR: " << std::endl;
136  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
137  return false;
138  }
139 
140  return true;
141 }
142 
143 int
144 main(int argc, const char ** argv)
145 {
146  try {
147  // Read the command line options
148  if (getOptions(argc, argv) == false) {
149  exit (-1);
150  }
151 
152  int i ;
153  vpServo task ;
154  vpSimulatorCamera robot ;
155 
156 
157  std::cout << std::endl ;
158  std::cout << "-------------------------------------------------------" << std::endl ;
159  std::cout << " Test program for vpServo " <<std::endl ;
160  std::cout << " Eye-in-hand task control, articular velocity are computed" << std::endl ;
161  std::cout << " Simulation " << std::endl ;
162  std::cout << " task : servo 4 points " << std::endl ;
163  std::cout << "-------------------------------------------------------" << std::endl ;
164  std::cout << std::endl ;
165 
166  // sets the initial camera location with respect to the object
167  vpHomogeneousMatrix cMo ;
168  cMo[0][3] = 0.1 ;
169  cMo[1][3] = 0.2 ;
170  cMo[2][3] = 2 ;
171 
172  // Compute the position of the object in the world frame
173  vpHomogeneousMatrix wMc, wMo;
174  robot.getPosition(wMc) ;
175  wMo = wMc * cMo;
176 
177  // sets the point coordinates in the object frame
178  vpPoint point[4] ;
179  point[0].setWorldCoordinates(-1,-1,0) ;
180  point[1].setWorldCoordinates(1,-1,0) ;
181  point[2].setWorldCoordinates(1,1,0) ;
182  point[3].setWorldCoordinates(-1,1,0) ;
183 
184  // computes the point coordinates in the camera frame and its 2D coordinates
185  for (i = 0 ; i < 4 ; i++)
186  point[i].track(cMo) ;
187 
188  // sets the desired position of the point
189  vpFeaturePoint p[4] ;
190  for (i = 0 ; i < 4 ; i++)
191  vpFeatureBuilder::create(p[i], point[i]) ; //retrieve x,y and Z of the vpPoint structure
192 
193  // sets the desired position of the point
194  vpFeaturePoint pd[4] ;
195 
196  pd[0].buildFrom(-0.1,-0.1, 1) ;
197  pd[1].buildFrom( 0.1,-0.1, 1) ;
198  pd[2].buildFrom( 0.1, 0.1, 1) ;
199  pd[3].buildFrom(-0.1, 0.1, 1) ;
200 
201  // define the task
202  // - we want an eye-in-hand control law
203  // - articular velocity are computed
206 
207  // Set the position of the camera in the end-effector frame
208  vpHomogeneousMatrix cMe ;
209  vpVelocityTwistMatrix cVe(cMe) ;
210  task.set_cVe(cVe) ;
211 
212  // Set the Jacobian (expressed in the end-effector frame)
213  vpMatrix eJe ;
214  robot.get_eJe(eJe) ;
215  task.set_eJe(eJe) ;
216 
217  // we want to see a point on a point
218  for (i = 0 ; i < 4 ; i++)
219  task.addFeature(p[i],pd[i]) ;
220 
221  // set the gain
222  task.setLambda(1) ;
223 
224  // Display task information
225  task.print() ;
226 
227  unsigned int iter=0 ;
228  // loop
229  while(iter++<1500)
230  {
231  std::cout << "---------------------------------------------" << iter <<std::endl ;
232  vpColVector v ;
233 
234  // Set the Jacobian (expressed in the end-effector frame)
235  // since q is modified eJe is modified
236  robot.get_eJe(eJe) ;
237  task.set_eJe(eJe) ;
238 
239  // get the robot position
240  robot.getPosition(wMc) ;
241  // Compute the position of the camera wrt the object frame
242  cMo = wMc.inverse() * wMo;
243 
244  // update new point position and corresponding features
245  for (i = 0 ; i < 4 ; i++)
246  {
247  point[i].track(cMo) ;
248  //retrieve x,y and Z of the vpPoint structure
249  vpFeatureBuilder::create(p[i],point[i]) ;
250  }
251  // since vpServo::MEAN interaction matrix is used, we need also to update the desired features at each iteration
252  pd[0].buildFrom(-0.1,-0.1, 1) ;
253  pd[1].buildFrom( 0.1,-0.1, 1) ;
254  pd[2].buildFrom( 0.1, 0.1, 1) ;
255  pd[3].buildFrom(-0.1, 0.1, 1) ;
256 
257  // compute the control law ") ;
258  v = task.computeControlLaw() ;
259 
260  // send the camera velocity to the controller ") ;
262 
263  std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() << std::endl;
264  }
265 
266  // Display task information
267  task.print() ;
268  task.kill();
269  return 0;
270  }
271  catch(vpException e) {
272  std::cout << "Catch a ViSP exception: " << e << std::endl;
273  return 1;
274  }
275 }
276 
Definition of the vpMatrix class.
Definition: vpMatrix.h:98
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
Class that defines the simplest robot: a free flying camera.
void set_eJe(const vpMatrix &eJe_)
Definition: vpServo.h:439
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 bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:79
Class that defines what is a point.
Definition: vpPoint.h:65
void kill()
Definition: vpServo.cpp:189
vpColVector getError() const
Definition: vpServo.h:257
vpColVector computeControlLaw()
Definition: vpServo.cpp:902
void getPosition(vpHomogeneousMatrix &wMc) const
void setLambda(double c)
Definition: vpServo.h:370
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
void buildFrom(const double x, const double y, const double Z)
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 set_cVe(const vpVelocityTwistMatrix &cVe_)
Definition: vpServo.h:414
vpHomogeneousMatrix inverse() const
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 get_eJe(vpMatrix &eJe)
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:220
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