Visual Servoing Platform  version 3.3.0 under development (2020-02-17)
servoSimuPoint2DhalfCamVelocity1.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Simulation of a 2 1/2 D visual servoing.
33  *
34  * Authors:
35  * Eric Marchand
36  * Fabien Spindler
37  *
38  *****************************************************************************/
39 
50 #include <stdio.h>
51 #include <stdlib.h>
52 
53 #include <visp3/core/vpHomogeneousMatrix.h>
54 #include <visp3/core/vpMath.h>
55 #include <visp3/core/vpPoint.h>
56 #include <visp3/io/vpParseArgv.h>
57 #include <visp3/robot/vpSimulatorCamera.h>
58 #include <visp3/visual_features/vpFeatureBuilder.h>
59 #include <visp3/visual_features/vpFeaturePoint.h>
60 #include <visp3/visual_features/vpFeatureThetaU.h>
61 #include <visp3/visual_features/vpGenericFeature.h>
62 #include <visp3/vs/vpServo.h>
63 
64 // List of allowed command line options
65 #define GETOPTARGS "h"
66 
67 void usage(const char *name, const char *badparam);
68 bool getOptions(int argc, const char **argv);
69 
78 void usage(const char *name, const char *badparam)
79 {
80  fprintf(stdout, "\n\
81 Simulation of a 2 1/2 D visual servoing (x,y,Z,theta U):\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(stderr, "ERROR: \n");
97  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
98  }
99 }
100 
111 bool getOptions(int argc, const char **argv)
112 {
113  const char *optarg_;
114  int c;
115  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
116 
117  switch (c) {
118  case 'h':
119  usage(argv[0], NULL);
120  return false;
121  break;
122 
123  default:
124  usage(argv[0], optarg_);
125  return false;
126  break;
127  }
128  }
129 
130  if ((c == 1) || (c == -1)) {
131  // standalone param or error
132  usage(argv[0], NULL);
133  std::cerr << "ERROR: " << std::endl;
134  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
135  return false;
136  }
137 
138  return true;
139 }
140 
141 int main(int argc, const char **argv)
142 {
143  try {
144  // Read the command line options
145  if (getOptions(argc, argv) == false) {
146  exit(-1);
147  }
148 
149  vpServo task;
150  vpSimulatorCamera robot;
151 
152  std::cout << std::endl;
153  std::cout << "-------------------------------------------------------" << std::endl;
154  std::cout << " Test program for vpServo " << std::endl;
155  std::cout << " task : 2 1/2 D visual servoing " << std::endl;
156  std::cout << "-------------------------------------------------------" << std::endl;
157  std::cout << std::endl;
158 
159  // sets the initial camera location
160  vpPoseVector c_r_o(0.1, 0.2, 2, vpMath::rad(20), vpMath::rad(10), vpMath::rad(50));
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, vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
170  vpHomogeneousMatrix cdMo(cd_r_o);
171 
172  // sets the point coordinates in the world frame
173  vpPoint point(0, 0, 0);
174  // computes the point coordinates in the camera frame and its 2D
175  // coordinates
176  point.track(cMo);
177 
178  vpPoint pointd(0, 0, 0);
179  pointd.track(cdMo);
180  //------------------------------------------------------------------
181  // 1st feature (x,y)
182  // want to it at (0,0)
183  vpFeaturePoint p;
184  vpFeatureBuilder::create(p, point);
185 
186  vpFeaturePoint pd;
187  vpFeatureBuilder::create(pd, pointd);
188 
189  //------------------------------------------------------------------
190  // 2nd feature (Z)
191  // not necessary to project twice (reuse p)
193  vpFeatureBuilder::create(Z, point); // retrieve x,y and Z of the vpPoint structure
194 
195  // want to see it one meter away (here again use pd)
196  vpFeaturePoint3D Zd;
197  vpFeatureBuilder::create(Zd, pointd); // retrieve x,y and Z of the vpPoint structure
198 
199  //------------------------------------------------------------------
200  // 3rd feature ThetaU
201  // compute the rotation that the camera has to achieve
202  vpHomogeneousMatrix cdMc;
203  cdMc = cdMo * cMo.inverse();
204 
206  tu.buildFrom(cdMc);
207 
208  // sets the desired rotation (always zero !)
209  // since s is the rotation that the camera has to achieve
210 
211  //------------------------------------------------------------------
212  // define the task
213  // - we want an eye-in-hand control law
214  // - robot is controlled in the camera frame
216 
217  task.addFeature(p, pd);
218  task.addFeature(Z, Zd, vpFeaturePoint3D::selectZ());
219  task.addFeature(tu);
220 
221  // set the gain
222  task.setLambda(1);
223 
224  // Display task information
225  task.print();
226 
227  unsigned int iter = 0;
228  // loop
229  while (iter++ < 200) {
230  std::cout << "---------------------------------------------" << iter << std::endl;
231  vpColVector v;
232 
233  // get the robot position
234  robot.getPosition(wMc);
235  // Compute the position of the object frame in the camera frame
236  cMo = wMc.inverse() * wMo;
237 
238  // update the feature
239  point.track(cMo);
240  vpFeatureBuilder::create(p, point);
241  vpFeatureBuilder::create(Z, point);
242 
243  cdMc = cdMo * cMo.inverse();
244  tu.buildFrom(cdMc);
245 
246  // compute the control law
247  v = task.computeControlLaw();
248  // send the camera velocity to the controller ") ;
250 
251  std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
252  }
253 
254  // Display task information
255  task.print();
256  task.kill();
257  std::cout << "Final camera location:\n " << cMo << std::endl;
258  return EXIT_SUCCESS;
259  } catch (const vpException &e) {
260  std::cout << "Catch a ViSP exception: " << e << std::endl;
261  return EXIT_SUCCESS;
262  }
263 }
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, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:497
error that can be emited by ViSP classes.
Definition: vpException.h:71
vpHomogeneousMatrix inverse() const
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
vpHomogeneousMatrix getPosition() const
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
Class that defines what is a point.
Definition: vpPoint.h:58
void kill()
Definition: vpServo.cpp:192
vpColVector computeControlLaw()
Definition: vpServo.cpp:935
Class that defines the 3D point visual feature.
void setLambda(double c)
Definition: vpServo.h:406
static unsigned int selectZ()
void buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
static double rad(double deg)
Definition: vpMath.h:108
Implementation of column vector and the associated operations.
Definition: vpColVector.h:130
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:151
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:313
vpColVector getError() const
Definition: vpServo.h:282
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:223