Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
servoSimuThetaUCamVelocity.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 visual servoing using theta U visual features.
33  * tests the control law
34  * eye-in-hand control
35  * velocity computed in the camera frame
36  * using theta U visual feature
37  *
38 *****************************************************************************/
39 
48 #include <stdio.h>
49 #include <stdlib.h>
50 
51 #include <visp3/core/vpConfig.h>
52 #include <visp3/core/vpHomogeneousMatrix.h>
53 #include <visp3/core/vpMath.h>
54 #include <visp3/io/vpParseArgv.h>
55 #include <visp3/robot/vpSimulatorCamera.h>
56 #include <visp3/visual_features/vpFeatureThetaU.h>
57 #include <visp3/visual_features/vpFeatureTranslation.h>
58 #include <visp3/vs/vpServo.h>
59 
60 // List of allowed command line options
61 #define GETOPTARGS "h"
62 
63 #ifdef ENABLE_VISP_NAMESPACE
64 using namespace VISP_NAMESPACE_NAME;
65 #endif
66 
67 void usage(const char *name, const char *badparam);
68 bool getOptions(int argc, const char **argv);
77 void usage(const char *name, const char *badparam)
78 {
79  fprintf(stdout, "\n\
80 Simulation of avisual servoing using theta U visual feature:\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 
109 bool getOptions(int argc, const char **argv)
110 {
111  const char *optarg_;
112  int c;
113  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
114 
115  switch (c) {
116  case 'h':
117  usage(argv[0], nullptr);
118  return false;
119 
120  default:
121  usage(argv[0], optarg_);
122  return false;
123  }
124  }
125 
126  if ((c == 1) || (c == -1)) {
127  // standalone param or error
128  usage(argv[0], nullptr);
129  std::cerr << "ERROR: " << std::endl;
130  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
131  return false;
132  }
133 
134  return true;
135 }
136 
137 int main(int argc, const char **argv)
138 {
139 #if (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
140  try {
141  // Read the command line options
142  if (getOptions(argc, argv) == false) {
143  return EXIT_FAILURE;
144  }
145 
146  vpServo task;
147  vpSimulatorCamera robot;
148 
149  std::cout << std::endl;
150  std::cout << "-------------------------------------------------------" << std::endl;
151  std::cout << " Test program for vpServo " << std::endl;
152  std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl;
153  std::cout << " Simulation " << std::endl;
154  std::cout << " task : servo using theta U visual feature " << std::endl;
155  std::cout << "-------------------------------------------------------" << std::endl;
156  std::cout << std::endl;
157 
158  // sets the initial camera location
159  vpPoseVector c_r_o(0.1, 0.2, 2, vpMath::rad(20), vpMath::rad(10), vpMath::rad(50));
160 
161  vpHomogeneousMatrix cMo(c_r_o);
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 desired camera location
168  vpPoseVector cd_r_o(0, 0, 1, vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
169  vpHomogeneousMatrix cdMo(cd_r_o);
170 
171  // compute the rotation that the camera has to realize
172  vpHomogeneousMatrix cdMc;
173  cdMc = cdMo * cMo.inverse();
175  tu.buildFrom(cdMc);
176 
177  // define the task
178  // - we want an eye-in-hand control law
179  // - robot is controlled in the camera frame
182 
183  task.addFeature(tu);
184 
185  // - set the gain
186  task.setLambda(1);
187 
188  // Display task information
189  task.print();
190 
191  unsigned int iter = 0;
192  // loop
193  while (iter++ < 200) {
194  std::cout << "---------------------------------------------" << iter << std::endl;
195  vpColVector v;
196 
197  // get the robot position
198  robot.getPosition(wMc);
199  // Compute the position of the object frame in the camera frame
200  cMo = wMc.inverse() * wMo;
201 
202  // new rotation to achieve
203  cdMc = cdMo * cMo.inverse();
204  tu.buildFrom(cdMc);
205 
206  // compute the control law
207  v = task.computeControlLaw();
208 
209  // send the camera velocity to the controller
211 
212  std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
213  }
214 
215  // Display task information
216  task.print();
217  return EXIT_SUCCESS;
218  }
219  catch (const vpException &e) {
220  std::cout << "Catch a ViSP exception: " << e << std::endl;
221  return EXIT_FAILURE;
222  }
223 #else
224  (void)argc;
225  (void)argv;
226  std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
227  return EXIT_SUCCESS;
228 #endif
229 }
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 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 & buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
vpHomogeneousMatrix inverse() const
static double rad(double deg)
Definition: vpMath.h:129
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:70
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:203
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel) VP_OVERRIDE
@ CAMERA_FRAME
Definition: vpRobot.h:84
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:380
@ 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
@ DESIRED
Definition: vpServo.h:208
Class that defines the simplest robot: a free flying camera.