ViSP  3.0.0
servoViper850Point2DArtVelocity-jointAvoidance-basic.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 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%d", i+1);
174  plot.setLegend(0, i, legend);
175  sprintf(legend, "q%d", 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_0, t_1 = vpTime::measureTimeMs(), Tv;
254  dc1394video_frame_t *frame = NULL;
255 
256  std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
257  for ( ; ; ) {
258  iter ++;
259 
260  t_0 = vpTime::measureTimeMs(); // t_0: current time
261 
262  // Update loop time in second
263  Tv = (double)(t_0 - t_1) / 1000.0;
264  std::cout << "Tv: " << Tv << std::endl;
265 
266  // Update time for next iteration
267  t_1 = t_0;
268 
269  // Acquire a new image from the camera
270  frame = g.dequeue(I);
271 
272  // Display this image
273  vpDisplay::display(I) ;
274 
275  // Achieve the tracking of the dot in the image
276  dot.track(I) ;
277  cog = dot.getCog();
278 
279  // Display a green cross at the center of gravity position in the image
281 
282  // Get the measured joint positions of the robot
284 
285  // Update the point feature from the dot location
286  vpFeatureBuilder::create(p, cam, dot);
287 
288  // Get the jacobian of the robot
289  robot.get_eJe(eJe) ;
290  // Update this jacobian in the task structure. It will be used to compute
291  // the velocity skew (as an articular velocity)
292  // qdot = -lambda * L^+ * cVe * eJe * (s-s*)
293  task.set_eJe(eJe) ;
294 
295  vpColVector prim_task ;
296  vpColVector e2(6) ;
297  // Compute the visual servoing skew vector
298  prim_task = task.computeControlLaw() ;
299 
300  vpColVector qpre(6);
301 
302  qpre = q ;
303  qpre += -lambda*prim_task*(4*Tloop) ;
304 
305  // Identify the joints near the limits
306  vpColVector pb(6) ; pb = 0 ;
307  unsigned int npb =0 ;
308  for (unsigned int i=0 ; i < 6 ;i++) {
309  if (q[i] < tQmin[i])
310  if (fabs(Qmin[i]-q[i]) > fabs(Qmin[i]-qpre[i])) {
311  pb[i] = 1 ; npb++ ;
312  std::cout << "Joint " << i << " near limit " << std::endl ;
313  }
314  if (q[i]>tQmax[i]) {
315  if (fabs(Qmax[i]-q[i]) > fabs(Qmax[i]-qpre[i])) {
316  pb[i] = 1 ; npb++ ;
317  std::cout << "Joint " << i << " near limit " << std::endl ;
318  }
319  }
320  }
321 
322  vpColVector a0 ;
323  vpMatrix J1 = task.getTaskJacobian();
324  vpMatrix kernelJ1;
325  J1.kernel(kernelJ1);
326 
327  unsigned int dimKernelL = kernelJ1.getCols() ;
328  if (npb != 0) {
329  // Build linear system a0*E = S
330  vpMatrix E(npb, dimKernelL) ;
331  vpColVector S(npb) ;
332 
333  unsigned int k=0 ;
334 
335  for (unsigned int j=0 ; j < 6 ; j++) // j is the joint
336  //if (pb[j]==1) {
337  if (std::fabs(pb[j]-1) <= std::numeric_limits<double>::epsilon()) {
338  for (unsigned int i=0 ; i < dimKernelL ; i++)
339  E[k][i] = kernelJ1[j][i] ;
340 
341  S[k] = -prim_task[j] ;
342  k++ ;
343  }
344  vpMatrix Ep ;
345  //vpTRACE("nbp %d", npb);
346  Ep = E.t()*(E*E.t()).pseudoInverse() ;
347  a0 = Ep*S ;
348 
349  e2 = (kernelJ1*a0) ;
350  //cout << "e2 " << e2.t() ;
351  }
352  else {
353  e2 = 0;
354  }
355  // std::cout << "e2: " << e2.t() << std::endl;
356 
357  vpColVector v ;
358  v = -lambda * (prim_task + e2);
359 
360  // Display the current and desired feature points in the image display
361  vpServoDisplay::display(task, cam, I) ;
362 
363  // Apply the computed joint velocities to the robot
365 
366  {
367  // Add the material to plot curves
368 
369  // q normalized between (entre -1 et 1)
370  for (unsigned int i=0 ; i < 6 ; i++) {
371  data[i] = (q[i] - Qmiddle[i]) ;
372  data[i] /= (Qmax[i] - Qmin[i]) ;
373  data[i]*=2 ;
374  }
375  unsigned int joint = 2;
376  data[6] = 2*(tQmin[joint]-Qmiddle[joint])/(Qmax[joint] - Qmin[joint]) ;
377  data[7] = 2*(tQmax[joint]-Qmiddle[joint])/(Qmax[joint] - Qmin[joint]) ;
378  data[8] = -1 ; data[9] = 1 ;
379 
380  plot.plot(0, iter, data); // plot q, Qmin, Qmax, tQmin, tQmax
381  plot.plot(1, iter, v); // plot joint velocities applied to the robot
382  }
383 
384  vpDisplay::flush(I) ;
385 
386  // Synchronize the loop with the image frame rate
387  vpTime::wait(t_0, 1000.*Tloop);
388  // Release the ring buffer used for the last image to start a new acq
389  g.enqueue(frame);
390  }
391 
392  // Display task information
393  task.print() ;
394  task.kill();
395  return 0;
396  }
397  catch (...)
398  {
399  vpERROR_TRACE(" Test failed") ;
400  return 0;
401  }
402 }
403 
404 
405 #else
406 int
407 main()
408 {
409  vpERROR_TRACE("You do not have an afma6 robot or a firewire framegrabber connected to your computer...");
410 }
411 #endif
void getPosition(const vpRobot::vpControlFrameType frame, vpColVector &position)
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:92
VISP_EXPORT int wait(double t0, double t)
Definition: vpTime.cpp:150
unsigned int kernel(vpMatrix &KerA, double svThreshold=1e-6) const
Definition: vpMatrix.cpp:3201
vpMatrix getTaskJacobian() const
Definition: vpServo.cpp:1759
void getCameraParameters(vpCameraParameters &cam, const unsigned int &image_width, const unsigned int &image_height) const
Definition: vpViper850.cpp:584
Control of Irisa'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
void set_eJe(const vpMatrix &eJe_)
Definition: vpServo.h:459
Define the X11 console to display images.
Definition: vpDisplayX.h:148
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, const unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:446
vpRobot::vpRobotStateType setRobotState(vpRobot::vpRobotStateType newState)
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
unsigned int getCols() const
Return the number of columns of the 2D array.
Definition: vpArray2D.h:154
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:124
void track(const vpImage< unsigned char > &I)
Definition: vpDot2.cpp:461
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:2233
static const vpColor red
Definition: vpColor.h:163
static const vpColor orange
Definition: vpColor.h:173
vpImagePoint getCog() const
Definition: vpDot2.h:160
void kill()
Definition: vpServo.cpp:186
Initialize the velocity controller.
Definition: vpRobot.h:68
static const vpColor cyan
Definition: vpColor.h:172
vpColVector computeControlLaw()
Definition: vpServo.cpp:899
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:390
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
Definition: vpDisplayGTK.h:141
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:519
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:1215
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:93
void get_cVe(vpVelocityTwistMatrix &cVe) const
vpMatrix t() const
Definition: vpMatrix.cpp:221
vpColVector getJointMax() const
Definition: vpViper.cpp:1228
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
void set_cVe(const vpVelocityTwistMatrix &cVe_)
Definition: vpServo.h:434
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:248
This class enables real time drawing of 2D or 3D graphics. An instance of the class open a window whi...
Definition: vpPlot.h:113
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:217
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:169