Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
servoSimuSphere2DCamVelocitySecondaryTask.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 
50 #include <stdlib.h>
51 #include <stdio.h>
52 
53 #include <visp3/visual_features/vpFeatureBuilder.h>
54 #include <visp3/visual_features/vpFeatureEllipse.h>
55 #include <visp3/core/vpHomogeneousMatrix.h>
56 #include <visp3/core/vpMath.h>
57 #include <visp3/io/vpParseArgv.h>
58 #include <visp3/vs/vpServo.h>
59 #include <visp3/core/vpSphere.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 2D visual servoing on a sphere:\n\
80 - eye-in-hand control law,\n\
81 - velocity computed in the camera frame,\n\
82 - without display,\n\
83 - a secondary task is the added.\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 
108 bool getOptions(int argc, const char **argv)
109 {
110  const char *optarg_;
111  int c;
112  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
113 
114  switch (c) {
115  case 'h': usage(argv[0], NULL); return false; break;
116 
117  default:
118  usage(argv[0], optarg_);
119  return false; break;
120  }
121  }
122 
123  if ((c == 1) || (c == -1)) {
124  // standalone param or error
125  usage(argv[0], NULL);
126  std::cerr << "ERROR: " << std::endl;
127  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
128  return false;
129  }
130 
131  return true;
132 }
133 
134 int
135 main(int argc, const char ** argv)
136 {
137  try {
138  // Read the command line options
139  if (getOptions(argc, argv) == false) {
140  exit (-1);
141  }
142 
143  vpServo task ;
144  vpSimulatorCamera robot ;
145 
146  std::cout << std::endl ;
147  std::cout << "-------------------------------------------------------" << std::endl ;
148  std::cout << " Test program for vpServo " <<std::endl ;
149  std::cout << " Simulation " << std::endl ;
150  std::cout << " task : servo a sphere with a secondary task" << std::endl ;
151  std::cout << "-------------------------------------------------------" << std::endl ;
152  std::cout << std::endl ;
153 
154 
155  // sets the initial camera location
156  vpHomogeneousMatrix cMo ;
157  cMo[0][3] = 0.1 ;
158  cMo[1][3] = 0.2 ;
159  cMo[2][3] = 2 ;
160  // Compute the position of the object in the world frame
161  vpHomogeneousMatrix wMc, wMo;
162  robot.getPosition(wMc) ;
163  wMo = wMc * cMo;
164 
165  vpHomogeneousMatrix cMod ;
166  cMod[0][3] = 0 ;
167  cMod[1][3] = 0 ;
168  cMod[2][3] = 1 ;
169 
170  // sets the sphere coordinates in the world frame
171  vpSphere sphere ;
172  sphere.setWorldCoordinates(0,0,0,0.1) ;
173 
174  // sets the desired position of the visual feature
175  vpFeatureEllipse pd ;
176  sphere.track(cMod) ;
177  vpFeatureBuilder::create(pd,sphere) ;
178 
179  // computes the sphere coordinates in the camera frame and its 2D coordinates
180  // sets the current position of the visual feature
181  vpFeatureEllipse p ;
182  sphere.track(cMo) ;
183  vpFeatureBuilder::create(p,sphere) ;
184 
185  // define the task
186  // - we want an eye-in-hand control law
187  // - robot is controlled in the camera frame
189 
190  // we want to see a sphere on a sphere
191  std::cout << std::endl ;
192  task.addFeature(p,pd) ;
193 
194  // set the gain
195  task.setLambda(1) ;
196 
197  // Display task information
198  task.print() ;
199  // exit(1) ;
200  unsigned int iter=0 ;
201  // loop
202  while(iter++ < 500)
203  {
204  std::cout << "---------------------------------------------" << iter <<std::endl ;
205  vpColVector v ;
206 
207  // get the robot position
208  robot.getPosition(wMc) ;
209  // Compute the position of the camera wrt the object frame
210  cMo = wMc.inverse() * wMo;
211 
212  // new sphere position: retrieve x,y and Z of the vpSphere structure
213  sphere.track(cMo) ;
214  vpFeatureBuilder::create(p,sphere);
215 
216  vpColVector de2dt(6) ;
217  de2dt[2] = 1 ; // should be zero in (I-WpW)de2dt
218  de2dt[5] = 0.01 ; // should be ok
219  de2dt[0] = 0.01 ; // should generate a motion on (I-WpW)de2dt[4]
220 
221  // compute the control law
222  v = task.computeControlLaw() ;
223 
224  std::cout << "de2dt :"<< de2dt.t() << std::endl;
225  vpColVector sec ;
226  sec = task.secondaryTask(de2dt) ;
227  std::cout << "(I-WpW)de2dt :"<< sec.t() << std::endl;
228 
229  if (iter>20) v += sec ;
230 
231  // send the camera velocity to the controller
233 
234  std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() <<std::endl ;
235  }
236 
237  // Display task information
238  task.print() ;
239  task.kill();
240  return 0;
241  }
242  catch(vpException &e) {
243  std::cout << "Catch a ViSP exception: " << e << std::endl;
244  return 1;
245  }
246 }
247 
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
vpColVector secondaryTask(const vpColVector &de2dt, const bool &useLargeProjectionOperator=false)
Definition: vpServo.cpp:1513
void kill()
Definition: vpServo.cpp:191
vpColVector getError() const
Definition: vpServo.h:271
vpColVector computeControlLaw()
Definition: vpServo.cpp:954
vpRowVector t() const
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