ViSP  2.9.0
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 - 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 
43 
58 #include <stdlib.h>
59 #include <stdio.h>
60 
61 #include <visp/vpFeatureBuilder.h>
62 #include <visp/vpFeaturePoint.h>
63 #include <visp/vpHomogeneousMatrix.h>
64 #include <visp/vpMath.h>
65 #include <visp/vpParseArgv.h>
66 #include <visp/vpServo.h>
67 #include <visp/vpSimulatorCamera.h>
68 
69 // List of allowed command line options
70 #define GETOPTARGS "h"
71 
72 void usage(const char *name, const char *badparam);
73 bool getOptions(int argc, const char **argv);
74 
83 void usage(const char *name, const char *badparam)
84 {
85  fprintf(stdout, "\n\
86 Simulation of a 2D visual servoing on a point:\n\
87 - eye-in-hand control law,\n\
88 - articular velocity are computed,\n\
89 - without display,\n\
90 - only the X coordinate of the point is selected.\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  std::cout << std::endl ;
154  std::cout << "-------------------------------------------------------" << std::endl ;
155  std::cout << " Test program for vpServo " <<std::endl ;
156  std::cout << " Eye-in-hand task control, articular velocity are computed" << std::endl ;
157  std::cout << " Simulation " << std::endl ;
158  std::cout << " task : servo a point " << std::endl ;
159  std::cout << "-------------------------------------------------------" << std::endl ;
160  std::cout << std::endl ;
161 
162  // sets the initial camera location
163  vpHomogeneousMatrix cMo ;
164  cMo[0][3] = 0.1 ;
165  cMo[1][3] = 0.2 ;
166  cMo[2][3] = 2 ;
167  // Compute the position of the object in the world frame
168  vpHomogeneousMatrix wMc, wMo;
169  robot.getPosition(wMc) ;
170  wMo = wMc * cMo;
171 
172  // sets the point coordinates in the world frame
173  vpPoint point ;
174  point.setWorldCoordinates(0,0,0) ;
175 
176  // computes the point coordinates in the camera frame and its 2D coordinates
177  point.track(cMo) ;
178 
179  // sets the current position of the visual feature
180  vpFeaturePoint p ;
181  vpFeatureBuilder::create(p,point) ; //retrieve x,y and Z of the vpPoint structure
182 
183  // sets the desired position of the visual feature
184  vpFeaturePoint pd ;
185  pd.buildFrom(0,0,1) ; // buildFrom(x,y,Z) ;
186 
187  // define the task
188  // - we want an eye-in-hand control law
189  // - articular velocity are computed
191 
192  // Set the position of the camera in the end-effector frame
193  vpHomogeneousMatrix cMe ;
194  vpVelocityTwistMatrix cVe(cMe) ;
195  task.set_cVe(cVe) ;
196 
197  // Set the Jacobian (expressed in the end-effector frame)
198  vpMatrix eJe ;
199  robot.get_eJe(eJe) ;
200  task.set_eJe(eJe) ;
201 
202  // we want to see a point on a point
203  task.addFeature(p,pd, vpFeaturePoint::selectX()) ;
204 
205  // set the gain
206  task.setLambda(1) ;
207 
208  // Display task information
209  task.print() ;
210 
211  unsigned int iter=0 ;
212  // loop
213  while(iter++ < 100)
214  {
215  std::cout << "---------------------------------------------" << iter <<std::endl ;
216  vpColVector v ;
217 
218  // Set the Jacobian (expressed in the end-effector frame)
219  // since q is modified eJe is modified
220  robot.get_eJe(eJe) ;
221  task.set_eJe(eJe) ;
222 
223  // get the robot position
224  robot.getPosition(wMc) ;
225  // Compute the position of the camera wrt the object frame
226  cMo = wMc.inverse() * wMo;
227 
228  // new point position
229  point.track(cMo) ;
230  vpFeatureBuilder::create(p,point) ; //retrieve x,y and Z of the vpPoint structure
231 
232  // compute the control law
233  v = task.computeControlLaw() ;
234 
235  // send the camera velocity to the controller
237 
238  std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() <<std::endl ;
239  }
240 
241  // Display task information
242  task.print() ;
243  task.kill();
244  return 0;
245  }
246  catch(vpException e) {
247  std::cout << "Catch a ViSP exception: " << e << std::endl;
248  return 1;
249  }
250 }
251 
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 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)
static unsigned int selectX()
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