ViSP  2.9.0
servoViper850Point2DArtVelocity-jointAvoidance-gpa.cpp
1 /****************************************************************************
2  *
3  * $Id: servoViper850Point2DArtVelocity-jointAvoidance-gpa.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 
53 #include <visp/vpConfig.h>
54 #include <visp/vpDebug.h> // Debug trace
55 
56 #include <stdlib.h>
57 #include <stdio.h>
58 #include <iostream>
59 #include <fstream>
60 #include <sstream>
61 
62 #if (defined (VISP_HAVE_VIPER850) && defined (VISP_HAVE_DC1394_2))
63 
64 #include <visp/vp1394TwoGrabber.h>
65 #include <visp/vpImage.h>
66 #include <visp/vpDisplay.h>
67 #include <visp/vpDisplayX.h>
68 #include <visp/vpDisplayOpenCV.h>
69 #include <visp/vpDisplayGTK.h>
70 #include <visp/vpMath.h>
71 #include <visp/vpHomogeneousMatrix.h>
72 #include <visp/vpFeaturePoint.h>
73 #include <visp/vpPoint.h>
74 #include <visp/vpServo.h>
75 #include <visp/vpFeatureBuilder.h>
76 #include <visp/vpRobotViper850.h>
77 #include <visp/vpIoTools.h>
78 #include <visp/vpException.h>
79 #include <visp/vpMatrixException.h>
80 #include <visp/vpServoDisplay.h>
81 #include <visp/vpDot2.h>
82 #include <visp/vpPlot.h>
83 
84 
85 int
86 main()
87 {
88  try {
89  vpRobotViper850 robot ;
90 
91  vpServo task ;
92 
94 
95  bool reset = false;
96  vp1394TwoGrabber g(reset);
98  g.setFramerate(vp1394TwoGrabber::vpFRAMERATE_60);
99  g.open(I) ;
100 
101  g.acquire(I) ;
102 
103 #ifdef VISP_HAVE_X11
104  vpDisplayX display(I,800,100,"Current image") ;
105 #elif defined(VISP_HAVE_OPENCV)
106  vpDisplayOpenCV display(I,800,100,"Current image") ;
107 #elif defined(VISP_HAVE_GTK)
108  vpDisplayGTK display(I,800,100,"Current image") ;
109 #endif
110 
111  vpDisplay::display(I) ;
112  vpDisplay::flush(I) ;
113 
114  vpColVector jointMin(6), jointMax(6) ;
115  jointMin = robot.getJointMin();
116  jointMax = robot.getJointMax();
117 
118  vpColVector Qmin(6), tQmin(6) ;
119  vpColVector Qmax(6), tQmax(6) ;
120  vpColVector Qmiddle(6);
121  vpColVector data(10) ;
122 
123  double rho = 0.15 ;
124  for (unsigned int i=0 ; i < 6 ; i++)
125  {
126  Qmin[i] = jointMin[i] + 0.5*rho*(jointMax[i]-jointMin[i]) ;
127  Qmax[i] = jointMax[i] - 0.5*rho*(jointMax[i]-jointMin[i]) ;
128  }
129  Qmiddle = (Qmin + Qmax) /2.;
130  double rho1 = 0.1 ;
131 
132  for (unsigned int i=0 ; i < 6 ; i++) {
133  tQmin[i]=Qmin[i]+ 0.5*(rho1)*(Qmax[i]-Qmin[i]) ;
134  tQmax[i]=Qmax[i]- 0.5*(rho1)*(Qmax[i]-Qmin[i]) ;
135  }
136 
137  vpColVector q(6) ;
138 
139  // Create a window with two graphics
140  // - first graphic to plot q(t), Qmin, Qmax, tQmin and tQmax
141  // - second graphic to plot the cost function h_s
142  vpPlot plot(2);
143 
144  // The first graphic contains 10 data to plot: q(t), Qmin, Qmax, tQmin and
145  // tQmax
146  plot.initGraph(0, 10);
147  // The second graphic contains 1 curve, the cost function h_s
148  plot.initGraph(1, 1);
149 
150 
151  // For the first graphic :
152  // - along the x axis the expected values are between 0 and 200
153  // - along the y axis the expected values are between -1.2 and 1.2
154  plot.initRange(0, 0., 200., -1.2, 1.2);
155  plot.setTitle(0, "Joint behavior");
156 
157  // For the second graphic :
158  // - along the x axis the expected values are between 0 and 200 and
159  // the step is 1
160  // - along the y axis the expected values are between 0 and 0.0001 and the
161  // step is 0.00001
162  plot.initRange(1, 0., 200., 0., 1e-4);
163  plot.setTitle(1, "Cost function");
164 
165  // For the first graphic, set the curves legend
166  char legend[10];
167  for (unsigned int i=0; i < 6; i++) {
168  sprintf(legend, "q%d", i+1);
169  plot.setLegend(0, i, legend);
170  }
171  plot.setLegend(0, 6, "tQmin");
172  plot.setLegend(0, 7, "tQmax");
173  plot.setLegend(0, 8, "Qmin");
174  plot.setLegend(0, 9, "Qmax");
175 
176  // Set the curves color
177  plot.setColor(0, 0, vpColor::red);
178  plot.setColor(0, 1, vpColor::green);
179  plot.setColor(0, 2, vpColor::blue);
180  plot.setColor(0, 3, vpColor::orange);
181  plot.setColor(0, 4, vpColor(0, 128, 0));
182  plot.setColor(0, 5, vpColor::cyan);
183  for (unsigned int i= 6; i < 10; i++)
184  plot.setColor(0, i, vpColor::black); // for Q and tQ [min,max]
185 
186  // For the second graphic, set the curves legend
187  plot.setLegend(1, 0, "h_s");
188 
189  double beta = 1;
190 
191  // Set the amplitude of the control law due to the secondary task
192  std::cout << " Give the parameters beta (1) : ";
193  std::cin >> beta ;
194 
195  vpDot2 dot ;
196 
197 
198  std::cout << "Click on a dot..." << std::endl;
199  dot.initTracking(I) ;
200  vpImagePoint cog = dot.getCog();
202  vpDisplay::flush(I);
203 
204  vpCameraParameters cam ;
205  // Update camera parameters
206  robot.getCameraParameters (cam, I);
207 
208  // sets the current position of the visual feature
209  vpFeaturePoint p ;
210  vpFeatureBuilder::create(p,cam, dot) ; //retrieve x,y and Z of the vpPoint structure
211 
212  p.set_Z(1) ;
213  // sets the desired position of the visual feature
214  vpFeaturePoint pd ;
215  pd.buildFrom(0,0,1) ;
216 
217  // Define the task
218  // - we want an eye-in-hand control law
219  // - articular velocity are computed
222 
224  robot.get_cVe(cVe) ;
225  std::cout << cVe <<std::endl ;
226  task.set_cVe(cVe) ;
227 
228  // - Set the Jacobian (expressed in the end-effector frame)") ;
229  vpMatrix eJe ;
230  robot.get_eJe(eJe) ;
231  task.set_eJe(eJe) ;
232 
233  // - we want to see a point on a point..") ;
234  std::cout << std::endl ;
235  task.addFeature(p,pd) ;
236 
237  // - set the gain
238  task.setLambda(0.8) ;
239 
240  // Display task information " ) ;
241  task.print() ;
242 
244 
245  int iter = 0;
246  std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
247  for ( ; ; ) {
248  iter ++;
249  // Acquire a new image from the camera
250  g.acquire(I) ;
251 
252  // Display this image
253  vpDisplay::display(I) ;
254 
255  // Achieve the tracking of the dot in the image
256  dot.track(I) ;
257  cog = dot.getCog();
258 
259  // Display a green cross at the center of gravity position in the image
261 
262  // Get the measured joint positions of the robot
264 
265  // Update the point feature from the dot location
266  vpFeatureBuilder::create(p, cam, dot);
267 
268  // Get the jacobian of the robot
269  robot.get_eJe(eJe) ;
270  // Update this jacobian in the task structure. It will be used to compute
271  // the velocity skew (as an articular velocity)
272  // qdot = -lambda * L^+ * cVe * eJe * (s-s*)
273  task.set_eJe(eJe) ;
274 
275  vpColVector prim_task ;
276  vpColVector e2(6) ;
277  // Compute the visual servoing skew vector
278  prim_task = task.computeControlLaw() ;
279 
280  vpColVector sec_task(6) ;
281  double h_s = 0 ;
282  {
283  // joint limit avoidance with secondary task
284 
285  vpColVector de2dt(6);
286  de2dt = 0 ;
287  e2 = 0 ;
288  for (unsigned int i=0 ; i < 6 ; i++)
289  {
290  double S = 0 ;
291  if (q[i] > tQmax[i]) S = q[i] - tQmax[i] ;
292  if (q[i] < tQmin[i]) S = q[i] - tQmin[i] ;
293  double D = (Qmax[i]-Qmin[i]) ;
294  h_s += vpMath::sqr(S)/D ;
295  e2[i] = S/D ;
296  }
297  h_s = beta*h_s/2.0 ; // cost function
298  e2 *= beta ;
299  // std::cout << e2.t() << std::endl;
300  std::cout << "Cost function h_s: " << h_s << std::endl;
301 
302  sec_task = task.secondaryTask(e2, de2dt) ;
303  }
304 
305  vpColVector v ;
306  v = prim_task + sec_task;
307 
308  // Display the current and desired feature points in the image display
309  vpServoDisplay::display(task, cam, I) ;
310 
311  // Apply the computed joint velocities to the robot
313 
314  {
315  // Add the material to plot curves
316 
317  // q normalized between (entre -1 et 1)
318  for (unsigned int i=0 ; i < 6 ; i++) {
319  data[i] = (q[i] - Qmiddle[i]) ;
320  data[i] /= (Qmax[i] - Qmin[i]) ;
321  data[i]*=2 ;
322  }
323  unsigned int joint = 2;
324  data[6] = 2*(tQmin[joint]-Qmiddle[joint])/(Qmax[joint] - Qmin[joint]) ;
325  data[7] = 2*(tQmax[joint]-Qmiddle[joint])/(Qmax[joint] - Qmin[joint]) ;
326  data[8] = -1 ; data[9] = 1 ;
327  plot.plot(0, iter, data); // plot q, Qmin, Qmax, tQmin, tQmax
328  plot.plot(1, 0, iter, h_s); // plot the cost function
329  }
330 
331  vpDisplay::flush(I) ;
332  }
333 
334  // Display task information
335  task.print() ;
336  task.kill();
337  return 0;
338  }
339  catch (...)
340  {
341  vpERROR_TRACE(" Test failed") ;
342  return 0;
343  }
344 }
345 
346 
347 #else
348 int
349 main()
350 {
351  vpERROR_TRACE("You do not have an afma6 robot or a firewire framegrabber connected to your computer...");
352 }
353 #endif
void getPosition(const vpRobot::vpControlFrameType frame, vpColVector &position)
Definition of the vpMatrix class.
Definition: vpMatrix.h:98
#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...
void get_eJe(vpMatrix &eJe)
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
vpColVector secondaryTask(const vpColVector &de2dt)
Definition: vpServo.cpp:1416
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 double sqr(double x)
Definition: vpMath.h:106
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
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
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