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