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