Visual Servoing Platform  version 3.6.1 under development (2024-11-14)
servoSimuPoint2DCamVelocity1.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 point.
33  *
34 *****************************************************************************/
35 
44 #include <stdio.h>
45 #include <stdlib.h>
46 
47 #include <visp3/core/vpConfig.h>
48 #include <visp3/core/vpHomogeneousMatrix.h>
49 #include <visp3/core/vpMath.h>
50 #include <visp3/io/vpParseArgv.h>
51 #include <visp3/robot/vpSimulatorCamera.h>
52 #include <visp3/visual_features/vpFeatureBuilder.h>
53 #include <visp3/visual_features/vpFeaturePoint.h>
54 #include <visp3/vs/vpServo.h>
55 
56 // List of allowed command line options
57 #define GETOPTARGS "h"
58 
59 #ifdef ENABLE_VISP_NAMESPACE
60 using namespace VISP_NAMESPACE_NAME;
61 #endif
62 
63 void usage(const char *name, const char *badparam);
64 bool getOptions(int argc, const char **argv);
65 
74 void usage(const char *name, const char *badparam)
75 {
76  fprintf(stdout, "\n\
77 Simulation of a 2D visual servoing on a point:\n\
78 - eye-in-hand control law,\n\
79 - velocity computed in the camera frame,\n\
80 - without display.\n\
81  \n\
82 SYNOPSIS\n\
83  %s [-h]\n",
84  name);
85 
86  fprintf(stdout, "\n\
87 OPTIONS: Default\n\
88  \n\
89  -h\n\
90  Print the help.\n");
91 
92  if (badparam)
93  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
94 }
95 
106 bool getOptions(int argc, const char **argv)
107 {
108  const char *optarg_;
109  int c;
110  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
111 
112  switch (c) {
113  case 'h':
114  usage(argv[0], nullptr);
115  return false;
116 
117  default:
118  usage(argv[0], optarg_);
119  return false;
120  }
121  }
122 
123  if ((c == 1) || (c == -1)) {
124  // standalone param or error
125  usage(argv[0], nullptr);
126  std::cerr << "ERROR: " << std::endl;
127  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
128  return false;
129  }
130 
131  return true;
132 }
133 
134 int main(int argc, const char **argv)
135 {
136 #if (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
137  try {
138  // Read the command line options
139  if (getOptions(argc, argv) == false) {
140  return EXIT_FAILURE;
141  }
142 
143  vpServo task;
144  vpSimulatorCamera robot;
145 
146  // sets the initial camera location
148  cMo[0][3] = 0.1;
149  cMo[1][3] = 0.2;
150  cMo[2][3] = 2;
151 
152  // Compute the position of the object in the world frame
153  vpHomogeneousMatrix wMc, wMo;
154  robot.getPosition(wMc);
155  wMo = wMc * cMo;
156 
157  // sets the point coordinates in the world frame
158  vpPoint point(0, 0, 0);
159 
160  // computes the point coordinates in the camera frame and its 2D
161  // coordinates
162  point.track(cMo);
163 
164  // sets the current position of the visual feature
165  vpFeaturePoint p;
166  vpFeatureBuilder::create(p, point); // retrieve x,y and Z of the vpPoint structure
167 
168  // sets the desired position of the visual feature
169  vpFeaturePoint pd;
170  pd.buildFrom(0, 0, 1); // buildFrom(x,y,Z) ;
171 
172  // define the task
173  // - we want an eye-in-hand control law
174  // - robot is controlled in the camera frame
176 
177  // we want to see a point on a point
178  std::cout << std::endl;
179  task.addFeature(p, pd);
180 
181  // set the gain
182  task.setLambda(1);
183 
184  // Display task information
185  task.print();
186 
187  unsigned int iter = 0;
188  // loop
189  while (iter++ < 100) {
190  std::cout << "---------------------------------------------" << iter << std::endl;
191  vpColVector v;
192 
193  // get the robot position
194  robot.getPosition(wMc);
195  // Compute the position of the object frame in the camera frame
196  cMo = wMc.inverse() * wMo;
197 
198  // new point position
199  point.track(cMo);
200  // retrieve x,y and Z of the vpPoint structure
201  vpFeatureBuilder::create(p, point);
202 
203  // compute the control law
204  v = task.computeControlLaw();
205 
206  // send the camera velocity to the controller
208 
209  std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
210  }
211 
212  // Display task information
213  task.print();
214  return EXIT_SUCCESS;
215  }
216  catch (const vpException &e) {
217  std::cout << "Catch a ViSP exception: " << e << std::endl;
218  return EXIT_FAILURE;
219  }
220 #else
221  (void)argc;
222  (void)argv;
223  std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
224  return EXIT_SUCCESS;
225 #endif
226 }
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 a 2D point visual feature which is composed by two parameters that are the cartes...
vpFeaturePoint & buildFrom(const double &x, const double &y, const double &Z)
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
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:79
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
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.