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