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