ViSP  2.8.0
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 - 2013 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 
59 #include <stdlib.h>
60 #include <stdio.h>
61 
62 #include <visp/vpFeatureBuilder.h>
63 #include <visp/vpFeaturePoint.h>
64 #include <visp/vpHomogeneousMatrix.h>
65 #include <visp/vpMath.h>
66 #include <visp/vpParseArgv.h>
67 #include <visp/vpServo.h>
68 #include <visp/vpSimulatorCamera.h>
69 
70 // List of allowed command line options
71 #define GETOPTARGS "h"
72 
81 void usage(const char *name, const char *badparam)
82 {
83  fprintf(stdout, "\n\
84 Simulation of a 2D visual servoing on a point:\n\
85 - eye-in-hand control law,\n\
86 - articular velocity are computed,\n\
87 - without display.\n\
88 \n\
89 SYNOPSIS\n\
90  %s [-h]\n", name);
91 
92  fprintf(stdout, "\n\
93 OPTIONS: Default\n\
94 \n\
95  -h\n\
96  Print the help.\n");
97 
98  if (badparam)
99  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
100 }
101 
112 bool getOptions(int argc, const char **argv)
113 {
114  const char *optarg;
115  int c;
116  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
117 
118  switch (c) {
119  case 'h': usage(argv[0], NULL); return false; break;
120 
121  default:
122  usage(argv[0], optarg);
123  return false; break;
124  }
125  }
126 
127  if ((c == 1) || (c == -1)) {
128  // standalone param or error
129  usage(argv[0], NULL);
130  std::cerr << "ERROR: " << std::endl;
131  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
132  return false;
133  }
134 
135  return true;
136 }
137 
138 int
139 main(int argc, const char ** argv)
140 {
141  // Read the command line options
142  if (getOptions(argc, argv) == false) {
143  exit (-1);
144  }
145 
146  vpServo task ;
147  vpSimulatorCamera robot ;
148 
149 
150  std::cout << std::endl ;
151  std::cout << "-------------------------------------------------------" << std::endl ;
152  std::cout << " Test program for vpServo " <<std::endl ;
153  std::cout << " Eye-in-hand task control, articular velocity are computed" << std::endl ;
154  std::cout << " Simulation " << std::endl ;
155  std::cout << " task : servo a point " << std::endl ;
156  std::cout << "-------------------------------------------------------" << std::endl ;
157  std::cout << std::endl ;
158 
159  // 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  // Compute the position of the object in the world frame
165  vpHomogeneousMatrix wMc, wMo;
166  robot.getPosition(wMc) ;
167  wMo = wMc * cMo;
168 
169  // sets the point coordinates in the world frame
170  vpPoint point ;
171  point.setWorldCoordinates(0,0,0) ;
172 
173  // computes the point coordinates in the camera frame and its 2D coordinates
174  point.track(cMo) ;
175 
176  // sets the current position of the visual feature
177  vpFeaturePoint p ;
178  vpFeatureBuilder::create(p,point) ; //retrieve x,y and Z of the vpPoint structure
179 
180  // sets the desired position of the visual feature
181  vpFeaturePoint pd ;
182  pd.buildFrom(0,0,1) ;
183 
184  // define the task
185  // - we want an eye-in-hand control law
186  // - articular velocity are computed
189 
190  // Set the position of the camera in the end-effector frame
191  vpHomogeneousMatrix cMe ;
192  vpVelocityTwistMatrix cVe(cMe) ;
193  task.set_cVe(cVe) ;
194 
195  // Set the Jacobian (expressed in the end-effector frame)
196  vpMatrix eJe ;
197  robot.get_eJe(eJe) ;
198  task.set_eJe(eJe) ;
199 
200  // we want to see a point on a point
201  task.addFeature(p,pd) ;
202 
203  // set the gain
204  task.setLambda(1) ;
205  // Display task information
206  task.print() ;
207 
208  unsigned int iter=0 ;
209  // loop
210  while(iter++<100)
211  {
212  std::cout << "---------------------------------------------" << iter <<std::endl ;
213  vpColVector v ;
214 
215  // Set the Jacobian (expressed in the end-effector frame)
216  // since q is modified eJe is modified
217  robot.get_eJe(eJe) ;
218  task.set_eJe(eJe) ;
219 
220  // get the robot position
221  robot.getPosition(wMc) ;
222  // Compute the position of the camera wrt the object frame
223  cMo = wMc.inverse() * wMo;
224 
225  // new point position
226  point.track(cMo) ;
227  vpFeatureBuilder::create(p,point) ; //retrieve x,y and Z of the vpPoint structure
228  pd.buildFrom(0,0,1) ; // Since vpServo::MEAN interaction matrix is used, we need to update the desired feature at each iteration
229 
230  // compute the control law
231  v = task.computeControlLaw() ;
232 
233  // send the camera velocity to the controller
235 
236  std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() <<std::endl ;
237  }
238 
239  // Display task information
240  task.print() ;
241  task.kill();
242 }
243 
Definition of the vpMatrix class.
Definition: vpMatrix.h:96
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 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:253
void track(const vpHomogeneousMatrix &cMo)
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
void set_cVe(vpVelocityTwistMatrix &_cVe)
Definition: vpServo.h:230
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:301
vpColVector computeControlLaw()
compute the desired control law
Definition: vpServo.cpp:883
void set_eJe(vpMatrix &_eJe)
Definition: vpServo.h:238
void getPosition(vpHomogeneousMatrix &wMc) const
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)
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
vpHomogeneousMatrix inverse() const
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 descbribed in and .
Definition: vpServo.h:153
void get_eJe(vpMatrix &eJe)
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