Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
servoSimuPoint2DCamVelocity2.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 2D visual servoing on a point.
32  *
33  * Authors:
34  * Eric Marchand
35  * Fabien Spindler
36  *
37  *****************************************************************************/
38 
55 #include <stdlib.h>
56 #include <stdio.h>
57 
58 #include <visp3/visual_features/vpFeatureBuilder.h>
59 #include <visp3/visual_features/vpFeaturePoint.h>
60 #include <visp3/core/vpHomogeneousMatrix.h>
61 #include <visp3/core/vpMath.h>
62 #include <visp3/io/vpParseArgv.h>
63 #include <visp3/vs/vpServo.h>
64 #include <visp3/robot/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 2D visual servoing on a point:\n\
84 - eye-in-hand control law,\n\
85 - articular velocity are computed,\n\
86 - without display.\n\
87 \n\
88 SYNOPSIS\n\
89  %s [-h]\n", name);
90 
91  fprintf(stdout, "\n\
92 OPTIONS: Default\n\
93 \n\
94  -h\n\
95  Print the help.\n");
96 
97  if (badparam)
98  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
99 }
100 
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 
150  std::cout << std::endl ;
151  std::cout << "-------------------------------------------------------" << std::endl ;
152  std::cout << " Test program for vpServo " <<std::endl ;
153  std::cout << " Eye-in-hand task control, articular velocity are computed" << std::endl ;
154  std::cout << " Simulation " << std::endl ;
155  std::cout << " task : servo a point " << std::endl ;
156  std::cout << "-------------------------------------------------------" << std::endl ;
157  std::cout << std::endl ;
158 
159  // sets the initial camera location
161  cMo[0][3] = 0.1;
162  cMo[1][3] = 0.2;
163  cMo[2][3] = 2;
164  // Compute the position of the object in the world frame
165  vpHomogeneousMatrix wMc, wMo;
166  robot.getPosition(wMc);
167  wMo = wMc * cMo;
168 
169  // sets the point coordinates in the world frame
170  vpPoint point(0, 0, 0);
171 
172  // computes the point coordinates in the camera frame and its 2D coordinates
173  point.track(cMo);
174 
175  // sets the current position of the visual feature
176  vpFeaturePoint p;
177  vpFeatureBuilder::create(p, point); //retrieve x,y and Z of the vpPoint structure
178 
179  // sets the desired position of the visual feature
180  vpFeaturePoint pd;
181  pd.buildFrom(0,0,1);
182 
183  // define the task
184  // - we want an eye-in-hand control law
185  // - articular velocity are computed
188 
189  // Set the position of the camera in the end-effector frame
190  vpHomogeneousMatrix cMe ;
191  vpVelocityTwistMatrix cVe(cMe) ;
192  task.set_cVe(cVe) ;
193 
194  // Set the Jacobian (expressed in the end-effector frame)
195  vpMatrix eJe ;
196  robot.get_eJe(eJe) ;
197  task.set_eJe(eJe) ;
198 
199  // we want to see a point on a point
200  task.addFeature(p,pd) ;
201 
202  // set the gain
203  task.setLambda(1) ;
204  // Display task information
205  task.print() ;
206 
207  unsigned int iter=0 ;
208  // loop
209  while(iter++<100)
210  {
211  std::cout << "---------------------------------------------" << iter <<std::endl ;
212  vpColVector v ;
213 
214  // Set the Jacobian (expressed in the end-effector frame)
215  // since q is modified eJe is modified
216  robot.get_eJe(eJe) ;
217  task.set_eJe(eJe) ;
218 
219  // get the robot position
220  robot.getPosition(wMc) ;
221  // Compute the position of the camera wrt the object frame
222  cMo = wMc.inverse() * wMo;
223 
224  // new point position
225  point.track(cMo) ;
226  vpFeatureBuilder::create(p,point) ; //retrieve x,y and Z of the vpPoint structure
227  pd.buildFrom(0,0,1) ; // Since vpServo::MEAN interaction matrix is used, we need to update the desired feature at each iteration
228 
229  // compute the control law
230  v = task.computeControlLaw() ;
231 
232  // send the camera velocity to the controller
234 
235  std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() <<std::endl ;
236  }
237 
238  // Display task information
239  task.print() ;
240  task.kill();
241  return 0;
242  }
243  catch(vpException &e) {
244  std::cout << "Catch a ViSP exception: " << e << std::endl;
245  return 1;
246  }
247 }
248 
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:97
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 set_eJe(const vpMatrix &eJe_)
Definition: vpServo.h:460
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
void setLambda(double c)
Definition: vpServo.h:391
vpHomogeneousMatrix getPosition() const
Implementation of a velocity twist matrix and operations on such kind of matrices.
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:585
void buildFrom(const double x, const double y, const double Z)
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
void set_cVe(const vpVelocityTwistMatrix &cVe_)
Definition: vpServo.h:435
vpHomogeneousMatrix inverse() const
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 get_eJe(vpMatrix &eJe)
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:222