Visual Servoing Platform  version 3.0.1
servoBiclopsPoint2DArtVelocity.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
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * tests the control law
32  * eye-in-hand control
33  * velocity computed in articular
34  *
35  * Authors:
36  * Fabien Spindler
37  *
38  *****************************************************************************/
39 
63 #include <visp3/core/vpTime.h>
64 #include <visp3/core/vpConfig.h>
65 #include <visp3/core/vpDebug.h> // Debug trace
66 #include <signal.h>
67 #include <stdlib.h>
68 #if ( defined (VISP_HAVE_BICLOPS) && (defined (VISP_HAVE_DC1394) || defined(VISP_HAVE_DIRECTSHOW)) )
69 
70 #ifdef VISP_HAVE_PTHREAD
71 # include <pthread.h>
72 #endif
73 
74 #include <visp3/sensor/vp1394TwoGrabber.h>
75 #include <visp3/sensor/vpDirectShowGrabber.h>
76 #include <visp3/core/vpImage.h>
77 #include <visp3/core/vpDisplay.h>
78 #include <visp3/gui/vpDisplayX.h>
79 #include <visp3/gui/vpDisplayGTK.h>
80 #include <visp3/gui/vpDisplayGDI.h>
81 
82 #include <visp3/core/vpMath.h>
83 #include <visp3/core/vpHomogeneousMatrix.h>
84 #include <visp3/visual_features/vpFeaturePoint.h>
85 #include <visp3/core/vpPoint.h>
86 #include <visp3/vs/vpServo.h>
87 #include <visp3/visual_features/vpFeatureBuilder.h>
88 #include <visp3/robot/vpRobotBiclops.h>
89 #include <visp3/core/vpIoTools.h>
90 #include <visp3/io/vpParseArgv.h>
91 #include <visp3/vs/vpServoDisplay.h>
92 #include <visp3/blob/vpDot.h>
93 
94 // Exception
95 #include <visp3/core/vpException.h>
96 
97 #ifdef VISP_HAVE_PTHREAD
98 pthread_mutex_t mutexEndLoop = PTHREAD_MUTEX_INITIALIZER;
99 #endif
100 
101 void signalCtrC( int /* signumber */)
102 {
103 #ifdef VISP_HAVE_PTHREAD
104  pthread_mutex_unlock( &mutexEndLoop );
105 #endif
106  vpTime::wait(10);
107  vpTRACE("Ctrl-C pressed...");
108 }
109 
110 
111 // List of allowed command line options
112 #define GETOPTARGS "c:d:h"
113 
125 void usage(const char *name, const char *badparam, std::string& conf, std::string& debugdir, std::string& user)
126 {
127  fprintf(stdout, "\n\
128  Example of eye-in-hand control law. We control here a real robot, the biclops\n\
129  robot (pan-tilt head provided by Traclabs). The velocity is\n\
130  computed in articular. The visual feature is the center of gravity of a\n\
131  point.\n\
132 \n\
133 SYNOPSIS\n\
134  %s [-c <Biclops configuration file>] [-d <debug file directory>] [-h]\n", name);
135 
136  fprintf(stdout, "\n\
137 OPTIONS: Default\n\
138  -c <Biclops configuration file> %s\n\
139  Sets the biclops robot configuration file.\n\n\
140  -d <debug file directory> %s\n\
141  Sets the debug file directory.\n\
142  From this directory, creates the\"%s\"\n\
143  subdirectory depending on the username, where\n\
144  it writes biclops.txt file.\n", conf.c_str(), debugdir.c_str(), user.c_str());
145 
146  if (badparam) {
147  fprintf(stderr, "ERROR: \n" );
148  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
149  }
150 }
164 bool getOptions(int argc, const char **argv, std::string& conf, std::string &debugdir, std::string& user)
165 {
166  const char *optarg_;
167  int c;
168  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
169 
170  switch (c) {
171  case 'c': conf = optarg_; break;
172  case 'd': debugdir = optarg_; break;
173  case 'h': usage(argv[0], NULL, conf, debugdir, user); return false; break;
174 
175  default:
176  usage(argv[0], optarg_, conf, debugdir, user); return false; break;
177  }
178  }
179 
180  if ((c == 1) || (c == -1)) {
181  // standalone param or error
182  usage(argv[0], NULL, conf, debugdir, user);
183  std::cerr << "ERROR: " << std::endl;
184  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
185  return false;
186  }
187 
188  return true;
189 }
190 
191 
192 
193 int
194 main(int argc, const char ** argv)
195 {
196  std::cout << std::endl ;
197  std::cout << "-------------------------------------------------------" << std::endl ;
198  std::cout << " Test program for vpServo " <<std::endl ;
199  std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl ;
200  std::cout << " Simulation " << std::endl ;
201  std::cout << " task : servo a point " << std::endl ;
202  std::cout << "-------------------------------------------------------" << std::endl ;
203  std::cout << std::endl ;
204 
205  try{
206 
207 #ifdef VISP_HAVE_PTHREAD
208  pthread_mutex_lock( &mutexEndLoop );
209 #endif
210  signal( SIGINT,&signalCtrC );
211 
212  //default unix configuration file path
213  std::string opt_conf = "/usr/share/BiclopsDefault.cfg";
214 
215  std::string username;
216  std::string debugdir;
217  std::string opt_debugdir;
218 
219  // Set the default output path
220 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
221  opt_debugdir = "/tmp";
222 #elif defined(_WIN32)
223  opt_debugdir = "C:/temp";
224 #endif
225 
226  // Get the user login name
227  vpIoTools::getUserName(username);
228 
229  // Read the command line options
230  if (getOptions(argc, argv, opt_conf, opt_debugdir , username) == false) {
231  exit (-1);
232  }
233 
234  // Get the option value
235  if (!opt_debugdir.empty())
236  debugdir = opt_debugdir;
237 
238  // Append to the output path string, the login name of the user
239  std::string dirname = debugdir + "/" + username;
240 
241  // Test if the output path exist. If no try to create it
242  if (vpIoTools::checkDirectory(dirname) == false) {
243  try {
244  // Create the dirname
245  vpIoTools::makeDirectory(dirname);
246  }
247  catch (...) {
248  usage(argv[0], NULL, opt_conf, debugdir, username);
249  std::cerr << std::endl
250  << "ERROR:" << std::endl;
251  std::cerr << " Cannot create " << dirname << std::endl;
252  std::cerr << " Check your -d " << debugdir << " option " << std::endl;
253  exit(-1);
254  }
255  }
256 
257  // Create the debug file: debugdir/$user/biclops.txt
258  char *filename = new char[FILENAME_MAX];
259  sprintf(filename, "%s/biclops.txt", debugdir.c_str());
260  FILE *fd = fopen(filename, "w");
261 
262  vpRobotBiclops robot(opt_conf.c_str()) ;
263  robot.setDenavitHartenbergModel(vpBiclops::DH2);
264 
265  {
266  vpColVector q(2); q=0;
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  }
284  catch(...)
285  {
286  vpERROR_TRACE(" Error caught") ;
287  return(-1) ;
288  }
289 
290  // We open a window using either X11 or GTK or GDI.
291  // Its size is automatically defined by the image (I) size
292 #if defined VISP_HAVE_X11
293  vpDisplayX display(I, 100, 100,"Display X...") ;
294 #elif defined VISP_HAVE_GTK
295  vpDisplayGTK display(I, 100, 100,"Display GTK...") ;
296 #elif defined(_WIN32)
297  vpDisplayGDI display(I, 100, 100,"Display GDI...") ;
298 #endif
299 
300  try{
301  vpDisplay::display(I) ;
302  vpDisplay::flush(I) ;
303  }
304  catch(...)
305  {
306  vpERROR_TRACE(" Error caught") ;
307  return(-1) ;
308  }
309 
310 
311  vpServo task ;
312 
313  vpDot dot ;
314 
315  try{
316  std::cout << "Click on a dot to initialize the tracking..." << std::endl;
317  dot.setGraphics(true);
318  dot.initTracking(I) ;
319  dot.track(I);
320  vpERROR_TRACE("after dot.initTracking(I) ") ;
321  }
322  catch(...)
323  {
324  vpERROR_TRACE(" Error caught") ;
325  return(-1) ;
326  }
327 
328  vpCameraParameters cam ;
329 
330  // sets the current position of the visual feature
331  vpFeaturePoint p ;
332  vpFeatureBuilder::create(p,cam, dot) ; //retrieve x,y and Z of the vpPoint structure
333 
334  p.set_Z(1) ;
335  // sets the desired position of the visual feature
336  vpFeaturePoint pd ;
337  pd.buildFrom(0,0,1) ;
338 
339  // define the task
340  // - we want an eye-in-hand control law
341  // - articular velocity are computed
344 
345 
346  vpTRACE("Set the position of the camera in the end-effector frame ") ;
347  vpHomogeneousMatrix cMe ;
348  // robot.get_cMe(cMe) ;
349 
351  robot.get_cVe(cVe) ;
352  std::cout << cVe <<std::endl ;
353  task.set_cVe(cVe) ;
354 
355  std::cout << "Click in the image to start the servoing..." << std::endl;
357 
358  // Set the Jacobian (expressed in the end-effector frame)
359  vpMatrix eJe ;
360  robot.get_eJe(eJe) ;
361  task.set_eJe(eJe) ;
362 
363  // we want to see a point on a point
364  task.addFeature(p,pd) ;
365 
366  // set the gain
367  task.setLambda(0.2) ;
368 
369  // Display task information
370  task.print() ;
371 
373 
374  unsigned int iter=0 ;
375  vpTRACE("\t loop") ;
376 #ifdef VISP_HAVE_PTHREAD
377  while( 0 != pthread_mutex_trylock( &mutexEndLoop ) )
378 #else
379  for ( ; ; )
380 #endif
381  {
382  std::cout << "---------------------------------------------" << iter <<std::endl ;
383 
384  g.acquire(I) ;
385  vpDisplay::display(I) ;
386 
387  dot.track(I) ;
388 
389  // vpDisplay::displayCross(I,(int)dot.I(), (int)dot.J(),
390  // 10,vpColor::green) ;
391 
392 
393  vpFeatureBuilder::create(p,cam, dot);
394 
395  // get the jacobian
396  robot.get_eJe(eJe) ;
397  task.set_eJe(eJe) ;
398 
399  // std::cout << (vpMatrix)cVe*eJe << std::endl ;
400 
401  vpColVector v ;
402  v = task.computeControlLaw() ;
403 
404  vpServoDisplay::display(task,cam,I) ;
405  vpDisplay::flush(I) ;
406 
407  std::cout << "v: " << v.t() ;
409 
410  std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() << std::endl;
411 
412  {
413  vpColVector s_minus_sStar(2);
414  s_minus_sStar = task.s - task.sStar;
415  fprintf(fd, "%f %f %f %f %f\n",
416  v[0], v[1],
417  s_minus_sStar[0], s_minus_sStar[1],
418  ( task.getError() ).sumSquare());
419  }
420  }
421 
422  std::cout << "Display task information " << std::endl;
423  task.print() ;
424  task.kill();
425 
426  fclose(fd);
427 
428  } catch (...) { vpERROR_TRACE("Throw uncatched..."); }
429 
430 }
431 
432 
433 #else
434 int
435 main()
436 {
437  vpERROR_TRACE("You don't have a biclops head connected to your computer or 1394 framegrabbing capabilities...");
438 }
439 #endif
void setPosition(const vpHomogeneousMatrix &wMc)
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:97
VISP_EXPORT int wait(double t0, double t)
Definition: vpTime.cpp:157
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:358
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
Implementation of an homogeneous matrix and operations on such kind of matrices.
#define vpERROR_TRACE
Definition: vpDebug.h:391
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
void set_eJe(const vpMatrix &eJe_)
Definition: vpServo.h:460
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:153
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, const unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:512
Initialize the position controller.
Definition: vpRobot.h:69
void track(const vpImage< unsigned char > &I)
Definition: vpDot.cpp:800
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
vpRowVector t() const
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:76
virtual vpRobotStateType setRobotState(const vpRobot::vpRobotStateType newState)
Definition: vpRobot.cpp:201
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:427
void kill()
Definition: vpServo.cpp:191
Initialize the velocity controller.
Definition: vpRobot.h:68
vpColVector computeControlLaw()
Definition: vpServo.cpp:954
#define vpTRACE
Definition: vpDebug.h:414
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:391
static std::string getUserName()
Definition: vpIoTools.cpp:177
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:138
Implementation of a velocity twist matrix and operations on such kind of matrices.
vpColVector s
Definition: vpServo.h:498
Interface for the biclops, pan, tilt head control.
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:585
void buildFrom(const double x, const double y, const double Z)
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
void setGraphics(const bool activate)
Definition: vpDot.h:359
void set_cVe(const vpVelocityTwistMatrix &cVe_)
Definition: vpServo.h:435
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:314
vpColVector sStar
Definition: vpServo.h:501
This tracker is meant to track a dot (connected pixels with same gray level) on a vpImage...
Definition: vpDot.h:116
vpColVector getError() const
Definition: vpServo.h:271
Class for firewire ieee1394 video devices using libdc1394-2.x api.
void set_Z(const double Z)
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
void get_eJe(vpMatrix &eJe)
void get_cVe(vpVelocityTwistMatrix &cVe) const
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:222
void initTracking(const vpImage< unsigned char > &I)
Definition: vpDot.cpp:654
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)