Visual Servoing Platform  version 3.4.0
servoAfma62DhalfCamVelocity.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 the camera frame
35  *
36  * Authors:
37  * Nicolas Melchior
38  *
39  *****************************************************************************/
62 #include <cmath> // std::fabs
63 #include <limits> // numeric_limits
64 #include <stdlib.h>
65 #include <visp3/core/vpConfig.h>
66 #include <visp3/core/vpDebug.h> // Debug trace
67 #if (defined(VISP_HAVE_AFMA6) && defined(VISP_HAVE_DC1394))
68 
69 #include <visp3/core/vpDisplay.h>
70 #include <visp3/core/vpImage.h>
71 #include <visp3/core/vpImagePoint.h>
72 #include <visp3/gui/vpDisplayGTK.h>
73 #include <visp3/gui/vpDisplayOpenCV.h>
74 #include <visp3/gui/vpDisplayX.h>
75 #include <visp3/io/vpImageIo.h>
76 #include <visp3/sensor/vp1394TwoGrabber.h>
77 
78 #include <visp3/core/vpHomogeneousMatrix.h>
79 #include <visp3/core/vpLine.h>
80 #include <visp3/core/vpMath.h>
81 #include <visp3/vision/vpPose.h>
82 #include <visp3/visual_features/vpFeatureBuilder.h>
83 #include <visp3/visual_features/vpFeatureDepth.h>
84 #include <visp3/visual_features/vpFeatureLine.h>
85 #include <visp3/visual_features/vpFeaturePoint.h>
86 #include <visp3/visual_features/vpGenericFeature.h>
87 #include <visp3/vs/vpServo.h>
88 
89 #include <visp3/robot/vpRobotAfma6.h>
90 
91 // Exception
92 #include <visp3/core/vpException.h>
93 #include <visp3/vs/vpServoDisplay.h>
94 
95 #include <visp3/blob/vpDot2.h>
96 #include <visp3/core/vpHomogeneousMatrix.h>
97 #include <visp3/core/vpPoint.h>
98 
99 int main()
100 {
101  try {
103 
107  g.open(I);
108 
109  g.acquire(I);
110 
111 #ifdef VISP_HAVE_X11
112  vpDisplayX display(I, 100, 100, "Current image");
113 #elif defined(VISP_HAVE_OPENCV)
114  vpDisplayOpenCV display(I, 100, 100, "Current image");
115 #elif defined(VISP_HAVE_GTK)
116  vpDisplayGTK display(I, 100, 100, "Current image");
117 #endif
118 
120  vpDisplay::flush(I);
121 
122  vpServo task;
123 
124  vpRobotAfma6 robot;
125  // robot.move("zero.pos") ;
126 
127  vpCameraParameters cam;
128  // Update camera parameters
129  robot.getCameraParameters(cam, I);
130 
131  std::cout << std::endl;
132  std::cout << "-------------------------------------------------------" << std::endl;
133  std::cout << " Test program for vpServo " << std::endl;
134  std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl;
135  std::cout << " Simulation " << std::endl;
136  std::cout << " task : servo a line " << std::endl;
137  std::cout << "-------------------------------------------------------" << std::endl;
138  std::cout << std::endl;
139 
140  int nbline = 4;
141  int nbpoint = 4;
142 
143  vpTRACE("sets the desired position of the visual feature ");
144  vpPoint pointd[nbpoint]; // position of the fours corners
145  vpPoint pointcd; // position of the center of the square
146  vpFeaturePoint pd;
147 
148  double L = 0.05;
149  pointd[0].setWorldCoordinates(L, -L, 0);
150  pointd[1].setWorldCoordinates(L, L, 0);
151  pointd[2].setWorldCoordinates(-L, L, 0);
152  pointd[3].setWorldCoordinates(-L, -L, 0);
153 
154  // The coordinates in the object frame of the point used as a feature ie
155  // the center of the square
156  pointcd.setWorldCoordinates(0, 0, 0);
157 
158  // The desired homogeneous matrix.
159  vpHomogeneousMatrix cMod(0, 0, 0.4, 0, 0, vpMath::rad(10));
160 
161  pointd[0].project(cMod);
162  pointd[1].project(cMod);
163  pointd[2].project(cMod);
164  pointd[3].project(cMod);
165 
166  pointcd.project(cMod);
167 
168  vpFeatureBuilder::create(pd, pointcd);
169 
170  vpTRACE("Initialization of the tracking");
171  vpMeLine line[nbline];
172  vpPoint point[nbpoint];
173  int i;
174 
175  vpMe me;
176  me.setRange(10);
177  me.setPointsToTrack(100);
178  me.setThreshold(50000);
179  me.setSampleStep(10);
180 
181  // Initialize the tracking. Define the four lines to track
182  for (i = 0; i < nbline; i++) {
183  line[i].setMe(&me);
184 
185  line[i].initTracking(I);
186  line[i].track(I);
187  }
188 
189  // Compute the position of the four corners. The goal is to
190  // compute the pose
191  vpImagePoint ip;
192  for (i = 0; i < nbline; i++) {
193  double x = 0, y = 0;
194 
195  if (!vpMeLine::intersection(line[i % nbline], line[(i + 1) % nbline], ip)) {
196  exit(-1);
197  }
198 
200 
201  point[i].set_x(x);
202  point[i].set_y(y);
203  }
204 
205  // Compute the pose cMo
206  vpPose pose;
207  pose.clearPoint();
209 
210  point[0].setWorldCoordinates(L, -L, 0);
211  point[1].setWorldCoordinates(L, L, 0);
212  point[2].setWorldCoordinates(-L, L, 0);
213  point[3].setWorldCoordinates(-L, -L, 0);
214 
215  for (i = 0; i < nbline; i++) {
216  pose.addPoint(point[i]); // and added to the pose computation point list
217  }
218 
219  pose.computePose(vpPose::LAGRANGE, cMo);
220  pose.computePose(vpPose::VIRTUAL_VS, cMo);
221 
222  vpTRACE("sets the current position of the visual feature ");
223 
224  // The first features are the position in the camera frame x and y of the
225  // square center
226  vpPoint pointc; // The current position of the center of the square
227  double xc = (point[0].get_x() + point[2].get_x()) / 2;
228  double yc = (point[0].get_y() + point[2].get_y()) / 2;
229  pointc.set_x(xc);
230  pointc.set_y(yc);
231  vpFeaturePoint p;
232  pointc.project(cMo);
233  vpFeatureBuilder::create(p, pointc);
234 
235  // The second feature is the depth of the current square center relative
236  // to the depth of the desired square center.
237  vpFeatureDepth logZ;
238  logZ.buildFrom(pointc.get_x(), pointc.get_y(), pointc.get_Z(), log(pointc.get_Z() / pointcd.get_Z()));
239 
240  // The last three features are the rotations thetau between the current
241  // pose and the desired pose.
242  vpHomogeneousMatrix cdMc;
243  cdMc = cMod * cMo.inverse();
245  tu.buildFrom(cdMc);
246 
247  vpTRACE("define the task");
248  vpTRACE("\t we want an eye-in-hand control law");
249  vpTRACE("\t robot is controlled in the camera frame");
252 
253  vpTRACE("\t we want to see a point on a point..");
254  std::cout << std::endl;
255  task.addFeature(p, pd);
256  task.addFeature(logZ);
257  task.addFeature(tu);
258 
259  vpTRACE("\t set the gain");
260  task.setLambda(0.2);
261 
262  vpTRACE("Display task information ");
263  task.print();
264 
266 
267  unsigned int iter = 0;
268  vpTRACE("\t loop");
269  vpColVector v;
270  vpImage<vpRGBa> Ic;
271  double lambda_av = 0.05;
272  double alpha = 0.05;
273  double beta = 3;
274 
275  for (;;) {
276  std::cout << "---------------------------------------------" << iter << std::endl;
277 
278  try {
279  g.acquire(I);
281 
282  pose.clearPoint();
283 
284  // Track the lines and find the current position of the corners
285  for (i = 0; i < nbline; i++) {
286  line[i].track(I);
287 
288  line[i].display(I, vpColor::green);
289 
290  double x = 0, y = 0;
291 
292  if (!vpMeLine::intersection(line[i % nbline], line[(i + 1) % nbline], ip)) {
293  exit(-1);
294  }
295 
297 
298  point[i].set_x(x);
299  point[i].set_y(y);
300 
301  pose.addPoint(point[i]);
302  }
303 
304  // Compute the pose
305  pose.computePose(vpPose::VIRTUAL_VS, cMo);
306 
307  // Update the two first features x and y (position of the square
308  // center)
309  xc = (point[0].get_x() + point[2].get_x()) / 2;
310  yc = (point[0].get_y() + point[2].get_y()) / 2;
311  pointc.set_x(xc);
312  pointc.set_y(yc);
313  pointc.project(cMo);
314  vpFeatureBuilder::create(p, pointc);
315  // Print the current and the desired position of the center of the
316  // square Print the desired position of the four corners
317  p.display(cam, I, vpColor::green);
318  pd.display(cam, I, vpColor::red);
319  for (i = 0; i < nbpoint; i++)
320  pointd[i].display(I, cam, vpColor::red);
321 
322  // Update the second feature
323  logZ.buildFrom(pointc.get_x(), pointc.get_y(), pointc.get_Z(), log(pointc.get_Z() / pointcd.get_Z()));
324 
325  // Update the last three features
326  cdMc = cMod * cMo.inverse();
327  tu.buildFrom(cdMc);
328 
329  // Adaptive gain
330  double gain;
331  {
332  if (std::fabs(alpha) <= std::numeric_limits<double>::epsilon())
333  gain = lambda_av;
334  else {
335  gain = alpha * exp(-beta * (task.getError()).sumSquare()) + lambda_av;
336  }
337  }
338 
339  task.setLambda(gain);
340 
341  v = task.computeControlLaw();
342 
343  vpDisplay::flush(I);
344  std::cout << v.sumSquare() << std::endl;
345  if (iter == 0)
347  if (v.sumSquare() > 0.5) {
348  v = 0;
350  robot.stopMotion();
352  }
353 
355 
356  } catch (...) {
357  v = 0;
359  robot.stopMotion();
360  exit(1);
361  }
362 
363  vpTRACE("\t\t || s - s* || = %f ", (task.getError()).sumSquare());
364  iter++;
365  }
366 
367  vpTRACE("Display task information ");
368  task.print();
369  return EXIT_SUCCESS;
370  }
371  catch (const vpException &e) {
372  std::cout << "Test failed with exception: " << e << std::endl;
373  return EXIT_FAILURE;
374  }
375 }
376 
377 #else
378 int main()
379 {
380  std::cout << "You do not have an afma6 robot connected to your computer..." << std::endl;
381  return EXIT_SUCCESS;
382 }
383 
384 #endif
bool computePose(vpPoseMethodType method, vpHomogeneousMatrix &cMo, bool(*func)(const vpHomogeneousMatrix &)=NULL)
Definition: vpPose.cpp:374
void getCameraParameters(vpCameraParameters &cam, const unsigned int &image_width, const unsigned int &image_height) const
Definition: vpAfma6.cpp:1256
void setWorldCoordinates(double oX, double oY, double oZ)
Definition: vpPoint.cpp:113
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
void setPointsToTrack(const int &n)
Definition: vpMe.h:264
Implementation of an homogeneous matrix and operations on such kind of matrices.
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:490
void setSampleStep(const double &s)
Definition: vpMe.h:278
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:150
void buildFrom(double x, double y, double Z, double LogZoverZstar)
Class that defines a 3D point visual feature which is composed by one parameters that is that defin...
error that can be emited by ViSP classes.
Definition: vpException.h:71
void track(const vpImage< unsigned char > &Im)
Definition: vpMeLine.cpp:746
Definition: vpMe.h:60
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
static void convertPoint(const vpCameraParameters &cam, const double &u, const double &v, double &x, double &y)
double get_y() const
Get the point y coordinate in the image plane.
Definition: vpPoint.cpp:458
static const vpColor green
Definition: vpColor.h:220
void acquire(vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
Control of Irisa&#39;s gantry robot named Afma6.
Definition: vpRobotAfma6.h:211
static const vpColor red
Definition: vpColor.h:217
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:81
void set_x(double x)
Set the point x coordinate in the image plane.
Definition: vpPoint.cpp:497
void set_y(double y)
Set the point y coordinate in the image plane.
Definition: vpPoint.cpp:499
void display(const vpImage< unsigned char > &I, vpColor col)
Definition: vpMeLine.cpp:224
void open(vpImage< unsigned char > &I)
Initialize the velocity controller.
Definition: vpRobot.h:66
vpColVector getError() const
Definition: vpServo.h:278
vpColVector computeControlLaw()
Definition: vpServo.cpp:929
#define vpTRACE
Definition: vpDebug.h:416
static void display(const vpImage< unsigned char > &I)
Class that tracks in an image a line moving edges.
Definition: vpMeLine.h:151
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Class used for pose computation from N points (pose from point only). Some of the algorithms implemen...
Definition: vpPose.h:80
Generic class defining intrinsic camera parameters.
void setLambda(double c)
Definition: vpServo.h:404
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:134
vpRobot::vpRobotStateType setRobotState(vpRobot::vpRobotStateType newState)
double get_x() const
Get the point x coordinate in the image plane.
Definition: vpPoint.cpp:456
void display(const vpCameraParameters &cam, const vpImage< unsigned char > &I, const vpColor &color=vpColor::green, unsigned int thickness=1) const
void initTracking(const vpImage< unsigned char > &I)
Definition: vpMeLine.cpp:236
void buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:567
static double rad(double deg)
Definition: vpMath.h:110
double get_Z() const
Get the point cZ coordinate in the camera frame.
Definition: vpPoint.cpp:442
double sumSquare() const
Implementation of column vector and the associated operations.
Definition: vpColVector.h:130
vpHomogeneousMatrix inverse() const
void setFramerate(vp1394TwoFramerateType fps)
void setVideoMode(vp1394TwoVideoModeType videomode)
Class that defines a 3D visual feature from a axis/angle parametrization that represent the rotatio...
void setThreshold(const double &t)
Definition: vpMe.h:300
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:306
static bool intersection(const vpMeLine &line1, const vpMeLine &line2, vpImagePoint &ip)
Definition: vpMeLine.cpp:990
Class for firewire ieee1394 video devices using libdc1394-2.x api.
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &velocity)
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:87
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
void setRange(const unsigned int &r)
Definition: vpMe.h:271
void setMe(vpMe *p_me)
Definition: vpMeTracker.h:173
void addPoint(const vpPoint &P)
Definition: vpPose.cpp:149
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:218
void clearPoint()
Definition: vpPose.cpp:134