ViSP  2.8.0
servoSimuPoint2DCamVelocity1.cpp
1 /****************************************************************************
2  *
3  * $Id: servoSimuPoint2DCamVelocity1.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 2D visual servoing on a point.
36  *
37  * Authors:
38  * Eric Marchand
39  * Fabien Spindler
40  *
41  *****************************************************************************/
42 
51 #include <stdlib.h>
52 #include <stdio.h>
53 
54 #include <visp/vpFeatureBuilder.h>
55 #include <visp/vpFeaturePoint.h>
56 #include <visp/vpHomogeneousMatrix.h>
57 #include <visp/vpMath.h>
58 #include <visp/vpParseArgv.h>
59 #include <visp/vpServo.h>
60 #include <visp/vpSimulatorCamera.h>
61 
62 // List of allowed command line options
63 #define GETOPTARGS "h"
64 
73 void usage(const char *name, const char *badparam)
74 {
75  fprintf(stdout, "\n\
76 Simulation of a 2D visual servoing on a point:\n\
77 - eye-in-hand control law,\n\
78 - velocity computed in the camera frame,\n\
79 - without display.\n\
80  \n\
81 SYNOPSIS\n\
82  %s [-h]\n", name);
83 
84  fprintf(stdout, "\n\
85 OPTIONS: Default\n\
86  \n\
87  -h\n\
88  Print the help.\n");
89 
90  if (badparam)
91  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
92 }
93 
104 bool getOptions(int argc, const char **argv)
105 {
106  const char *optarg;
107  int c;
108  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
109 
110  switch (c) {
111  case 'h': usage(argv[0], NULL); return false; break;
112 
113  default:
114  usage(argv[0], optarg);
115  return false; break;
116  }
117  }
118 
119  if ((c == 1) || (c == -1)) {
120  // standalone param or error
121  usage(argv[0], NULL);
122  std::cerr << "ERROR: " << std::endl;
123  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
124  return false;
125  }
126 
127  return true;
128 }
129 
130 int
131 main(int argc, const char ** argv)
132 {
133  // Read the command line options
134  if (getOptions(argc, argv) == false) {
135  exit (-1);
136  }
137 
138  vpServo task ;
139  vpSimulatorCamera robot ;
140 
141  // sets the initial camera location
142  vpHomogeneousMatrix cMo ;
143  cMo[0][3] = 0.1 ;
144  cMo[1][3] = 0.2 ;
145  cMo[2][3] = 2 ;
146 
147  // Compute the position of the object in the world frame
148  vpHomogeneousMatrix wMc, wMo;
149  robot.getPosition(wMc) ;
150  wMo = wMc * cMo;
151 
152  // sets the point coordinates in the world frame
153  vpPoint point ;
154  point.setWorldCoordinates(0,0,0) ;
155 
156  // computes the point coordinates in the camera frame and its 2D coordinates
157  point.track(cMo) ;
158 
159  // sets the current position of the visual feature
160  vpFeaturePoint p ;
161  vpFeatureBuilder::create(p,point) ; //retrieve x,y and Z of the vpPoint structure
162 
163  // sets the desired position of the visual feature
164  vpFeaturePoint pd ;
165  pd.buildFrom(0,0,1) ; // buildFrom(x,y,Z) ;
166 
167  // define the task
168  // - we want an eye-in-hand control law
169  // - robot is controlled in the camera frame
171 
172  // we want to see a point on a point
173  std::cout << std::endl ;
174  task.addFeature(p,pd) ;
175 
176  // set the gain
177  task.setLambda(1) ;
178 
179  // Display task information
180  task.print() ;
181 
182  unsigned int iter=0 ;
183  // loop
184  while(iter++<100)
185  {
186  std::cout << "---------------------------------------------" << iter <<std::endl ;
187  vpColVector v ;
188 
189  // get the robot position
190  robot.getPosition(wMc) ;
191  // Compute the position of the camera wrt the object frame
192  cMo = wMc.inverse() * wMo;
193 
194  // new point position
195  point.track(cMo) ;
196  //retrieve x,y and Z of the vpPoint structure
197  vpFeatureBuilder::create(p,point);
198 
199  // compute the control law
200  v = task.computeControlLaw() ;
201 
202  // send the camera velocity to the controller
204 
205  std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() <<std::endl ;
206  }
207 
208  // Display task information
209  task.print() ;
210  task.kill();
211 }
212 
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
void getPosition(vpHomogeneousMatrix &wMc) const
void buildFrom(const double x, const double y, const double Z)
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
vpHomogeneousMatrix inverse() const
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