Visual Servoing Platform  version 3.6.1 under development (2024-04-19)
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/vpHomogeneousMatrix.h>
58 #include <visp3/core/vpMath.h>
59 #include <visp3/io/vpParseArgv.h>
60 #include <visp3/robot/vpSimulatorCamera.h>
61 #include <visp3/visual_features/vpFeatureBuilder.h>
62 #include <visp3/visual_features/vpFeatureEllipse.h>
63 #include <visp3/vs/vpServo.h>
64 
65 // List of allowed command line options
66 #define GETOPTARGS "h"
67 
68 void usage(const char *name, const char *badparam);
69 bool getOptions(int argc, const char **argv);
70 
79 void usage(const char *name, const char *badparam)
80 {
81  fprintf(stdout, "\n\
82 Simulation of a 2D visual servoing on a circle:\n\
83 - eye-in-hand control law,\n\
84 - velocity computed in the camera frame,\n\
85 - without display.\n\
86  \n\
87 SYNOPSIS\n\
88  %s [-h]\n",
89  name);
90 
91  fprintf(stdout, "\n\
92 OPTIONS: Default\n\
93  \n\
94  -h\n\
95  Print the help.\n");
96 
97  if (badparam)
98  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
99 }
100 
110 bool getOptions(int argc, const char **argv)
111 {
112  const char *optarg_;
113  int c;
114  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
115 
116  switch (c) {
117  case 'h':
118  usage(argv[0], nullptr);
119  return false;
120 
121  default:
122  usage(argv[0], optarg_);
123  return false;
124  }
125  }
126 
127  if ((c == 1) || (c == -1)) {
128  // standalone param or error
129  usage(argv[0], nullptr);
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 #if (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
141  try {
142  // Read the command line options
143  if (getOptions(argc, argv) == false) {
144  return EXIT_FAILURE;
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  // sets the initial camera location
160  cMo[0][3] = 0.1;
161  cMo[1][3] = 0.2;
162  cMo[2][3] = 2;
163 
164  vpHomogeneousMatrix wMc, wMo;
165  robot.getPosition(wMc);
166  wMo = wMc * cMo; // Compute the position of the object in the world frame
167 
168  vpHomogeneousMatrix cMod;
169  cMod[0][3] = 0;
170  cMod[1][3] = 0;
171  cMod[2][3] = 1;
172 
173  // sets the circle coordinates in the world frame
174  vpCircle circle;
175  circle.setWorldCoordinates(0, 0, 1, 0, 0, 0, 0.1);
176 
177  // sets the desired position of the visual feature
178  vpFeatureEllipse pd;
179  circle.track(cMod);
180  vpFeatureBuilder::create(pd, circle);
181 
182  // project : computes the circle coordinates in the camera frame and its
183  // 2D coordinates
184 
185  // sets the current position of the visual feature
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  std::cout << "---------------------------------------------" << iter << std::endl;
209  vpColVector v;
210 
211  // get the robot position
212  robot.getPosition(wMc);
213  // Compute the position of the object frame in the camera frame
214  cMo = wMc.inverse() * wMo;
215 
216  // new circle position: retrieve x,y and Z of the vpCircle structure
217  circle.track(cMo);
218  vpFeatureBuilder::create(p, circle);
219 
220  // compute the control law
221  v = task.computeControlLaw();
222  std::cout << "task rank: " << task.getTaskRank() << std::endl;
223  // send the camera velocity to the controller
225 
226  std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
227  }
228 
229  // Display task information
230  task.print();
231  return EXIT_SUCCESS;
232  }
233  catch (const vpException &e) {
234  std::cout << "Catch a ViSP exception: " << e << std::endl;
235  return EXIT_FAILURE;
236  }
237 #else
238  (void)argc;
239  (void)argv;
240  std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
241  return EXIT_SUCCESS;
242 #endif
243 }
Class that defines a 3D circle in the object frame and allows forward projection of a 3D circle in th...
Definition: vpCircle.h:86
void setWorldCoordinates(const vpColVector &oP) vp_override
Definition: vpCircle.cpp:57
Implementation of column vector and the associated operations.
Definition: vpColVector.h:163
error that can be emitted by ViSP classes.
Definition: vpException.h:59
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
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:69
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel) vp_override
@ CAMERA_FRAME
Definition: vpRobot.h:82
@ EYEINHAND_CAMERA
Definition: vpServo.h:155
void addFeature(vpBasicFeature &s_cur, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:329
unsigned int getTaskRank() const
Definition: vpServo.h:600
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:169
void setLambda(double c)
Definition: vpServo.h:976
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:132
vpColVector getError() const
Definition: vpServo.h:504
vpColVector computeControlLaw()
Definition: vpServo.cpp:703
Class that defines the simplest robot: a free flying camera.