Visual Servoing Platform  version 3.6.1 under development (2024-12-11)
servoAfma6Ellipse2DCamVelocity.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
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 https://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 the camera frame
34  */
35 
45 #include <cmath> // std::fabs
46 #include <limits> // numeric_limits
47 #include <stdlib.h>
48 #include <visp3/core/vpConfig.h>
49 #include <visp3/core/vpDebug.h> // Debug trace
50 #if (defined(VISP_HAVE_AFMA6) && defined(VISP_HAVE_DC1394))
51 
52 #include <visp3/core/vpDisplay.h>
53 #include <visp3/core/vpImage.h>
54 #include <visp3/gui/vpDisplayGTK.h>
55 #include <visp3/gui/vpDisplayOpenCV.h>
56 #include <visp3/gui/vpDisplayX.h>
57 #include <visp3/sensor/vpRealSense2.h>
58 
59 #include <visp3/core/vpHomogeneousMatrix.h>
60 #include <visp3/core/vpMath.h>
61 #include <visp3/visual_features/vpFeatureBuilder.h>
62 #include <visp3/visual_features/vpFeatureEllipse.h>
63 #include <visp3/vs/vpServo.h>
64 
65 #include <visp3/robot/vpRobotAfma6.h>
66 
67 // Exception
68 #include <visp3/core/vpException.h>
69 #include <visp3/vs/vpServoDisplay.h>
70 
71 #include <visp3/blob/vpDot.h>
72 
73 int main()
74 {
75 #ifdef ENABLE_VISP_NAMESPACE
76  using namespace VISP_NAMESPACE_NAME;
77 #endif
78 
79  try {
80  vpServo task;
81 
83  vpRealSense2 rs;
84  rs2::config config;
85  unsigned int width = 640, height = 480, fps = 60;
86  config.enable_stream(RS2_STREAM_COLOR, width, height, RS2_FORMAT_RGBA8, fps);
87  config.enable_stream(RS2_STREAM_DEPTH, width, height, RS2_FORMAT_Z16, fps);
88  config.enable_stream(RS2_STREAM_INFRARED, width, height, RS2_FORMAT_Y8, fps);
89  rs.open(config);
90 
91  // Warm up camera
92  for (size_t i = 0; i < 10; ++i) {
93  rs.acquire(I);
94  }
95 
96 #ifdef VISP_HAVE_X11
97  vpDisplayX display(I, 100, 100, "Current image");
98 #elif defined(HAVE_OPENCV_HIGHGUI)
99  vpDisplayOpenCV display(I, 100, 100, "Current image");
100 #elif defined(VISP_HAVE_GTK)
101  vpDisplayGTK display(I, 100, 100, "Current image");
102 #endif
103 
105  vpDisplay::flush(I);
106 
107  std::cout << "-------------------------------------------------------" << std::endl;
108  std::cout << " Test program for vpServo " << std::endl;
109  std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl;
110  std::cout << " Simulation " << std::endl;
111  std::cout << " task : servo a point " << std::endl;
112  std::cout << "-------------------------------------------------------" << std::endl;
113 
114  vpDot dot;
115 
116  dot.setMaxDotSize(0.30); // Max dot size is 30 % of the image size
117  // dot.setGraphics(true) ;
118  dot.setComputeMoments(true);
119  std::cout << "Click on an ellipse..." << std::endl;
120  dot.initTracking(I);
121  vpImagePoint cog = dot.getCog();
123  vpDisplay::flush(I);
124 
125  dot.track(I);
126 
127  vpRobotAfma6 robot;
129 
130  // Get camera intrinsics
131  vpCameraParameters cam;
132  robot.getCameraParameters(cam, I);
133 
134  vpTRACE("sets the current position of the visual feature ");
136  vpFeatureBuilder::create(c, cam, dot);
137 
138  std::cout << " Learning 0/1 " << std::endl;
139  int learning;
140  std::cin >> learning;
141  std::string name = "dat/ellipse.dat";
142  if (learning == 1) {
143  // save the object position
144  vpTRACE("Save the location of the object in a file dat/ellipse.dat");
145  std::ofstream f(name.c_str());
146  f << c.get_s().t();
147  f.close();
148  exit(1);
149  }
150 
151  vpTRACE("sets the desired position of the visual feature ");
152  vpFeatureEllipse cd;
153  std::ifstream f("dat/ellipse.dat");
154  double x, y, n20, n11, n02;
155  f >> x;
156  f >> y;
157  f >> n20;
158  f >> n11;
159  f >> n02;
160  f.close();
161  cd.buildFrom(x, y, n20, n11, n02);
162  cd.setABC(0, 0, 10);
163 
166 
167  task.addFeature(c, cd);
168 
169  task.setLambda(0.01);
170 
172  unsigned int iter = 0;
173  double lambda_av = 0.01;
174  double alpha = 0.1; // 1 ;
175  double beta = 3; // 3 ;
176 
177  std::cout << "alpha 0.7" << std::endl;
178  std::cin >> alpha;
179  std::cout << "beta 5" << std::endl;
180  std::cin >> beta;
181  bool quit = false;
182  while (!quit) {
183  std::cout << "---------------------------------------------" << iter++ << std::endl;
184 
185  rs.acquire(I);
187 
188  dot.track(I);
189 
190  // Get the dot cog
191  cog = dot.getCog();
192 
194 
195  vpFeatureBuilder::create(c, cam, dot);
196  // Compute the adaptative gain (speed up the convergence)
197  double gain;
198  if (iter > 2) {
199  if (std::fabs(alpha) <= std::numeric_limits<double>::epsilon())
200  gain = lambda_av;
201  else {
202  gain = alpha * exp(-beta * (task.getError()).sumSquare()) + lambda_av;
203  }
204  }
205  else
206  gain = lambda_av;
207 
208  vpTRACE("%f %f", (task.getError()).sumSquare(), gain);
209  task.setLambda(gain);
210  vpColVector v;
211  v = task.computeControlLaw();
212  std::cout << "rank " << task.getTaskRank() << std::endl;
213  vpServoDisplay::display(task, cam, I);
214  std::cout << v.t();
216 
217  vpDisplay::displayText(I, 20, 20, "Click to quit...", vpColor::red);
218  if (vpDisplay::getClick(I, false)) {
219  quit = true;
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  return EXIT_SUCCESS;
228  }
229  catch (const vpException &e) {
230  std::cout << "Test failed with exception: " << e << std::endl;
231  return EXIT_FAILURE;
232  }
233 }
234 
235 #else
236 int main()
237 {
238  std::cout << "You do not have an afma6 robot connected to your computer..." << std::endl;
239  return EXIT_SUCCESS;
240 }
241 #endif
@ TOOL_INTEL_D435_CAMERA
Definition: vpAfma6.h:131
vpColVector get_s(unsigned int select=FEATURE_ALL) const
Get the feature vector .
Generic class defining intrinsic camera parameters.
@ perspectiveProjWithoutDistortion
Perspective projection without distortion model.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
vpRowVector t() const
static const vpColor red
Definition: vpColor.h:217
static const vpColor blue
Definition: vpColor.h:223
static const vpColor green
Definition: vpColor.h:220
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:133
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
static void flush(const vpImage< unsigned char > &I)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
This tracker is meant to track a dot (connected pixels with same gray level) on a vpImage.
Definition: vpDot.h:116
void setMaxDotSize(double percentage)
Definition: vpDot.cpp:595
void initTracking(const vpImage< unsigned char > &I)
Definition: vpDot.cpp:630
void setComputeMoments(bool activate)
Definition: vpDot.h:329
vpImagePoint getCog() const
Definition: vpDot.h:247
void track(const vpImage< unsigned char > &I)
Definition: vpDot.cpp:760
error that can be emitted by ViSP classes.
Definition: vpException.h:60
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpImagePoint &t)
Class that defines 2D ellipse visual feature.
vpFeatureEllipse & buildFrom(const double &x, const double &y, const double &n20, const double &n11, const double &n02)
void setABC(double A, double B, double C)
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
void acquire(vpImage< unsigned char > &grey, double *ts=nullptr)
bool open(const rs2::config &cfg=rs2::config())
Control of Irisa's gantry robot named Afma6.
Definition: vpRobotAfma6.h:212
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel) VP_OVERRIDE
@ CAMERA_FRAME
Definition: vpRobot.h:84
@ STATE_VELOCITY_CONTROL
Initialize the velocity controller.
Definition: vpRobot.h:67
virtual vpRobotStateType setRobotState(const vpRobot::vpRobotStateType newState)
Definition: vpRobot.cpp:202
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)
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:380
@ EYEINHAND_CAMERA
Definition: vpServo.h:161
void addFeature(vpBasicFeature &s_cur, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:331
unsigned int getTaskRank() const
Definition: vpServo.h:606
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:171
void setLambda(double c)
Definition: vpServo.h:986
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:134
vpColVector getError() const
Definition: vpServo.h:510
@ PSEUDO_INVERSE
Definition: vpServo.h:235
vpColVector computeControlLaw()
Definition: vpServo.cpp:705
@ CURRENT
Definition: vpServo.h:202