Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
servoSimuSphere2DCamVelocity.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Simulation of a 2D visual servoing on a sphere.
33  *
34  * Authors:
35  * Eric Marchand
36  * Fabien Spindler
37  *
38  *****************************************************************************/
39 
49 #include <stdio.h>
50 #include <stdlib.h>
51 
52 #include <visp3/core/vpHomogeneousMatrix.h>
53 #include <visp3/core/vpMath.h>
54 #include <visp3/core/vpSphere.h>
55 #include <visp3/io/vpParseArgv.h>
56 #include <visp3/robot/vpSimulatorCamera.h>
57 #include <visp3/visual_features/vpFeatureBuilder.h>
58 #include <visp3/visual_features/vpFeatureEllipse.h>
59 #include <visp3/vs/vpServo.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':
114  usage(argv[0], NULL);
115  return false;
116  break;
117 
118  default:
119  usage(argv[0], optarg_);
120  return false;
121  break;
122  }
123  }
124 
125  if ((c == 1) || (c == -1)) {
126  // standalone param or error
127  usage(argv[0], NULL);
128  std::cerr << "ERROR: " << std::endl;
129  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
130  return false;
131  }
132 
133  return true;
134 }
135 
136 int main(int argc, const char **argv)
137 {
138  try {
139  // Read the command line options
140  if (getOptions(argc, argv) == false) {
141  exit(-1);
142  }
143 
144  vpServo task;
145  vpSimulatorCamera robot;
146 
147  std::cout << std::endl;
148  std::cout << "-------------------------------------------------------" << std::endl;
149  std::cout << " Test program for vpServo " << std::endl;
150  std::cout << " Simulation " << std::endl;
151  std::cout << " task : servo a sphere " << std::endl;
152  std::cout << "-------------------------------------------------------" << std::endl;
153  std::cout << std::endl;
154 
155  // sets the initial camera location
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
180  // coordinates sets the current position of the visual feature
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  task.addFeature(p, pd);
192 
193  // set the gain
194  task.setLambda(1);
195 
196  // Display task information
197  task.print();
198 
199  unsigned int iter = 0;
200  // loop
201  while (iter++ < 200) {
202  std::cout << "---------------------------------------------" << iter << std::endl;
203  vpColVector v;
204 
205  // get the robot position
206  robot.getPosition(wMc);
207  // Compute the position of the camera wrt the object frame
208  cMo = wMc.inverse() * wMo;
209 
210  // new sphere position: retrieve x,y and Z of the vpSphere structure
211  sphere.track(cMo);
212  vpFeatureBuilder::create(p, sphere);
213 
214  // compute the control law
215  v = task.computeControlLaw();
216 
217  std::cout << "Task rank: " << task.getTaskRank() << std::endl;
218  // send the camera velocity to the controller
220 
221  std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
222  }
223 
224  // Display task information
225  task.print();
226  task.kill();
227  return EXIT_SUCCESS;
228  } catch (const vpException &e) {
229  std::cout << "Catch a ViSP exception: " << e << std::endl;
230  return EXIT_FAILURE;
231  }
232 }
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:497
error that can be emited by ViSP classes.
Definition: vpException.h:71
void track(const vpHomogeneousMatrix &cMo)
Class that defines what is a sphere.
Definition: vpSphere.h:60
void setWorldCoordinates(const vpColVector &oP)
Definition: vpSphere.cpp:51
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
void kill()
Definition: vpServo.cpp:192
vpColVector getError() const
Definition: vpServo.h:282
vpColVector computeControlLaw()
Definition: vpServo.cpp:935
void setLambda(double c)
Definition: vpServo.h:406
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:313
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:223
unsigned int getTaskRank() const
Definition: vpServo.cpp:1821