ViSP  2.9.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 - 2014 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 
73 void usage(const char *name, const char *badparam);
74 bool getOptions(int argc, const char **argv);
75 
84 void usage(const char *name, const char *badparam)
85 {
86  fprintf(stdout, "\n\
87 Simulation of a 2D visual servoing on a point:\n\
88 - eye-in-hand control law,\n\
89 - articular velocity are computed,\n\
90 - without display.\n\
91 \n\
92 SYNOPSIS\n\
93  %s [-h]\n", name);
94 
95  fprintf(stdout, "\n\
96 OPTIONS: Default\n\
97 \n\
98  -h\n\
99  Print the help.\n");
100 
101  if (badparam)
102  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
103 }
104 
115 bool getOptions(int argc, const char **argv)
116 {
117  const char *optarg_;
118  int c;
119  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
120 
121  switch (c) {
122  case 'h': usage(argv[0], NULL); return false; break;
123 
124  default:
125  usage(argv[0], optarg_);
126  return false; break;
127  }
128  }
129 
130  if ((c == 1) || (c == -1)) {
131  // standalone param or error
132  usage(argv[0], NULL);
133  std::cerr << "ERROR: " << std::endl;
134  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
135  return false;
136  }
137 
138  return true;
139 }
140 
141 int
142 main(int argc, const char ** argv)
143 {
144  try {
145  // Read the command line options
146  if (getOptions(argc, argv) == false) {
147  exit (-1);
148  }
149 
150  vpServo task ;
151  vpSimulatorCamera robot ;
152 
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, articular velocity are computed" << std::endl ;
158  std::cout << " Simulation " << std::endl ;
159  std::cout << " task : servo a point " << std::endl ;
160  std::cout << "-------------------------------------------------------" << std::endl ;
161  std::cout << std::endl ;
162 
163  // sets the initial camera location
164  vpHomogeneousMatrix cMo ;
165  cMo[0][3] = 0.1 ;
166  cMo[1][3] = 0.2 ;
167  cMo[2][3] = 2 ;
168  // Compute the position of the object in the world frame
169  vpHomogeneousMatrix wMc, wMo;
170  robot.getPosition(wMc) ;
171  wMo = wMc * cMo;
172 
173  // sets the point coordinates in the world frame
174  vpPoint point ;
175  point.setWorldCoordinates(0,0,0) ;
176 
177  // computes the point coordinates in the camera frame and its 2D coordinates
178  point.track(cMo) ;
179 
180  // sets the current position of the visual feature
181  vpFeaturePoint p ;
182  vpFeatureBuilder::create(p,point) ; //retrieve x,y and Z of the vpPoint structure
183 
184  // sets the desired position of the visual feature
185  vpFeaturePoint pd ;
186  pd.buildFrom(0,0,1) ;
187 
188  // define the task
189  // - we want an eye-in-hand control law
190  // - articular velocity are computed
193 
194  // Set the position of the camera in the end-effector frame
195  vpHomogeneousMatrix cMe ;
196  vpVelocityTwistMatrix cVe(cMe) ;
197  task.set_cVe(cVe) ;
198 
199  // Set the Jacobian (expressed in the end-effector frame)
200  vpMatrix eJe ;
201  robot.get_eJe(eJe) ;
202  task.set_eJe(eJe) ;
203 
204  // we want to see a point on a point
205  task.addFeature(p,pd) ;
206 
207  // set the gain
208  task.setLambda(1) ;
209  // Display task information
210  task.print() ;
211 
212  unsigned int iter=0 ;
213  // loop
214  while(iter++<100)
215  {
216  std::cout << "---------------------------------------------" << iter <<std::endl ;
217  vpColVector v ;
218 
219  // Set the Jacobian (expressed in the end-effector frame)
220  // since q is modified eJe is modified
221  robot.get_eJe(eJe) ;
222  task.set_eJe(eJe) ;
223 
224  // get the robot position
225  robot.getPosition(wMc) ;
226  // Compute the position of the camera wrt the object frame
227  cMo = wMc.inverse() * wMo;
228 
229  // new point position
230  point.track(cMo) ;
231  vpFeatureBuilder::create(p,point) ; //retrieve x,y and Z of the vpPoint structure
232  pd.buildFrom(0,0,1) ; // Since vpServo::MEAN interaction matrix is used, we need to update the desired feature at each iteration
233 
234  // compute the control law
235  v = task.computeControlLaw() ;
236 
237  // send the camera velocity to the controller
239 
240  std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() <<std::endl ;
241  }
242 
243  // Display task information
244  task.print() ;
245  task.kill();
246  return 0;
247  }
248  catch(vpException e) {
249  std::cout << "Catch a ViSP exception: " << e << std::endl;
250  return 1;
251  }
252 }
253 
Definition of the vpMatrix class.
Definition: vpMatrix.h:98
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 set_eJe(const vpMatrix &eJe_)
Definition: vpServo.h:439
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, const unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:449
error that can be emited by ViSP classes.
Definition: vpException.h:76
void track(const vpHomogeneousMatrix &cMo)
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
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()
Definition: vpServo.cpp:189
vpColVector getError() const
Definition: vpServo.h:257
vpColVector computeControlLaw()
Definition: vpServo.cpp:902
void getPosition(vpHomogeneousMatrix &wMc) const
void setLambda(double c)
Definition: vpServo.h:370
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)
Definition: vpServo.cpp:522
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 set_cVe(const vpVelocityTwistMatrix &cVe_)
Definition: vpServo.h:414
vpHomogeneousMatrix inverse() const
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:251
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
void get_eJe(vpMatrix &eJe)
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:220
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