Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
servoSimuSphere2DCamVelocity.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 sphere.
32  *
33  * Authors:
34  * Eric Marchand
35  * Fabien Spindler
36  *
37  *****************************************************************************/
38 
39 
49 #include <stdlib.h>
50 #include <stdio.h>
51 
52 #include <visp3/visual_features/vpFeatureBuilder.h>
53 #include <visp3/visual_features/vpFeatureEllipse.h>
54 #include <visp3/core/vpHomogeneousMatrix.h>
55 #include <visp3/core/vpMath.h>
56 #include <visp3/io/vpParseArgv.h>
57 #include <visp3/vs/vpServo.h>
58 #include <visp3/core/vpSphere.h>
59 #include <visp3/robot/vpSimulatorCamera.h>
60 
61 // List of allowed command line options
62 #define GETOPTARGS "h"
63 
64 void usage(const char *name, const char *badparam);
65 bool getOptions(int argc, const char **argv);
66 
75 void usage(const char *name, const char *badparam)
76 {
77  fprintf(stdout, "\n\
78 Simulation of a 2D visual servoing on a sphere:\n\
79 - eye-in-hand control law,\n\
80 - velocity computed in the camera frame,\n\
81 - without display.\n\
82  \n\
83 SYNOPSIS\n\
84  %s [-h]\n", name);
85 
86  fprintf(stdout, "\n\
87 OPTIONS: Default\n\
88  \n\
89  -h\n\
90  Print the help.\n");
91 
92  if (badparam)
93  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
94 }
95 
106 bool getOptions(int argc, const char **argv)
107 {
108  const char *optarg_;
109  int c;
110  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
111 
112  switch (c) {
113  case 'h': usage(argv[0], NULL); return false; break;
114 
115  default:
116  usage(argv[0], optarg_);
117  return false; break;
118  }
119  }
120 
121  if ((c == 1) || (c == -1)) {
122  // standalone param or error
123  usage(argv[0], NULL);
124  std::cerr << "ERROR: " << std::endl;
125  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
126  return false;
127  }
128 
129  return true;
130 }
131 
132 int
133 main(int argc, const char ** argv)
134 {
135  try {
136  // Read the command line options
137  if (getOptions(argc, argv) == false) {
138  exit (-1);
139  }
140 
141  vpServo task ;
142  vpSimulatorCamera robot ;
143 
144  std::cout << std::endl ;
145  std::cout << "-------------------------------------------------------" << std::endl ;
146  std::cout << " Test program for vpServo " <<std::endl ;
147  std::cout << " Simulation " << std::endl ;
148  std::cout << " task : servo a sphere " << std::endl ;
149  std::cout << "-------------------------------------------------------" << std::endl ;
150  std::cout << std::endl ;
151 
152  // sets the initial camera location
153  vpHomogeneousMatrix cMo ;
154  cMo[0][3] = 0.1 ;
155  cMo[1][3] = 0.2 ;
156  cMo[2][3] = 2 ;
157  // Compute the position of the object in the world frame
158  vpHomogeneousMatrix wMc, wMo;
159  robot.getPosition(wMc) ;
160  wMo = wMc * cMo;
161 
162  vpHomogeneousMatrix cMod ;
163  cMod[0][3] = 0 ;
164  cMod[1][3] = 0 ;
165  cMod[2][3] = 1 ;
166 
167  // sets the sphere coordinates in the world frame
168  vpSphere sphere ;
169  sphere.setWorldCoordinates(0,0,0,0.1) ;
170 
171  // sets the desired position of the visual feature
172  vpFeatureEllipse pd ;
173  sphere.track(cMod) ;
174  vpFeatureBuilder::create(pd,sphere) ;
175 
176  // computes the sphere coordinates in the camera frame and its 2D coordinates
177  // sets the current position of the visual feature
178  vpFeatureEllipse p ;
179  sphere.track(cMo) ;
180  vpFeatureBuilder::create(p,sphere) ;
181 
182  // define the task
183  // - we want an eye-in-hand control law
184  // - robot is controlled in the camera frame
186 
187  // we want to see a sphere on a sphere
188  task.addFeature(p,pd) ;
189 
190  // set the gain
191  task.setLambda(1) ;
192 
193  // Display task information
194  task.print() ;
195 
196  unsigned int iter=0 ;
197  // loop
198  while(iter++ < 500)
199  {
200  std::cout << "---------------------------------------------" << iter <<std::endl ;
201  vpColVector v ;
202 
203  // get the robot position
204  robot.getPosition(wMc) ;
205  // Compute the position of the camera wrt the object frame
206  cMo = wMc.inverse() * wMo;
207 
208  // new sphere position: retrieve x,y and Z of the vpSphere structure
209  sphere.track(cMo) ;
210  vpFeatureBuilder::create(p,sphere);
211 
212  // compute the control law
213  v = task.computeControlLaw() ;
214 
215  std::cout << "Task rank: " << task.getTaskRank() << std::endl ;
216  // send the camera velocity to the controller
218 
219  std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() <<std::endl ;
220  }
221 
222  // Display task information
223  task.print() ;
224  task.kill();
225  return 0;
226  }
227  catch(vpException &e) {
228  std::cout << "Catch a ViSP exception: " << e << std::endl;
229  return 1;
230  }
231 }
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 track(const vpHomogeneousMatrix &cMo)
Class that defines what is a sphere.
Definition: vpSphere.h:60
void setWorldCoordinates(const vpColVector &oP)
Definition: vpSphere.cpp:54
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:76
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 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
Class that defines 2D ellipse visual feature.
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:222
unsigned int getTaskRank() const
Definition: vpServo.cpp:1849