Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
servoAfma6FourPoints2DCamVelocityLs_cur.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/vpFeaturePoint.h>
61#include <visp3/vs/vpServo.h>
62#include <visp3/vs/vpServoDisplay.h>
63
64// Define the object CAD model
65// Here we consider 4 black blobs whose centers are located on the corners of a square.
66#define L 0.06 // To deal with a 12cm by 12cm square
67
68#ifdef ENABLE_VISP_NAMESPACE
69using namespace VISP_NAMESPACE_NAME;
70#endif
71
85void compute_pose(std::vector<vpPoint> &point, const std::vector<vpDot2> &dot, const vpCameraParameters &cam,
86 vpHomogeneousMatrix &c_M_o, bool init)
87{
88 vpRotationMatrix c_R_o;
89 vpPose pose;
90 vpImagePoint cog;
91
92 for (size_t i = 0; i < point.size(); ++i) {
93 double x = 0, y = 0;
94 cog = dot[i].getCog();
95 vpPixelMeterConversion::convertPoint(cam, cog, x, y); // Pixel to meter conversion
96 point[i].set_x(x); // Perspective projection
97 point[i].set_y(y);
98 pose.addPoint(point[i]);
99 }
100
101 if (init == true) {
103 }
104 else { // init = false; use of the previous pose to initialise VIRTUAL_VS
105 pose.computePose(vpPose::VIRTUAL_VS, c_M_o);
106 }
107}
108
109int main()
110{
111 // Log file creation in /tmp/$USERNAME/log.dat
112 // This file contains by line:
113 // - the 6 computed camera velocities (m/s, rad/s) to achieve the task
114 // - the 6 measured joint velocities (m/s, rad/s)
115 // - the 6 measured joint positions (m, rad)
116 // - the 8 values of s - s*
117 // - the 6 values of the pose c_M_o (tx,ty,tz, rx,ry,rz) with translation
118 // in meters and rotations in radians
119
120 // Get the user login name
121 std::string username = vpIoTools::getUserName();
122
123 // Create a log filename to save velocities...
124 std::string logdirname = "/tmp/" + username;
125
126 // Test if the output path exist. If no try to create it
127 if (vpIoTools::checkDirectory(logdirname) == false) {
128 try {
129 // Create the dirname
130 vpIoTools::makeDirectory(logdirname);
131 }
132 catch (...) {
133 std::cerr << std::endl << "ERROR:" << std::endl;
134 std::cerr << " Cannot create " << logdirname << std::endl;
135 return EXIT_FAILURE;
136 }
137 }
138 std::string logfilename = logdirname + "/log.dat";
139
140 // Open the log file name
141 std::ofstream flog(logfilename.c_str());
142
143 try {
144 vpRealSense2 rs;
145 rs2::config config;
146 unsigned int width = 640, height = 480, fps = 60;
147 config.enable_stream(RS2_STREAM_COLOR, width, height, RS2_FORMAT_RGBA8, fps);
148 config.enable_stream(RS2_STREAM_DEPTH, width, height, RS2_FORMAT_Z16, fps);
149 config.enable_stream(RS2_STREAM_INFRARED, width, height, RS2_FORMAT_Y8, fps);
150 rs.open(config);
151
153
154 // Warm up camera
155 for (size_t i = 0; i < 10; ++i) {
156 rs.acquire(I);
157 }
158
159 std::shared_ptr<vpDisplay> d = vpDisplayFactory::createDisplay(I, 100, 100, "Current image");
160
163
164 std::cout << "-------------------------------------------------------" << std::endl;
165 std::cout << " Test program for vpServo " << std::endl;
166 std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl;
167 std::cout << " Use of the Afma6 robot " << std::endl;
168 std::cout << " Interaction matrix computed with the current features " << std::endl;
169 std::cout << " task : servo 4 points on a square with dimension " << L << " meters" << std::endl;
170 std::cout << "-------------------------------------------------------" << std::endl;
171
172 std::vector<vpDot2> dot(4);
173
174 std::cout << "Click on the 4 dots clockwise starting from upper/left dot..." << std::endl;
175 for (size_t i = 0; i < dot.size(); ++i) {
176 dot[i].initTracking(I);
177 vpImagePoint cog = dot[i].getCog();
180 }
181
182 vpRobotAfma6 robot;
184
185 // Load the end-effector to camera frame transformation obtained
186 // using a camera intrinsic model with distortion
187 robot.init(vpAfma6::TOOL_INTEL_D435_CAMERA, projModel);
188
189 // Get camera intrinsics
191 robot.getCameraParameters(cam, I);
192
193 // Sets the current position of the visual feature
194 std::vector<vpFeaturePoint> s(4);
195 for (size_t i = 0; i < s.size(); ++i) {
196 vpFeatureBuilder::create(s[i], cam, dot[i]); // retrieve x,y of the vpFeaturePoint structure
197 }
198
199 // Set the position of the square target in a frame which origin is
200 // centered in the middle of the square
201 std::vector<vpPoint> point(4);
202 point[0].setWorldCoordinates(-L, -L, 0);
203 point[1].setWorldCoordinates(+L, -L, 0);
204 point[2].setWorldCoordinates(+L, +L, 0);
205 point[3].setWorldCoordinates(-L, +L, 0);
206
207 // Initialise a desired pose to compute s*, the desired 2D point features
209 vpTranslationVector c_t_o(0, 0, 0.5); // tz = 0.5 meter
210 vpRxyzVector c_r_o(vpMath::rad(0), vpMath::rad(0), vpMath::rad(0)); // No rotations
211 vpRotationMatrix c_R_o(c_r_o); // Build the rotation matrix
212 c_M_o.buildFrom(c_t_o, c_R_o); // Build the homogeneous matrix
213
214 // Sets the desired position of the 2D visual feature
215 std::vector<vpFeaturePoint> s_d(4);
216 // Compute the desired position of the features from the desired pose
217 for (size_t i = 0; i < s_d.size(); ++i) {
218 vpColVector cP, p;
219 point[i].changeFrame(c_M_o, cP);
220 point[i].projection(cP, p);
221
222 s_d[i].set_x(p[0]);
223 s_d[i].set_y(p[1]);
224 s_d[i].set_Z(cP[2]);
225 }
226
227 // Define the task
228 // - we want an eye-in-hand control law
229 // - robot is controlled in the camera frame
230 // - Interaction matrix is computed with the current visual features
233 task.setInteractionMatrixType(vpServo::CURRENT, vpServo::PSEUDO_INVERSE);
234
235 // We want to see a point on a point
236 for (size_t i = 0; i < s.size(); ++i) {
237 task.addFeature(s[i], s_d[i]);
238 }
239
240 // Set the proportional gain
241 task.setLambda(0.3);
242
243 // Display task information
244 task.print();
245
246 // Initialise the velocity control of the robot
247 robot.setRobotState(vpRobot::STATE_VELOCITY_CONTROL);
248
249 std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
250
251 bool init_pose_from_linear_method = true;
252
253 bool quit = false;
254 while (!quit) {
255 // Acquire a new image from the camera
256 rs.acquire(I);
257
258 // Display this image
260
261 // For each point...
262 for (size_t i = 0; i < dot.size(); ++i) {
263 // Achieve the tracking of the dot in the image
264 dot[i].track(I);
265 }
266
267 // At first iteration, we initialise non linear pose estimation with a linear approach.
268 // For the other iterations, non linear pose estimation is initialized with the pose estimated at previous iteration of the loop
269 compute_pose(point, dot, cam, c_M_o, init_pose_from_linear_method);
270 if (init_pose_from_linear_method) {
271 init_pose_from_linear_method = false;
272 }
273
274 for (size_t i = 0; i < dot.size(); ++i) {
275 // Update the point feature from the dot location
276 vpFeatureBuilder::create(s[i], cam, dot[i]);
277 // Set the feature Z coordinate from the pose
278 vpColVector cP;
279 point[i].changeFrame(c_M_o, cP);
280
281 s[i].set_Z(cP[2]);
282 }
283
284 // Compute the visual servoing skew vector
285 vpColVector v = task.computeControlLaw();
286
287 // Display the current and desired feature points in the image display
288 vpServoDisplay::display(task, cam, I);
289
290 // Apply the computed camera velocities to the robot
291 robot.setVelocity(vpRobot::CAMERA_FRAME, v);
292
293 // Save velocities applied to the robot in the log file
294 // v[0], v[1], v[2] correspond to camera translation velocities in m/s
295 // v[3], v[4], v[5] correspond to camera rotation velocities in rad/s
296 flog << v[0] << " " << v[1] << " " << v[2] << " " << v[3] << " " << v[4] << " " << v[5] << " ";
297
298 // Get the measured joint velocities of the robot
299 vpColVector qvel;
300 robot.getVelocity(vpRobot::ARTICULAR_FRAME, qvel);
301 // Save measured joint velocities of the robot in the log file:
302 // - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
303 // velocities in m/s
304 // - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
305 // velocities in rad/s
306 flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " " << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
307
308 // Get the measured joint positions of the robot
309 vpColVector q;
310 robot.getPosition(vpRobot::ARTICULAR_FRAME, q);
311 // Save measured joint positions of the robot in the log file
312 // - q[0], q[1], q[2] correspond to measured joint translation
313 // positions in m
314 // - q[3], q[4], q[5] correspond to measured joint rotation
315 // positions in rad
316 flog << q[0] << " " << q[1] << " " << q[2] << " " << q[3] << " " << q[4] << " " << q[5] << " ";
317
318 // Save feature error (s-s*) for the 4 feature points. For each feature
319 // point, we have 2 errors (along x and y axis). This error is
320 // expressed in meters in the camera frame
321 flog << (task.getError()).t() << " "; // s-s* for points
322
323 // Save the current c_M_o pose: translations in meters, rotations (rx, ry,
324 // rz) in radians
325 flog << c_t_o[0] << " " << c_t_o[1] << " " << c_t_o[2] << " " // translation
326 << c_r_o[0] << " " << c_r_o[1] << " " << c_r_o[2] << std::endl; // rot
327
328 vpDisplay::displayText(I, 20, 20, "Click to quit...", vpColor::red);
329 if (vpDisplay::getClick(I, false)) {
330 quit = true;
331 }
332
333 // Flush the display
335 }
336
337 // Close the log file
338 flog.close();
339
340 // Display task information
341 task.print();
342
343 return EXIT_SUCCESS;
344 }
345 catch (const vpException &e) {
346 // Close the log file
347 flog.close();
348
349 std::cout << "Visual servo failed with exception: " << e << std::endl;
350 return EXIT_FAILURE;
351 }
352}
353
354#else
355int main()
356{
357 std::cout << "You do not have an afma6 robot connected to your computer..." << std::endl;
358 return EXIT_SUCCESS;
359}
360
361#endif
@ 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.
vpRowVector t() const
static const vpColor red
Definition vpColor.h:198
static const vpColor blue
Definition vpColor.h:204
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)
error that can be emitted by ViSP classes.
Definition vpException.h:60
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix & buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
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 bool checkDirectory(const std::string &dirname)
static std::string getUserName()
static void makeDirectory(const std::string &dirname)
static double rad(double deg)
Definition vpMath.h:129
static void convertPoint(const vpCameraParameters &cam, const double &u, const double &v, double &x, double &y)
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 acquire(vpImage< unsigned char > &grey, double *ts=nullptr)
bool open(const rs2::config &cfg=rs2::config())
Control of Irisa's gantry robot named Afma6.
@ ARTICULAR_FRAME
Definition vpRobot.h:77
@ CAMERA_FRAME
Definition vpRobot.h:81
@ STATE_VELOCITY_CONTROL
Initialize the velocity controller.
Definition vpRobot.h:64
Implementation of a rotation matrix and operations on such kind of matrices.
Implementation of a rotation vector as Euler angle minimal representation.
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
Class that consider the case of a translation vector.
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.