Visual Servoing Platform  version 3.3.0 under development (2020-02-17)
servoAfma6Ellipse2DCamVelocity.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  * Eric Marchand
38  *
39  *****************************************************************************/
40 
59 #include <cmath> // std::fabs
60 #include <limits> // numeric_limits
61 #include <stdlib.h>
62 #include <visp3/core/vpConfig.h>
63 #include <visp3/core/vpDebug.h> // Debug trace
64 #if (defined(VISP_HAVE_AFMA6) && defined(VISP_HAVE_DC1394))
65 
66 #include <visp3/core/vpDisplay.h>
67 #include <visp3/core/vpImage.h>
68 #include <visp3/gui/vpDisplayGTK.h>
69 #include <visp3/gui/vpDisplayOpenCV.h>
70 #include <visp3/gui/vpDisplayX.h>
71 #include <visp3/sensor/vp1394TwoGrabber.h>
72 
73 #include <visp3/core/vpHomogeneousMatrix.h>
74 #include <visp3/core/vpMath.h>
75 #include <visp3/visual_features/vpFeatureBuilder.h>
76 #include <visp3/visual_features/vpFeatureEllipse.h>
77 #include <visp3/vs/vpServo.h>
78 
79 #include <visp3/robot/vpRobotAfma6.h>
80 
81 // Exception
82 #include <visp3/core/vpException.h>
83 #include <visp3/vs/vpServoDisplay.h>
84 
85 #include <visp3/blob/vpDot.h>
86 
87 int main()
88 {
89  try {
90  vpServo task;
91 
96  g.open(I);
97  g.acquire(I);
98 
99 #ifdef VISP_HAVE_X11
100  vpDisplayX display(I, 100, 100, "Current image");
101 #elif defined(VISP_HAVE_OPENCV)
102  vpDisplayOpenCV display(I, 100, 100, "Current image");
103 #elif defined(VISP_HAVE_GTK)
104  vpDisplayGTK display(I, 100, 100, "Current image");
105 #endif
106 
108  vpDisplay::flush(I);
109 
110  std::cout << std::endl;
111  std::cout << "-------------------------------------------------------" << std::endl;
112  std::cout << " Test program for vpServo " << std::endl;
113  std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl;
114  std::cout << " Simulation " << std::endl;
115  std::cout << " task : servo a point " << std::endl;
116  std::cout << "-------------------------------------------------------" << std::endl;
117  std::cout << std::endl;
118 
119  vpDot dot;
120 
121  dot.setMaxDotSize(0.30); // Max dot size is 30 % of the image size
122  // dot.setGraphics(true) ;
123  dot.setComputeMoments(true);
124  std::cout << "Click on an ellipse..." << std::endl;
125  dot.initTracking(I);
126  vpImagePoint cog = dot.getCog();
128  vpDisplay::flush(I);
129 
130  dot.track(I);
131 
132  vpCameraParameters cam;
133 
134  vpRobotAfma6 robot;
135 
136  // Update camera parameters
137  robot.getCameraParameters(cam, I);
138 
139  vpTRACE("sets the current position of the visual feature ");
141  vpFeatureBuilder::create(c, cam, dot);
142 
143  std::cout << " Learning 0/1 " << std::endl;
144  int learning;
145  std::cin >> learning;
146  char name[FILENAME_MAX];
147  sprintf(name, "dat/ellipse.dat");
148  if (learning == 1) {
149  // save the object position
150  vpTRACE("Save the location of the object in a file dat/ellipse.dat");
151  std::ofstream f(name);
152  f << c.get_s().t();
153  f.close();
154  exit(1);
155  }
156 
157  vpTRACE("sets the desired position of the visual feature ");
158  vpFeatureEllipse cd;
159  std::ifstream f("dat/ellipse.dat");
160  double x, y, mu20, mu11, mu02;
161  f >> x;
162  f >> y;
163  f >> mu20;
164  f >> mu11;
165  f >> mu02;
166  f.close();
167  cd.buildFrom(x, y, mu20, mu11, mu02);
168  cd.setABC(0, 0, 10);
169 
172 
173  task.addFeature(c, cd);
174 
175  task.setLambda(0.01);
176 
178  unsigned int iter = 0;
179  double lambda_av = 0.01;
180  double alpha = 0.1; // 1 ;
181  double beta = 3; // 3 ;
182 
183  std::cout << "alpha 0.7" << std::endl;
184  std::cin >> alpha;
185  std::cout << "beta 5" << std::endl;
186  std::cin >> beta;
187  for (;;) {
188  std::cout << "---------------------------------------------" << iter++ << std::endl;
189 
190  g.acquire(I);
192 
193  dot.track(I);
194 
195  // Get the dot cog
196  cog = dot.getCog();
197 
199 
200  vpFeatureBuilder::create(c, cam, dot);
201  // Compute the adaptative gain (speed up the convergence)
202  double gain;
203  if (iter > 2) {
204  if (std::fabs(alpha) <= std::numeric_limits<double>::epsilon())
205  gain = lambda_av;
206  else {
207  gain = alpha * exp(-beta * (task.getError()).sumSquare()) + lambda_av;
208  }
209  } else
210  gain = lambda_av;
211 
212  vpTRACE("%f %f", (task.getError()).sumSquare(), gain);
213  task.setLambda(gain);
214  vpColVector v;
215  v = task.computeControlLaw();
216  std::cout << "rank " << task.getTaskRank() << std::endl;
217  vpServoDisplay::display(task, cam, I);
218  std::cout << v.t();
220 
221  vpDisplay::flush(I);
222  vpTRACE("\t\t || s - s* || = %f ", (task.getError()).sumSquare());
223  }
224 
225  vpTRACE("Display task information ");
226  task.print();
227  task.kill();
228  return EXIT_SUCCESS;
229  }
230  catch (const vpException &e) {
231  std::cout << "Test failed with exception: " << e << std::endl;
232  return EXIT_FAILURE;
233  }
234 }
235 
236 #else
237 int main()
238 {
239  std::cout << "You do not have an afma6 robot connected to your computer..." << std::endl;
240  return EXIT_SUCCESS;
241 }
242 #endif
void buildFrom(double x, double y, double mu20, double mu11, double mu02)
void getCameraParameters(vpCameraParameters &cam, const unsigned int &image_width, const unsigned int &image_height) const
Definition: vpAfma6.cpp:1189
unsigned int getTaskRank() const
Definition: vpServo.cpp:1821
void setMaxDotSize(double percentage)
Definition: vpDot.cpp:601
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:497
void setComputeMoments(bool activate)
Definition: vpDot.h:310
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 track(const vpImage< unsigned char > &I)
Definition: vpDot.cpp:770
error that can be emited by ViSP classes.
Definition: vpException.h:71
vpRowVector t() const
static const vpColor green
Definition: vpColor.h:182
void acquire(vpImage< unsigned char > &I)
vpColVector get_s(unsigned int select=FEATURE_ALL) const
Get the feature vector .
static void flush(const vpImage< unsigned char > &I)
Control of Irisa&#39;s gantry robot named Afma6.
Definition: vpRobotAfma6.h:211
void open(vpImage< unsigned char > &I)
void kill()
Definition: vpServo.cpp:192
vpImagePoint getCog() const
Definition: vpDot.h:221
Initialize the velocity controller.
Definition: vpRobot.h:66
vpColVector computeControlLaw()
Definition: vpServo.cpp:935
#define vpTRACE
Definition: vpDebug.h:416
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
vpRobot::vpRobotStateType setRobotState(vpRobot::vpRobotStateType newState)
void setABC(double A, double B, double C)
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:574
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
Implementation of column vector and the associated operations.
Definition: vpColVector.h:130
void setFramerate(vp1394TwoFramerateType fps)
void setVideoMode(vp1394TwoVideoModeType videomode)
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:313
This tracker is meant to track a dot (connected pixels with same gray level) on a vpImage...
Definition: vpDot.h:115
vpColVector getError() const
Definition: vpServo.h:282
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:88
Class that defines 2D ellipse visual feature.
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:223
void initTracking(const vpImage< unsigned char > &I)
Definition: vpDot.cpp:635
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:185