ViSP  2.6.2
servoSimuPoint3DCamVelocity.cpp
1 /****************************************************************************
2  *
3  * $Id: servoSimuPoint3DCamVelocity.cpp 2457 2010-01-07 10:41:18Z nmelchio $
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 3D visual servoing on a 3D point.
36  *
37  * Authors:
38  * Eric Marchand
39  * Fabien Spindler
40  *
41  *****************************************************************************/
42 
65 #include <visp/vpMath.h>
66 #include <visp/vpHomogeneousMatrix.h>
67 #include <visp/vpFeaturePoint3D.h>
68 #include <visp/vpPoint.h>
69 #include <visp/vpServo.h>
70 #include <visp/vpRobotCamera.h>
71 #include <visp/vpDebug.h>
72 #include <visp/vpParseArgv.h>
73 #include <stdlib.h>
74 #include <stdio.h>
75 // List of allowed command line options
76 #define GETOPTARGS "h"
77 
86 void usage(const char *name, const char *badparam)
87 {
88  fprintf(stdout, "\n\
89 Simulation of a 3D visual servoing:\n\
90 - servo a 3D point,\n\
91 - eye-in-hand control law,\n\
92 - velocity computed in the camera frame,\n\
93 - without display.\n\
94  \n\
95 SYNOPSIS\n\
96  %s [-h]\n", name);
97 
98  fprintf(stdout, "\n\
99 OPTIONS: Default\n\
100  \n\
101  -h\n\
102  Print the help.\n");
103 
104  if (badparam)
105  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
106 }
107 
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  // Read the command line options
147  if (getOptions(argc, argv) == false) {
148  exit (-1);
149  }
150 
151  vpServo task ;
152  vpRobotCamera robot ;
153 
154  std::cout << std::endl ;
155  std::cout << "-------------------------------------------------------" << std::endl ;
156  std::cout << " Test program for vpServo " <<std::endl ;
157  std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl ;
158  std::cout << " Simulation " << std::endl ;
159  std::cout << " task : servo a 3D point " << std::endl ;
160  std::cout << "-------------------------------------------------------" << std::endl ;
161  std::cout << std::endl ;
162 
163 
164  vpTRACE("sets the initial camera location " ) ;
165  vpHomogeneousMatrix cMo ;
166  cMo[0][3] = 0.1 ;
167  cMo[1][3] = 0.2 ;
168  cMo[2][3] = 2 ;
169  robot.setPosition(cMo) ;
170 
171 
172  vpTRACE("sets the point coordinates in the world frame " ) ;
173  vpPoint point ;
174  point.setWorldCoordinates(0,0,0) ;
175 
176 
177  vpTRACE("project : computes the point coordinates in the camera frame " ) ;
178  point.track(cMo) ;
179 
180  std::cout << point.cP.t() ;
181 
182  vpFeaturePoint3D p ;
183  p.buildFrom(point) ;
184 
185  vpTRACE("sets the desired position of the point ") ;
186  vpFeaturePoint3D pd ;
187  pd.set_XYZ(0,0,1) ;
188 
189 
190 
191  vpTRACE("define the task") ;
192  vpTRACE("\t we want an eye-in-hand control law") ;
193  vpTRACE("\t robot is controlled in the camera frame") ;
195 
196  vpTRACE("\t we want to see a point on a point..") ;
197  std::cout << std::endl ;
198  task.addFeature(p,pd) ;
199 
200  vpTRACE("\t set the gain") ;
201  task.setLambda(1) ;
202 
203 
204  vpTRACE("Display task information " ) ;
205  task.print() ;
206 
207  unsigned int iter=0 ;
208  vpTRACE("\t loop") ;
209  while(iter++<200)
210  {
211  std::cout << "---------------------------------------------" << iter <<std::endl ;
212  vpColVector v ;
213 
214  if (iter==1) vpTRACE("\t\t get the robot position ") ;
215  robot.getPosition(cMo) ;
216  if (iter==1) vpTRACE("\t\t new point position ") ;
217  point.track(cMo) ;
218  p.buildFrom(point) ;
219  // std::cout << p.cP.t() ;
220  // std::cout << (p.get_s()).t() ;
221 
222 
223  if (iter==1) vpTRACE("\t\t compute the control law ") ;
224  v = task.computeControlLaw() ;
225  // vpTRACE("computeControlLaw" ) ;
226 
227  if (iter==1) vpTRACE("\t\t send the camera velocity to the controller ") ;
229 
230  // vpTRACE("\t\t || s - s* || ") ;
231  std::cout << ( task.getError() ).sumSquare() <<std::endl ; ;
232  }
233 
234  vpTRACE("Display task information " ) ;
235  task.print() ;
236  task.kill();
237 }
238 
void set_XYZ(const double X, const double Y, const double Z)
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)
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
void buildFrom(const vpPoint &p)
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
vpColVector cP
Definition: vpTracker.h:82
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
Class that defines the 3D point visual feature.
vpRowVector t() const
transpose of Vector
void getPosition(vpColVector &q)
void setPosition(const vpRobot::vpControlFrameType, const vpColVector &)
Set a displacement (frame has to be specified) in position control.
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 print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:258
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