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