Visual Servoing Platform  version 3.5.0 under development (2022-02-15)
servoBiclopsPoint2DArtVelocity.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  * tests the control law
33  * eye-in-hand control
34  * velocity computed in articular
35  *
36  * Authors:
37  * Fabien Spindler
38  *
39  *****************************************************************************/
40 
59 #include <signal.h>
60 #include <stdlib.h>
61 #include <visp3/core/vpConfig.h>
62 #include <visp3/core/vpDebug.h> // Debug trace
63 #include <visp3/core/vpTime.h>
64 #if (defined(VISP_HAVE_BICLOPS) && (defined(VISP_HAVE_DC1394) || defined(VISP_HAVE_DIRECTSHOW)))
65 
66 #ifdef VISP_HAVE_PTHREAD
67 #include <pthread.h>
68 #endif
69 
70 #include <visp3/core/vpDisplay.h>
71 #include <visp3/core/vpImage.h>
72 #include <visp3/gui/vpDisplayGDI.h>
73 #include <visp3/gui/vpDisplayGTK.h>
74 #include <visp3/gui/vpDisplayX.h>
75 #include <visp3/sensor/vp1394TwoGrabber.h>
76 #include <visp3/sensor/vpDirectShowGrabber.h>
77 
78 #include <visp3/blob/vpDot.h>
79 #include <visp3/core/vpHomogeneousMatrix.h>
80 #include <visp3/core/vpIoTools.h>
81 #include <visp3/core/vpMath.h>
82 #include <visp3/core/vpPoint.h>
83 #include <visp3/io/vpParseArgv.h>
84 #include <visp3/robot/vpRobotBiclops.h>
85 #include <visp3/visual_features/vpFeatureBuilder.h>
86 #include <visp3/visual_features/vpFeaturePoint.h>
87 #include <visp3/vs/vpServo.h>
88 #include <visp3/vs/vpServoDisplay.h>
89 
90 // Exception
91 #include <visp3/core/vpException.h>
92 
93 #ifdef VISP_HAVE_PTHREAD
94 pthread_mutex_t mutexEndLoop = PTHREAD_MUTEX_INITIALIZER;
95 #endif
96 
97 void signalCtrC(int /* signumber */)
98 {
99 #ifdef VISP_HAVE_PTHREAD
100  pthread_mutex_unlock(&mutexEndLoop);
101 #endif
102  vpTime::wait(10);
103  vpTRACE("Ctrl-C pressed...");
104 }
105 
106 // List of allowed command line options
107 #define GETOPTARGS "c:d:h"
108 
120 void usage(const char *name, const char *badparam, std::string &conf, std::string &debugdir, std::string &user)
121 {
122  fprintf(stdout, "\n\
123  Example of eye-in-hand control law. We control here a real robot, the biclops\n\
124  robot (pan-tilt head provided by Traclabs). The velocity is\n\
125  computed in articular. The visual feature is the center of gravity of a\n\
126  point.\n\
127 \n\
128 SYNOPSIS\n\
129  %s [-c <Biclops configuration file>] [-d <debug file directory>] [-h]\n", name);
130 
131  fprintf(stdout, "\n\
132 OPTIONS: Default\n\
133  -c <Biclops configuration file> %s\n\
134  Sets the biclops robot configuration file.\n\n\
135  -d <debug file directory> %s\n\
136  Sets the debug file directory.\n\
137  From this directory, creates the\"%s\"\n\
138  subdirectory depending on the username, where\n\
139  it writes biclops.txt file.\n", conf.c_str(), debugdir.c_str(), user.c_str());
140 
141  if (badparam) {
142  fprintf(stderr, "ERROR: \n");
143  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
144  }
145 }
159 bool getOptions(int argc, const char **argv, std::string &conf, std::string &debugdir, std::string &user)
160 {
161  const char *optarg_;
162  int c;
163  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
164 
165  switch (c) {
166  case 'c':
167  conf = optarg_;
168  break;
169  case 'd':
170  debugdir = optarg_;
171  break;
172  case 'h':
173  usage(argv[0], NULL, conf, debugdir, user);
174  return false;
175  break;
176 
177  default:
178  usage(argv[0], optarg_, conf, debugdir, user);
179  return false;
180  break;
181  }
182  }
183 
184  if ((c == 1) || (c == -1)) {
185  // standalone param or error
186  usage(argv[0], NULL, conf, debugdir, user);
187  std::cerr << "ERROR: " << std::endl;
188  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
189  return false;
190  }
191 
192  return true;
193 }
194 
195 int main(int argc, const char **argv)
196 {
197  std::cout << std::endl;
198  std::cout << "-------------------------------------------------------" << std::endl;
199  std::cout << " Test program for vpServo " << std::endl;
200  std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl;
201  std::cout << " Simulation " << std::endl;
202  std::cout << " task : servo a point " << std::endl;
203  std::cout << "-------------------------------------------------------" << std::endl;
204  std::cout << std::endl;
205 
206  try {
207 
208 #ifdef VISP_HAVE_PTHREAD
209  pthread_mutex_lock(&mutexEndLoop);
210 #endif
211  signal(SIGINT, &signalCtrC);
212 
213  // default unix configuration file path
214  std::string opt_conf = "/usr/share/BiclopsDefault.cfg";
215 
216  std::string username;
217  std::string debugdir;
218  std::string opt_debugdir;
219 
220 // Set the default output path
221 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
222  opt_debugdir = "/tmp";
223 #elif defined(_WIN32)
224  opt_debugdir = "C:/temp";
225 #endif
226 
227  // Get the user login name
228  vpIoTools::getUserName(username);
229 
230  // Read the command line options
231  if (getOptions(argc, argv, opt_conf, opt_debugdir, username) == false) {
232  exit(-1);
233  }
234 
235  // Get the option value
236  if (!opt_debugdir.empty())
237  debugdir = opt_debugdir;
238 
239  // Append to the output path string, the login name of the user
240  std::string dirname = debugdir + "/" + username;
241 
242  // Test if the output path exist. If no try to create it
243  if (vpIoTools::checkDirectory(dirname) == false) {
244  try {
245  // Create the dirname
246  vpIoTools::makeDirectory(dirname);
247  } catch (...) {
248  usage(argv[0], NULL, opt_conf, debugdir, username);
249  std::cerr << std::endl << "ERROR:" << std::endl;
250  std::cerr << " Cannot create " << dirname << std::endl;
251  std::cerr << " Check your -d " << debugdir << " option " << std::endl;
252  exit(-1);
253  }
254  }
255 
256  // Create the debug file: debugdir/$user/biclops.txt
257  char filename[FILENAME_MAX];
258  sprintf(filename, "%s/biclops.txt", debugdir.c_str());
259  FILE *fd = fopen(filename, "w");
260 
261  vpRobotBiclops robot(opt_conf.c_str());
262  robot.setDenavitHartenbergModel(vpBiclops::DH2);
263 
264  {
265  vpColVector q(2);
266  q = 0;
268  robot.setPosition(vpRobot::ARTICULAR_FRAME, q);
269  }
270 
272 
273 #if defined VISP_HAVE_DC1394
275 #elif defined VISP_HAVE_DIRECTSHOW
277 #endif
278 
279  g.open(I);
280 
281  try {
282  g.acquire(I);
283  } catch (...) {
284  vpERROR_TRACE(" Error caught");
285  return (-1);
286  }
287 
288 // We open a window using either X11 or GTK or GDI.
289 // Its size is automatically defined by the image (I) size
290 #if defined VISP_HAVE_X11
291  vpDisplayX display(I, 100, 100, "Display X...");
292 #elif defined VISP_HAVE_GTK
293  vpDisplayGTK display(I, 100, 100, "Display GTK...");
294 #elif defined(_WIN32)
295  vpDisplayGDI display(I, 100, 100, "Display GDI...");
296 #endif
297 
298  try {
300  vpDisplay::flush(I);
301  } catch (...) {
302  vpERROR_TRACE(" Error caught");
303  return (-1);
304  }
305 
306  vpServo task;
307 
308  vpDot dot;
309 
310  try {
311  std::cout << "Click on a dot to initialize the tracking..." << std::endl;
312  dot.setGraphics(true);
313  dot.initTracking(I);
314  dot.track(I);
315  vpERROR_TRACE("after dot.initTracking(I) ");
316  } catch (...) {
317  vpERROR_TRACE(" Error caught");
318  return (-1);
319  }
320 
321  vpCameraParameters cam;
322 
323  // sets the current position of the visual feature
324  vpFeaturePoint p;
325  vpFeatureBuilder::create(p, cam, dot); // retrieve x,y and Z of the vpPoint structure
326 
327  p.set_Z(1);
328  // sets the desired position of the visual feature
329  vpFeaturePoint pd;
330  pd.buildFrom(0, 0, 1);
331 
332  // define the task
333  // - we want an eye-in-hand control law
334  // - articular velocity are computed
337 
338  vpTRACE("Set the position of the end-effector frame in the camera frame");
340  // robot.get_cMe(cMe) ;
341 
343  robot.get_cVe(cVe);
344  std::cout << cVe << std::endl;
345  task.set_cVe(cVe);
346 
347  std::cout << "Click in the image to start the servoing..." << std::endl;
349 
350  // Set the Jacobian (expressed in the end-effector frame)
351  vpMatrix eJe;
352  robot.get_eJe(eJe);
353  task.set_eJe(eJe);
354 
355  // we want to see a point on a point
356  task.addFeature(p, pd);
357 
358  // set the gain
359  task.setLambda(0.2);
360 
361  // Display task information
362  task.print();
363 
365 
366  unsigned int iter = 0;
367  vpTRACE("\t loop");
368 #ifdef VISP_HAVE_PTHREAD
369  while (0 != pthread_mutex_trylock(&mutexEndLoop))
370 #else
371  for (;;)
372 #endif
373  {
374  std::cout << "---------------------------------------------" << iter << std::endl;
375 
376  g.acquire(I);
378 
379  dot.track(I);
380 
381  // vpDisplay::displayCross(I,(int)dot.I(), (int)dot.J(),
382  // 10,vpColor::green) ;
383 
384  vpFeatureBuilder::create(p, cam, dot);
385 
386  // get the jacobian
387  robot.get_eJe(eJe);
388  task.set_eJe(eJe);
389 
390  // std::cout << (vpMatrix)cVe*eJe << std::endl ;
391 
392  vpColVector v;
393  v = task.computeControlLaw();
394 
395  vpServoDisplay::display(task, cam, I);
396  vpDisplay::flush(I);
397 
398  std::cout << "v: " << v.t();
400 
401  std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
402 
403  {
404  vpColVector s_minus_sStar(2);
405  s_minus_sStar = task.s - task.sStar;
406  fprintf(fd, "%f %f %f %f %f\n", v[0], v[1], s_minus_sStar[0], s_minus_sStar[1], (task.getError()).sumSquare());
407  }
408  }
409 
410  std::cout << "Display task information " << std::endl;
411  task.print();
412 
413  fclose(fd);
414 
415  return EXIT_SUCCESS;
416  }
417  catch (const vpException &e) {
418  std::cout << "Catch an exception: " << e.getMessage() << std::endl;
419  return EXIT_FAILURE;
420  }
421 }
422 
423 #else
424 int main()
425 {
426  std::cout << "You do not have an biclops PT robot connected to your computer..." << std::endl;
427  return EXIT_SUCCESS;
428 }
429 #endif
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:153
vpVelocityTwistMatrix get_cVe() const
Definition: vpUnicycle.h:82
static void makeDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:570
VISP_EXPORT int wait(double t0, double t)
Definition: vpTime.cpp:173
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
void buildFrom(double x, double y, double Z)
void get_eJe(vpMatrix &eJe)
Implementation of an homogeneous matrix and operations on such kind of matrices.
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:490
#define vpERROR_TRACE
Definition: vpDebug.h:393
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
void set_eJe(const vpMatrix &eJe_)
Definition: vpServo.h:506
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:134
Initialize the position controller.
Definition: vpRobot.h:67
void track(const vpImage< unsigned char > &I)
Definition: vpDot.cpp:770
error that can be emited by ViSP classes.
Definition: vpException.h:71
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
vpRowVector t() const
const char * getMessage() const
Definition: vpException.cpp:90
class for windows direct show devices
static void flush(const vpImage< unsigned char > &I)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
virtual vpRobotStateType setRobotState(const vpRobot::vpRobotStateType newState)
Definition: vpRobot.cpp:201
static bool checkDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:420
Initialize the velocity controller.
Definition: vpRobot.h:66
vpColVector computeControlLaw()
Definition: vpServo.cpp:929
void set_Z(double Z)
#define vpTRACE
Definition: vpDebug.h:416
static void display(const vpImage< unsigned char > &I)
void acquire(vpImage< unsigned char > &I)
Generic class defining intrinsic camera parameters.
void setLambda(double c)
Definition: vpServo.h:404
static std::string getUserName()
Definition: vpIoTools.cpp:316
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:134
vpColVector s
Definition: vpServo.h:558
Interface for the biclops, pan, tilt head control.
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:567
Implementation of column vector and the associated operations.
Definition: vpColVector.h:130
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:306
vpColVector sStar
Definition: vpServo.h:562
This tracker is meant to track a dot (connected pixels with same gray level) on a vpImage...
Definition: vpDot.h:115
vpColVector getError() const
Definition: vpServo.h:278
Class for firewire ieee1394 video devices using libdc1394-2.x api.
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
void setGraphics(bool activate)
Definition: vpDot.h:361
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:218
void initTracking(const vpImage< unsigned char > &I)
Definition: vpDot.cpp:635
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)