Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
servoSimuCircle2DCamVelocity.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 circle.
32  *
33  * Authors:
34  * Eric Marchand
35  * Fabien Spindler
36  *
37  *****************************************************************************/
56 #include <stdlib.h>
57 #include <stdio.h>
58 
59 #include <visp3/core/vpCircle.h>
60 #include <visp3/visual_features/vpFeatureEllipse.h>
61 #include <visp3/visual_features/vpFeatureBuilder.h>
62 #include <visp3/core/vpHomogeneousMatrix.h>
63 #include <visp3/core/vpMath.h>
64 #include <visp3/io/vpParseArgv.h>
65 #include <visp3/vs/vpServo.h>
66 #include <visp3/robot/vpSimulatorCamera.h>
67 
68 // List of allowed command line options
69 #define GETOPTARGS "h"
70 
71 void usage(const char *name, const char *badparam);
72 bool getOptions(int argc, const char **argv);
73 
82 void usage(const char *name, const char *badparam)
83 {
84  fprintf(stdout, "\n\
85 Simulation of a 2D visual servoing on a circle:\n\
86 - eye-in-hand control law,\n\
87 - velocity computed in the camera frame,\n\
88 - without display.\n\
89  \n\
90 SYNOPSIS\n\
91  %s [-h]\n", name);
92 
93  fprintf(stdout, "\n\
94 OPTIONS: Default\n\
95  \n\
96  -h\n\
97  Print the help.\n");
98 
99  if (badparam)
100  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
101 }
102 
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 << " Simulation " << std::endl ;
154  std::cout << " task : servo a circle " << std::endl ;
155  std::cout << "-------------------------------------------------------" << std::endl ;
156  std::cout << std::endl ;
157 
158 
159  // sets the initial camera location
160  vpHomogeneousMatrix cMo ;
161  cMo[0][3] = 0.1 ;
162  cMo[1][3] = 0.2 ;
163  cMo[2][3] = 2 ;
164 
165  vpHomogeneousMatrix wMc, wMo;
166  robot.getPosition(wMc) ;
167  wMo = wMc * cMo; // Compute the position of the object in the world frame
168 
169  vpHomogeneousMatrix cMod ;
170  cMod[0][3] = 0 ;
171  cMod[1][3] = 0 ;
172  cMod[2][3] = 1 ;
173 
174  // sets the circle coordinates in the world frame
175  vpCircle circle ;
176  circle.setWorldCoordinates(0,0,1,0,0,0,0.1) ;
177 
178  // sets the desired position of the visual feature
179  vpFeatureEllipse pd ;
180  circle.track(cMod) ;
181  vpFeatureBuilder::create(pd,circle) ;
182 
183  // project : computes the circle coordinates in the camera frame and its 2D coordinates
184 
185  // sets the current position of the visual feature
186  vpFeatureEllipse p ;
187  circle.track(cMo) ;
188  vpFeatureBuilder::create(p,circle) ;
189 
190  // define the task
191  // - we want an eye-in-hand control law
192  // - robot is controlled in the camera frame
194 
195  // - we want to see a circle on a circle
196  std::cout << std::endl ;
197  task.addFeature(p,pd) ;
198 
199  // - set the gain
200  task.setLambda(1) ;
201 
202  // Display task information
203  task.print() ;
204 
205  unsigned int iter=0 ;
206  // loop
207  while(iter++ < 500)
208  {
209  std::cout << "---------------------------------------------" << iter <<std::endl ;
210  vpColVector v ;
211 
212  // get the robot position
213  robot.getPosition(wMc) ;
214  // Compute the position of the camera wrt the object frame
215  cMo = wMc.inverse() * wMo;
216 
217  // new circle position: retrieve x,y and Z of the vpCircle structure
218  circle.track(cMo) ;
219  vpFeatureBuilder::create(p,circle);
220 
221  // compute the control law
222  v = task.computeControlLaw() ;
223  std::cout << "task rank: " << task.getTaskRank() <<std::endl ;
224  // send the camera velocity to the controller
226 
227  std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() << std::endl ;
228  }
229 
230  // Display task information
231  task.print() ;
232  task.kill();
233  return 0;
234  }
235  catch(vpException &e) {
236  std::cout << "Catch a ViSP exception: " << e << std::endl;
237  return 1;
238  }
239 }
240 
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)
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)
Class that defines what is a circle.
Definition: vpCircle.h:57
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:222
unsigned int getTaskRank() const
Definition: vpServo.cpp:1849
void setWorldCoordinates(const vpColVector &oP)
Definition: vpCircle.cpp:62