ViSP  2.6.2
servoSimuPoint2DCamVelocity2.cpp
1 /****************************************************************************
2  *
3  * $Id: servoSimuPoint2DCamVelocity2.cpp 2503 2010-02-16 18:55:01Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2012 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 on a point.
36  *
37  * Authors:
38  * Eric Marchand
39  * Fabien Spindler
40  *
41  *****************************************************************************/
42 
78 #include <visp/vpMath.h>
79 #include <visp/vpHomogeneousMatrix.h>
80 #include <visp/vpFeaturePoint.h>
81 #include <visp/vpPoint.h>
82 #include <visp/vpServo.h>
83 #include <visp/vpRobotCamera.h>
84 #include <visp/vpDebug.h>
85 #include <visp/vpFeatureBuilder.h>
86 #include <visp/vpParseArgv.h>
87 #include <stdlib.h>
88 #include <stdio.h>
89 // List of allowed command line options
90 #define GETOPTARGS "h"
91 
100 void usage(const char *name, const char *badparam)
101 {
102  fprintf(stdout, "\n\
103 Simulation of a 2D visual servoing on a point:\n\
104 - eye-in-hand control law,\n\
105 - articular velocity are computed,\n\
106 - without display.\n\
107 \n\
108 SYNOPSIS\n\
109  %s [-h]\n", name);
110 
111  fprintf(stdout, "\n\
112 OPTIONS: Default\n\
113 \n\
114  -h\n\
115  Print the help.\n");
116 
117  if (badparam)
118  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
119 }
120 
131 bool getOptions(int argc, const char **argv)
132 {
133  const char *optarg;
134  int c;
135  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
136 
137  switch (c) {
138  case 'h': usage(argv[0], NULL); return false; break;
139 
140  default:
141  usage(argv[0], optarg);
142  return false; break;
143  }
144  }
145 
146  if ((c == 1) || (c == -1)) {
147  // standalone param or error
148  usage(argv[0], NULL);
149  std::cerr << "ERROR: " << std::endl;
150  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
151  return false;
152  }
153 
154  return true;
155 }
156 
157 int
158 main(int argc, const char ** argv)
159 {
160  // Read the command line options
161  if (getOptions(argc, argv) == false) {
162  exit (-1);
163  }
164 
165  vpServo task ;
166  vpRobotCamera robot ;
167 
168 
169  std::cout << std::endl ;
170  std::cout << "-------------------------------------------------------" << std::endl ;
171  std::cout << " Test program for vpServo " <<std::endl ;
172  std::cout << " Eye-in-hand task control, articular velocity are computed" << std::endl ;
173  std::cout << " Simulation " << std::endl ;
174  std::cout << " task : servo a point " << std::endl ;
175  std::cout << "-------------------------------------------------------" << std::endl ;
176  std::cout << std::endl ;
177 
178 
179  vpTRACE("sets the initial camera location " ) ;
180  vpHomogeneousMatrix cMo ;
181  cMo[0][3] = 0.1 ;
182  cMo[1][3] = 0.2 ;
183  cMo[2][3] = 2 ;
184  robot.setPosition(cMo) ;
185 
186 
187  vpTRACE("sets the point coordinates in the world frame " ) ;
188  vpPoint point ;
189  point.setWorldCoordinates(0,0,0) ;
190 
191  vpTRACE("project : computes the point coordinates in the camera frame and its 2D coordinates" ) ;
192  point.track(cMo) ;
193 
194  vpTRACE("sets the current position of the visual feature ") ;
195  vpFeaturePoint p ;
196  vpFeatureBuilder::create(p,point) ; //retrieve x,y and Z of the vpPoint structure
197 
198  vpTRACE("sets the desired position of the visual feature ") ;
199  vpFeaturePoint pd ;
200  pd.buildFrom(0,0,1) ;
201 
202 
203  vpTRACE("define the task") ;
204  vpTRACE("\t we want an eye-in-hand control law") ;
205  vpTRACE("\t articular velocity are computed") ;
208 
209 
210  vpTRACE("Set the position of the camera in the end-effector frame ") ;
211  vpHomogeneousMatrix cMe ;
212  vpVelocityTwistMatrix cVe(cMe) ;
213  task.set_cVe(cVe) ;
214 
215  vpTRACE("Set the Jacobian (expressed in the end-effector frame)") ;
216  vpMatrix eJe ;
217  robot.get_eJe(eJe) ;
218  task.set_eJe(eJe) ;
219 
220  vpTRACE("\t we want to see a point on a point..") ;
221  task.addFeature(p,pd) ;
222 
223  vpTRACE("\t set the gain") ;
224  task.setLambda(1) ;
225 
226 
227  vpTRACE("Display task information " ) ;
228  task.print() ;
229 
230  unsigned int iter=0 ;
231  vpTRACE("\t loop") ;
232  while(iter++<100)
233  {
234  std::cout << "---------------------------------------------" << iter <<std::endl ;
235  vpColVector v ;
236 
237 
238  if (iter==1)
239  {
240  vpTRACE("Set the Jacobian (expressed in the end-effector frame)") ;
241  vpTRACE("since q is modified eJe is modified") ;
242  }
243  robot.get_eJe(eJe) ;
244  task.set_eJe(eJe) ;
245 
246 
247  if (iter==1) vpTRACE("\t\t get the robot position ") ;
248  robot.getPosition(cMo) ;
249  if (iter==1) vpTRACE("\t\t new point position ") ;
250  point.track(cMo) ;
251  vpFeatureBuilder::create(p,point) ; //retrieve x,y and Z of the vpPoint structure
252 
253 
254 
255  if (iter==1) vpTRACE("\t\t compute the control law ") ;
256  v = task.computeControlLaw() ;
257 
258  if (iter==1)
259  {
260  vpTRACE("Display task information " ) ;
261  task.print() ;
262  }
263 
264  if (iter==1) vpTRACE("\t\t send the camera velocity to the controller ") ;
266 
267  vpTRACE("\t\t || s - s* || ") ;
268  std::cout << ( task.getError() ).sumSquare() <<std::endl ; ;
269  }
270 
271  vpTRACE("Display task information " ) ;
272  task.print() ;
273  task.kill();
274 }
275 
Definition of the vpMatrix class.
Definition: vpMatrix.h:96
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 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:250
void track(const vpHomogeneousMatrix &cMo)
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
void set_cVe(vpVelocityTwistMatrix &_cVe)
Definition: vpServo.h:227
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()
destruction (memory deallocation if required)
Definition: vpServo.cpp:177
vpColVector getError() const
Definition: vpServo.h:298
vpColVector computeControlLaw()
compute the desired control law
Definition: vpServo.cpp:883
Class that defines the simplest robot: a free flying camera.
Definition: vpRobotCamera.h:65
void set_eJe(vpMatrix &_eJe)
Definition: vpServo.h:235
void getPosition(vpColVector &q)
Class that consider the particular case of twist transformation matrix that allows to transform a vel...
void setPosition(const vpRobot::vpControlFrameType, const vpColVector &)
Set a displacement (frame has to be specified) in position control.
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
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 get_eJe(vpMatrix &_eJe)
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:258
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
Class required to compute the visual servoing control law.
Definition: vpServo.h:150
void setServo(vpServoType _servo_type)
Choice of the visual servoing control law.
Definition: vpServo.cpp:214
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