Visual Servoing Platform  version 3.6.1 under development (2024-04-23)
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/vpHomogeneousMatrix.h>
52 #include <visp3/core/vpMath.h>
53 #include <visp3/io/vpParseArgv.h>
54 #include <visp3/robot/vpSimulatorCamera.h>
55 #include <visp3/visual_features/vpFeatureThetaU.h>
56 #include <visp3/visual_features/vpFeatureTranslation.h>
57 #include <visp3/vs/vpServo.h>
58 
59 // List of allowed command line options
60 #define GETOPTARGS "h"
61 void usage(const char *name, const char *badparam);
62 bool getOptions(int argc, const char **argv);
71 void usage(const char *name, const char *badparam)
72 {
73  fprintf(stdout, "\n\
74 Simulation of avisual servoing using theta U visual feature:\n\
75 - eye-in-hand control law,\n\
76 - velocity computed in the camera frame,\n\
77 - without display.\n\
78  \n\
79 SYNOPSIS\n\
80  %s [-h]\n",
81  name);
82 
83  fprintf(stdout, "\n\
84 OPTIONS: Default\n\
85  \n\
86  -h\n\
87  Print the help.\n");
88 
89  if (badparam)
90  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
91 }
92 
103 bool getOptions(int argc, const char **argv)
104 {
105  const char *optarg_;
106  int c;
107  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
108 
109  switch (c) {
110  case 'h':
111  usage(argv[0], nullptr);
112  return false;
113 
114  default:
115  usage(argv[0], optarg_);
116  return false;
117  }
118  }
119 
120  if ((c == 1) || (c == -1)) {
121  // standalone param or error
122  usage(argv[0], nullptr);
123  std::cerr << "ERROR: " << std::endl;
124  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
125  return false;
126  }
127 
128  return true;
129 }
130 
131 int main(int argc, const char **argv)
132 {
133 #if (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
134  try {
135  // Read the command line options
136  if (getOptions(argc, argv) == false) {
137  return EXIT_FAILURE;
138  }
139 
140  vpServo task;
141  vpSimulatorCamera robot;
142 
143  std::cout << std::endl;
144  std::cout << "-------------------------------------------------------" << std::endl;
145  std::cout << " Test program for vpServo " << std::endl;
146  std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl;
147  std::cout << " Simulation " << std::endl;
148  std::cout << " task : servo using theta U visual feature " << std::endl;
149  std::cout << "-------------------------------------------------------" << std::endl;
150  std::cout << std::endl;
151 
152  // sets the initial camera location
153  vpPoseVector c_r_o(0.1, 0.2, 2, vpMath::rad(20), vpMath::rad(10), vpMath::rad(50));
154 
155  vpHomogeneousMatrix cMo(c_r_o);
156  // Compute the position of the object in the world frame
157  vpHomogeneousMatrix wMc, wMo;
158  robot.getPosition(wMc);
159  wMo = wMc * cMo;
160 
161  // sets the desired camera location
162  vpPoseVector cd_r_o(0, 0, 1, vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
163  vpHomogeneousMatrix cdMo(cd_r_o);
164 
165  // compute the rotation that the camera has to realize
166  vpHomogeneousMatrix cdMc;
167  cdMc = cdMo * cMo.inverse();
169  tu.buildFrom(cdMc);
170 
171  // define the task
172  // - we want an eye-in-hand control law
173  // - robot is controlled in the camera frame
176 
177  task.addFeature(tu);
178 
179  // - set the gain
180  task.setLambda(1);
181 
182  // Display task information
183  task.print();
184 
185  unsigned int iter = 0;
186  // loop
187  while (iter++ < 200) {
188  std::cout << "---------------------------------------------" << iter << std::endl;
189  vpColVector v;
190 
191  // get the robot position
192  robot.getPosition(wMc);
193  // Compute the position of the object frame in the camera frame
194  cMo = wMc.inverse() * wMo;
195 
196  // new rotation to achieve
197  cdMc = cdMo * cMo.inverse();
198  tu.buildFrom(cdMc);
199 
200  // compute the control law
201  v = task.computeControlLaw();
202 
203  // send the camera velocity to the controller
205 
206  std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
207  }
208 
209  // Display task information
210  task.print();
211  return EXIT_SUCCESS;
212  } catch (const vpException &e) {
213  std::cout << "Catch a ViSP exception: " << e << std::endl;
214  return EXIT_FAILURE;
215  }
216 #else
217  (void)argc;
218  (void)argv;
219  std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
220  return EXIT_SUCCESS;
221 #endif
222 }
Implementation of column vector and the associated operations.
Definition: vpColVector.h:163
error that can be emitted by ViSP classes.
Definition: vpException.h:59
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
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
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:378
@ 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
@ DESIRED
Definition: vpServo.h:202
Class that defines the simplest robot: a free flying camera.