Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
servoSimuThetaUCamVelocity.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Simulation of a visual servoing using theta U visual features.
32  * tests the control law
33  * eye-in-hand control
34  * velocity computed in the camera frame
35  * using theta U visual feature
36  *
37  * Authors:
38  * Eric Marchand
39  * Fabien Spindler
40  *
41  *****************************************************************************/
42 
43 
52 #include <stdlib.h>
53 #include <stdio.h>
54 
55 #include <visp3/visual_features/vpFeatureThetaU.h>
56 #include <visp3/visual_features/vpFeatureTranslation.h>
57 #include <visp3/core/vpHomogeneousMatrix.h>
58 #include <visp3/core/vpMath.h>
59 #include <visp3/io/vpParseArgv.h>
60 #include <visp3/vs/vpServo.h>
61 #include <visp3/robot/vpSimulatorCamera.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': usage(argv[0], NULL); return false; break;
114 
115  default:
116  usage(argv[0], optarg_);
117  return false; break;
118  }
119  }
120 
121  if ((c == 1) || (c == -1)) {
122  // standalone param or error
123  usage(argv[0], NULL);
124  std::cerr << "ERROR: " << std::endl;
125  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
126  return false;
127  }
128 
129  return true;
130 }
131 
132 int
133 main(int argc, const char ** argv)
134 {
135  try {
136  // Read the command line options
137  if (getOptions(argc, argv) == false) {
138  exit (-1);
139  }
140 
141  vpServo task ;
142  vpSimulatorCamera robot ;
143 
144  std::cout << std::endl ;
145  std::cout << "-------------------------------------------------------" << std::endl ;
146  std::cout << " Test program for vpServo " <<std::endl ;
147  std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl ;
148  std::cout << " Simulation " << std::endl ;
149  std::cout << " task : servo using theta U visual feature " << std::endl ;
150  std::cout << "-------------------------------------------------------" << std::endl ;
151  std::cout << std::endl ;
152 
153 
154  // sets the initial camera location
155  vpPoseVector c_r_o(0.1,0.2,2,
156  vpMath::rad(20), vpMath::rad(10), vpMath::rad(50)
157  ) ;
158 
159  vpHomogeneousMatrix cMo(c_r_o) ;
160  // Compute the position of the object in the world frame
161  vpHomogeneousMatrix wMc, wMo;
162  robot.getPosition(wMc) ;
163  wMo = wMc * cMo;
164 
165  // sets the desired camera location
166  vpPoseVector cd_r_o(0,0,1,
168  vpHomogeneousMatrix cdMo(cd_r_o) ;
169 
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  {
195  std::cout << "---------------------------------------------" << iter <<std::endl ;
196  vpColVector v ;
197 
198  // get the robot position
199  robot.getPosition(wMc) ;
200  // Compute the position of the camera wrt the object frame
201  cMo = wMc.inverse() * wMo;
202 
203  // new rotation to achieve
204  cdMc = cdMo*cMo.inverse() ;
205  tu.buildFrom(cdMc) ;
206 
207  // compute the control law
208  v = task.computeControlLaw() ;
209 
210  // send the camera velocity to the controller
212 
213  std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() <<std::endl ; ;
214  }
215 
216  // Display task information
217  task.print() ;
218  task.kill();
219  return 0;
220  }
221  catch(vpException &e) {
222  std::cout << "Catch a ViSP exception: " << e << std::endl;
223  return 1;
224  }
225 }
226 
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, const unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:512
error that can be emited by ViSP classes.
Definition: vpException.h:73
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:76
void kill()
Definition: vpServo.cpp:191
vpColVector getError() const
Definition: vpServo.h:271
vpColVector computeControlLaw()
Definition: vpServo.cpp:954
void setLambda(double c)
Definition: vpServo.h:391
vpHomogeneousMatrix getPosition() const
void buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:585
static double rad(double deg)
Definition: vpMath.h:104
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:93
vpHomogeneousMatrix inverse() const
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:314
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:222