ViSP  2.8.0
servoSimuPoint2DhalfCamVelocity1.cpp
1 /****************************************************************************
2  *
3  * $Id: servoSimuPoint2DhalfCamVelocity1.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 2 1/2 D visual servoing.
36  *
37  * Authors:
38  * Eric Marchand
39  * Fabien Spindler
40  *
41  *****************************************************************************/
42 
43 
55 #include <stdlib.h>
56 #include <stdio.h>
57 
58 #include <visp/vpFeatureBuilder.h>
59 #include <visp/vpFeaturePoint.h>
60 #include <visp/vpFeatureThetaU.h>
61 #include <visp/vpGenericFeature.h>
62 #include <visp/vpHomogeneousMatrix.h>
63 #include <visp/vpMath.h>
64 #include <visp/vpParseArgv.h>
65 #include <visp/vpPoint.h>
66 #include <visp/vpServo.h>
67 #include <visp/vpSimulatorCamera.h>
68 
69 // List of allowed command line options
70 #define GETOPTARGS "h"
71 
80 void usage(const char *name, const char *badparam)
81 {
82  fprintf(stdout, "\n\
83 Simulation of a 2 1/2 D visual servoing (x,y,Z,theta U):\n\
84 - eye-in-hand control law,\n\
85 - velocity computed in the camera frame,\n\
86 - without display.\n\
87  \n\
88 SYNOPSIS\n\
89  %s [-h]\n", name);
90 
91  fprintf(stdout, "\n\
92 OPTIONS: Default\n\
93  \n\
94  -h\n\
95  Print the help.\n");
96 
97  if (badparam) {
98  fprintf(stderr, "ERROR: \n" );
99  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
100  }
101 }
102 
113 bool getOptions(int argc, const char **argv)
114 {
115  const char *optarg;
116  int c;
117  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
118 
119  switch (c) {
120  case 'h': usage(argv[0], NULL); return false; break;
121 
122  default:
123  usage(argv[0], optarg);
124  return false; break;
125  }
126  }
127 
128  if ((c == 1) || (c == -1)) {
129  // standalone param or error
130  usage(argv[0], NULL);
131  std::cerr << "ERROR: " << std::endl;
132  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
133  return false;
134  }
135 
136  return true;
137 }
138 
139 int
140 main(int argc, const char ** argv)
141 {
142  // Read the command line options
143  if (getOptions(argc, argv) == false) {
144  exit (-1);
145  }
146 
147  vpServo task ;
148  vpSimulatorCamera robot ;
149 
150  std::cout << std::endl ;
151  std::cout << "-------------------------------------------------------" << std::endl ;
152  std::cout << " Test program for vpServo " <<std::endl ;
153  std::cout << " task : 2 1/2 D visual servoing " << std::endl ;
154  std::cout << "-------------------------------------------------------" << std::endl ;
155  std::cout << std::endl ;
156 
157  // sets the initial camera location
158  vpPoseVector c_r_o(0.1,0.2,2,
159  vpMath::rad(20), vpMath::rad(10), vpMath::rad(50)
160  ) ;
161 
162  vpHomogeneousMatrix cMo(c_r_o) ;
163  // Compute the position of the object in the world frame
164  vpHomogeneousMatrix wMc, wMo;
165  robot.getPosition(wMc) ;
166  wMo = wMc * cMo;
167 
168  // sets the desired camera location
169  vpPoseVector cd_r_o(0,0,1,
171  vpHomogeneousMatrix cdMo(cd_r_o) ;
172 
173  // sets the point coordinates in the world frame
174  vpPoint point ;
175  point.setWorldCoordinates(0,0,0) ;
176  // computes the point coordinates in the camera frame and its 2D coordinates
177  point.track(cMo) ;
178 
179  vpPoint pointd ;
180  pointd.setWorldCoordinates(0,0,0) ;
181  pointd.track(cdMo) ;
182  //------------------------------------------------------------------
183  // 1st feature (x,y)
184  // want to it at (0,0)
185  vpFeaturePoint p ;
186  vpFeatureBuilder::create(p,point) ;
187 
188  vpFeaturePoint pd ;
189  vpFeatureBuilder::create(pd,pointd) ;
190 
191  //------------------------------------------------------------------
192  // 2nd feature (Z)
193  // not necessary to project twice (reuse p)
194  vpFeaturePoint3D Z ;
195  vpFeatureBuilder::create(Z,point) ; //retrieve x,y and Z of the vpPoint structure
196 
197  // want to see it one meter away (here again use pd)
198  vpFeaturePoint3D Zd ;
199  vpFeatureBuilder::create(Zd,pointd) ; //retrieve x,y and Z of the vpPoint structure
200 
201  //------------------------------------------------------------------
202  // 3rd feature ThetaU
203  // compute the rotation that the camera has to achieve
204  vpHomogeneousMatrix cdMc ;
205  cdMc = cdMo*cMo.inverse() ;
206 
208  tu.buildFrom(cdMc) ;
209 
210  // sets the desired rotation (always zero !)
211  // since s is the rotation that the camera has to achieve
212 
213  //------------------------------------------------------------------
214  // define the task
215  // - we want an eye-in-hand control law
216  // - robot is controlled in the camera frame
218 
219  task.addFeature(p,pd) ;
221  task.addFeature(tu) ;
222 
223  // set the gain
224  task.setLambda(1) ;
225 
226  // Display task information
227  task.print() ;
228 
229  unsigned int iter=0 ;
230  // loop
231  while(iter++<200)
232  {
233  std::cout << "---------------------------------------------" << iter <<std::endl ;
234  vpColVector v ;
235 
236  // get the robot position
237  robot.getPosition(wMc) ;
238  // Compute the position of the camera wrt the object frame
239  cMo = wMc.inverse() * wMo;
240 
241  // update the feature
242  point.track(cMo) ;
243  vpFeatureBuilder::create(p,point) ;
244  vpFeatureBuilder::create(Z,point) ;
245 
246  cdMc = cdMo*cMo.inverse() ;
247  tu.buildFrom(cdMc) ;
248 
249  // compute the control law
250  v = task.computeControlLaw() ;
251  // send the camera velocity to the controller ") ;
253 
254  std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() <<std::endl ;
255  }
256 
257  // Display task information
258  task.print() ;
259  task.kill();
260  std::cout << "Final camera location:\n " << cMo << std::endl ;
261 }
262 
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
void track(const vpHomogeneousMatrix &cMo)
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:79
Class that defines what is a point.
Definition: vpPoint.h:65
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
Class that defines the 3D point visual feature.
void getPosition(vpHomogeneousMatrix &wMc) const
static unsigned int selectZ()
void buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
Construction from translation vector and rotation matrix.
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
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
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
void setWorldCoordinates(const double ox, const double oy, const double oz)
Set the point world coordinates. We mean here the coordinates of the point in the object frame...
Definition: vpPoint.cpp:74