ViSP  2.8.0
servoViper850Point2DArtVelocity-jointAvoidance-basic.cpp
1 /****************************************************************************
2  *
3  * $Id: servoViper850Point2DArtVelocity-jointAvoidance-basic.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  * Eric Marchand
41  * Fabien Spindler
42  *
43  *****************************************************************************/
44 
56 #include <visp/vpConfig.h>
57 #include <visp/vpDebug.h> // Debug trace
58 
59 #include <stdlib.h>
60 #include <stdio.h>
61 #include <iostream>
62 #include <fstream>
63 #include <sstream>
64 #include <cmath> // std::fabs
65 #include <limits> // numeric_limits
66 
67 #if (defined (VISP_HAVE_VIPER850) && defined (VISP_HAVE_DC1394_2))
68 
69 #include <visp/vp1394TwoGrabber.h>
70 #include <visp/vpImage.h>
71 #include <visp/vpDisplay.h>
72 #include <visp/vpDisplayX.h>
73 #include <visp/vpDisplayOpenCV.h>
74 #include <visp/vpDisplayGTK.h>
75 #include <visp/vpMath.h>
76 #include <visp/vpHomogeneousMatrix.h>
77 #include <visp/vpFeaturePoint.h>
78 #include <visp/vpPoint.h>
79 #include <visp/vpServo.h>
80 #include <visp/vpFeatureBuilder.h>
81 #include <visp/vpRobotViper850.h>
82 #include <visp/vpIoTools.h>
83 #include <visp/vpException.h>
84 #include <visp/vpMatrixException.h>
85 #include <visp/vpServoDisplay.h>
86 #include <visp/vpDot2.h>
87 #include <visp/vpPlot.h>
88 
89 
90 int
91 main()
92 {
93  try {
94  vpRobotViper850 robot ;
95 
96  vpServo task ;
97 
99 
100  bool reset = false;
101  vp1394TwoGrabber g(reset);
103  g.setFramerate(vp1394TwoGrabber::vpFRAMERATE_60);
104  g.open(I) ;
105 
106  g.acquire(I) ;
107 
108  double Tloop = 1./60.f;
109 
111  g.getFramerate(fps);
112  switch(fps) {
113  case vp1394TwoGrabber::vpFRAMERATE_15 : Tloop = 1.f/15.f; break;
114  case vp1394TwoGrabber::vpFRAMERATE_30 : Tloop = 1.f/30.f; break;
115  case vp1394TwoGrabber::vpFRAMERATE_60 : Tloop = 1.f/60.f; break;
116  case vp1394TwoGrabber::vpFRAMERATE_120: Tloop = 1.f/120.f; break;
117  default: break;
118  }
119  std::cout << "Tloop: " << Tloop << std::endl;
120 
121 #ifdef VISP_HAVE_X11
122  vpDisplayX display(I,800,100,"Current image") ;
123 #elif defined(VISP_HAVE_OPENCV)
124  vpDisplayOpenCV display(I,800,100,"Current image") ;
125 #elif defined(VISP_HAVE_GTK)
126  vpDisplayGTK display(I,800,100,"Current image") ;
127 #endif
128 
129  vpDisplay::display(I) ;
130  vpDisplay::flush(I) ;
131 
132  vpColVector jointMin(6), jointMax(6) ;
133  jointMin = robot.getJointMin();
134  jointMax = robot.getJointMax();
135 
136  vpColVector Qmin(6), tQmin(6) ;
137  vpColVector Qmax(6), tQmax(6) ;
138  vpColVector Qmiddle(6);
139  vpColVector data(10) ;
140 
141  double rho = 0.25 ;
142  for (unsigned int i=0 ; i < 6 ; i++)
143  {
144  Qmin[i] = jointMin[i] + 0.5*rho*(jointMax[i]-jointMin[i]) ;
145  Qmax[i] = jointMax[i] - 0.5*rho*(jointMax[i]-jointMin[i]) ;
146  }
147  Qmiddle = (Qmin + Qmax) /2.;
148  double rho1 = 0.1 ;
149 
150  for (unsigned int i=0 ; i < 6 ; i++) {
151  tQmin[i]=Qmin[i]+ 0.5*(rho1)*(Qmax[i]-Qmin[i]) ;
152  tQmax[i]=Qmax[i]- 0.5*(rho1)*(Qmax[i]-Qmin[i]) ;
153  }
154 
155  vpColVector q(6) ;
156 
157  // Create a window with two graphics
158  // - first graphic to plot q(t), Qmin, Qmax, tQmin and tQmax
159  // - second graphic to plot the cost function h_s
160  vpPlot plot(2);
161 
162  // The first graphic contains 10 data to plot: q(t), Qmin, Qmax, tQmin and
163  // tQmax
164  plot.initGraph(0, 10);
165  plot.initGraph(1, 6);
166 
167  // For the first graphic :
168  // - along the x axis the expected values are between 0 and 200 and
169  // the step is 1
170  // - along the y axis the expected values are between -1.2 and 1.2 and the
171  // step is 0.1
172  plot.initRange(0,0,200,1,-1.2,1.2,0.1);
173  plot.setTitle(0, "Joint behavior");
174  plot.initRange(1,0,200,1,-0.01,0.01,0.05);
175  plot.setTitle(1, "Joint velocity");
176 
177  // For the first graphic, set the curves legend
178  char legend[10];
179  for (unsigned int i=0; i < 6; i++) {
180  sprintf(legend, "q%d", i+1);
181  plot.setLegend(0, i, legend);
182  sprintf(legend, "q%d", i+1);
183  plot.setLegend(1, i, legend);
184  }
185  plot.setLegend(0, 6, "tQmin");
186  plot.setLegend(0, 7, "tQmax");
187  plot.setLegend(0, 8, "Qmin");
188  plot.setLegend(0, 9, "Qmax");
189 
190  // Set the curves color
191  plot.setColor(0, 0, vpColor::red);
192  plot.setColor(0, 1, vpColor::green);
193  plot.setColor(0, 2, vpColor::blue);
194  plot.setColor(0, 3, vpColor::orange);
195  plot.setColor(0, 4, vpColor(0, 128, 0));
196  plot.setColor(0, 5, vpColor::cyan);
197  for (unsigned int i= 6; i < 10; i++)
198  plot.setColor(0, i, vpColor::black); // for Q and tQ [min,max]
199  // Set the curves color
200 
201  plot.setColor(1, 0, vpColor::red);
202  plot.setColor(1, 1, vpColor::green);
203  plot.setColor(1, 2, vpColor::blue);
204  plot.setColor(1, 3, vpColor::orange);
205  plot.setColor(1, 4, vpColor(0, 128, 0));
206  plot.setColor(1, 5, vpColor::cyan);
207  vpDot2 dot ;
208 
209 
210  std::cout << "Click on a dot..." << std::endl;
211  dot.initTracking(I) ;
212  vpImagePoint cog = dot.getCog();
214  vpDisplay::flush(I);
215 
216  vpCameraParameters cam ;
217  // Update camera parameters
218  robot.getCameraParameters (cam, I);
219 
220  // sets the current position of the visual feature
221  vpFeaturePoint p ;
222  vpFeatureBuilder::create(p,cam, dot) ; //retrieve x,y and Z of the vpPoint structure
223 
224  p.set_Z(1) ;
225  // sets the desired position of the visual feature
226  vpFeaturePoint pd ;
227  pd.buildFrom(0,0,1) ;
228 
229  // Define the task
230  // - we want an eye-in-hand control law
231  // - articular velocity are computed
234 
236  robot.get_cVe(cVe) ;
237  std::cout << cVe <<std::endl ;
238  task.set_cVe(cVe) ;
239 
240  // - Set the Jacobian (expressed in the end-effector frame)") ;
241  vpMatrix eJe ;
242  robot.get_eJe(eJe) ;
243  task.set_eJe(eJe) ;
244 
245  // - we want to see a point on a point..") ;
246  std::cout << std::endl ;
247  task.addFeature(p,pd) ;
248 
249  // - set the gain
250  double lambda = 0.8;
251  // set to -1 to suppress the lambda used in the vpServo::computeControlLaw()
252  task.setLambda(-1) ;
253 
254  // Display task information " ) ;
255  task.print() ;
256 
258 
259  int iter = 0;
260  double t_0, t_1 = vpTime::measureTimeMs(), Tv;
261  dc1394video_frame_t *frame = NULL;
262 
263  std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
264  for ( ; ; ) {
265  iter ++;
266 
267  t_0 = vpTime::measureTimeMs(); // t_0: current time
268 
269  // Update loop time in second
270  Tv = (double)(t_0 - t_1) / 1000.0;
271  std::cout << "Tv: " << Tv << std::endl;
272 
273  // Update time for next iteration
274  t_1 = t_0;
275 
276  // Acquire a new image from the camera
277  frame = g.dequeue(I);
278 
279  // Display this image
280  vpDisplay::display(I) ;
281 
282  // Achieve the tracking of the dot in the image
283  dot.track(I) ;
284  cog = dot.getCog();
285 
286  // Display a green cross at the center of gravity position in the image
288 
289  // Get the measured joint positions of the robot
291 
292  // Update the point feature from the dot location
293  vpFeatureBuilder::create(p, cam, dot);
294 
295  // Get the jacobian of the robot
296  robot.get_eJe(eJe) ;
297  // Update this jacobian in the task structure. It will be used to compute
298  // the velocity skew (as an articular velocity)
299  // qdot = -lambda * L^+ * cVe * eJe * (s-s*)
300  task.set_eJe(eJe) ;
301 
302  vpColVector prim_task ;
303  vpColVector e2(6) ;
304  // Compute the visual servoing skew vector
305  prim_task = task.computeControlLaw() ;
306 
307  vpColVector qpre(6);
308 
309  qpre = q ;
310  qpre += -lambda*prim_task*(4*Tloop) ;
311 
312  // Identify the joints near the limits
313  vpColVector pb(6) ; pb = 0 ;
314  unsigned int npb =0 ;
315  for (unsigned int i=0 ; i < 6 ;i++) {
316  if (q[i] < tQmin[i])
317  if (fabs(Qmin[i]-q[i]) > fabs(Qmin[i]-qpre[i])) {
318  pb[i] = 1 ; npb++ ;
319  std::cout << "Joint " << i << " near limit " << std::endl ;
320  }
321  if (q[i]>tQmax[i]) {
322  if (fabs(Qmax[i]-q[i]) > fabs(Qmax[i]-qpre[i])) {
323  pb[i] = 1 ; npb++ ;
324  std::cout << "Joint " << i << " near limit " << std::endl ;
325  }
326  }
327  }
328 
329  vpColVector a0 ;
330  vpMatrix J1 = task.getTaskJacobian();
331  vpMatrix kernelJ1;
332  J1.kernel(kernelJ1);
333 
334  unsigned int dimKernelL = kernelJ1.getCols() ;
335  if (npb != 0) {
336  // Build linear system a0*E = S
337  vpMatrix E(npb, dimKernelL) ;
338  vpColVector S(npb) ;
339 
340  unsigned int k=0 ;
341 
342  for (unsigned int j=0 ; j < 6 ; j++) // j is the joint
343  //if (pb[j]==1) {
344  if (std::fabs(pb[j]-1) <= std::numeric_limits<double>::epsilon()) {
345  for (unsigned int i=0 ; i < dimKernelL ; i++)
346  E[k][i] = kernelJ1[j][i] ;
347 
348  S[k] = -prim_task[j] ;
349  k++ ;
350  }
351  vpMatrix Ep ;
352  //vpTRACE("nbp %d", npb);
353  Ep = E.t()*(E*E.t()).pseudoInverse() ;
354  a0 = Ep*S ;
355 
356  e2 = (kernelJ1*a0) ;
357  //cout << "e2 " << e2.t() ;
358  }
359  else {
360  e2 = 0;
361  }
362  // std::cout << "e2: " << e2.t() << std::endl;
363 
364  vpColVector v ;
365  v = -lambda * (prim_task + e2);
366 
367  // Display the current and desired feature points in the image display
368  vpServoDisplay::display(task, cam, I) ;
369 
370  // Apply the computed joint velocities to the robot
372 
373  {
374  // Add the material to plot curves
375 
376  // q normalized between (entre -1 et 1)
377  for (unsigned int i=0 ; i < 6 ; i++) {
378  data[i] = (q[i] - Qmiddle[i]) ;
379  data[i] /= (Qmax[i] - Qmin[i]) ;
380  data[i]*=2 ;
381  }
382  unsigned int joint = 2;
383  data[6] = 2*(tQmin[joint]-Qmiddle[joint])/(Qmax[joint] - Qmin[joint]) ;
384  data[7] = 2*(tQmax[joint]-Qmiddle[joint])/(Qmax[joint] - Qmin[joint]) ;
385  data[8] = -1 ; data[9] = 1 ;
386 
387  plot.plot(0, iter, data); // plot q, Qmin, Qmax, tQmin, tQmax
388  plot.plot(1, iter, v); // plot joint velocities applied to the robot
389  }
390 
391  vpDisplay::flush(I) ;
392 
393  // Synchronize the loop with the image frame rate
394  vpTime::wait(t_0, 1000.*Tloop);
395  // Release the ring buffer used for the last image to start a new acq
396  g.enqueue(frame);
397  }
398 
399  // Display task information
400  task.print() ;
401  task.kill();
402  return 0;
403  }
404  catch (...)
405  {
406  vpERROR_TRACE(" Test failed") ;
407  return 0;
408  }
409 }
410 
411 
412 #else
413 int
414 main()
415 {
416  vpERROR_TRACE("You do not have an afma6 robot or a firewire framegrabber connected to your computer...");
417 }
418 #endif
void getPosition(const vpRobot::vpControlFrameType frame, vpColVector &position)
Definition of the vpMatrix class.
Definition: vpMatrix.h:96
unsigned int kernel(vpMatrix &KerA, double svThreshold=1e-6)
Definition: vpMatrix.cpp:3217
static void display(vpServo &s, const vpCameraParameters &cam, vpImage< unsigned char > &I, vpColor currentColor=vpColor::green, vpColor desiredColor=vpColor::red, unsigned int thickness=1)
vpMatrix getTaskJacobian() const
Definition: vpServo.h:357
#define vpERROR_TRACE
Definition: vpDebug.h:379
Control of Irisa's Viper S850 robot named Viper850.
static const vpColor black
Definition: vpColor.h:161
Class to define colors available for display functionnalities.
Definition: vpColor.h:125
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
vpRobot::vpRobotStateType setRobotState(vpRobot::vpRobotStateType newState)
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
static double measureTimeMs()
Definition: vpTime.cpp:86
void get_eJe(vpMatrix &eJe)
vpColVector getJointMin()
Definition: vpViper.cpp:1219
static int wait(double t0, double t)
Definition: vpTime.cpp:149
static const vpColor green
Definition: vpColor.h:170
This tracker is meant to track a blob (connex pixels with same gray level) on a vpImage.
Definition: vpDot2.h:131
void track(const vpImage< unsigned char > &I)
Definition: vpDot2.cpp:444
void set_cVe(vpVelocityTwistMatrix &_cVe)
Definition: vpServo.h:230
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1991
static const vpColor red
Definition: vpColor.h:167
static const vpColor orange
Definition: vpColor.h:177
vpImagePoint getCog() const
Definition: vpDot2.h:167
void kill()
destruction (memory deallocation if required)
Definition: vpServo.cpp:177
Initialize the velocity controller.
Definition: vpRobot.h:70
static const vpColor cyan
Definition: vpColor.h:176
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
The vpDisplayOpenCV allows to display image using the opencv library.
virtual void displayCross(const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)=0
Generic class defining intrinsic camera parameters.
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...
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 setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &velocity)
void buildFrom(const double x, const double y, const double Z)
vpColVector getJointMax()
Definition: vpViper.cpp:1232
vpMatrix t() const
Definition: vpMatrix.cpp:1176
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 getCameraParameters(vpCameraParameters &cam, const unsigned int &image_width, const unsigned int &image_height)
Definition: vpViper850.cpp:575
void get_cVe(vpVelocityTwistMatrix &cVe)
unsigned int getCols() const
Return the number of columns of the matrix.
Definition: vpMatrix.h:159
void initTracking(const vpImage< unsigned char > &I, unsigned int size=0)
Definition: vpDot2.cpp:245
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:258
This class enables real time drawing of 2D or 3D graphics. An instance of the class open a window whi...
Definition: vpPlot.h:117
Class for firewire ieee1394 video devices using libdc1394-2.x api.
void set_Z(const double Z)
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:92
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 setServo(vpServoType _servo_type)
Choice of the visual servoing control law.
Definition: vpServo.cpp:214
static const vpColor blue
Definition: vpColor.h:173