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