Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
servoAfma6Ellipse2DCamVelocity.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 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
44
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_REALSENSE2))
51
52#include <visp3/core/vpDisplay.h>
53#include <visp3/core/vpImage.h>
54#include <visp3/gui/vpDisplayFactory.h>
55#include <visp3/sensor/vpRealSense2.h>
56
57#include <visp3/core/vpHomogeneousMatrix.h>
58#include <visp3/core/vpMath.h>
59#include <visp3/visual_features/vpFeatureBuilder.h>
60#include <visp3/visual_features/vpFeatureEllipse.h>
61#include <visp3/vs/vpServo.h>
62
63#include <visp3/robot/vpRobotAfma6.h>
64
65// Exception
66#include <visp3/core/vpException.h>
67#include <visp3/vs/vpServoDisplay.h>
68
69#include <visp3/blob/vpDot.h>
70
71int main()
72{
73#ifdef ENABLE_VISP_NAMESPACE
74 using namespace VISP_NAMESPACE_NAME;
75#endif
76
77#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
78 std::shared_ptr<vpDisplay> display;
79#else
80 vpDisplay *display = nullptr;
81#endif
82 try {
84
86 vpRealSense2 rs;
87 rs2::config config;
88 unsigned int width = 640, height = 480, fps = 60;
89 config.enable_stream(RS2_STREAM_COLOR, width, height, RS2_FORMAT_RGBA8, fps);
90 config.enable_stream(RS2_STREAM_DEPTH, width, height, RS2_FORMAT_Z16, fps);
91 config.enable_stream(RS2_STREAM_INFRARED, width, height, RS2_FORMAT_Y8, fps);
92 rs.open(config);
93
94 // Warm up camera
95 for (size_t i = 0; i < 10; ++i) {
96 rs.acquire(I);
97 }
98
99#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
100 display = vpDisplayFactory::createDisplay(I, 100, 100, "Current image");
101#else
102 display = vpDisplayFactory::allocateDisplay(I, 100, 100, "Current image");
103#endif
104
107
108 std::cout << "-------------------------------------------------------" << std::endl;
109 std::cout << " Test program for vpServo " << std::endl;
110 std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl;
111 std::cout << " Simulation " << std::endl;
112 std::cout << " task : servo a point " << std::endl;
113 std::cout << "-------------------------------------------------------" << std::endl;
114
115 vpDot dot;
116
117 dot.setMaxDotSize(0.30); // Max dot size is 30 % of the image size
118 // dot.setGraphics(true) ;
119 dot.setComputeMoments(true);
120 std::cout << "Click on an ellipse..." << std::endl;
121 dot.initTracking(I);
122 vpImagePoint cog = dot.getCog();
125
126 dot.track(I);
127
128 vpRobotAfma6 robot;
130
131 // Get camera intrinsics
133 robot.getCameraParameters(cam, I);
134
135 vpTRACE("sets the current position of the visual feature ");
137 vpFeatureBuilder::create(c, cam, dot);
138
139 std::cout << " Learning 0/1 " << std::endl;
140 int learning;
141 std::cin >> learning;
142 std::string name = "dat/ellipse.dat";
143 if (learning == 1) {
144 // save the object position
145 vpTRACE("Save the location of the object in a file dat/ellipse.dat");
146 std::ofstream f(name.c_str());
147 f << c.get_s().t();
148 f.close();
149 exit(1);
150 }
151
152 vpTRACE("sets the desired position of the visual feature ");
154 std::ifstream f("dat/ellipse.dat");
155 double x, y, n20, n11, n02;
156 f >> x;
157 f >> y;
158 f >> n20;
159 f >> n11;
160 f >> n02;
161 f.close();
162 cd.buildFrom(x, y, n20, n11, n02);
163 cd.setABC(0, 0, 10);
164
166 task.setInteractionMatrixType(vpServo::CURRENT, vpServo::PSEUDO_INVERSE);
167
168 task.addFeature(c, cd);
169
170 task.setLambda(0.01);
171
172 robot.setRobotState(vpRobot::STATE_VELOCITY_CONTROL);
173 unsigned int iter = 0;
174 double lambda_av = 0.01;
175 double alpha = 0.1; // 1 ;
176 double beta = 3; // 3 ;
177
178 std::cout << "alpha 0.7" << std::endl;
179 std::cin >> alpha;
180 std::cout << "beta 5" << std::endl;
181 std::cin >> beta;
182 bool quit = false;
183 while (!quit) {
184 std::cout << "---------------------------------------------" << iter++ << std::endl;
185
186 rs.acquire(I);
188
189 dot.track(I);
190
191 // Get the dot cog
192 cog = dot.getCog();
193
195
196 vpFeatureBuilder::create(c, cam, dot);
197 // Compute the adaptative gain (speed up the convergence)
198 double gain;
199 if (iter > 2) {
200 if (std::fabs(alpha) <= std::numeric_limits<double>::epsilon())
201 gain = lambda_av;
202 else {
203 gain = alpha * exp(-beta * (task.getError()).sumSquare()) + lambda_av;
204 }
205 }
206 else
207 gain = lambda_av;
208
209 vpTRACE("%f %f", (task.getError()).sumSquare(), gain);
210 task.setLambda(gain);
212 v = task.computeControlLaw();
213 std::cout << "rank " << task.getTaskRank() << std::endl;
214 vpServoDisplay::display(task, cam, I);
215 std::cout << v.t();
216 robot.setVelocity(vpRobot::CAMERA_FRAME, v);
217
218 vpDisplay::displayText(I, 20, 20, "Click to quit...", vpColor::red);
219 if (vpDisplay::getClick(I, false)) {
220 quit = true;
221 }
223 vpTRACE("\t\t || s - s* || = %f ", (task.getError()).sumSquare());
224 }
225
226 vpTRACE("Display task information ");
227 task.print();
228#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
229 if (display != nullptr) {
230 delete display;
231 }
232#endif
233 return EXIT_SUCCESS;
234 }
235 catch (const vpException &e) {
236 std::cout << "Test failed with exception: " << e << std::endl;
237#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
238 if (display != nullptr) {
239 delete display;
240 }
241#endif
242 return EXIT_FAILURE;
243 }
244}
245
246#else
247int main()
248{
249 std::cout << "You do not have an afma6 robot connected to your computer..." << std::endl;
250 return EXIT_SUCCESS;
251}
252#endif
@ TOOL_INTEL_D435_CAMERA
Definition vpAfma6.h:129
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.
vpRowVector t() const
static const vpColor red
Definition vpColor.h:198
static const vpColor blue
Definition vpColor.h:204
static const vpColor green
Definition vpColor.h:201
Class that defines generic functionalities for display.
Definition vpDisplay.h:171
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:123
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:337
vpImagePoint getCog() const
Definition vpDot.h:255
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 vpDot &d)
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 of the vpImage class member functions.
Definition vpImage.h:131
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.
void init(void)
@ CAMERA_FRAME
Definition vpRobot.h:81
@ STATE_VELOCITY_CONTROL
Initialize the velocity controller.
Definition vpRobot.h:64
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)
@ EYEINHAND_CAMERA
Definition vpServo.h:176
@ PSEUDO_INVERSE
Definition vpServo.h:250
@ CURRENT
Definition vpServo.h:217
#define vpTRACE
Definition vpDebug.h:450
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.