ViSP  2.6.2
servoSimuPoint2DCamVelocity3.cpp
1 /****************************************************************************
2  *
3  * $Id: servoSimuPoint2DCamVelocity3.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 
43 
75 #include <visp/vpMath.h>
76 #include <visp/vpHomogeneousMatrix.h>
77 #include <visp/vpFeaturePoint.h>
78 #include <visp/vpServo.h>
79 #include <visp/vpRobotCamera.h>
80 #include <visp/vpDebug.h>
81 #include <visp/vpFeatureBuilder.h>
82 #include <visp/vpParseArgv.h>
83 
84 #include <stdlib.h>
85 #include <stdio.h>
86 
87 // List of allowed command line options
88 #define GETOPTARGS "h"
89 
98 void usage(const char *name, const char *badparam)
99 {
100  fprintf(stdout, "\n\
101 Simulation of a 2D visual servoing on a point:\n\
102 - eye-in-hand control law,\n\
103 - articular velocity are computed,\n\
104 - without display,\n\
105 - only the X coordinate of the point is selected.\n\
106  \n\
107 SYNOPSIS\n\
108  %s [-h]\n", name);
109 
110  fprintf(stdout, "\n\
111 OPTIONS: Default\n\
112  \n\
113  -h\n\
114  Print the help.\n");
115 
116  if (badparam)
117  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
118 }
119 
130 bool getOptions(int argc, const char **argv)
131 {
132  const char *optarg;
133  int c;
134  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
135 
136  switch (c) {
137  case 'h': usage(argv[0], NULL); return false; break;
138 
139  default:
140  usage(argv[0], optarg);
141  return false; break;
142  }
143  }
144 
145  if ((c == 1) || (c == -1)) {
146  // standalone param or error
147  usage(argv[0], NULL);
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 int
157 main(int argc, const char ** argv)
158 {
159  // Read the command line options
160  if (getOptions(argc, argv) == false) {
161  exit (-1);
162  }
163 
164  vpServo task ;
165  vpRobotCamera robot ;
166 
167 
168  std::cout << std::endl ;
169  std::cout << "-------------------------------------------------------" << std::endl ;
170  std::cout << " Test program for vpServo " <<std::endl ;
171  std::cout << " Eye-in-hand task control, articular velocity are computed" << std::endl ;
172  std::cout << " Simulation " << std::endl ;
173  std::cout << " task : servo a point " << std::endl ;
174  std::cout << "-------------------------------------------------------" << std::endl ;
175  std::cout << std::endl ;
176 
177 
178  vpTRACE("sets the initial camera location " ) ;
179  vpHomogeneousMatrix cMo ;
180  cMo[0][3] = 0.1 ;
181  cMo[1][3] = 0.2 ;
182  cMo[2][3] = 2 ;
183  robot.setPosition(cMo) ;
184 
185 
186  vpTRACE("sets the point coordinates in the world frame " ) ;
187  vpPoint point ;
188  point.setWorldCoordinates(0,0,0) ;
189 
190  vpTRACE("project : computes the point coordinates in the camera frame and its 2D coordinates" ) ;
191  point.track(cMo) ;
192 
193  vpTRACE("sets the current position of the visual feature ") ;
194  vpFeaturePoint p ;
195  vpFeatureBuilder::create(p,point) ; //retrieve x,y and Z of the vpPoint structure
196 
197 
198  vpTRACE("sets the desired position of the visual feature ") ;
199  vpFeaturePoint pd ;
200  pd.buildFrom(0,0,1) ; // buildFrom(x,y,Z) ;
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") ;
207 
208 
209  vpTRACE("Set the position of the camera in the end-effector frame ") ;
210  vpHomogeneousMatrix cMe ;
211  vpVelocityTwistMatrix cVe(cMe) ;
212  task.set_cVe(cVe) ;
213 
214  vpTRACE("Set the Jacobian (expressed in the end-effector frame)") ;
215  vpMatrix eJe ;
216  robot.get_eJe(eJe) ;
217  task.set_eJe(eJe) ;
218 
219  vpTRACE("\t we want to see a point on a point..") ;
220  task.addFeature(p,pd, vpFeaturePoint::selectX()) ;
221 
222  vpTRACE("\t set the gain") ;
223  task.setLambda(1) ;
224 
225 
226  vpTRACE("Display task information " ) ;
227  task.print() ;
228 
229  unsigned int iter=0 ;
230  vpTRACE("\t loop") ;
231  while(iter++ < 100)
232  {
233  std::cout << "---------------------------------------------" << iter <<std::endl ;
234  vpColVector v ;
235 
236 
237  if (iter==1)
238  {
239  vpTRACE("Set the Jacobian (expressed in the end-effector frame)") ;
240  vpTRACE("since q is modified eJe is modified") ;
241  }
242  robot.get_eJe(eJe) ;
243  task.set_eJe(eJe) ;
244 
245 
246  if (iter==1) vpTRACE("\t\t get the robot position ") ;
247  robot.getPosition(cMo) ;
248  if (iter==1) vpTRACE("\t\t new point position ") ;
249  point.track(cMo) ;
250  vpFeatureBuilder::create(p,point) ; //retrieve x,y and Z of the vpPoint structure
251 
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 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
static unsigned int selectX()
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