Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
servoSimuPoint2DhalfCamVelocity1.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Simulation of a 2 1/2 D visual servoing.
32  *
33  * Authors:
34  * Eric Marchand
35  * Fabien Spindler
36  *
37  *****************************************************************************/
38 
39 
51 #include <stdlib.h>
52 #include <stdio.h>
53 
54 #include <visp3/visual_features/vpFeatureBuilder.h>
55 #include <visp3/visual_features/vpFeaturePoint.h>
56 #include <visp3/visual_features/vpFeatureThetaU.h>
57 #include <visp3/visual_features/vpGenericFeature.h>
58 #include <visp3/core/vpHomogeneousMatrix.h>
59 #include <visp3/core/vpMath.h>
60 #include <visp3/io/vpParseArgv.h>
61 #include <visp3/core/vpPoint.h>
62 #include <visp3/vs/vpServo.h>
63 #include <visp3/robot/vpSimulatorCamera.h>
64 
65 // List of allowed command line options
66 #define GETOPTARGS "h"
67 
68 void usage(const char *name, const char *badparam);
69 bool getOptions(int argc, const char **argv);
70 
79 void usage(const char *name, const char *badparam)
80 {
81  fprintf(stdout, "\n\
82 Simulation of a 2 1/2 D visual servoing (x,y,Z,theta U):\n\
83 - eye-in-hand control law,\n\
84 - velocity computed in the camera frame,\n\
85 - without display.\n\
86  \n\
87 SYNOPSIS\n\
88  %s [-h]\n", name);
89 
90  fprintf(stdout, "\n\
91 OPTIONS: Default\n\
92  \n\
93  -h\n\
94  Print the help.\n");
95 
96  if (badparam) {
97  fprintf(stderr, "ERROR: \n" );
98  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
99  }
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  try {
142  // Read the command line options
143  if (getOptions(argc, argv) == false) {
144  exit (-1);
145  }
146 
147  vpServo task ;
148  vpSimulatorCamera robot ;
149 
150  std::cout << std::endl ;
151  std::cout << "-------------------------------------------------------" << std::endl ;
152  std::cout << " Test program for vpServo " <<std::endl ;
153  std::cout << " task : 2 1/2 D visual servoing " << std::endl ;
154  std::cout << "-------------------------------------------------------" << std::endl ;
155  std::cout << std::endl ;
156 
157  // sets the initial camera location
158  vpPoseVector c_r_o(0.1,0.2,2,
159  vpMath::rad(20), vpMath::rad(10), vpMath::rad(50)
160  ) ;
161 
162  vpHomogeneousMatrix cMo(c_r_o) ;
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 desired camera location
169  vpPoseVector cd_r_o(0,0,1,
171  vpHomogeneousMatrix cdMo(cd_r_o);
172 
173  // sets the point coordinates in the world frame
174  vpPoint point(0, 0, 0);
175  // computes the point coordinates in the camera frame and its 2D coordinates
176  point.track(cMo);
177 
178  vpPoint pointd(0, 0, 0);
179  pointd.track(cdMo);
180  //------------------------------------------------------------------
181  // 1st feature (x,y)
182  // want to it at (0,0)
183  vpFeaturePoint p;
184  vpFeatureBuilder::create(p,point);
185 
186  vpFeaturePoint pd;
187  vpFeatureBuilder::create(pd,pointd);
188 
189  //------------------------------------------------------------------
190  // 2nd feature (Z)
191  // not necessary to project twice (reuse p)
192  vpFeaturePoint3D Z ;
193  vpFeatureBuilder::create(Z,point) ; //retrieve x,y and Z of the vpPoint structure
194 
195  // want to see it one meter away (here again use pd)
196  vpFeaturePoint3D Zd ;
197  vpFeatureBuilder::create(Zd,pointd) ; //retrieve x,y and Z of the vpPoint structure
198 
199  //------------------------------------------------------------------
200  // 3rd feature ThetaU
201  // compute the rotation that the camera has to achieve
202  vpHomogeneousMatrix cdMc ;
203  cdMc = cdMo*cMo.inverse() ;
204 
206  tu.buildFrom(cdMc) ;
207 
208  // sets the desired rotation (always zero !)
209  // since s is the rotation that the camera has to achieve
210 
211  //------------------------------------------------------------------
212  // define the task
213  // - we want an eye-in-hand control law
214  // - robot is controlled in the camera frame
216 
217  task.addFeature(p,pd) ;
219  task.addFeature(tu) ;
220 
221  // set the gain
222  task.setLambda(1) ;
223 
224  // Display task information
225  task.print() ;
226 
227  unsigned int iter=0 ;
228  // loop
229  while(iter++<200)
230  {
231  std::cout << "---------------------------------------------" << iter <<std::endl ;
232  vpColVector v ;
233 
234  // get the robot position
235  robot.getPosition(wMc) ;
236  // Compute the position of the camera wrt the object frame
237  cMo = wMc.inverse() * wMo;
238 
239  // update the feature
240  point.track(cMo) ;
241  vpFeatureBuilder::create(p,point) ;
242  vpFeatureBuilder::create(Z,point) ;
243 
244  cdMc = cdMo*cMo.inverse() ;
245  tu.buildFrom(cdMc) ;
246 
247  // compute the control law
248  v = task.computeControlLaw() ;
249  // send the camera velocity to the controller ") ;
251 
252  std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() <<std::endl ;
253  }
254 
255  // Display task information
256  task.print() ;
257  task.kill();
258  std::cout << "Final camera location:\n " << cMo << std::endl ;
259  return 0;
260  }
261  catch(vpException &e) {
262  std::cout << "Catch a ViSP exception: " << e << std::endl;
263  return 1;
264  }
265 }
266 
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
Implementation of an homogeneous matrix and operations on such kind of matrices.
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:512
error that can be emited by ViSP classes.
Definition: vpException.h:73
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:76
Class that defines what is a point.
Definition: vpPoint.h:59
void kill()
Definition: vpServo.cpp:191
vpColVector getError() const
Definition: vpServo.h:271
vpColVector computeControlLaw()
Definition: vpServo.cpp:954
Class that defines the 3D point visual feature.
void setLambda(double c)
Definition: vpServo.h:391
static unsigned int selectZ()
vpHomogeneousMatrix getPosition() const
void buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
static double rad(double deg)
Definition: vpMath.h:104
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:93
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:314
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:222