Visual Servoing Platform  version 3.3.0 under development (2020-02-17)
servoViper850Point2DArtVelocity-jointAvoidance-large.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * tests the control law
33  * eye-in-hand control
34  * velocity computed in articular
35  *
36  * Authors:
37  * Eric Marchand
38  * Fabien Spindler
39  * Giovanni Claudio
40  *
41  *****************************************************************************/
42 
51 #include <visp3/core/vpConfig.h>
52 #include <visp3/core/vpDebug.h> // Debug trace
53 
54 #include <fstream>
55 #include <iostream>
56 #include <sstream>
57 #include <stdio.h>
58 #include <stdlib.h>
59 
60 #if (defined(VISP_HAVE_VIPER850) && defined(VISP_HAVE_DC1394_2) && defined(VISP_HAVE_DISPLAY))
61 
62 #include <visp3/blob/vpDot2.h>
63 #include <visp3/core/vpDisplay.h>
64 #include <visp3/core/vpException.h>
65 #include <visp3/core/vpHomogeneousMatrix.h>
66 #include <visp3/core/vpImage.h>
67 #include <visp3/core/vpIoTools.h>
68 #include <visp3/core/vpMath.h>
69 #include <visp3/core/vpPoint.h>
70 #include <visp3/gui/vpDisplayGTK.h>
71 #include <visp3/gui/vpDisplayOpenCV.h>
72 #include <visp3/gui/vpDisplayX.h>
73 #include <visp3/gui/vpPlot.h>
74 #include <visp3/robot/vpRobotViper850.h>
75 #include <visp3/sensor/vp1394TwoGrabber.h>
76 #include <visp3/visual_features/vpFeatureBuilder.h>
77 #include <visp3/visual_features/vpFeaturePoint.h>
78 #include <visp3/vs/vpServo.h>
79 #include <visp3/vs/vpServoDisplay.h>
80 
81 int 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 
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
126  // Ql1max
127  vpPlot plot(2);
128 
129  // The first graphic contains 12 data to plot: q(t), Low Limits, Upper
130  // Limits, ql0min, ql1min, ql0max and ql1max
131  plot.initGraph(0, 12);
132  // The second graphic contains the values of the secondaty task velocities
133  plot.initGraph(1, 6);
134 
135  // For the first graphic :
136  // - along the x axis the expected values are between 0 and 200
137  // - along the y axis the expected values are between -1.2 and 1.2
138  plot.initRange(0, 0., 200., -1.2, 1.2);
139  plot.setTitle(0, "Joint behavior");
140 
141  // For the second graphic :
142  plot.setTitle(1, "Q secondary task");
143 
144  // For the first and second graphic, set the curves legend
145  char legend[10];
146  for (unsigned int i = 0; i < 6; i++) {
147  sprintf(legend, "q%u", i + 1);
148  plot.setLegend(0, i, legend);
149  plot.setLegend(1, i, legend);
150  }
151  plot.setLegend(0, 6, "Low Limit");
152  plot.setLegend(0, 7, "Upper Limit");
153  plot.setLegend(0, 8, "ql0 min");
154  plot.setLegend(0, 9, "ql0 max");
155  plot.setLegend(0, 10, "ql1 min");
156  plot.setLegend(0, 11, "ql1 max");
157 
158  // Set the curves color
159  plot.setColor(0, 0, vpColor::red);
160  plot.setColor(0, 1, vpColor::green);
161  plot.setColor(0, 2, vpColor::blue);
162  plot.setColor(0, 3, vpColor::orange);
163  plot.setColor(0, 4, vpColor(0, 128, 0));
164  plot.setColor(0, 5, vpColor::cyan);
165  for (unsigned int i = 6; i < 12; i++)
166  plot.setColor(0, i, vpColor::black); // for Q and tQ [min,max]
167 
168  vpColVector sec_task(6);
169 
170  vpDot2 dot;
171 
172  std::cout << "Click on a dot..." << std::endl;
173  dot.initTracking(I);
174  vpImagePoint cog = dot.getCog();
176  vpDisplay::flush(I);
177 
178  vpCameraParameters cam;
179  // Update camera parameters
180  robot.getCameraParameters(cam, I);
181 
182  // sets the current position of the visual feature
183  vpFeaturePoint p;
184  vpFeatureBuilder::create(p, cam, dot); // retrieve x,y and Z of the vpPoint structure
185 
186  p.set_Z(1);
187  // sets the desired position of the visual feature
188  vpFeaturePoint pd;
189  pd.buildFrom(0, 0, 1);
190 
191  // Define the task
192  // - we want an eye-in-hand control law
193  // - articular velocity are computed
196 
198  robot.get_cVe(cVe);
199  std::cout << cVe << std::endl;
200  task.set_cVe(cVe);
201 
202  // - Set the Jacobian (expressed in the end-effector frame)") ;
203  vpMatrix eJe;
204  robot.get_eJe(eJe);
205  task.set_eJe(eJe);
206 
207  // - we want to see a point on a point..") ;
208  std::cout << std::endl;
209  task.addFeature(p, pd);
210 
211  // - set the gain
212  task.setLambda(0.8);
213 
214  // Display task information " ) ;
215  task.print();
216 
218 
219  int iter = 0;
220  std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
221  for (;;) {
222  iter++;
223  // Acquire a new image from the camera
224  g.acquire(I);
225 
226  // Display this image
228 
229  // Achieve the tracking of the dot in the image
230  dot.track(I);
231  cog = dot.getCog();
232 
233  // Display a green cross at the center of gravity position in the image
235 
236  // Get the measured joint positions of the robot
238 
239  // Update the point feature from the dot location
240  vpFeatureBuilder::create(p, cam, dot);
241 
242  // Get the jacobian of the robot
243  robot.get_eJe(eJe);
244  // Update this jacobian in the task structure. It will be used to
245  // compute the velocity skew (as an articular velocity) qdot = -lambda *
246  // L^+ * cVe * eJe * (s-s*)
247  task.set_eJe(eJe);
248 
249  vpColVector prim_task;
250  // Compute the visual servoing skew vector
251  prim_task = task.computeControlLaw();
252 
253  // Compute the secondary task for the joint limit avoidance
254  sec_task = task.secondaryTaskJointLimitAvoidance(q, prim_task, jointMin, jointMax, rho, rho1);
255 
256  vpColVector v;
257  v = prim_task + sec_task;
258 
259  // Display the current and desired feature points in the image display
260  vpServoDisplay::display(task, cam, I);
261 
262  // Apply the computed joint velocities to the robot
264 
265  {
266  // Add the material to plot curves
267 
268  // q normalized between (entre -1 et 1)
269  for (unsigned int i = 0; i < 6; i++) {
270  data[i] = (q[i] - Qmiddle[i]);
271  data[i] /= (jointMax[i] - jointMin[i]);
272  data[i] *= 2;
273  }
274 
275  data[6] = -1.0;
276  data[7] = 1.0;
277 
278  unsigned int joint = 2;
279  double tQmin_l0 = jointMin[joint] + rho * (jointMax[joint] - jointMin[joint]);
280  double tQmax_l0 = jointMax[joint] - rho * (jointMax[joint] - jointMin[joint]);
281 
282  double tQmin_l1 = tQmin_l0 - rho * rho1 * (jointMax[joint] - jointMin[joint]);
283  double tQmax_l1 = tQmax_l0 + rho * rho1 * (jointMax[joint] - jointMin[joint]);
284 
285  data[8] = 2 * (tQmin_l0 - Qmiddle[joint]) / (jointMax[joint] - jointMin[joint]);
286  data[9] = 2 * (tQmax_l0 - Qmiddle[joint]) / (jointMax[joint] - jointMin[joint]);
287  data[10] = 2 * (tQmin_l1 - Qmiddle[joint]) / (jointMax[joint] - jointMin[joint]);
288  data[11] = 2 * (tQmax_l1 - Qmiddle[joint]) / (jointMax[joint] - jointMin[joint]);
289  plot.plot(0, iter, data); // plot q(t), Low Limits, Upper Limits,
290  // ql0min, ql1min, ql0max and ql1max
291  plot.plot(1, iter, sec_task); // plot secondary task velocities
292  }
293 
294  vpDisplay::flush(I);
295  }
296 
297  // Display task information
298  task.print();
299  task.kill();
300  return EXIT_SUCCESS;
301  }
302  catch (const vpException &e) {
303  std::cout << "Catch an exception: " << e.getMessage() << std::endl;
304  return EXIT_FAILURE;
305  }
306 }
307 
308 #else
309 int main()
310 {
311  std::cout << "You do not have an Viper 850 robot connected to your computer..." << std::endl;
312  return EXIT_SUCCESS;
313 }
314 #endif
void getPosition(const vpRobot::vpControlFrameType frame, vpColVector &position)
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:164
void buildFrom(double x, double y, double Z)
void getCameraParameters(vpCameraParameters &cam, const unsigned int &image_width, const unsigned int &image_height) const
Definition: vpViper850.cpp:539
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:497
static const vpColor black
Definition: vpColor.h:173
Class to define colors available for display functionnalities.
Definition: vpColor.h:119
vpColVector getJointMin() const
Definition: vpViper.cpp:1199
void set_eJe(const vpMatrix &eJe_)
Definition: vpServo.h:508
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:150
vpRobot::vpRobotStateType setRobotState(vpRobot::vpRobotStateType newState)
error that can be emited by ViSP classes.
Definition: vpException.h:71
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
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:1667
void get_eJe(vpMatrix &eJe)
static const vpColor green
Definition: vpColor.h:182
This tracker is meant to track a blob (connex pixels with same gray level) on a vpImage.
Definition: vpDot2.h:126
static void flush(const vpImage< unsigned char > &I)
static const vpColor red
Definition: vpColor.h:179
static const vpColor orange
Definition: vpColor.h:189
void kill()
Definition: vpServo.cpp:192
Initialize the velocity controller.
Definition: vpRobot.h:66
static const vpColor cyan
Definition: vpColor.h:188
vpColVector computeControlLaw()
Definition: vpServo.cpp:935
void set_Z(double Z)
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Generic class defining intrinsic camera parameters.
void setLambda(double c)
Definition: vpServo.h:406
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:137
void track(const vpImage< unsigned char > &I, bool canMakeTheWindowGrow=true)
Definition: vpDot2.cpp:441
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:574
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &velocity)
const char * getMessage(void) const
Definition: vpException.cpp:90
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
void get_cVe(vpVelocityTwistMatrix &cVe) const
Implementation of column vector and the associated operations.
Definition: vpColVector.h:130
void set_cVe(const vpVelocityTwistMatrix &cVe_)
Definition: vpServo.h:450
void initTracking(const vpImage< unsigned char > &I, unsigned int size=0)
Definition: vpDot2.cpp:253
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:313
This class enables real time drawing of 2D or 3D graphics. An instance of the class open a window whi...
Definition: vpPlot.h:115
vpImagePoint getCog() const
Definition: vpDot2.h:161
Class for firewire ieee1394 video devices using libdc1394-2.x api.
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:223
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)
vpColVector getJointMax() const
Definition: vpViper.cpp:1208
static const vpColor blue
Definition: vpColor.h:185