ViSP  2.9.0
servoBiclopsPoint2DArtVelocity.cpp
1 /****************************************************************************
2  *
3  * $Id: servoBiclopsPoint2DArtVelocity.cpp 4604 2014-01-21 14:15:23Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 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_DIRECTSHOW)) )
73 
74 #ifdef VISP_HAVE_PTHREAD
75 # include <pthread.h>
76 #endif
77 
78 #include <visp/vp1394TwoGrabber.h>
79 #include <visp/vpDirectShowGrabber.h>
80 #include <visp/vpImage.h>
81 #include <visp/vpDisplay.h>
82 #include <visp/vpDisplayX.h>
83 #include <visp/vpDisplayGTK.h>
84 #include <visp/vpDisplayGDI.h>
85 
86 #include <visp/vpMath.h>
87 #include <visp/vpHomogeneousMatrix.h>
88 #include <visp/vpFeaturePoint.h>
89 #include <visp/vpPoint.h>
90 #include <visp/vpServo.h>
91 #include <visp/vpFeatureBuilder.h>
92 #include <visp/vpRobotBiclops.h>
93 #include <visp/vpIoTools.h>
94 #include <visp/vpParseArgv.h>
95 #include <visp/vpServoDisplay.h>
96 #include <visp/vpDot.h>
97 
98 // Exception
99 #include <visp/vpException.h>
100 #include <visp/vpMatrixException.h>
101 
102 
103 #ifdef VISP_HAVE_PTHREAD
104 pthread_mutex_t mutexEndLoop = PTHREAD_MUTEX_INITIALIZER;
105 #endif
106 
107 void signalCtrC( int /* signumber */)
108 {
109 #ifdef VISP_HAVE_PTHREAD
110  pthread_mutex_unlock( &mutexEndLoop );
111 #endif
112  vpTime::wait(10);
113  vpTRACE("Ctrl-C pressed...");
114 }
115 
116 
117 // List of allowed command line options
118 #define GETOPTARGS "c:d:h"
119 
131 void usage(const char *name, const char *badparam, std::string& conf, std::string& debugdir, std::string& user)
132 {
133  fprintf(stdout, "\n\
134  Example of eye-in-hand control law. We control here a real robot, the biclops\n\
135  robot (pan-tilt head provided by Traclabs). The velocity is\n\
136  computed in articular. The visual feature is the center of gravity of a\n\
137  point.\n\
138 \n\
139 SYNOPSIS\n\
140  %s [-c <Biclops configuration file>] [-d <debug file directory>] [-h]\n", name);
141 
142  fprintf(stdout, "\n\
143 OPTIONS: Default\n\
144  -c <Biclops configuration file> %s\n\
145  Sets the biclops robot configuration file.\n\n\
146  -d <debug file directory> %s\n\
147  Sets the debug file directory.\n\
148  From this directory, creates the\"%s\"\n\
149  subdirectory depending on the username, where\n\
150  it writes biclops.txt file.\n", conf.c_str(), debugdir.c_str(), user.c_str());
151 
152  if (badparam) {
153  fprintf(stderr, "ERROR: \n" );
154  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
155  }
156 }
170 bool getOptions(int argc, const char **argv, std::string& conf, std::string &debugdir, std::string& user)
171 {
172  const char *optarg;
173  int c;
174  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
175 
176  switch (c) {
177  case 'c': conf = optarg; break;
178  case 'd': debugdir = optarg; break;
179  case 'h': usage(argv[0], NULL, conf, debugdir, user); return false; break;
180 
181  default:
182  usage(argv[0], optarg, conf, debugdir, user); return false; break;
183  }
184  }
185 
186  if ((c == 1) || (c == -1)) {
187  // standalone param or error
188  usage(argv[0], NULL, conf, debugdir, user);
189  std::cerr << "ERROR: " << std::endl;
190  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
191  return false;
192  }
193 
194  return true;
195 }
196 
197 
198 
199 int
200 main(int argc, const char ** argv)
201 {
202  std::cout << std::endl ;
203  std::cout << "-------------------------------------------------------" << std::endl ;
204  std::cout << " Test program for vpServo " <<std::endl ;
205  std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl ;
206  std::cout << " Simulation " << std::endl ;
207  std::cout << " task : servo a point " << std::endl ;
208  std::cout << "-------------------------------------------------------" << std::endl ;
209  std::cout << std::endl ;
210 
211  try{
212 
213 #ifdef VISP_HAVE_PTHREAD
214  pthread_mutex_lock( &mutexEndLoop );
215 #endif
216  signal( SIGINT,&signalCtrC );
217 
218  //default unix configuration file path
219  std::string opt_conf = "/usr/share/BiclopsDefault.cfg";
220 
221  std::string username;
222  std::string debugdir;
223  std::string opt_debugdir;
224 
225  // Set the default output path
226 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
227  opt_debugdir = "/tmp";
228 #elif defined(_WIN32)
229  opt_debugdir = "C:/temp";
230 #endif
231 
232  // Get the user login name
233  vpIoTools::getUserName(username);
234 
235  // Read the command line options
236  if (getOptions(argc, argv, opt_conf, opt_debugdir , username) == false) {
237  exit (-1);
238  }
239 
240  // Get the option value
241  if (!opt_debugdir.empty())
242  debugdir = opt_debugdir;
243 
244  // Append to the output path string, the login name of the user
245  std::string dirname = debugdir + "/" + username;
246 
247  // Test if the output path exist. If no try to create it
248  if (vpIoTools::checkDirectory(dirname) == false) {
249  try {
250  // Create the dirname
251  vpIoTools::makeDirectory(dirname);
252  }
253  catch (...) {
254  usage(argv[0], NULL, opt_conf, debugdir, username);
255  std::cerr << std::endl
256  << "ERROR:" << std::endl;
257  std::cerr << " Cannot create " << dirname << std::endl;
258  std::cerr << " Check your -d " << debugdir << " option " << std::endl;
259  exit(-1);
260  }
261  }
262 
263  // Create the debug file: debugdir/$user/biclops.txt
264  char *filename = new char[FILENAME_MAX];
265  sprintf(filename, "%s/biclops.txt", debugdir.c_str());
266  FILE *fd = fopen(filename, "w");
267 
268  vpRobotBiclops robot(opt_conf.c_str()) ;
269  robot.setDenavitHartenbergModel(vpBiclops::DH2);
270 
271  {
272  vpColVector q(2); q=0;
275  }
276 
278 
279 #if defined VISP_HAVE_DC1394_2
281 #elif defined VISP_HAVE_DIRECTSHOW
283 #endif
284 
285  g.open(I) ;
286 
287  try{
288  g.acquire(I) ;
289  }
290  catch(...)
291  {
292  vpERROR_TRACE(" Error caught") ;
293  return(-1) ;
294  }
295 
296  // We open a window using either X11 or GTK or GDI.
297  // Its size is automatically defined by the image (I) size
298 #if defined VISP_HAVE_X11
299  vpDisplayX display(I, 100, 100,"Display X...") ;
300 #elif defined VISP_HAVE_GTK
301  vpDisplayGTK display(I, 100, 100,"Display GTK...") ;
302 #elif defined(_WIN32)
303  vpDisplayGDI display(I, 100, 100,"Display GDI...") ;
304 #endif
305 
306  try{
307  vpDisplay::display(I) ;
308  vpDisplay::flush(I) ;
309  }
310  catch(...)
311  {
312  vpERROR_TRACE(" Error caught") ;
313  return(-1) ;
314  }
315 
316 
317  vpServo task ;
318 
319  vpDot dot ;
320 
321  try{
322  std::cout << "Click on a dot to initialize the tracking..." << std::endl;
323  dot.setGraphics(true);
324  dot.initTracking(I) ;
325  dot.track(I);
326  vpERROR_TRACE("after dot.initTracking(I) ") ;
327  }
328  catch(...)
329  {
330  vpERROR_TRACE(" Error caught") ;
331  return(-1) ;
332  }
333 
334  vpCameraParameters cam ;
335 
336  // sets the current position of the visual feature
337  vpFeaturePoint p ;
338  vpFeatureBuilder::create(p,cam, dot) ; //retrieve x,y and Z of the vpPoint structure
339 
340  p.set_Z(1) ;
341  // sets the desired position of the visual feature
342  vpFeaturePoint pd ;
343  pd.buildFrom(0,0,1) ;
344 
345  // define the task
346  // - we want an eye-in-hand control law
347  // - articular velocity are computed
350 
351 
352  vpTRACE("Set the position of the camera in the end-effector frame ") ;
353  vpHomogeneousMatrix cMe ;
354  // robot.get_cMe(cMe) ;
355 
357  robot.get_cVe(cVe) ;
358  std::cout << cVe <<std::endl ;
359  task.set_cVe(cVe) ;
360 
361  std::cout << "Click in the image to start the servoing..." << std::endl;
363 
364  // Set the Jacobian (expressed in the end-effector frame)
365  vpMatrix eJe ;
366  robot.get_eJe(eJe) ;
367  task.set_eJe(eJe) ;
368 
369  // we want to see a point on a point
370  task.addFeature(p,pd) ;
371 
372  // set the gain
373  task.setLambda(0.2) ;
374 
375  // Display task information
376  task.print() ;
377 
379 
380  unsigned int iter=0 ;
381  vpTRACE("\t loop") ;
382 #ifdef VISP_HAVE_PTHREAD
383  while( 0 != pthread_mutex_trylock( &mutexEndLoop ) )
384 #else
385  for ( ; ; )
386 #endif
387  {
388  std::cout << "---------------------------------------------" << iter <<std::endl ;
389 
390  g.acquire(I) ;
391  vpDisplay::display(I) ;
392 
393  dot.track(I) ;
394 
395  // vpDisplay::displayCross(I,(int)dot.I(), (int)dot.J(),
396  // 10,vpColor::green) ;
397 
398 
399  vpFeatureBuilder::create(p,cam, dot);
400 
401  // get the jacobian
402  robot.get_eJe(eJe) ;
403  task.set_eJe(eJe) ;
404 
405  // std::cout << (vpMatrix)cVe*eJe << std::endl ;
406 
407  vpColVector v ;
408  v = task.computeControlLaw() ;
409 
410  vpServoDisplay::display(task,cam,I) ;
411  vpDisplay::flush(I) ;
412 
413  std::cout << "v: " << v.t() ;
415 
416  std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() << std::endl;
417 
418  {
419  vpColVector s_minus_sStar(2);
420  s_minus_sStar = task.s - task.sStar;
421  fprintf(fd, "%f %f %f %f %f\n",
422  v[0], v[1],
423  s_minus_sStar[0], s_minus_sStar[1],
424  ( task.getError() ).sumSquare());
425  }
426  }
427 
428  std::cout << "Display task information " << std::endl;
429  task.print() ;
430  task.kill();
431 
432  fclose(fd);
433 
434  } catch (...) { vpERROR_TRACE("Throw uncatched..."); }
435 
436 }
437 
438 
439 #else
440 int
441 main()
442 {
443  vpERROR_TRACE("You don't have a biclops head connected to your computer or 1394 framegrabbing capabilities...");
444 }
445 #endif
Definition of the vpMatrix class.
Definition: vpMatrix.h:98
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:395
#define vpTRACE
Definition: vpDebug.h:418
void setPosition(const vpHomogeneousMatrix &cMw)
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:132
void set_eJe(const vpMatrix &eJe_)
Definition: vpServo.h:439
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)
Definition: vpServo.cpp:449
Initialize the position controller.
Definition: vpRobot.h:71
void track(const vpImage< unsigned char > &I)
Definition: vpDot.cpp:807
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
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1994
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:190
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:404
void kill()
Definition: vpServo.cpp:189
Initialize the velocity controller.
Definition: vpRobot.h:70
vpColVector getError() const
Definition: vpServo.h:257
vpColVector computeControlLaw()
Definition: vpServo.cpp:902
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
vpRowVector t() const
transpose of Vector
void acquire(vpImage< unsigned char > &I)
Generic class defining intrinsic camera parameters.
void setLambda(double c)
Definition: vpServo.h:370
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:472
Interface for the biclops, pan, tilt head control.
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:522
void buildFrom(const double x, const double y, const double Z)
void get_cVe(vpVelocityTwistMatrix &cVe) const
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:350
void set_cVe(const vpVelocityTwistMatrix &cVe_)
Definition: vpServo.h:414
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:251
vpColVector sStar
Definition: vpServo.h:475
This tracker is meant to track a dot (connected pixels with same gray level) on a vpImage...
Definition: vpDot.h:114
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)
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:220
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &v)
void initTracking(const vpImage< unsigned char > &I)
Definition: vpDot.cpp:658
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)