Visual Servoing Platform  version 3.3.0 under development (2020-02-17)
servoSimuPoint2DCamVelocity3.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 2D visual servoing on a point.
33  *
34  * Authors:
35  * Eric Marchand
36  * Fabien Spindler
37  *
38  *****************************************************************************/
39 
54 #include <stdio.h>
55 #include <stdlib.h>
56 
57 #include <visp3/core/vpHomogeneousMatrix.h>
58 #include <visp3/core/vpMath.h>
59 #include <visp3/io/vpParseArgv.h>
60 #include <visp3/robot/vpSimulatorCamera.h>
61 #include <visp3/visual_features/vpFeatureBuilder.h>
62 #include <visp3/visual_features/vpFeaturePoint.h>
63 #include <visp3/vs/vpServo.h>
64 
65 // List of allowed command line options
66 #define GETOPTARGS "h"
67 
68 void usage(const char *name, const char *badparam);
69 bool getOptions(int argc, const char **argv);
70 
79 void usage(const char *name, const char *badparam)
80 {
81  fprintf(stdout, "\n\
82 Simulation of a 2D visual servoing on a point:\n\
83 - eye-in-hand control law,\n\
84 - articular velocity are computed,\n\
85 - without display,\n\
86 - only the X coordinate of the point is selected.\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(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
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 << " Eye-in-hand task control, articular velocity are computed" << std::endl;
156  std::cout << " Simulation " << std::endl;
157  std::cout << " task : servo a point " << std::endl;
158  std::cout << "-------------------------------------------------------" << std::endl;
159  std::cout << std::endl;
160 
161  // sets the initial camera location
163  cMo[0][3] = 0.1;
164  cMo[1][3] = 0.2;
165  cMo[2][3] = 2;
166  // Compute the position of the object in the world frame
167  vpHomogeneousMatrix wMc, wMo;
168  robot.getPosition(wMc);
169  wMo = wMc * cMo;
170 
171  // sets the point coordinates in the world frame
172  vpPoint point(0, 0, 0);
173 
174  // computes the point coordinates in the camera frame and its 2D
175  // coordinates
176  point.track(cMo);
177 
178  // sets the current position of the visual feature
179  vpFeaturePoint p;
180  vpFeatureBuilder::create(p, point); // retrieve x,y and Z of the vpPoint structure
181 
182  // sets the desired position of the visual feature
183  vpFeaturePoint pd;
184  pd.buildFrom(0, 0, 1); // buildFrom(x,y,Z) ;
185 
186  // define the task
187  // - we want an eye-in-hand control law
188  // - articular velocity are computed
190 
191  // Set the position of the end-effector frame in the camera frame as identity
193  vpVelocityTwistMatrix cVe(cMe);
194  task.set_cVe(cVe);
195 
196  // Set the Jacobian (expressed in the end-effector frame)
197  vpMatrix eJe;
198  robot.get_eJe(eJe);
199  task.set_eJe(eJe);
200 
201  // we want to see a point on a point
202  task.addFeature(p, pd, vpFeaturePoint::selectX());
203 
204  // set the gain
205  task.setLambda(1);
206 
207  // Display task information
208  task.print();
209 
210  unsigned int iter = 0;
211  // loop
212  while (iter++ < 100) {
213  std::cout << "---------------------------------------------" << iter << std::endl;
214  vpColVector v;
215 
216  // Set the Jacobian (expressed in the end-effector frame)
217  // since q is modified eJe is modified
218  robot.get_eJe(eJe);
219  task.set_eJe(eJe);
220 
221  // get the robot position
222  robot.getPosition(wMc);
223  // Compute the position of the object frame in the camera frame
224  cMo = wMc.inverse() * wMo;
225 
226  // new point position
227  point.track(cMo);
228  vpFeatureBuilder::create(p, point); // retrieve x,y and Z of the vpPoint structure
229 
230  // compute the control law
231  v = task.computeControlLaw();
232 
233  // send the camera velocity to the controller
235 
236  std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
237  }
238 
239  // Display task information
240  task.print();
241  task.kill();
242  return EXIT_SUCCESS;
243  } catch (const vpException &e) {
244  std::cout << "Catch a ViSP exception: " << e << std::endl;
245  return EXIT_FAILURE;
246  }
247 }
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:164
void buildFrom(double x, double y, double Z)
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
void set_eJe(const vpMatrix &eJe_)
Definition: vpServo.h:508
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
static unsigned int selectX()
void setLambda(double c)
Definition: vpServo.h:406
Implementation of column vector and the associated operations.
Definition: vpColVector.h:130
void set_cVe(const vpVelocityTwistMatrix &cVe_)
Definition: vpServo.h:450
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 get_eJe(vpMatrix &eJe)
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:223