ViSP  2.6.2
servoSimuPoint2DCamVelocity1.cpp
1 /****************************************************************************
2  *
3  * $Id: servoSimuPoint2DCamVelocity1.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 2D visual servoing on a point.
36  *
37  * Authors:
38  * Eric Marchand
39  * Fabien Spindler
40  *
41  *****************************************************************************/
42 
61 #include <visp/vpMath.h>
62 #include <visp/vpHomogeneousMatrix.h>
63 #include <visp/vpFeaturePoint.h>
64 #include <visp/vpPoint.h>
65 #include <visp/vpServo.h>
66 #include <visp/vpRobotCamera.h>
67 #include <visp/vpFeatureBuilder.h>
68 
69 
70 // Exception
71 #include <visp/vpException.h>
72 #include <visp/vpMatrixException.h>
73 
74 // Debug trace
75 #include <visp/vpDebug.h>
76 #include <visp/vpParseArgv.h>
77 
78 #include <stdlib.h>
79 #include <stdio.h>
80 // List of allowed command line options
81 #define GETOPTARGS "h"
82 
91 void usage(const char *name, const char *badparam)
92 {
93  fprintf(stdout, "\n\
94 Simulation of a 2D visual servoing on a point:\n\
95 - eye-in-hand control law,\n\
96 - velocity computed in the camera frame,\n\
97 - without display.\n\
98  \n\
99 SYNOPSIS\n\
100  %s [-h]\n", name);
101 
102  fprintf(stdout, "\n\
103 OPTIONS: Default\n\
104  \n\
105  -h\n\
106  Print the help.\n");
107 
108  if (badparam)
109  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
110 }
111 
122 bool getOptions(int argc, const char **argv)
123 {
124  const char *optarg;
125  int c;
126  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
127 
128  switch (c) {
129  case 'h': usage(argv[0], NULL); return false; break;
130 
131  default:
132  usage(argv[0], optarg);
133  return false; break;
134  }
135  }
136 
137  if ((c == 1) || (c == -1)) {
138  // standalone param or error
139  usage(argv[0], NULL);
140  std::cerr << "ERROR: " << std::endl;
141  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
142  return false;
143  }
144 
145  return true;
146 }
147 
148 int
149 main(int argc, const char ** argv)
150 {
151  // Read the command line options
152  if (getOptions(argc, argv) == false) {
153  exit (-1);
154  }
155 
156  vpServo task ;
157  vpRobotCamera robot ;
158 
159  vpTRACE("sets the initial camera location " ) ;
160  vpHomogeneousMatrix cMo ;
161  cMo[0][3] = 0.1 ;
162  cMo[1][3] = 0.2 ;
163  cMo[2][3] = 2 ;
164  robot.setPosition(cMo) ;
165 
166 
167  vpTRACE("sets the point coordinates in the world frame " ) ;
168  vpPoint point ;
169  point.setWorldCoordinates(0,0,0) ;
170 
171  vpTRACE("project : computes the point coordinates in the camera frame and its 2D coordinates" ) ;
172  point.track(cMo) ;
173 
174  vpTRACE("sets the current position of the visual feature ") ;
175  vpFeaturePoint p ;
176  vpFeatureBuilder::create(p,point) ; //retrieve x,y and Z of the vpPoint structure
177 
178  vpTRACE("sets the desired position of the visual feature ") ;
179  vpFeaturePoint pd ;
180  pd.buildFrom(0,0,1) ; // buildFrom(x,y,Z) ;
181 
182  vpTRACE("define the task") ;
183  vpTRACE("\t we want an eye-in-hand control law") ;
184  vpTRACE("\t robot is controlled in the camera frame") ;
186 
187  vpTRACE("\t we want to see a point on a point..") ;
188  std::cout << std::endl ;
189  task.addFeature(p,pd) ;
190 
191  vpTRACE("\t set the gain") ;
192  task.setLambda(1) ;
193 
194 
195  vpTRACE("Display task information " ) ;
196  task.print() ;
197 
198  unsigned int iter=0 ;
199  vpTRACE("\t loop") ;
200  while(iter++<100)
201  {
202  std::cout << "---------------------------------------------" << iter <<std::endl ;
203  vpColVector v ;
204 
205  if (iter==1) vpTRACE("\t\t get the robot position ") ;
206  robot.getPosition(cMo) ;
207  if (iter==1) vpTRACE("\t\t new point position ") ;
208  //retrieve x,y and Z of the vpPoint structure
209 
210  point.track(cMo) ;
211  vpFeatureBuilder::create(p,point);
212 
213  if (iter==1) vpTRACE("\t\t compute the control law ") ;
214  v = task.computeControlLaw() ;
215  // vpTRACE("computeControlLaw" ) ;
216 
217  if (iter==1) vpTRACE("\t\t send the camera velocity to the controller ") ;
219 
220  vpTRACE("\t\t || s - s* || ") ;
221  std::cout << ( task.getError() ).sumSquare() <<std::endl ; ;
222  }
223 
224  vpTRACE("Display task information " ) ;
225  task.print() ;
226  task.kill();
227 }
228 
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)
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 getPosition(vpColVector &q)
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 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