Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
servoAfma6FourPoints2DArtVelocity.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 articular frame
34 */
35
45
46#include <iostream>
47#include <visp3/core/vpConfig.h>
48
49#if defined(VISP_HAVE_AFMA6) && defined(VISP_HAVE_REALSENSE2) && defined(VISP_HAVE_DISPLAY)
50
51#include <visp3/core/vpImage.h>
52#include <visp3/core/vpIoTools.h>
53#include <visp3/gui/vpDisplayFactory.h>
54#include <visp3/sensor/vpRealSense2.h>
55#include <visp3/blob/vpDot2.h>
56#include <visp3/robot/vpRobotAfma6.h>
57#include <visp3/visual_features/vpFeatureBuilder.h>
58#include <visp3/visual_features/vpFeaturePoint.h>
59#include <visp3/vs/vpServo.h>
60#include <visp3/vs/vpServoDisplay.h>
61
62// Define the object CAD model
63// Here we consider 4 black blobs whose centers are located on the corners of a square.
64#define L 0.06 // To deal with a 12cm by 12cm square
65
66// Distance between the camera and the square at the desired
67// position after visual servoing convergence
68#define D 0.5
69
70int main()
71{
72#ifdef ENABLE_VISP_NAMESPACE
73 using namespace VISP_NAMESPACE_NAME;
74#endif
75
76 // Log file creation in /tmp/$USERNAME/log.dat
77 // This file contains by line:
78 // - the 6 computed joint velocities (m/s, rad/s) to achieve the task
79 // - the 6 measured joint velocities (m/s, rad/s)
80 // - the 6 measured joint positions (m, rad)
81 // - the 8 values of s - s*
82
83 // Get the user login name
84 std::string username = vpIoTools::getUserName();
85
86 // Create a log filename to save velocities...
87 std::string logdirname = "/tmp/" + username;
88
89 // Test if the output path exist. If no try to create it
90 if (vpIoTools::checkDirectory(logdirname) == false) {
91 try {
92 // Create the dirname
93 vpIoTools::makeDirectory(logdirname);
94 }
95 catch (...) {
96 std::cerr << std::endl << "ERROR:" << std::endl;
97 std::cerr << " Cannot create " << logdirname << std::endl;
98 return EXIT_FAILURE;
99 }
100 }
101 std::string logfilename = logdirname + "/log.dat";
102
103 // Open the log file name
104 std::ofstream flog(logfilename.c_str());
105
106 try {
107 vpRealSense2 rs;
108 rs2::config config;
109 unsigned int width = 640, height = 480, fps = 60;
110 config.enable_stream(RS2_STREAM_COLOR, width, height, RS2_FORMAT_RGBA8, fps);
111 config.enable_stream(RS2_STREAM_DEPTH, width, height, RS2_FORMAT_Z16, fps);
112 config.enable_stream(RS2_STREAM_INFRARED, width, height, RS2_FORMAT_Y8, fps);
113 rs.open(config);
114
116
117 // Warm up camera
118 for (size_t i = 0; i < 10; ++i) {
119 rs.acquire(I);
120 }
121
122 std::shared_ptr<vpDisplay> d = vpDisplayFactory::createDisplay(I, 100, 100, "Current image");
123
126
127 std::cout << "-------------------------------------------------------" << std::endl;
128 std::cout << " Test program for vpServo " << std::endl;
129 std::cout << " Eye-in-hand task control, velocity computed in the joint space" << std::endl;
130 std::cout << " Use of the Afma6 robot " << std::endl;
131 std::cout << " task : servo 4 points on a square with dimension " << L << " meters" << std::endl;
132 std::cout << "-------------------------------------------------------" << std::endl;
133
134 std::vector<vpDot2> dot(4);
135
136 std::cout << "Click on the 4 dots clockwise starting from upper/left dot..." << std::endl;
137 for (size_t i = 0; i < dot.size(); ++i) {
138 dot[i].initTracking(I);
139 vpImagePoint cog = dot[i].getCog();
142 }
143
144 vpRobotAfma6 robot;
146
147 // Load the end-effector to camera frame transformation obtained
148 // using a camera intrinsic model with distortion
149 robot.init(vpAfma6::TOOL_INTEL_D435_CAMERA, projModel);
150
151 // Get camera intrinsics
153 robot.getCameraParameters(cam, I);
154
155 // Sets the current position of the visual feature
156 std::vector<vpFeaturePoint> s(4);
157 for (size_t i = 0; i < s.size(); ++i) {
158 vpFeatureBuilder::create(s[i], cam, dot[i]); // retrieve x,y of the vpFeaturePoint structure
159 }
160
161 // Sets the desired position of the visual feature
162 vpFeaturePoint s_d[4];
163
164 s_d[0].buildFrom(-L/D, -L/D, D);
165 s_d[1].buildFrom(+L/D, -L/D, D);
166 s_d[2].buildFrom(+L/D, +L/D, D);
167 s_d[3].buildFrom(-L/D, +L/D, D);
168
169 // Define the task
170 // - we want an eye-in-hand control law
171 // - joint velocity are computed
172 // - Interaction matrix is computed with the current visual features
175 task.setInteractionMatrixType(vpServo::DESIRED, vpServo::PSEUDO_INVERSE);
176
177 // We want to see a point on a point
178 for (size_t i = 0; i < s.size(); ++i) {
179 task.addFeature(s[i], s_d[i]);
180 }
181
182 // Set task proportional gain
183 task.setLambda(0.4);
184
185 // Set the camera to end-effector velocity twist matrix transformation
187 robot.get_cVe(c_V_e);
188 task.set_cVe(c_V_e);
189
190 // Set the Jacobian (expressed in the end-effector frame)
191 vpMatrix e_J_e;
192 robot.get_eJe(e_J_e);
193 task.set_eJe(e_J_e);
194
195 // Display task information
196 task.print();
197
198 // Initialise the velocity control of the robot
199 robot.setRobotState(vpRobot::STATE_VELOCITY_CONTROL);
200
201 std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
202
203 bool quit = false;
204 while (!quit) {
205 // Acquire a new image from the camera
206 rs.acquire(I);
207
208 // Display this image
210
211 // For each point...
212 for (size_t i = 0; i < dot.size(); ++i) {
213 // Achieve the tracking of the dot in the image
214 dot[i].track(I);
215 // Update the point feature from the dot location
216 vpFeatureBuilder::create(s[i], cam, dot[i]);
217 }
218
219 // Get the Jacobian of the robot
220 robot.get_eJe(e_J_e);
221
222 // Update this jacobian in the task structure. It will be used to
223 task.set_eJe(e_J_e);
224
225 // Compute the visual servoing skew vector (as a joint velocity)
226 // qdot = -lambda * L^+ * cVe * eJe * (s-s*)
227 vpColVector qdot = task.computeControlLaw();
228
229 // Display the current and desired feature points in the image display
230 vpServoDisplay::display(task, cam, I);
231
232 // Apply the computed joint velocities to the robot
233 robot.setVelocity(vpRobot::JOINT_STATE, qdot);
234
235 // Save velocities applied to the robot in the log file
236 // qdot[0], qdot[1], qdot[2] correspond to joint translation velocities in m/s
237 // qdot[3], qdot[4], qdot[5] correspond to joint rotation velocities in rad/s
238 flog << qdot[0] << " " << qdot[1] << " " << qdot[2] << " " << qdot[3] << " " << qdot[4] << " " << qdot[5] << " ";
239
240 // Get the measured joint velocities of the robot
241 vpColVector qdot_mes;
242 robot.getVelocity(vpRobot::JOINT_STATE, qdot_mes);
243 // Save measured joint velocities of the robot in the log file:
244 // - qdot_mes[0], qdot_mes[1], qdot_mes[2] correspond to measured joint translation velocities in m/s
245 // - qdot_mes[3], qdot_mes[4], qdot_mes[5] correspond to measured joint rotation velocities in rad/s
246 flog << qdot_mes[0] << " " << qdot_mes[1] << " " << qdot_mes[2] << " " << qdot_mes[3] << " " << qdot_mes[4] << " " << qdot_mes[5] << " ";
247
248 // Get the measured joint positions of the robot
249 vpColVector q;
250 robot.getPosition(vpRobot::JOINT_STATE, q);
251 // Save measured joint positions of the robot in the log file
252 // - q[0], q[1], q[2] correspond to measured joint translation
253 // positions in m
254 // - q[3], q[4], q[5] correspond to measured joint rotation
255 // positions in rad
256 flog << q[0] << " " << q[1] << " " << q[2] << " " << q[3] << " " << q[4] << " " << q[5] << " ";
257
258 // Save feature error (s-s*) for the 4 feature points. For each feature
259 // point, we have 2 errors (along x and y axis). This error is
260 // expressed in meters in the camera frame
261 flog << (task.getError()).t() << std::endl;
262
263 vpDisplay::displayText(I, 20, 20, "Click to quit...", vpColor::red);
264 if (vpDisplay::getClick(I, false)) {
265 quit = true;
266 }
267 // Flush the display
269 }
270
271 // Close the log file
272 flog.close();
273
274 // Display task information
275 task.print();
276
277 return EXIT_SUCCESS;
278 }
279 catch (const vpException &e) {
280 // Close the log file
281 flog.close();
282 std::cout << "Visual servo failed with exception: " << e << std::endl;
283 return EXIT_FAILURE;
284 }
285}
286
287#else
288int main()
289{
290 std::cout << "You do not have an afma6 robot connected to your computer..." << std::endl;
291 return EXIT_SUCCESS;
292}
293
294#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)
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
vpFeaturePoint & buildFrom(const double &x, const double &y, const double &Z)
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)
Implementation of a matrix and operations on matrices.
Definition vpMatrix.h:175
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.
@ JOINT_STATE
Definition vpRobot.h:79
@ 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_L_cVe_eJe
Definition vpServo.h:183
@ PSEUDO_INVERSE
Definition vpServo.h:250
@ DESIRED
Definition vpServo.h:223
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.