Visual Servoing Platform  version 3.1.0
servoSimuFourPoints2DCamVelocityDisplay.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 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 using 4 points as visual feature.
33  *
34  * Authors:
35  * Eric Marchand
36  * Fabien Spindler
37  *
38  *****************************************************************************/
39 
56 #include <visp3/core/vpConfig.h>
57 
58 #if (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
59 
60 #include <stdio.h>
61 #include <stdlib.h>
62 
63 #include <visp3/core/vpCameraParameters.h>
64 #include <visp3/core/vpHomogeneousMatrix.h>
65 #include <visp3/core/vpImage.h>
66 #include <visp3/core/vpMath.h>
67 #include <visp3/gui/vpDisplayGDI.h>
68 #include <visp3/gui/vpDisplayGTK.h>
69 #include <visp3/gui/vpDisplayOpenCV.h>
70 #include <visp3/gui/vpDisplayX.h>
71 #include <visp3/gui/vpProjectionDisplay.h>
72 #include <visp3/io/vpParseArgv.h>
73 #include <visp3/robot/vpSimulatorCamera.h>
74 #include <visp3/visual_features/vpFeatureBuilder.h>
75 #include <visp3/visual_features/vpFeaturePoint.h>
76 #include <visp3/vs/vpServo.h>
77 #include <visp3/vs/vpServoDisplay.h>
78 
79 // List of allowed command line options
80 #define GETOPTARGS "cdh"
81 
82 void usage(const char *name, const char *badparam);
83 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
84 
93 void usage(const char *name, const char *badparam)
94 {
95  fprintf(stdout, "\n\
96 Tests a control law with the following characteristics:\n\
97 - eye-in-hand control\n\
98 - articular velocity are computed\n\
99 - servo on 4 points,\n\
100 - internal and external camera view displays.\n\
101  \n\
102 SYNOPSIS\n\
103  %s [-c] [-d] [-h]\n", name);
104 
105  fprintf(stdout, "\n\
106 OPTIONS: Default\n\
107  -c\n\
108  Disable the mouse click. Useful to automaze the \n\
109  execution of this program without humain intervention.\n\
110  \n\
111  -d \n\
112  Turn off the display.\n\
113  \n\
114  -h\n\
115  Print the help.\n");
116 
117  if (badparam)
118  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
119 }
132 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
133 {
134  const char *optarg_;
135  int c;
136  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
137 
138  switch (c) {
139  case 'c':
140  click_allowed = false;
141  break;
142  case 'd':
143  display = false;
144  break;
145  case 'h':
146  usage(argv[0], NULL);
147  return false;
148  break;
149 
150  default:
151  usage(argv[0], optarg_);
152  return false;
153  break;
154  }
155  }
156 
157  if ((c == 1) || (c == -1)) {
158  // standalone param or error
159  usage(argv[0], NULL);
160  std::cerr << "ERROR: " << std::endl;
161  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
162  return false;
163  }
164 
165  return true;
166 }
167 
168 int main(int argc, const char **argv)
169 {
170  try {
171  bool opt_click_allowed = true;
172  bool opt_display = true;
173 
174  // Read the command line options
175  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
176  exit(-1);
177  }
178 
179 // We open two displays, one for the internal camera view, the other one for
180 // the external view, using either X11, GTK or GDI.
181 #if defined VISP_HAVE_X11
182  vpDisplayX displayInt;
183  vpDisplayX displayExt;
184 #elif defined VISP_HAVE_GTK
185  vpDisplayGTK displayInt;
186  vpDisplayGTK displayExt;
187 #elif defined VISP_HAVE_GDI
188  vpDisplayGDI displayInt;
189  vpDisplayGDI displayExt;
190 #elif defined VISP_HAVE_OPENCV
191  vpDisplayOpenCV displayInt;
192  vpDisplayOpenCV displayExt;
193 #endif
194 
195  // open a display for the visualization
196 
197  vpImage<unsigned char> Iint(300, 300, 0);
198  vpImage<unsigned char> Iext(300, 300, 0);
199 
200  if (opt_display) {
201  displayInt.init(Iint, 0, 0, "Internal view");
202  displayExt.init(Iext, 330, 000, "External view");
203  }
204  vpProjectionDisplay externalview;
205 
206  double px, py;
207  px = py = 500;
208  double u0, v0;
209  u0 = 150, v0 = 160;
210 
211  vpCameraParameters cam(px, py, u0, v0);
212 
213  int i;
214  vpServo task;
215  vpSimulatorCamera robot;
216 
217  std::cout << std::endl;
218  std::cout << "----------------------------------------------" << std::endl;
219  std::cout << " Test program for vpServo " << std::endl;
220  std::cout << " Eye-in-hand task control, articular velocity are computed" << std::endl;
221  std::cout << " Simulation " << std::endl;
222  std::cout << " task : servo 4 points " << std::endl;
223  std::cout << "----------------------------------------------" << std::endl;
224  std::cout << std::endl;
225 
226  // sets the initial camera location
227  vpHomogeneousMatrix cMo(-0.1, -0.1, 1, vpMath::rad(40), vpMath::rad(10), vpMath::rad(60));
228 
229  // Compute the position of the object in the world frame
230  vpHomogeneousMatrix wMc, wMo;
231  robot.getPosition(wMc);
232  wMo = wMc * cMo;
233 
234  vpHomogeneousMatrix cextMo(0, 0, 2, 0, 0,
235  0); // vpMath::rad(40), vpMath::rad(10), vpMath::rad(60)) ;
236 
237  // sets the point coordinates in the object frame
238  vpPoint point[4];
239  point[0].setWorldCoordinates(-0.1, -0.1, 0);
240  point[1].setWorldCoordinates(0.1, -0.1, 0);
241  point[2].setWorldCoordinates(0.1, 0.1, 0);
242  point[3].setWorldCoordinates(-0.1, 0.1, 0);
243 
244  for (i = 0; i < 4; i++)
245  externalview.insert(point[i]);
246 
247  // computes the point coordinates in the camera frame and its 2D
248  // coordinates
249  for (i = 0; i < 4; i++)
250  point[i].track(cMo);
251 
252  // sets the desired position of the point
253  vpFeaturePoint p[4];
254  for (i = 0; i < 4; i++)
255  vpFeatureBuilder::create(p[i], point[i]); // retrieve x,y and Z of the vpPoint structure
256 
257  // sets the desired position of the feature point s*
258  vpFeaturePoint pd[4];
259 
260  pd[0].buildFrom(-0.1, -0.1, 1);
261  pd[1].buildFrom(0.1, -0.1, 1);
262  pd[2].buildFrom(0.1, 0.1, 1);
263  pd[3].buildFrom(-0.1, 0.1, 1);
264 
265  // define the task
266  // - we want an eye-in-hand control law
267  // - articular velocity are computed
270 
271  // Set the position of the camera in the end-effector frame ") ;
273  vpVelocityTwistMatrix cVe(cMe);
274  task.set_cVe(cVe);
275 
276  // Set the Jacobian (expressed in the end-effector frame)
277  vpMatrix eJe;
278  robot.get_eJe(eJe);
279  task.set_eJe(eJe);
280 
281  // we want to see a point on a point
282  for (i = 0; i < 4; i++)
283  task.addFeature(p[i], pd[i]);
284 
285  // set the gain
286  task.setLambda(1);
287 
288  // Display task information " ) ;
289  task.print();
290 
291  unsigned int iter = 0;
292  // loop
293  while (iter++ < 200) {
294  std::cout << "---------------------------------------------" << iter << std::endl;
295  vpColVector v;
296 
297  // Set the Jacobian (expressed in the end-effector frame)
298  // since q is modified eJe is modified
299  robot.get_eJe(eJe);
300  task.set_eJe(eJe);
301 
302  // get the robot position
303  robot.getPosition(wMc);
304  // Compute the position of the camera wrt the object frame
305  cMo = wMc.inverse() * wMo;
306 
307  // update new point position and corresponding features
308  for (i = 0; i < 4; i++) {
309  point[i].track(cMo);
310  // retrieve x,y and Z of the vpPoint structure
311  vpFeatureBuilder::create(p[i], point[i]);
312  }
313  // since vpServo::MEAN interaction matrix is used, we need also to
314  // update the desired features at each iteration
315  pd[0].buildFrom(-0.1, -0.1, 1);
316  pd[1].buildFrom(0.1, -0.1, 1);
317  pd[2].buildFrom(0.1, 0.1, 1);
318  pd[3].buildFrom(-0.1, 0.1, 1);
319 
320  if (opt_display) {
321  vpDisplay::display(Iint);
322  vpDisplay::display(Iext);
323  vpServoDisplay::display(task, cam, Iint);
324  externalview.display(Iext, cextMo, cMo, cam, vpColor::green);
325  vpDisplay::flush(Iint);
326  vpDisplay::flush(Iext);
327  }
328 
329  // compute the control law
330  v = task.computeControlLaw();
331 
332  // send the camera velocity to the controller
334 
335  std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
336  }
337 
338  // Display task information
339  task.print();
340  task.kill();
341 
342  std::cout << "Final robot position with respect to the object frame:\n";
343  cMo.print();
344 
345  if (opt_display && opt_click_allowed) {
346  // suppressed for automate test
347  std::cout << "\n\nClick in the internal view window to end..." << std::endl;
348  vpDisplay::getClick(Iint);
349  }
350  return 0;
351  } catch (vpException &e) {
352  std::cout << "Catch a ViSP exception: " << e << std::endl;
353  return 1;
354  }
355 }
356 #else
357 #include <iostream>
358 
359 int main() { std::cout << "You do not have X11, GTK, GDI or OpenCV display functionalities..." << std::endl; }
360 
361 #endif
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:104
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
void display(vpImage< unsigned char > &I, const vpHomogeneousMatrix &cextMo, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, const vpColor &color, const bool &displayTraj=false, const unsigned int thickness=1)
Implementation of an homogeneous matrix and operations on such kind of matrices.
Class that defines the simplest robot: a free flying camera.
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:129
void set_eJe(const vpMatrix &eJe_)
Definition: vpServo.h:508
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:151
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, const unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:497
error that can be emited by ViSP classes.
Definition: vpException.h:71
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
void track(const vpHomogeneousMatrix &cMo)
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 const vpColor green
Definition: vpColor.h:183
static void flush(const vpImage< unsigned char > &I)
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 void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Generic class defining intrinsic camera parameters.
void setLambda(double c)
Definition: vpServo.h:406
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:138
void insert(vpForwardProjection &fp)
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:574
static double rad(double deg)
Definition: vpMath.h:102
void buildFrom(const double x, const double y, const double Z)
void setWorldCoordinates(const double oX, const double oY, const double oZ)
Definition: vpPoint.cpp:113
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
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)
interface with the image for feature display
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:223
static void display(const vpServo &s, const vpCameraParameters &cam, const vpImage< unsigned char > &I, vpColor currentColor=vpColor::green, vpColor desiredColor=vpColor::red, unsigned int thickness=1)