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