Visual Servoing Platform  version 3.5.1 under development (2022-12-02)
servoSimuPoint2DhalfCamVelocity1.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 2 1/2 D visual servoing.
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/vpFeatureBuilder.h>
59 #include <visp3/visual_features/vpFeaturePoint.h>
60 #include <visp3/visual_features/vpFeatureThetaU.h>
61 #include <visp3/visual_features/vpGenericFeature.h>
62 #include <visp3/vs/vpServo.h>
63 
64 // List of allowed command line options
65 #define GETOPTARGS "h"
66 
67 void usage(const char *name, const char *badparam);
68 bool getOptions(int argc, const char **argv);
69 
78 void usage(const char *name, const char *badparam)
79 {
80  fprintf(stdout, "\n\
81 Simulation of a 2 1/2 D visual servoing (x,y,Z,theta U):\n\
82 - eye-in-hand control law,\n\
83 - velocity computed in the camera frame,\n\
84 - without display.\n\
85  \n\
86 SYNOPSIS\n\
87  %s [-h]\n",
88  name);
89 
90  fprintf(stdout, "\n\
91 OPTIONS: Default\n\
92  \n\
93  -h\n\
94  Print the help.\n");
95 
96  if (badparam) {
97  fprintf(stderr, "ERROR: \n");
98  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
99  }
100 }
101 
112 bool getOptions(int argc, const char **argv)
113 {
114  const char *optarg_;
115  int c;
116  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
117 
118  switch (c) {
119  case 'h':
120  usage(argv[0], NULL);
121  return false;
122 
123  default:
124  usage(argv[0], optarg_);
125  return false;
126  }
127  }
128 
129  if ((c == 1) || (c == -1)) {
130  // standalone param or error
131  usage(argv[0], NULL);
132  std::cerr << "ERROR: " << std::endl;
133  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
134  return false;
135  }
136 
137  return true;
138 }
139 
140 int main(int argc, const char **argv)
141 {
142 #if (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
143  try {
144  // Read the command line options
145  if (getOptions(argc, argv) == false) {
146  exit(-1);
147  }
148 
149  vpServo task;
150  vpSimulatorCamera robot;
151 
152  std::cout << std::endl;
153  std::cout << "-------------------------------------------------------" << std::endl;
154  std::cout << " Test program for vpServo " << std::endl;
155  std::cout << " task : 2 1/2 D visual servoing " << std::endl;
156  std::cout << "-------------------------------------------------------" << std::endl;
157  std::cout << std::endl;
158 
159  // sets the initial camera location
160  vpPoseVector c_r_o(0.1, 0.2, 2, vpMath::rad(20), vpMath::rad(10), vpMath::rad(50));
161 
162  vpHomogeneousMatrix cMo(c_r_o);
163  // Compute the position of the object in the world frame
164  vpHomogeneousMatrix wMc, wMo;
165  robot.getPosition(wMc);
166  wMo = wMc * cMo;
167 
168  // sets the desired camera location
169  vpPoseVector cd_r_o(0, 0, 1, vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
170  vpHomogeneousMatrix cdMo(cd_r_o);
171 
172  // sets the point coordinates in the world frame
173  vpPoint point(0, 0, 0);
174  // computes the point coordinates in the camera frame and its 2D
175  // coordinates
176  point.track(cMo);
177 
178  vpPoint pointd(0, 0, 0);
179  pointd.track(cdMo);
180  //------------------------------------------------------------------
181  // 1st feature (x,y)
182  // want to it at (0,0)
183  vpFeaturePoint p;
184  vpFeatureBuilder::create(p, point);
185 
186  vpFeaturePoint pd;
187  vpFeatureBuilder::create(pd, pointd);
188 
189  //------------------------------------------------------------------
190  // 2nd feature (Z)
191  // not necessary to project twice (reuse p)
193  vpFeatureBuilder::create(Z, point); // retrieve x,y and Z of the vpPoint structure
194 
195  // want to see it one meter away (here again use pd)
196  vpFeaturePoint3D Zd;
197  vpFeatureBuilder::create(Zd, pointd); // retrieve x,y and Z of the vpPoint structure
198 
199  //------------------------------------------------------------------
200  // 3rd feature ThetaU
201  // compute the rotation that the camera has to achieve
202  vpHomogeneousMatrix cdMc;
203  cdMc = cdMo * cMo.inverse();
204 
206  tu.buildFrom(cdMc);
207 
208  // sets the desired rotation (always zero !)
209  // since s is the rotation that the camera has to achieve
210 
211  //------------------------------------------------------------------
212  // define the task
213  // - we want an eye-in-hand control law
214  // - robot is controlled in the camera frame
216 
217  task.addFeature(p, pd);
218  task.addFeature(Z, Zd, vpFeaturePoint3D::selectZ());
219  task.addFeature(tu);
220 
221  // set the gain
222  task.setLambda(1);
223 
224  // Display task information
225  task.print();
226 
227  unsigned int iter = 0;
228  // loop
229  while (iter++ < 200) {
230  std::cout << "---------------------------------------------" << iter << std::endl;
231  vpColVector v;
232 
233  // get the robot position
234  robot.getPosition(wMc);
235  // Compute the position of the object frame in the camera frame
236  cMo = wMc.inverse() * wMo;
237 
238  // update the feature
239  point.track(cMo);
240  vpFeatureBuilder::create(p, point);
241  vpFeatureBuilder::create(Z, point);
242 
243  cdMc = cdMo * cMo.inverse();
244  tu.buildFrom(cdMc);
245 
246  // compute the control law
247  v = task.computeControlLaw();
248  // send the camera velocity to the controller ") ;
250 
251  std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
252  }
253 
254  // Display task information
255  task.print();
256  std::cout << "Final camera location:\n " << cMo << std::endl;
257  return EXIT_SUCCESS;
258  } catch (const vpException &e) {
259  std::cout << "Catch a ViSP exception: " << e << std::endl;
260  return EXIT_SUCCESS;
261  }
262 #else
263  (void)argc;
264  (void)argv;
265  std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
266  return EXIT_SUCCESS;
267 #endif
268 }
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 the 3D point visual feature.
static unsigned int selectZ()
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
Class that defines a 3D visual feature from a axis/angle parametrization that represent the rotatio...
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix inverse() const
void buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
static double rad(double deg)
Definition: vpMath.h:117
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
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:152
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.