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