ViSP  2.9.0
servoSimuPoint2DhalfCamVelocity1.cpp
1 /****************************************************************************
2  *
3  * $Id: servoSimuPoint2DhalfCamVelocity1.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 2 1/2 D visual servoing.
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/vpFeatureBuilder.h>
59 #include <visp/vpFeaturePoint.h>
60 #include <visp/vpFeatureThetaU.h>
61 #include <visp/vpGenericFeature.h>
62 #include <visp/vpHomogeneousMatrix.h>
63 #include <visp/vpMath.h>
64 #include <visp/vpParseArgv.h>
65 #include <visp/vpPoint.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 2 1/2 D visual servoing (x,y,Z,theta U):\n\
87 - eye-in-hand control law,\n\
88 - velocity computed in the camera frame,\n\
89 - without display.\n\
90  \n\
91 SYNOPSIS\n\
92  %s [-h]\n", name);
93 
94  fprintf(stdout, "\n\
95 OPTIONS: Default\n\
96  \n\
97  -h\n\
98  Print the help.\n");
99 
100  if (badparam) {
101  fprintf(stderr, "ERROR: \n" );
102  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
103  }
104 }
105 
116 bool getOptions(int argc, const char **argv)
117 {
118  const char *optarg_;
119  int c;
120  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
121 
122  switch (c) {
123  case 'h': usage(argv[0], NULL); return false; break;
124 
125  default:
126  usage(argv[0], optarg_);
127  return false; break;
128  }
129  }
130 
131  if ((c == 1) || (c == -1)) {
132  // standalone param or error
133  usage(argv[0], NULL);
134  std::cerr << "ERROR: " << std::endl;
135  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
136  return false;
137  }
138 
139  return true;
140 }
141 
142 int
143 main(int argc, const char ** argv)
144 {
145  try {
146  // Read the command line options
147  if (getOptions(argc, argv) == false) {
148  exit (-1);
149  }
150 
151  vpServo task ;
152  vpSimulatorCamera robot ;
153 
154  std::cout << std::endl ;
155  std::cout << "-------------------------------------------------------" << std::endl ;
156  std::cout << " Test program for vpServo " <<std::endl ;
157  std::cout << " task : 2 1/2 D visual servoing " << std::endl ;
158  std::cout << "-------------------------------------------------------" << std::endl ;
159  std::cout << std::endl ;
160 
161  // sets the initial camera location
162  vpPoseVector c_r_o(0.1,0.2,2,
163  vpMath::rad(20), vpMath::rad(10), vpMath::rad(50)
164  ) ;
165 
166  vpHomogeneousMatrix cMo(c_r_o) ;
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 desired camera location
173  vpPoseVector cd_r_o(0,0,1,
175  vpHomogeneousMatrix cdMo(cd_r_o) ;
176 
177  // sets the point coordinates in the world frame
178  vpPoint point ;
179  point.setWorldCoordinates(0,0,0) ;
180  // computes the point coordinates in the camera frame and its 2D coordinates
181  point.track(cMo) ;
182 
183  vpPoint pointd ;
184  pointd.setWorldCoordinates(0,0,0) ;
185  pointd.track(cdMo) ;
186  //------------------------------------------------------------------
187  // 1st feature (x,y)
188  // want to it at (0,0)
189  vpFeaturePoint p ;
190  vpFeatureBuilder::create(p,point) ;
191 
192  vpFeaturePoint pd ;
193  vpFeatureBuilder::create(pd,pointd) ;
194 
195  //------------------------------------------------------------------
196  // 2nd feature (Z)
197  // not necessary to project twice (reuse p)
198  vpFeaturePoint3D Z ;
199  vpFeatureBuilder::create(Z,point) ; //retrieve x,y and Z of the vpPoint structure
200 
201  // want to see it one meter away (here again use pd)
202  vpFeaturePoint3D Zd ;
203  vpFeatureBuilder::create(Zd,pointd) ; //retrieve x,y and Z of the vpPoint structure
204 
205  //------------------------------------------------------------------
206  // 3rd feature ThetaU
207  // compute the rotation that the camera has to achieve
208  vpHomogeneousMatrix cdMc ;
209  cdMc = cdMo*cMo.inverse() ;
210 
212  tu.buildFrom(cdMc) ;
213 
214  // sets the desired rotation (always zero !)
215  // since s is the rotation that the camera has to achieve
216 
217  //------------------------------------------------------------------
218  // define the task
219  // - we want an eye-in-hand control law
220  // - robot is controlled in the camera frame
222 
223  task.addFeature(p,pd) ;
225  task.addFeature(tu) ;
226 
227  // set the gain
228  task.setLambda(1) ;
229 
230  // Display task information
231  task.print() ;
232 
233  unsigned int iter=0 ;
234  // loop
235  while(iter++<200)
236  {
237  std::cout << "---------------------------------------------" << iter <<std::endl ;
238  vpColVector v ;
239 
240  // get the robot position
241  robot.getPosition(wMc) ;
242  // Compute the position of the camera wrt the object frame
243  cMo = wMc.inverse() * wMo;
244 
245  // update the feature
246  point.track(cMo) ;
247  vpFeatureBuilder::create(p,point) ;
248  vpFeatureBuilder::create(Z,point) ;
249 
250  cdMc = cdMo*cMo.inverse() ;
251  tu.buildFrom(cdMc) ;
252 
253  // compute the control law
254  v = task.computeControlLaw() ;
255  // send the camera velocity to the controller ") ;
257 
258  std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() <<std::endl ;
259  }
260 
261  // Display task information
262  task.print() ;
263  task.kill();
264  std::cout << "Final camera location:\n " << cMo << std::endl ;
265  return 0;
266  }
267  catch(vpException e) {
268  std::cout << "Catch a ViSP exception: " << e << std::endl;
269  return 1;
270  }
271 }
272 
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)
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
Class that defines the 3D point visual feature.
void getPosition(vpHomogeneousMatrix &wMc) const
void setLambda(double c)
Definition: vpServo.h:370
static unsigned int selectZ()
void buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
Construction from translation vector and rotation matrix.
static double rad(double deg)
Definition: vpMath.h:100
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
The pose is a complete representation of every rigid motion in the euclidian space.
Definition: vpPoseVector.h:92
vpHomogeneousMatrix inverse() const
Class that defines a 3D visual feature from a axis/angle parametrization that represent the rotatio...
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 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