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