Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
servoSimuCircle2DCamVelocity.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 circle.
33  *
34 *****************************************************************************/
53 #include <stdio.h>
54 #include <stdlib.h>
55 
56 #include <visp3/core/vpCircle.h>
57 #include <visp3/core/vpConfig.h>
58 #include <visp3/core/vpHomogeneousMatrix.h>
59 #include <visp3/core/vpMath.h>
60 #include <visp3/io/vpParseArgv.h>
61 #include <visp3/robot/vpSimulatorCamera.h>
62 #include <visp3/visual_features/vpFeatureBuilder.h>
63 #include <visp3/visual_features/vpFeatureEllipse.h>
64 #include <visp3/vs/vpServo.h>
65 
66 // List of allowed command line options
67 #define GETOPTARGS "h"
68 
69 #ifdef ENABLE_VISP_NAMESPACE
70 using namespace VISP_NAMESPACE_NAME;
71 #endif
72 
73 void usage(const char *name, const char *badparam);
74 bool getOptions(int argc, const char **argv);
75 
84 void usage(const char *name, const char *badparam)
85 {
86  fprintf(stdout, "\n\
87 Simulation of a 2D visual servoing on a circle:\n\
88 - eye-in-hand control law,\n\
89 - velocity computed in the camera frame,\n\
90 - without display.\n\
91  \n\
92 SYNOPSIS\n\
93  %s [-h]\n",
94  name);
95 
96  fprintf(stdout, "\n\
97 OPTIONS: Default\n\
98  \n\
99  -h\n\
100  Print the help.\n");
101 
102  if (badparam)
103  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
104 }
105 
115 bool getOptions(int argc, const char **argv)
116 {
117  const char *optarg_;
118  int c;
119  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
120 
121  switch (c) {
122  case 'h':
123  usage(argv[0], nullptr);
124  return false;
125 
126  default:
127  usage(argv[0], optarg_);
128  return false;
129  }
130  }
131 
132  if ((c == 1) || (c == -1)) {
133  // standalone param or error
134  usage(argv[0], nullptr);
135  std::cerr << "ERROR: " << std::endl;
136  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
137  return false;
138  }
139 
140  return true;
141 }
142 
143 int main(int argc, const char **argv)
144 {
145 #if (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
146  try {
147  // Read the command line options
148  if (getOptions(argc, argv) == false) {
149  return EXIT_FAILURE;
150  }
151 
152  vpServo task;
153  vpSimulatorCamera robot;
154 
155  std::cout << std::endl;
156  std::cout << "-------------------------------------------------------" << std::endl;
157  std::cout << " Test program for vpServo " << std::endl;
158  std::cout << " Simulation " << std::endl;
159  std::cout << " task : servo a circle " << std::endl;
160  std::cout << "-------------------------------------------------------" << std::endl;
161  std::cout << std::endl;
162 
163  // sets the initial camera location
165  cMo[0][3] = 0.1;
166  cMo[1][3] = 0.2;
167  cMo[2][3] = 2;
168 
169  vpHomogeneousMatrix wMc, wMo;
170  robot.getPosition(wMc);
171  wMo = wMc * cMo; // Compute the position of the object in the world frame
172 
173  vpHomogeneousMatrix cMod;
174  cMod[0][3] = 0;
175  cMod[1][3] = 0;
176  cMod[2][3] = 1;
177 
178  // sets the circle coordinates in the world frame
179  vpCircle circle;
180  circle.setWorldCoordinates(0, 0, 1, 0, 0, 0, 0.1);
181 
182  // sets the desired position of the visual feature
183  vpFeatureEllipse pd;
184  circle.track(cMod);
185  vpFeatureBuilder::create(pd, circle);
186 
187  // project : computes the circle coordinates in the camera frame and its
188  // 2D coordinates
189 
190  // sets the current position of the visual feature
192  circle.track(cMo);
193  vpFeatureBuilder::create(p, circle);
194 
195  // define the task
196  // - we want an eye-in-hand control law
197  // - robot is controlled in the camera frame
199 
200  // - we want to see a circle on a circle
201  std::cout << std::endl;
202  task.addFeature(p, pd);
203 
204  // - set the gain
205  task.setLambda(1);
206 
207  // Display task information
208  task.print();
209 
210  unsigned int iter = 0;
211  // loop
212  while (iter++ < 500) {
213  std::cout << "---------------------------------------------" << iter << std::endl;
214  vpColVector v;
215 
216  // get the robot position
217  robot.getPosition(wMc);
218  // Compute the position of the object frame in the camera frame
219  cMo = wMc.inverse() * wMo;
220 
221  // new circle position: retrieve x,y and Z of the vpCircle structure
222  circle.track(cMo);
223  vpFeatureBuilder::create(p, circle);
224 
225  // compute the control law
226  v = task.computeControlLaw();
227  std::cout << "task rank: " << task.getTaskRank() << std::endl;
228  // send the camera velocity to the controller
230 
231  std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
232  }
233 
234  // Display task information
235  task.print();
236  return EXIT_SUCCESS;
237  }
238  catch (const vpException &e) {
239  std::cout << "Catch a ViSP exception: " << e << std::endl;
240  return EXIT_FAILURE;
241  }
242 #else
243  (void)argc;
244  (void)argv;
245  std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
246  return EXIT_SUCCESS;
247 #endif
248  }
Class that defines a 3D circle in the object frame and allows forward projection of a 3D circle in th...
Definition: vpCircle.h:87
void setWorldCoordinates(const vpColVector &oP) VP_OVERRIDE
Definition: vpCircle.cpp:57
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.