Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
servoAfma62DhalfCamVelocity.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
46
47#include <iostream>
48#include <visp3/core/vpConfig.h>
49
50#if defined(VISP_HAVE_AFMA6) && defined(VISP_HAVE_REALSENSE2) && defined(VISP_HAVE_DISPLAY)
51
52#include <visp3/core/vpImage.h>
53#include <visp3/core/vpIoTools.h>
54#include <visp3/gui/vpDisplayFactory.h>
55#include <visp3/sensor/vpRealSense2.h>
56#include <visp3/blob/vpDot2.h>
57#include <visp3/robot/vpRobotAfma6.h>
58#include <visp3/vision/vpPose.h>
59#include <visp3/visual_features/vpFeatureBuilder.h>
60#include <visp3/visual_features/vpFeatureDepth.h>
61#include <visp3/visual_features/vpFeaturePoint.h>
62#include <visp3/visual_features/vpFeatureThetaU.h>
63#include <visp3/vs/vpServo.h>
64#include <visp3/vs/vpServoDisplay.h>
65
66// Define the object CAD model
67// Here we consider 4 black blobs whose centers are located on the corners of a square.
68#define L 0.06 // To deal with a 12cm by 12cm square
69
70int main()
71{
72#ifdef ENABLE_VISP_NAMESPACE
73 using namespace VISP_NAMESPACE_NAME;
74#endif
75
76 try {
77 vpRealSense2 rs;
78 rs2::config config;
79 unsigned int width = 640, height = 480, fps = 60;
80 config.enable_stream(RS2_STREAM_COLOR, width, height, RS2_FORMAT_RGBA8, fps);
81 config.enable_stream(RS2_STREAM_DEPTH, width, height, RS2_FORMAT_Z16, fps);
82 config.enable_stream(RS2_STREAM_INFRARED, width, height, RS2_FORMAT_Y8, fps);
83 rs.open(config);
84
86
87 // Warm up camera
88 for (size_t i = 0; i < 10; ++i) {
89 rs.acquire(I);
90 }
91
92 std::shared_ptr<vpDisplay> d = vpDisplayFactory::createDisplay(I, 100, 100, "Current image");
93
96
97 vpRobotAfma6 robot;
99
100 // Load the end-effector to camera frame transformation obtained
101 // using a camera intrinsic model with distortion
102 robot.init(vpAfma6::TOOL_INTEL_D435_CAMERA, projModel);
103
104 // Get camera intrinsics
106 robot.getCameraParameters(cam, I);
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 line " << std::endl;
113 std::cout << "-------------------------------------------------------" << std::endl;
114
115 int nbline = 4;
116 int nbpoint = 4;
117
118 vpTRACE("sets the desired position of the visual feature ");
119 vpPoint pointd[nbpoint]; // position of the fours corners
120 vpPoint pointcd; // position of the center of the square
121
122 pointd[0].setWorldCoordinates(+L, -L, 0);
123 pointd[1].setWorldCoordinates(+L, +L, 0);
124 pointd[2].setWorldCoordinates(-L, +L, 0);
125 pointd[3].setWorldCoordinates(-L, -L, 0);
126
127 // The coordinates in the object frame of the point used as a feature ie
128 // the center of the square
129 pointcd.setWorldCoordinates(0, 0, 0);
130
131 // The desired homogeneous matrix.
132 vpHomogeneousMatrix cd_M_o(0, 0, 0.4, 0, 0, vpMath::rad(10));
133
134 pointd[0].project(cd_M_o);
135 pointd[1].project(cd_M_o);
136 pointd[2].project(cd_M_o);
137 pointd[3].project(cd_M_o);
138
139 pointcd.project(cd_M_o);
140
141 vpFeaturePoint s_pd;
142 vpFeatureBuilder::create(s_pd, pointcd);
143
144 // Tracking initialization
145 vpMeLine line[nbline];
146 vpPoint point[nbpoint];
147
148 vpMe me;
149 me.setRange(10);
150 me.setPointsToTrack(100);
152 me.setThreshold(15);
153 me.setSampleStep(10);
154
155 // Initialize the tracking. Define the four lines to track
156 for (int i = 0; i < nbline; ++i) {
157 line[i].setMe(&me);
158
159 line[i].initTracking(I);
160 line[i].track(I);
161 }
162
163 // Compute the position of the four corners. The goal is to compute the pose
164 vpImagePoint ip;
165 for (int i = 0; i < nbline; ++i) {
166 double x = 0, y = 0;
167
168 if (!vpMeLine::intersection(line[i % nbline], line[(i + 1) % nbline], ip)) {
169 return EXIT_FAILURE;
170 }
171
173
174 point[i].set_x(x);
175 point[i].set_y(y);
176 }
177
178 // Compute the pose c_M_o
179 vpPose pose;
180 pose.clearPoint();
182
183 point[0].setWorldCoordinates(+L, -L, 0);
184 point[1].setWorldCoordinates(+L, +L, 0);
185 point[2].setWorldCoordinates(-L, +L, 0);
186 point[3].setWorldCoordinates(-L, -L, 0);
187
188 for (int i = 0; i < nbline; ++i) {
189 pose.addPoint(point[i]); // and added to the pose computation point list
190 }
191
192 // Pose by Dementhon or Lagrange provides an initialization of the non linear virtual visual-servoing pose estimation
194
195 // The first features are the position in the camera frame x and y of the square center
196 vpPoint pointc; // The current position of the center of the square
197 double xc = (point[0].get_x() + point[2].get_x()) / 2;
198 double yc = (point[0].get_y() + point[2].get_y()) / 2;
199 pointc.set_x(xc);
200 pointc.set_y(yc);
201
202 // Sets the current position of the visual feature
203 vpFeaturePoint s_p;
204 pointc.project(c_M_o);
205 vpFeatureBuilder::create(s_p, pointc);
206
207 // The second feature is the depth of the current square center relative
208 // to the depth of the desired square center.
209 vpFeatureDepth s_logZ;
210 s_logZ.buildFrom(pointc.get_x(), pointc.get_y(), pointc.get_Z(), log(pointc.get_Z() / pointcd.get_Z()));
211
212 // The last three features are the rotations thetau between the current
213 // pose and the desired pose.
214 vpHomogeneousMatrix cd_M_c;
215 cd_M_c = cd_M_o * c_M_o.inverse();
217 s_tu.buildFrom(cd_M_c);
218
219 // Define the task
220 // - we want an eye-in-hand control law
221 // - robot is controlled in the camera frame
224 task.setInteractionMatrixType(vpServo::CURRENT, vpServo::PSEUDO_INVERSE);
225
226 // - we want to see a point on a point
227 task.addFeature(s_p, s_pd);
228 task.addFeature(s_logZ);
229 task.addFeature(s_tu);
230
231 // - set the gain
232 vpAdaptiveGain lambda(1.5, 0.4, 30); // lambda(0)=4, lambda(oo)=0.4 and lambda'(0)=30
233 task.setLambda(lambda);
234
235 // - display task information ");
236 task.print();
237
238 robot.setRobotState(vpRobot::STATE_VELOCITY_CONTROL);
239
240 bool quit = false;
241
242 while (!quit) {
243 rs.acquire(I);
245
246 pose.clearPoint();
247
248 // Track the lines and find the current position of the corners
249 for (int i = 0; i < nbline; ++i) {
250 line[i].track(I);
251
252 line[i].display(I, vpColor::green);
253
254 double x = 0, y = 0;
255
256 if (!vpMeLine::intersection(line[i % nbline], line[(i + 1) % nbline], ip)) {
257 return EXIT_FAILURE;
258 }
259
261
262 point[i].set_x(x);
263 point[i].set_y(y);
264
265 pose.addPoint(point[i]);
266 }
267
268 // Compute the pose
269 pose.computePose(vpPose::VIRTUAL_VS, c_M_o);
270
271 // Update the two first features x and y (position of the square center)
272 xc = (point[0].get_x() + point[2].get_x()) / 2;
273 yc = (point[0].get_y() + point[2].get_y()) / 2;
274 pointc.set_x(xc);
275 pointc.set_y(yc);
276 pointc.project(c_M_o);
277 vpFeatureBuilder::create(s_p, pointc);
278 // Print the current and the desired position of the center of the
279 // square Print the desired position of the four corners
280 s_p.display(cam, I, vpColor::green);
281 s_pd.display(cam, I, vpColor::red);
282 for (int i = 0; i < nbpoint; ++i) {
283 pointd[i].display(I, cam, vpColor::red);
284 }
285
286 // Update the second feature
287 s_logZ.buildFrom(pointc.get_x(), pointc.get_y(), pointc.get_Z(), log(pointc.get_Z() / pointcd.get_Z()));
288
289 // Update the last three features
290 cd_M_c = cd_M_o * c_M_o.inverse();
291 s_tu.buildFrom(cd_M_c);
292
293 vpColVector v_c = task.computeControlLaw();
294
295 robot.setVelocity(vpRobot::CAMERA_FRAME, v_c);
296
297 vpDisplay::displayText(I, 20, 20, "Click to quit...", vpColor::red);
298 if (vpDisplay::getClick(I, false)) {
299 quit = true;
300 }
302 }
303
304 // Display task information
305 task.print();
306
307 return EXIT_SUCCESS;
308 }
309 catch (const vpException &e) {
310 std::cout << "Visual servo failed with exception: " << e << std::endl;
311 return EXIT_FAILURE;
312 }
313}
314
315#else
316int main()
317{
318 std::cout << "You do not have an afma6 robot connected to your computer..." << std::endl;
319 return EXIT_SUCCESS;
320}
321
322#endif
Adaptive gain computation.
@ TOOL_INTEL_D435_CAMERA
Definition vpAfma6.h:129
Generic class defining intrinsic camera parameters.
@ perspectiveProjWithDistortion
Perspective projection with distortion model.
Implementation of column vector and the associated operations.
static const vpColor red
Definition vpColor.h:198
static const vpColor green
Definition vpColor.h:201
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
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)
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 a 3D point visual feature which is composed by one parameters that is that defin...
vpFeatureDepth & buildFrom(const double &x, const double &y, const double &Z, const double &LogZoverZstar)
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
void display(const vpCameraParameters &cam, const vpImage< unsigned char > &I, const vpColor &color=vpColor::green, unsigned int thickness=1) const VP_OVERRIDE
Class that defines a 3D visual feature from a axis/angle parametrization that represent the rotati...
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix & buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
vpHomogeneousMatrix inverse() const
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
static double rad(double deg)
Definition vpMath.h:129
Class that tracks in an image a line moving edges.
Definition vpMeLine.h:157
void display(const vpImage< unsigned char > &I, const vpColor &color, unsigned int thickness=1)
Definition vpMeLine.cpp:177
void track(const vpImage< unsigned char > &I)
Definition vpMeLine.cpp:607
void initTracking(const vpImage< unsigned char > &I)
Definition vpMeLine.cpp:187
static bool intersection(const vpMeLine &line1, const vpMeLine &line2, vpImagePoint &iP)
Definition vpMeLine.cpp:714
void setMe(vpMe *me)
Definition vpMe.h:143
void setPointsToTrack(const int &points_to_track)
Definition vpMe.h:431
void setRange(const unsigned int &range)
Definition vpMe.h:438
void setLikelihoodThresholdType(const vpLikelihoodThresholdType likelihood_threshold_type)
Definition vpMe.h:531
void setThreshold(const double &threshold)
Definition vpMe.h:489
void setSampleStep(const double &sample_step)
Definition vpMe.h:445
@ NORMALIZED_THRESHOLD
Definition vpMe.h:154
static void convertPoint(const vpCameraParameters &cam, const double &u, const double &v, double &x, double &y)
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition vpPoint.h:79
void set_x(double x)
Set the point x coordinate in the image plane.
Definition vpPoint.cpp:471
double get_y() const
Get the point y coordinate in the image plane.
Definition vpPoint.cpp:429
double get_x() const
Get the point x coordinate in the image plane.
Definition vpPoint.cpp:427
double get_Z() const
Get the point cZ coordinate in the camera frame.
Definition vpPoint.cpp:413
void display(const vpImage< unsigned char > &I, const vpCameraParameters &cam, const vpColor &color=vpColor::green, unsigned int thickness=1) VP_OVERRIDE
Definition vpPoint.cpp:387
void setWorldCoordinates(double oX, double oY, double oZ)
Definition vpPoint.cpp:116
void set_y(double y)
Set the point y coordinate in the image plane.
Definition vpPoint.cpp:473
Class used for pose computation from N points (pose from point only). Some of the algorithms implemen...
Definition vpPose.h:82
void addPoint(const vpPoint &P)
Definition vpPose.cpp:96
@ DEMENTHON_LAGRANGE_VIRTUAL_VS
Definition vpPose.h:103
@ VIRTUAL_VS
Definition vpPose.h:97
bool computePose(vpPoseMethodType method, vpHomogeneousMatrix &cMo, FuncCheckValidityPose func=nullptr)
Definition vpPose.cpp:385
void clearPoint()
Definition vpPose.cpp:89
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.
@ CAMERA_FRAME
Definition vpRobot.h:81
@ STATE_VELOCITY_CONTROL
Initialize the velocity controller.
Definition vpRobot.h:64
@ 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.