ViSP  2.8.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 - 2013 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 
78 void usage(const char *name, const char *badparam)
79 {
80  fprintf(stdout, "\n\
81 Simulation of avisual servoing using theta U visual feature:\n\
82 - eye-in-hand control law,\n\
83 - velocity computed in the camera frame,\n\
84 - without display.\n\
85  \n\
86 SYNOPSIS\n\
87  %s [-h]\n", 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': usage(argv[0], NULL); return false; break;
117 
118  default:
119  usage(argv[0], optarg);
120  return false; break;
121  }
122  }
123 
124  if ((c == 1) || (c == -1)) {
125  // standalone param or error
126  usage(argv[0], NULL);
127  std::cerr << "ERROR: " << std::endl;
128  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
129  return false;
130  }
131 
132  return true;
133 }
134 
135 int
136 main(int argc, const char ** argv)
137 {
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 
156  // sets the initial camera location
157  vpPoseVector c_r_o(0.1,0.2,2,
158  vpMath::rad(20), vpMath::rad(10), vpMath::rad(50)
159  ) ;
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,
170  vpHomogeneousMatrix cdMo(cd_r_o) ;
171 
172 
173  // compute the rotation that the camera has to realize
174  vpHomogeneousMatrix cdMc ;
175  cdMc = cdMo*cMo.inverse() ;
177  tu.buildFrom(cdMc) ;
178 
179  // define the task
180  // - we want an eye-in-hand control law
181  // - robot is controlled in the camera frame
184 
185  task.addFeature(tu) ;
186 
187  // - set the gain
188  task.setLambda(1) ;
189 
190  // Display task information
191  task.print() ;
192 
193  unsigned int iter=0 ;
194  // loop
195  while(iter++ < 200)
196  {
197  std::cout << "---------------------------------------------" << iter <<std::endl ;
198  vpColVector v ;
199 
200  // get the robot position
201  robot.getPosition(wMc) ;
202  // Compute the position of the camera wrt the object frame
203  cMo = wMc.inverse() * wMo;
204 
205  // new rotation to achieve
206  cdMc = cdMo*cMo.inverse() ;
207  tu.buildFrom(cdMc) ;
208 
209  // compute the control law
210  v = task.computeControlLaw() ;
211 
212  // send the camera velocity to the controller
214 
215  std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() <<std::endl ; ;
216  }
217 
218  // Display task information
219  task.print() ;
220  task.kill();
221 }
222 
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)
create a new ste of two visual features
Definition: vpServo.cpp:444
void setLambda(double _lambda)
set the gain lambda
Definition: vpServo.h:253
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:79
void kill()
destruction (memory deallocation if required)
Definition: vpServo.cpp:177
vpColVector getError() const
Definition: vpServo.h:301
vpColVector computeControlLaw()
compute the desired control law
Definition: vpServo.cpp:883
void getPosition(vpHomogeneousMatrix &wMc) const
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)
Set the type of the interaction matrix (current, mean, desired, user).
Definition: vpServo.cpp:509
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:258
Class required to compute the visual servoing control law descbribed in and .
Definition: vpServo.h:153
void setServo(vpServoType _servo_type)
Choice of the visual servoing control law.
Definition: vpServo.cpp:214