Visual Servoing Platform  version 3.1.0
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 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 
50 #include <stdio.h>
51 #include <stdlib.h>
52 
53 #include <visp3/core/vpHomogeneousMatrix.h>
54 #include <visp3/core/vpMath.h>
55 #include <visp3/core/vpSphere.h>
56 #include <visp3/io/vpParseArgv.h>
57 #include <visp3/robot/vpSimulatorCamera.h>
58 #include <visp3/visual_features/vpFeatureBuilder.h>
59 #include <visp3/visual_features/vpFeatureEllipse.h>
60 #include <visp3/vs/vpServo.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':
116  usage(argv[0], NULL);
117  return false;
118  break;
119 
120  default:
121  usage(argv[0], optarg_);
122  return false;
123  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 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  std::cout << std::endl;
150  std::cout << "-------------------------------------------------------" << std::endl;
151  std::cout << " Test program for vpServo " << std::endl;
152  std::cout << " Simulation " << std::endl;
153  std::cout << " task : servo a sphere with a secondary task" << std::endl;
154  std::cout << "-------------------------------------------------------" << std::endl;
155  std::cout << std::endl;
156 
157  // sets the initial camera location
159  cMo[0][3] = 0.1;
160  cMo[1][3] = 0.2;
161  cMo[2][3] = 2;
162  // Compute the position of the object in the world frame
163  vpHomogeneousMatrix wMc, wMo;
164  robot.getPosition(wMc);
165  wMo = wMc * cMo;
166 
167  vpHomogeneousMatrix cMod;
168  cMod[0][3] = 0;
169  cMod[1][3] = 0;
170  cMod[2][3] = 1;
171 
172  // sets the sphere coordinates in the world frame
173  vpSphere sphere;
174  sphere.setWorldCoordinates(0, 0, 0, 0.1);
175 
176  // sets the desired position of the visual feature
177  vpFeatureEllipse pd;
178  sphere.track(cMod);
179  vpFeatureBuilder::create(pd, sphere);
180 
181  // computes the sphere coordinates in the camera frame and its 2D
182  // coordinates sets the current position of the visual feature
184  sphere.track(cMo);
185  vpFeatureBuilder::create(p, sphere);
186 
187  // define the task
188  // - we want an eye-in-hand control law
189  // - robot is controlled in the camera frame
191 
192  // we want to see a sphere on a sphere
193  std::cout << std::endl;
194  task.addFeature(p, pd);
195 
196  // set the gain
197  task.setLambda(1);
198 
199  // Display task information
200  task.print();
201  // exit(1) ;
202  unsigned int iter = 0;
203  // loop
204  while (iter++ < 500) {
205  std::cout << "---------------------------------------------" << iter << std::endl;
206  vpColVector v;
207 
208  // get the robot position
209  robot.getPosition(wMc);
210  // Compute the position of the camera wrt the object frame
211  cMo = wMc.inverse() * wMo;
212 
213  // new sphere position: retrieve x,y and Z of the vpSphere structure
214  sphere.track(cMo);
215  vpFeatureBuilder::create(p, sphere);
216 
217  vpColVector de2dt(6);
218  de2dt[2] = 1; // should be zero in (I-WpW)de2dt
219  de2dt[5] = 0.01; // should be ok
220  de2dt[0] = 0.01; // should generate a motion on (I-WpW)de2dt[4]
221 
222  // compute the control law
223  v = task.computeControlLaw();
224 
225  std::cout << "de2dt :" << de2dt.t() << std::endl;
226  vpColVector sec;
227  sec = task.secondaryTask(de2dt);
228  std::cout << "(I-WpW)de2dt :" << sec.t() << std::endl;
229 
230  if (iter > 20)
231  v += sec;
232 
233  // send the camera velocity to the controller
235 
236  std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
237  }
238 
239  // Display task information
240  task.print();
241  task.kill();
242  return 0;
243  } catch (vpException &e) {
244  std::cout << "Catch a ViSP exception: " << e << std::endl;
245  return 1;
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:497
error that can be emited by ViSP classes.
Definition: vpException.h:71
void track(const vpHomogeneousMatrix &cMo)
vpHomogeneousMatrix inverse() const
vpRowVector t() const
Class that defines what is a sphere.
Definition: vpSphere.h:60
void setWorldCoordinates(const vpColVector &oP)
Definition: vpSphere.cpp:51
vpHomogeneousMatrix getPosition() const
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
vpColVector secondaryTask(const vpColVector &de2dt, const bool &useLargeProjectionOperator=false)
Definition: vpServo.cpp:1485
void kill()
Definition: vpServo.cpp:192
vpColVector computeControlLaw()
Definition: vpServo.cpp:935
void setLambda(double c)
Definition: vpServo.h:406
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:313
vpColVector getError() const
Definition: vpServo.h:282
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