ViSP  2.9.0
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 - 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 3D visual servoing on a 3D point.
36  *
37  * Authors:
38  * Eric Marchand
39  * Fabien Spindler
40  *
41  *****************************************************************************/
42 
43 
55 #include <stdlib.h>
56 #include <stdio.h>
57 
58 #include <visp/vpFeaturePoint3D.h>
59 #include <visp/vpHomogeneousMatrix.h>
60 #include <visp/vpMath.h>
61 #include <visp/vpParseArgv.h>
62 #include <visp/vpPoint.h>
63 #include <visp/vpServo.h>
64 #include <visp/vpSimulatorCamera.h>
65 
66 // List of allowed command line options
67 #define GETOPTARGS "h"
68 
69 void usage(const char *name, const char *badparam);
70 bool getOptions(int argc, const char **argv);
71 
80 void usage(const char *name, const char *badparam)
81 {
82  fprintf(stdout, "\n\
83 Simulation of a 3D visual servoing:\n\
84 - servo a 3D point,\n\
85 - eye-in-hand control law,\n\
86 - velocity computed in the camera frame,\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 
111 bool getOptions(int argc, const char **argv)
112 {
113  const char *optarg_;
114  int c;
115  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
116 
117  switch (c) {
118  case 'h': usage(argv[0], NULL); return false; break;
119 
120  default:
121  usage(argv[0], optarg_);
122  return false; break;
123  }
124  }
125 
126  if ((c == 1) || (c == -1)) {
127  // standalone param or error
128  usage(argv[0], NULL);
129  std::cerr << "ERROR: " << std::endl;
130  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
131  return false;
132  }
133 
134  return true;
135 }
136 
137 int
138 main(int argc, const char ** argv)
139 {
140  try {
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  std::cout << std::endl ;
150  std::cout << "-------------------------------------------------------" << std::endl ;
151  std::cout << " Test program for vpServo " <<std::endl ;
152  std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl ;
153  std::cout << " Simulation " << std::endl ;
154  std::cout << " task : servo a 3D point " << std::endl ;
155  std::cout << "-------------------------------------------------------" << std::endl ;
156  std::cout << std::endl ;
157 
158  // sets the initial camera location
159  vpHomogeneousMatrix cMo ;
160  cMo[0][3] = 0.1 ;
161  cMo[1][3] = 0.2 ;
162  cMo[2][3] = 2 ;
163  // Compute the position of the object in the world frame
164  vpHomogeneousMatrix wMc, wMo;
165  robot.getPosition(wMc) ;
166  wMo = wMc * cMo;
167 
168  // sets the point coordinates in the world frame
169  vpPoint point ;
170  point.setWorldCoordinates(0,0,0) ;
171 
172  // computes the point coordinates in the camera frame
173  point.track(cMo) ;
174 
175  std::cout << "Point coordinates in the camera frame: " << point.cP.t() ;
176 
177  vpFeaturePoint3D p ;
178  p.buildFrom(point) ;
179 
180  // sets the desired position of the point
181  vpFeaturePoint3D pd ;
182  pd.set_XYZ(0,0,1) ;
183 
184  // define the task
185  // - we want an eye-in-hand control law
186  // - robot is controlled in the camera frame
188 
189  // we want to see a point on a point
190  std::cout << std::endl ;
191  task.addFeature(p,pd) ;
192 
193  // set the gain") ;
194  task.setLambda(1) ;
195 
196  // Display task information
197  task.print() ;
198 
199  unsigned int iter=0 ;
200  // loop
201  while(iter++<200)
202  {
203  std::cout << "---------------------------------------------" << iter <<std::endl ;
204  vpColVector v ;
205 
206  // get the robot position
207  robot.getPosition(wMc) ;
208  // Compute the position of the camera wrt the object frame
209  cMo = wMc.inverse() * wMo;
210 
211  // new point position
212  point.track(cMo) ;
213  p.buildFrom(point) ;
214  // std::cout << p.cP.t() ;
215  // std::cout << (p.get_s()).t() ;
216 
217  // compute the control law
218  v = task.computeControlLaw() ;
219  // send the camera velocity to the controller
221 
222  std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() <<std::endl ;
223  }
224 
225  // Display task information
226  task.print() ;
227  task.kill();
228  return 0;
229  }
230  catch(vpException e) {
231  std::cout << "Catch a ViSP exception: " << e << std::endl;
232  return 1;
233  }
234 }
235 
void set_XYZ(const double X, const double Y, const double Z)
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)
Definition: vpServo.cpp:449
error that can be emited by ViSP classes.
Definition: vpException.h:76
void track(const vpHomogeneousMatrix &cMo)
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()
Definition: vpServo.cpp:189
vpColVector getError() const
Definition: vpServo.h:257
vpColVector computeControlLaw()
Definition: vpServo.cpp:902
Class that defines the 3D point visual feature.
vpRowVector t() const
transpose of Vector
void getPosition(vpHomogeneousMatrix &wMc) const
void setLambda(double c)
Definition: vpServo.h:370
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:251
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