Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
servoSimuSphere2DCamVelocity.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 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 https://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 *****************************************************************************/
35 
45 #include <stdio.h>
46 #include <stdlib.h>
47 
48 #include <visp3/core/vpConfig.h>
49 #include <visp3/core/vpHomogeneousMatrix.h>
50 #include <visp3/core/vpMath.h>
51 #include <visp3/core/vpSphere.h>
52 #include <visp3/io/vpParseArgv.h>
53 #include <visp3/robot/vpSimulatorCamera.h>
54 #include <visp3/visual_features/vpFeatureBuilder.h>
55 #include <visp3/visual_features/vpFeatureEllipse.h>
56 #include <visp3/vs/vpServo.h>
57 
58 // List of allowed command line options
59 #define GETOPTARGS "h"
60 
61 #ifdef ENABLE_VISP_NAMESPACE
62 using namespace VISP_NAMESPACE_NAME;
63 #endif
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  \n\
84 SYNOPSIS\n\
85  %s [-h]\n",
86  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], nullptr);
117  return false;
118 
119  default:
120  usage(argv[0], optarg_);
121  return false;
122  }
123  }
124 
125  if ((c == 1) || (c == -1)) {
126  // standalone param or error
127  usage(argv[0], nullptr);
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 #if (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
139  try {
140  // Read the command line options
141  if (getOptions(argc, argv) == false) {
142  return EXIT_FAILURE;
143  }
144 
145  vpServo task;
146  vpSimulatorCamera robot;
147 
148  std::cout << std::endl;
149  std::cout << "-------------------------------------------------------" << std::endl;
150  std::cout << " Test program for vpServo " << std::endl;
151  std::cout << " Simulation " << std::endl;
152  std::cout << " task : servo a sphere " << std::endl;
153  std::cout << "-------------------------------------------------------" << std::endl;
154  std::cout << std::endl;
155 
156  // sets the initial camera location
158  cMo[0][3] = 0.1;
159  cMo[1][3] = 0.2;
160  cMo[2][3] = 2;
161  // Compute the position of the object in the world frame
162  vpHomogeneousMatrix wMc, wMo;
163  robot.getPosition(wMc);
164  wMo = wMc * cMo;
165 
166  vpHomogeneousMatrix cMod;
167  cMod[0][3] = 0;
168  cMod[1][3] = 0;
169  cMod[2][3] = 1;
170 
171  // sets the sphere coordinates in the world frame
172  vpSphere sphere;
173  sphere.setWorldCoordinates(0, 0, 0, 0.1);
174 
175  // sets the desired position of the visual feature
176  vpFeatureEllipse pd;
177  sphere.track(cMod);
178  vpFeatureBuilder::create(pd, sphere);
179 
180  // computes the sphere coordinates in the camera frame and its 2D
181  // coordinates sets the current position of the visual feature
183  sphere.track(cMo);
184  vpFeatureBuilder::create(p, sphere);
185 
186  // define the task
187  // - we want an eye-in-hand control law
188  // - robot is controlled in the camera frame
190 
191  // we want to see a sphere on a sphere
192  task.addFeature(p, pd);
193 
194  // set the gain
195  task.setLambda(1);
196 
197  // Display task information
198  task.print();
199 
200  unsigned int iter = 0;
201  // loop
202  while (iter++ < 200) {
203  std::cout << "---------------------------------------------" << iter << std::endl;
204  vpColVector v;
205 
206  // get the robot position
207  robot.getPosition(wMc);
208  // Compute the position of the object frame in the camera frame
209  cMo = wMc.inverse() * wMo;
210 
211  // new sphere position: retrieve x,y and Z of the vpSphere structure
212  sphere.track(cMo);
213  vpFeatureBuilder::create(p, sphere);
214 
215  // compute the control law
216  v = task.computeControlLaw();
217 
218  std::cout << "Task rank: " << task.getTaskRank() << std::endl;
219  // send the camera velocity to the controller
221 
222  std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
223  }
224 
225  // Display task information
226  task.print();
227  return EXIT_SUCCESS;
228  }
229  catch (const vpException &e) {
230  std::cout << "Catch a ViSP exception: " << e << std::endl;
231  return EXIT_FAILURE;
232  }
233 #else
234  (void)argc;
235  (void)argv;
236  std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
237  return EXIT_SUCCESS;
238 #endif
239  }
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
error that can be emitted by ViSP classes.
Definition: vpException.h:60
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpImagePoint &t)
Class that defines 2D ellipse visual feature.
void track(const vpHomogeneousMatrix &cMo)
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix inverse() const
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:70
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel) VP_OVERRIDE
@ CAMERA_FRAME
Definition: vpRobot.h:84
@ EYEINHAND_CAMERA
Definition: vpServo.h:161
void addFeature(vpBasicFeature &s_cur, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:331
unsigned int getTaskRank() const
Definition: vpServo.h:606
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:171
void setLambda(double c)
Definition: vpServo.h:986
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:134
vpColVector getError() const
Definition: vpServo.h:510
vpColVector computeControlLaw()
Definition: vpServo.cpp:705
Class that defines the simplest robot: a free flying camera.
Class that defines a 3D sphere in the object frame and allows forward projection of a 3D sphere in th...
Definition: vpSphere.h:80
void setWorldCoordinates(const vpColVector &oP) VP_OVERRIDE
Definition: vpSphere.cpp:59