Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
servoSimuPoint3DCamVelocity.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 3D visual servoing on a 3D point.
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/vpFeaturePoint3D.h>
55 #include <visp3/core/vpHomogeneousMatrix.h>
56 #include <visp3/core/vpMath.h>
57 #include <visp3/io/vpParseArgv.h>
58 #include <visp3/core/vpPoint.h>
59 #include <visp3/vs/vpServo.h>
60 #include <visp3/robot/vpSimulatorCamera.h>
61 
62 // List of allowed command line options
63 #define GETOPTARGS "h"
64 
65 void usage(const char *name, const char *badparam);
66 bool getOptions(int argc, const char **argv);
67 
76 void usage(const char *name, const char *badparam)
77 {
78  fprintf(stdout, "\n\
79 Simulation of a 3D visual servoing:\n\
80 - servo a 3D point,\n\
81 - eye-in-hand control law,\n\
82 - velocity computed in the camera frame,\n\
83 - without display.\n\
84  \n\
85 SYNOPSIS\n\
86  %s [-h]\n", name);
87 
88  fprintf(stdout, "\n\
89 OPTIONS: Default\n\
90  \n\
91  -h\n\
92  Print the help.\n");
93 
94  if (badparam)
95  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
96 }
97 
107 bool getOptions(int argc, const char **argv)
108 {
109  const char *optarg_;
110  int c;
111  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
112 
113  switch (c) {
114  case 'h': usage(argv[0], NULL); return false; break;
115 
116  default:
117  usage(argv[0], optarg_);
118  return false; break;
119  }
120  }
121 
122  if ((c == 1) || (c == -1)) {
123  // standalone param or error
124  usage(argv[0], NULL);
125  std::cerr << "ERROR: " << std::endl;
126  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
127  return false;
128  }
129 
130  return true;
131 }
132 
133 int
134 main(int argc, const char ** argv)
135 {
136  try {
137  // Read the command line options
138  if (getOptions(argc, argv) == false) {
139  exit (-1);
140  }
141 
142  vpServo task ;
143  vpSimulatorCamera robot ;
144 
145  std::cout << std::endl ;
146  std::cout << "-------------------------------------------------------" << std::endl ;
147  std::cout << " Test program for vpServo " <<std::endl ;
148  std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl ;
149  std::cout << " Simulation " << std::endl ;
150  std::cout << " task : servo a 3D point " << std::endl ;
151  std::cout << "-------------------------------------------------------" << std::endl ;
152  std::cout << std::endl ;
153 
154  // sets the initial camera location
155  vpHomogeneousMatrix cMo ;
156  cMo[0][3] = 0.1 ;
157  cMo[1][3] = 0.2 ;
158  cMo[2][3] = 2 ;
159  // Compute the position of the object in the world frame
160  vpHomogeneousMatrix wMc, wMo;
161  robot.getPosition(wMc) ;
162  wMo = wMc * cMo;
163 
164  // sets the point coordinates in the world frame
165  vpPoint point(0,0,0) ;
166 
167  // computes the point coordinates in the camera frame
168  point.track(cMo) ;
169 
170  std::cout << "Point coordinates in the camera frame: " << point.cP.t() ;
171 
172  vpFeaturePoint3D p ;
173  p.buildFrom(point) ;
174 
175  // sets the desired position of the point
176  vpFeaturePoint3D pd ;
177  pd.set_XYZ(0,0,1) ;
178 
179  // define the task
180  // - we want an eye-in-hand control law
181  // - robot is controlled in the camera frame
183 
184  // we want to see a point on a point
185  std::cout << std::endl ;
186  task.addFeature(p,pd) ;
187 
188  // set the gain") ;
189  task.setLambda(1) ;
190 
191  // Display task information
192  task.print() ;
193 
194  unsigned int iter=0 ;
195  // loop
196  while(iter++<200)
197  {
198  std::cout << "---------------------------------------------" << iter <<std::endl ;
199  vpColVector v ;
200 
201  // get the robot position
202  robot.getPosition(wMc) ;
203  // Compute the position of the camera wrt the object frame
204  cMo = wMc.inverse() * wMo;
205 
206  // new point position
207  point.track(cMo) ;
208  p.buildFrom(point) ;
209  // std::cout << p.cP.t() ;
210  // std::cout << (p.get_s()).t() ;
211 
212  // compute the control law
213  v = task.computeControlLaw() ;
214  // send the camera velocity to the controller
216 
217  std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() <<std::endl ;
218  }
219 
220  // Display task information
221  task.print() ;
222  task.kill();
223  return 0;
224  }
225  catch(vpException &e) {
226  std::cout << "Catch a ViSP exception: " << e << std::endl;
227  return 1;
228  }
229 }
230 
void set_XYZ(const double X, const double Y, const double Z)
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
void buildFrom(const vpPoint &p)
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
vpHomogeneousMatrix getPosition() const
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
vpHomogeneousMatrix inverse() const
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:314
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:222