ViSP  2.9.0
servoSimuThetaUCamVelocity.cpp
1 /****************************************************************************
2  *
3  * $Id: servoSimuThetaUCamVelocity.cpp 2457 2010-01-07 10:41:18Z nmelchio $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Simulation of a visual servoing using theta U visual features.
36  * tests the control law
37  * eye-in-hand control
38  * velocity computed in the camera frame
39  * using theta U visual feature
40  *
41  * Authors:
42  * Eric Marchand
43  * Fabien Spindler
44  *
45  *****************************************************************************/
46 
47 
56 #include <stdlib.h>
57 #include <stdio.h>
58 
59 #include <visp/vpFeatureThetaU.h>
60 #include <visp/vpFeatureTranslation.h>
61 #include <visp/vpHomogeneousMatrix.h>
62 #include <visp/vpMath.h>
63 #include <visp/vpParseArgv.h>
64 #include <visp/vpServo.h>
65 #include <visp/vpSimulatorCamera.h>
66 
67 // List of allowed command line options
68 #define GETOPTARGS "h"
69 void usage(const char *name, const char *badparam);
70 bool getOptions(int argc, const char **argv);
79 void usage(const char *name, const char *badparam)
80 {
81  fprintf(stdout, "\n\
82 Simulation of avisual servoing using theta U visual feature:\n\
83 - eye-in-hand control law,\n\
84 - velocity computed in the camera frame,\n\
85 - without display.\n\
86  \n\
87 SYNOPSIS\n\
88  %s [-h]\n", name);
89 
90  fprintf(stdout, "\n\
91 OPTIONS: Default\n\
92  \n\
93  -h\n\
94  Print the help.\n");
95 
96  if (badparam)
97  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
98 }
99 
110 bool getOptions(int argc, const char **argv)
111 {
112  const char *optarg_;
113  int c;
114  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
115 
116  switch (c) {
117  case 'h': usage(argv[0], NULL); return false; break;
118 
119  default:
120  usage(argv[0], optarg_);
121  return false; break;
122  }
123  }
124 
125  if ((c == 1) || (c == -1)) {
126  // standalone param or error
127  usage(argv[0], NULL);
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
137 main(int argc, const char ** argv)
138 {
139  try {
140  // Read the command line options
141  if (getOptions(argc, argv) == false) {
142  exit (-1);
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 << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl ;
152  std::cout << " Simulation " << std::endl ;
153  std::cout << " task : servo using theta U visual feature " << std::endl ;
154  std::cout << "-------------------------------------------------------" << std::endl ;
155  std::cout << std::endl ;
156 
157 
158  // sets the initial camera location
159  vpPoseVector c_r_o(0.1,0.2,2,
160  vpMath::rad(20), vpMath::rad(10), vpMath::rad(50)
161  ) ;
162 
163  vpHomogeneousMatrix cMo(c_r_o) ;
164  // Compute the position of the object in the world frame
165  vpHomogeneousMatrix wMc, wMo;
166  robot.getPosition(wMc) ;
167  wMo = wMc * cMo;
168 
169  // sets the desired camera location
170  vpPoseVector cd_r_o(0,0,1,
172  vpHomogeneousMatrix cdMo(cd_r_o) ;
173 
174 
175  // compute the rotation that the camera has to realize
176  vpHomogeneousMatrix cdMc ;
177  cdMc = cdMo*cMo.inverse() ;
179  tu.buildFrom(cdMc) ;
180 
181  // define the task
182  // - we want an eye-in-hand control law
183  // - robot is controlled in the camera frame
186 
187  task.addFeature(tu) ;
188 
189  // - set the gain
190  task.setLambda(1) ;
191 
192  // Display task information
193  task.print() ;
194 
195  unsigned int iter=0 ;
196  // loop
197  while(iter++ < 200)
198  {
199  std::cout << "---------------------------------------------" << iter <<std::endl ;
200  vpColVector v ;
201 
202  // get the robot position
203  robot.getPosition(wMc) ;
204  // Compute the position of the camera wrt the object frame
205  cMo = wMc.inverse() * wMo;
206 
207  // new rotation to achieve
208  cdMc = cdMo*cMo.inverse() ;
209  tu.buildFrom(cdMc) ;
210 
211  // compute the control law
212  v = task.computeControlLaw() ;
213 
214  // send the camera velocity to the controller
216 
217  std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() <<std::endl ; ;
218  }
219 
220  // Display task information
221  task.print() ;
222  task.kill();
223  return 0;
224  }
225  catch(vpException e) {
226  std::cout << "Catch a ViSP exception: " << e << std::endl;
227  return 1;
228  }
229 }
230 
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
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:449
error that can be emited by ViSP classes.
Definition: vpException.h:76
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:79
void kill()
Definition: vpServo.cpp:189
vpColVector getError() const
Definition: vpServo.h:257
vpColVector computeControlLaw()
Definition: vpServo.cpp:902
void getPosition(vpHomogeneousMatrix &wMc) const
void setLambda(double c)
Definition: vpServo.h:370
void buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
Construction from translation vector and rotation matrix.
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:522
static double rad(double deg)
Definition: vpMath.h:100
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
The pose is a complete representation of every rigid motion in the euclidian space.
Definition: vpPoseVector.h:92
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:251
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:220