ViSP  3.0.0
servoViper850Point2DArtVelocity-jointAvoidance-large.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  * Giovanni Claudio
39  *
40  *****************************************************************************/
41 
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_2) && 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 Qmiddle(6);
114  vpColVector data(12) ;
115 
116  Qmiddle = (jointMin + jointMax) /2.;
117  // double rho1 = 0.1 ;
118 
119  double rho = 0.1;
120  double rho1 = 0.3;
121 
122  vpColVector q(6) ;
123 
124  // Create a window with two graphics
125  // - first graphic to plot q(t), Qmin, Qmax, Ql0min, Ql1min, Ql0max and Ql1max
126  vpPlot plot(2);
127 
128  // The first graphic contains 12 data to plot: q(t), Low Limits, Upper Limits, ql0min, ql1min, ql0max and ql1max
129  plot.initGraph(0, 12);
130  // The second graphic contains the values of the secondaty task velocities
131  plot.initGraph(1, 6);
132 
133  // For the first graphic :
134  // - along the x axis the expected values are between 0 and 200
135  // - along the y axis the expected values are between -1.2 and 1.2
136  plot.initRange(0, 0., 200., -1.2, 1.2);
137  plot.setTitle(0, "Joint behavior");
138 
139  // For the second graphic :
140  plot.setTitle(1, "Q secondary task");
141 
142  // For the first and second graphic, set the curves legend
143  char legend[10];
144  for (unsigned int i=0; i < 6; i++) {
145  sprintf(legend, "q%d", i+1);
146  plot.setLegend(0, i, legend);
147  plot.setLegend(1, i, legend);
148  }
149  plot.setLegend(0, 6, "Low Limit");
150  plot.setLegend(0, 7, "Upper Limit");
151  plot.setLegend(0, 8, "ql0 min");
152  plot.setLegend(0, 9, "ql0 max");
153  plot.setLegend(0, 10, "ql1 min");
154  plot.setLegend(0, 11, "ql1 max");
155 
156  // Set the curves color
157  plot.setColor(0, 0, vpColor::red);
158  plot.setColor(0, 1, vpColor::green);
159  plot.setColor(0, 2, vpColor::blue);
160  plot.setColor(0, 3, vpColor::orange);
161  plot.setColor(0, 4, vpColor(0, 128, 0));
162  plot.setColor(0, 5, vpColor::cyan);
163  for (unsigned int i= 6; i < 12; i++)
164  plot.setColor(0, i, vpColor::black); // for Q and tQ [min,max]
165 
166  vpColVector sec_task(6) ;
167 
168  vpDot2 dot ;
169 
170  std::cout << "Click on a dot..." << std::endl;
171  dot.initTracking(I) ;
172  vpImagePoint cog = dot.getCog();
174  vpDisplay::flush(I);
175 
176  vpCameraParameters cam ;
177  // Update camera parameters
178  robot.getCameraParameters (cam, I);
179 
180  // sets the current position of the visual feature
181  vpFeaturePoint p ;
182  vpFeatureBuilder::create(p,cam, dot) ; //retrieve x,y and Z of the vpPoint structure
183 
184  p.set_Z(1) ;
185  // sets the desired position of the visual feature
186  vpFeaturePoint pd ;
187  pd.buildFrom(0,0,1) ;
188 
189  // Define the task
190  // - we want an eye-in-hand control law
191  // - articular velocity are computed
194 
196  robot.get_cVe(cVe) ;
197  std::cout << cVe <<std::endl ;
198  task.set_cVe(cVe) ;
199 
200  // - Set the Jacobian (expressed in the end-effector frame)") ;
201  vpMatrix eJe ;
202  robot.get_eJe(eJe) ;
203  task.set_eJe(eJe) ;
204 
205  // - we want to see a point on a point..") ;
206  std::cout << std::endl ;
207  task.addFeature(p,pd) ;
208 
209  // - set the gain
210  task.setLambda(0.8) ;
211 
212  // Display task information " ) ;
213  task.print() ;
214 
216 
217  int iter = 0;
218  std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
219  for ( ; ; ) {
220  iter ++;
221  // Acquire a new image from the camera
222  g.acquire(I) ;
223 
224  // Display this image
225  vpDisplay::display(I) ;
226 
227  // Achieve the tracking of the dot in the image
228  dot.track(I) ;
229  cog = dot.getCog();
230 
231  // Display a green cross at the center of gravity position in the image
233 
234  // Get the measured joint positions of the robot
236 
237  // Update the point feature from the dot location
238  vpFeatureBuilder::create(p, cam, dot);
239 
240  // Get the jacobian of the robot
241  robot.get_eJe(eJe) ;
242  // Update this jacobian in the task structure. It will be used to compute
243  // the velocity skew (as an articular velocity)
244  // qdot = -lambda * L^+ * cVe * eJe * (s-s*)
245  task.set_eJe(eJe) ;
246 
247  vpColVector prim_task ;
248  // Compute the visual servoing skew vector
249  prim_task = task.computeControlLaw() ;
250 
251  // Compute the secondary task for the joint limit avoidance
252  sec_task = task.secondaryTaskJointLimitAvoidance(q, prim_task, jointMin, jointMax, rho, rho1);
253 
254  vpColVector v ;
255  v = prim_task + sec_task;
256 
257  // Display the current and desired feature points in the image display
258  vpServoDisplay::display(task, cam, I) ;
259 
260  // Apply the computed joint velocities to the robot
262 
263  {
264  // Add the material to plot curves
265 
266  // q normalized between (entre -1 et 1)
267  for (unsigned int i=0 ; i < 6 ; i++) {
268  data[i] = (q[i] - Qmiddle[i]) ;
269  data[i] /= (jointMax[i] - jointMin[i]) ;
270  data[i]*=2 ;
271  }
272 
273  data[6] = -1.0;
274  data[7] = 1.0;
275 
276  unsigned int joint = 2;
277  double tQmin_l0 = jointMin[joint] + rho *(jointMax[joint] - jointMin[joint]);
278  double tQmax_l0 = jointMax[joint] - rho *(jointMax[joint] - jointMin[joint]);
279 
280  double tQmin_l1 = tQmin_l0 - rho * rho1 * (jointMax[joint] - jointMin[joint]);
281  double tQmax_l1 = tQmax_l0 + rho * rho1 * (jointMax[joint] - jointMin[joint]);
282 
283  data[8] = 2*(tQmin_l0 - Qmiddle[joint])/(jointMax[joint] - jointMin[joint]);
284  data[9] = 2*(tQmax_l0 - Qmiddle[joint])/(jointMax[joint] - jointMin[joint]);
285  data[10] = 2*(tQmin_l1 - Qmiddle[joint])/(jointMax[joint] - jointMin[joint]);
286  data[11] = 2*(tQmax_l1 - Qmiddle[joint])/(jointMax[joint] - jointMin[joint]);
287  plot.plot(0, iter, data); // plot q(t), Low Limits, Upper Limits, ql0min, ql1min, ql0max and ql1max
288  plot.plot(1, iter, sec_task); //plot secondary task velocities
289  }
290 
291  vpDisplay::flush(I) ;
292  }
293 
294  // Display task information
295  task.print() ;
296  task.kill();
297  return 0;
298  }
299  catch (...)
300  {
301  vpERROR_TRACE(" Test failed") ;
302  return 0;
303  }
304 }
305 
306 
307 #else
308 int
309 main()
310 {
311  vpERROR_TRACE("You do not have an afma6 robot or a firewire framegrabber connected to your computer...");
312 }
313 #endif
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'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...
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
void get_cVe(vpVelocityTwistMatrix &cVe) const
vpColVector secondaryTaskJointLimitAvoidance(const vpColVector &q, const vpColVector &dq, const vpColVector &jointMin, const vpColVector &jointMax, const double &rho=0.1, const double &rho1=0.3, const double &lambda_tune=0.7) const
Definition: vpServo.cpp:1629
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