Visual Servoing Platform  version 3.5.0 under development (2022-02-15)
servoSimuThetaUCamVelocity.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 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  * Authors:
39  * Eric Marchand
40  * Fabien Spindler
41  *
42  *****************************************************************************/
43 
52 #include <stdio.h>
53 #include <stdlib.h>
54 
55 #include <visp3/core/vpHomogeneousMatrix.h>
56 #include <visp3/core/vpMath.h>
57 #include <visp3/io/vpParseArgv.h>
58 #include <visp3/robot/vpSimulatorCamera.h>
59 #include <visp3/visual_features/vpFeatureThetaU.h>
60 #include <visp3/visual_features/vpFeatureTranslation.h>
61 #include <visp3/vs/vpServo.h>
62 
63 // List of allowed command line options
64 #define GETOPTARGS "h"
65 void usage(const char *name, const char *badparam);
66 bool getOptions(int argc, const char **argv);
75 void usage(const char *name, const char *badparam)
76 {
77  fprintf(stdout, "\n\
78 Simulation of avisual servoing using theta U visual feature:\n\
79 - eye-in-hand control law,\n\
80 - velocity computed in the camera frame,\n\
81 - without display.\n\
82  \n\
83 SYNOPSIS\n\
84  %s [-h]\n", 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(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
94 }
95 
106 bool getOptions(int argc, const char **argv)
107 {
108  const char *optarg_;
109  int c;
110  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
111 
112  switch (c) {
113  case 'h':
114  usage(argv[0], NULL);
115  return false;
116 
117  default:
118  usage(argv[0], optarg_);
119  return false;
120  }
121  }
122 
123  if ((c == 1) || (c == -1)) {
124  // standalone param or error
125  usage(argv[0], NULL);
126  std::cerr << "ERROR: " << std::endl;
127  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
128  return false;
129  }
130 
131  return true;
132 }
133 
134 int main(int argc, const char **argv)
135 {
136 #if (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
137  try {
138  // Read the command line options
139  if (getOptions(argc, argv) == false) {
140  exit(-1);
141  }
142 
143  vpServo task;
144  vpSimulatorCamera robot;
145 
146  std::cout << std::endl;
147  std::cout << "-------------------------------------------------------" << std::endl;
148  std::cout << " Test program for vpServo " << std::endl;
149  std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl;
150  std::cout << " Simulation " << std::endl;
151  std::cout << " task : servo using theta U visual feature " << 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  // compute the rotation that the camera has to realize
169  vpHomogeneousMatrix cdMc;
170  cdMc = cdMo * cMo.inverse();
172  tu.buildFrom(cdMc);
173 
174  // define the task
175  // - we want an eye-in-hand control law
176  // - robot is controlled in the camera frame
179 
180  task.addFeature(tu);
181 
182  // - set the gain
183  task.setLambda(1);
184 
185  // Display task information
186  task.print();
187 
188  unsigned int iter = 0;
189  // loop
190  while (iter++ < 200) {
191  std::cout << "---------------------------------------------" << iter << std::endl;
192  vpColVector v;
193 
194  // get the robot position
195  robot.getPosition(wMc);
196  // Compute the position of the object frame in the camera frame
197  cMo = wMc.inverse() * wMo;
198 
199  // new rotation to achieve
200  cdMc = cdMo * cMo.inverse();
201  tu.buildFrom(cdMc);
202 
203  // compute the control law
204  v = task.computeControlLaw();
205 
206  // send the camera velocity to the controller
208 
209  std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
210  }
211 
212  // Display task information
213  task.print();
214  return EXIT_SUCCESS;
215  } catch (const vpException &e) {
216  std::cout << "Catch a ViSP exception: " << e << std::endl;
217  return EXIT_FAILURE;
218  }
219 #else
220  (void)argc;
221  (void)argv;
222  std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
223  return EXIT_SUCCESS;
224 #endif
225 }
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
Implementation of an homogeneous matrix and operations on such kind of matrices.
Class that defines the simplest robot: a free flying camera.
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:490
error that can be emited by ViSP classes.
Definition: vpException.h:71
vpHomogeneousMatrix inverse() const
vpHomogeneousMatrix getPosition() const
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
vpColVector computeControlLaw()
Definition: vpServo.cpp:929
void setLambda(double c)
Definition: vpServo.h:404
void buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:567
static double rad(double deg)
Definition: vpMath.h:110
Implementation of column vector and the associated operations.
Definition: vpColVector.h:130
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:151
Class that defines a 3D visual feature from a axis/angle parametrization that represent the rotatio...
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:306
vpColVector getError() const
Definition: vpServo.h:278
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:218