Visual Servoing Platform  version 3.5.1 under development (2022-12-02)
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  (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
62 
63 #include <stdio.h>
64 #include <stdlib.h>
65 
66 #include <visp3/core/vpCameraParameters.h>
67 #include <visp3/core/vpHomogeneousMatrix.h>
68 #include <visp3/core/vpImage.h>
69 #include <visp3/core/vpMath.h>
70 #include <visp3/gui/vpDisplayGDI.h>
71 #include <visp3/gui/vpDisplayGTK.h>
72 #include <visp3/gui/vpDisplayOpenCV.h>
73 #include <visp3/gui/vpDisplayX.h>
74 #include <visp3/gui/vpProjectionDisplay.h>
75 #include <visp3/io/vpParseArgv.h>
76 #include <visp3/robot/vpSimulatorCamera.h>
77 #include <visp3/visual_features/vpFeatureBuilder.h>
78 #include <visp3/visual_features/vpFeaturePoint.h>
79 #include <visp3/vs/vpServo.h>
80 #include <visp3/vs/vpServoDisplay.h>
81 
82 // List of allowed command line options
83 #define GETOPTARGS "cdh"
84 
85 void usage(const char *name, const char *badparam);
86 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
87 
96 void usage(const char *name, const char *badparam)
97 {
98  fprintf(stdout, "\n\
99 Tests a control law with the following characteristics:\n\
100 - eye-in-hand control\n\
101 - articular velocity are computed\n\
102 - servo on 4 points,\n\
103 - internal and external camera view displays.\n\
104  \n\
105 SYNOPSIS\n\
106  %s [-c] [-d] [-h]\n",
107  name);
108 
109  fprintf(stdout, "\n\
110 OPTIONS: Default\n\
111  -c\n\
112  Disable the mouse click. Useful to automaze the \n\
113  execution of this program without humain intervention.\n\
114  \n\
115  -d \n\
116  Turn off the display.\n\
117  \n\
118  -h\n\
119  Print the help.\n");
120 
121  if (badparam)
122  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
123 }
136 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
137 {
138  const char *optarg_;
139  int c;
140  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
141 
142  switch (c) {
143  case 'c':
144  click_allowed = false;
145  break;
146  case 'd':
147  display = false;
148  break;
149  case 'h':
150  usage(argv[0], NULL);
151  return false;
152 
153  default:
154  usage(argv[0], optarg_);
155  return false;
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 = 500, py = 500;
209  double u0 = 150, v0 = 160;
210 
211  vpCameraParameters cam(px, py, u0, v0);
212 
213  vpServo task;
214  vpSimulatorCamera robot;
215 
216  std::cout << std::endl;
217  std::cout << "----------------------------------------------" << std::endl;
218  std::cout << " Test program for vpServo " << std::endl;
219  std::cout << " Eye-in-hand task control, articular velocity are computed" << std::endl;
220  std::cout << " Simulation " << std::endl;
221  std::cout << " task : servo 4 points " << std::endl;
222  std::cout << "----------------------------------------------" << std::endl;
223  std::cout << std::endl;
224 
225  // sets the initial camera location
226  vpHomogeneousMatrix cMo(-0.1, -0.1, 1, vpMath::rad(40), vpMath::rad(10), vpMath::rad(60));
227 
228  // Compute the position of the object in the world frame
229  vpHomogeneousMatrix wMc, wMo;
230  robot.getPosition(wMc);
231  wMo = wMc * cMo;
232 
233  vpHomogeneousMatrix cextMo(0, 0, 2, 0, 0, 0); // vpMath::rad(40), vpMath::rad(10), vpMath::rad(60));
234 
235  // sets the point coordinates in the object frame
236  vpPoint point[4];
237  point[0].setWorldCoordinates(-0.1, -0.1, 0);
238  point[1].setWorldCoordinates(0.1, -0.1, 0);
239  point[2].setWorldCoordinates(0.1, 0.1, 0);
240  point[3].setWorldCoordinates(-0.1, 0.1, 0);
241 
242  for (unsigned i = 0; i < 4; i++)
243  externalview.insert(point[i]);
244 
245  // computes the point coordinates in the camera frame and its 2D
246  // coordinates
247  for (unsigned i = 0; i < 4; i++)
248  point[i].track(cMo);
249 
250  // sets the desired position of the point
251  vpFeaturePoint p[4];
252  for (unsigned i = 0; i < 4; i++)
253  vpFeatureBuilder::create(p[i], point[i]); // retrieve x,y and Z of the vpPoint structure
254 
255  // sets the desired position of the feature point s*
256  vpFeaturePoint pd[4];
257 
258  pd[0].buildFrom(-0.1, -0.1, 1);
259  pd[1].buildFrom(0.1, -0.1, 1);
260  pd[2].buildFrom(0.1, 0.1, 1);
261  pd[3].buildFrom(-0.1, 0.1, 1);
262 
263  // define the task
264  // - we want an eye-in-hand control law
265  // - articular velocity are computed
268 
269  // Set the position of the end-effector frame in the camera frame as identity
271  vpVelocityTwistMatrix cVe(cMe);
272  task.set_cVe(cVe);
273 
274  // Set the Jacobian (expressed in the end-effector frame
275  vpMatrix eJe;
276  robot.get_eJe(eJe);
277  task.set_eJe(eJe);
278 
279  // we want to see a point on a point
280  for (unsigned i = 0; i < 4; i++)
281  task.addFeature(p[i], pd[i]);
282 
283  // set the gain
284  task.setLambda(1);
285 
286  // Display task information
287  task.print();
288 
289  unsigned int iter = 0;
290  // loop
291  while (iter++ < 200) {
292  std::cout << "---------------------------------------------" << iter << std::endl;
293  vpColVector v;
294 
295  // Set the Jacobian (expressed in the end-effector frame)
296  // since q is modified eJe is modified
297  robot.get_eJe(eJe);
298  task.set_eJe(eJe);
299 
300  // get the robot position
301  robot.getPosition(wMc);
302  // Compute the position of the object frame in the camera frame
303  cMo = wMc.inverse() * wMo;
304 
305  // update new point position and corresponding features
306  for (unsigned i = 0; i < 4; i++) {
307  point[i].track(cMo);
308  // retrieve x,y and Z of the vpPoint structure
309  vpFeatureBuilder::create(p[i], point[i]);
310  }
311  // since vpServo::MEAN interaction matrix is used, we need also to
312  // update the desired features at each iteration
313  pd[0].buildFrom(-0.1, -0.1, 1);
314  pd[1].buildFrom(0.1, -0.1, 1);
315  pd[2].buildFrom(0.1, 0.1, 1);
316  pd[3].buildFrom(-0.1, 0.1, 1);
317 
318  if (opt_display) {
319  vpDisplay::display(Iint);
320  vpDisplay::display(Iext);
321  vpServoDisplay::display(task, cam, Iint);
322  externalview.display(Iext, cextMo, cMo, cam, vpColor::green);
323  vpDisplay::flush(Iint);
324  vpDisplay::flush(Iext);
325  }
326 
327  // compute the control law
328  v = task.computeControlLaw();
329 
330  // send the camera velocity to the controller
332 
333  std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
334  }
335 
336  // Display task information
337  task.print();
338 
339  std::cout << "Final robot position with respect to the object frame:\n";
340  cMo.print();
341 
342  if (opt_display && opt_click_allowed) {
343  vpDisplay::displayText(Iint, 20, 20, "Click to quit...", vpColor::white);
344  vpDisplay::flush(Iint);
345  vpDisplay::getClick(Iint);
346  }
347  return EXIT_SUCCESS;
348  } catch (const vpException &e) {
349  std::cout << "Catch a ViSP exception: " << e << std::endl;
350  return EXIT_FAILURE;
351  }
352 }
353 #elif !(defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
354 int main()
355 {
356  std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
357  return EXIT_SUCCESS;
358 }
359 #else
360 int main()
361 {
362  std::cout << "You do not have X11, or GTK, or GDI (Graphical Device Interface) functionalities to display images..."
363  << std::endl;
364  std::cout << "Tip if you are on a unix-like system:" << std::endl;
365  std::cout << "- Install X11, configure again ViSP using cmake and build again this example" << std::endl;
366  std::cout << "Tip if you are on a windows-like system:" << std::endl;
367  std::cout << "- Install GDI, configure again ViSP using cmake and build again this example" << std::endl;
368  return EXIT_SUCCESS;
369 }
370 #endif
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:131
static const vpColor white
Definition: vpColor.h:212
static const vpColor green
Definition: vpColor.h:220
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:129
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:135
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:135
void init(vpImage< unsigned char > &I, int win_x=-1, int win_y=-1, const std::string &win_title="")
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
error that can be emited by ViSP classes.
Definition: vpException.h:72
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
void buildFrom(double x, double y, double Z)
void track(const vpHomogeneousMatrix &cMo)
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix inverse() const
static double rad(double deg)
Definition: vpMath.h:117
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:154
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:82
void setWorldCoordinates(double oX, double oY, double oZ)
Definition: vpPoint.cpp:113
interface with the image for feature display
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 insert(vpForwardProjection &fp)
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
void get_eJe(vpMatrix &eJe)
@ CAMERA_FRAME
Definition: vpRobot.h:83
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)
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:564
@ EYEINHAND_L_cVe_eJe
Definition: vpServo.h:159
void set_cVe(const vpVelocityTwistMatrix &cVe_)
Definition: vpServo.h:448
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:303
void setLambda(double c)
Definition: vpServo.h:404
void set_eJe(const vpMatrix &eJe_)
Definition: vpServo.h:506
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:215
vpColVector getError() const
Definition: vpServo.h:278
vpColVector computeControlLaw()
Definition: vpServo.cpp:926
@ MEAN
Definition: vpServo.h:190
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:487
Class that defines the simplest robot: a free flying camera.