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