Visual Servoing Platform  version 3.6.1 under development (2024-03-28)
servoSimuPoint2DhalfCamVelocity1.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 2 1/2 D visual servoing.
33  *
34 *****************************************************************************/
35 
46 #include <stdio.h>
47 #include <stdlib.h>
48 
49 #include <visp3/core/vpHomogeneousMatrix.h>
50 #include <visp3/core/vpMath.h>
51 #include <visp3/core/vpPoint.h>
52 #include <visp3/io/vpParseArgv.h>
53 #include <visp3/robot/vpSimulatorCamera.h>
54 #include <visp3/visual_features/vpFeatureBuilder.h>
55 #include <visp3/visual_features/vpFeaturePoint.h>
56 #include <visp3/visual_features/vpFeatureThetaU.h>
57 #include <visp3/visual_features/vpGenericFeature.h>
58 #include <visp3/vs/vpServo.h>
59 
60 // List of allowed command line options
61 #define GETOPTARGS "h"
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 2 1/2 D visual servoing (x,y,Z,theta U):\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(stderr, "ERROR: \n");
94  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
95  }
96 }
97 
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 << " task : 2 1/2 D visual servoing " << std::endl;
152  std::cout << "-------------------------------------------------------" << std::endl;
153  std::cout << std::endl;
154 
155  // sets the initial camera location
156  vpPoseVector c_r_o(0.1, 0.2, 2, vpMath::rad(20), vpMath::rad(10), vpMath::rad(50));
157 
158  vpHomogeneousMatrix cMo(c_r_o);
159  // Compute the position of the object in the world frame
160  vpHomogeneousMatrix wMc, wMo;
161  robot.getPosition(wMc);
162  wMo = wMc * cMo;
163 
164  // sets the desired camera location
165  vpPoseVector cd_r_o(0, 0, 1, vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
166  vpHomogeneousMatrix cdMo(cd_r_o);
167 
168  // sets the point coordinates in the world frame
169  vpPoint point(0, 0, 0);
170  // computes the point coordinates in the camera frame and its 2D
171  // coordinates
172  point.track(cMo);
173 
174  vpPoint pointd(0, 0, 0);
175  pointd.track(cdMo);
176  //------------------------------------------------------------------
177  // 1st feature (x,y)
178  // want to it at (0,0)
179  vpFeaturePoint p;
180  vpFeatureBuilder::create(p, point);
181 
182  vpFeaturePoint pd;
183  vpFeatureBuilder::create(pd, pointd);
184 
185  //------------------------------------------------------------------
186  // 2nd feature (Z)
187  // not necessary to project twice (reuse p)
189  vpFeatureBuilder::create(Z, point); // retrieve x,y and Z of the vpPoint structure
190 
191  // want to see it one meter away (here again use pd)
192  vpFeaturePoint3D Zd;
193  vpFeatureBuilder::create(Zd, pointd); // retrieve x,y and Z of the vpPoint structure
194 
195  //------------------------------------------------------------------
196  // 3rd feature ThetaU
197  // compute the rotation that the camera has to achieve
198  vpHomogeneousMatrix cdMc;
199  cdMc = cdMo * cMo.inverse();
200 
202  tu.buildFrom(cdMc);
203 
204  // sets the desired rotation (always zero !)
205  // since s is the rotation that the camera has to achieve
206 
207  //------------------------------------------------------------------
208  // define the task
209  // - we want an eye-in-hand control law
210  // - robot is controlled in the camera frame
212 
213  task.addFeature(p, pd);
214  task.addFeature(Z, Zd, vpFeaturePoint3D::selectZ());
215  task.addFeature(tu);
216 
217  // set the gain
218  task.setLambda(1);
219 
220  // Display task information
221  task.print();
222 
223  unsigned int iter = 0;
224  // loop
225  while (iter++ < 200) {
226  std::cout << "---------------------------------------------" << iter << std::endl;
227  vpColVector v;
228 
229  // get the robot position
230  robot.getPosition(wMc);
231  // Compute the position of the object frame in the camera frame
232  cMo = wMc.inverse() * wMo;
233 
234  // update the feature
235  point.track(cMo);
236  vpFeatureBuilder::create(p, point);
237  vpFeatureBuilder::create(Z, point);
238 
239  cdMc = cdMo * cMo.inverse();
240  tu.buildFrom(cdMc);
241 
242  // compute the control law
243  v = task.computeControlLaw();
244  // send the camera velocity to the controller ") ;
246 
247  std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
248  }
249 
250  // Display task information
251  task.print();
252  std::cout << "Final camera location:\n " << cMo << std::endl;
253  return EXIT_SUCCESS;
254  } catch (const vpException &e) {
255  std::cout << "Catch a ViSP exception: " << e << std::endl;
256  return EXIT_SUCCESS;
257  }
258 #else
259  (void)argc;
260  (void)argv;
261  std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
262  return EXIT_SUCCESS;
263 #endif
264 }
Implementation of column vector and the associated operations.
Definition: vpColVector.h:163
error that can be emitted by ViSP classes.
Definition: vpException.h:59
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:127
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:77
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:189
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel) vp_override
@ CAMERA_FRAME
Definition: vpRobot.h:82
@ EYEINHAND_CAMERA
Definition: vpServo.h:155
void addFeature(vpBasicFeature &s_cur, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:329
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:169
void setLambda(double c)
Definition: vpServo.h:976
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:132
vpColVector getError() const
Definition: vpServo.h:504
vpColVector computeControlLaw()
Definition: vpServo.cpp:703
Class that defines the simplest robot: a free flying camera.