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