Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
servoAfma6FourPoints2DCamVelocityLs_des.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/visual_features/vpFeatureBuilder.h>
59#include <visp3/visual_features/vpFeaturePoint.h>
60#include <visp3/vs/vpServo.h>
61#include <visp3/vs/vpServoDisplay.h>
62
63// Define the object CAD model
64// Here we consider 4 black blobs whose centers are located on the corners of a square.
65#define L 0.06 // To deal with a 12cm by 12cm square
66
67int main()
68{
69#ifdef ENABLE_VISP_NAMESPACE
70 using namespace VISP_NAMESPACE_NAME;
71#endif
72
73 // Log file creation in /tmp/$USERNAME/log.dat
74 // This file contains by line:}
75 // - the 6 computed camera velocities (m/s, rad/s) to achieve the task
76 // - the 6 measured camera velocities (m/s, rad/s)
77 // - the 6 measured joint positions (m, rad)
78 // - the 8 values of s - s*
79
80 // Get the user login name
81 std::string username = vpIoTools::getUserName();
82
83 // Create a log filename to save velocities...
84 std::string logdirname = "/tmp/" + username;
85
86 // Test if the output path exist. If no try to create it
87 if (vpIoTools::checkDirectory(logdirname) == false) {
88 try {
89 // Create the dirname
90 vpIoTools::makeDirectory(logdirname);
91 }
92 catch (...) {
93 std::cerr << std::endl << "ERROR:" << std::endl;
94 std::cerr << " Cannot create " << logdirname << std::endl;
95 return EXIT_FAILURE;
96 }
97 }
98 std::string logfilename = logdirname + "/log.dat";
99
100 // Open the log file name
101 std::ofstream flog(logfilename.c_str());
102
103 try {
104 vpRealSense2 rs;
105 rs2::config config;
106 unsigned int width = 640, height = 480, fps = 60;
107 config.enable_stream(RS2_STREAM_COLOR, width, height, RS2_FORMAT_RGBA8, fps);
108 config.enable_stream(RS2_STREAM_DEPTH, width, height, RS2_FORMAT_Z16, fps);
109 config.enable_stream(RS2_STREAM_INFRARED, width, height, RS2_FORMAT_Y8, fps);
110 rs.open(config);
111
113
114 // Warm up camera
115 for (size_t i = 0; i < 10; ++i) {
116 rs.acquire(I);
117 }
118
119 std::shared_ptr<vpDisplay> d = vpDisplayFactory::createDisplay(I, 100, 100, "Current image");
120
123
124 std::cout << "-------------------------------------------------------" << std::endl;
125 std::cout << " Test program for vpServo " << std::endl;
126 std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl;
127 std::cout << " Use of the Afma6 robot " << std::endl;
128 std::cout << " Interaction matrix computed with the desired features " << std::endl;
129
130 std::cout << " task : servo 4 points on a square with dimension " << L << " meters" << std::endl;
131 std::cout << "-------------------------------------------------------" << std::endl;
132
133 std::vector<vpDot2> dot(4);
134
135 std::cout << "Click on the 4 dots clockwise starting from upper/left dot..." << std::endl;
136 for (size_t i = 0; i < dot.size(); ++i) {
137 dot[i].initTracking(I);
138 vpImagePoint cog = dot[i].getCog();
141 }
142
143 vpRobotAfma6 robot;
145
146 // Load the end-effector to camera frame transformation obtained
147 // using a camera intrinsic model with distortion
148 robot.init(vpAfma6::TOOL_INTEL_D435_CAMERA, projModel);
149
150 // Get camera intrinsics
152 robot.getCameraParameters(cam, I);
153
154 // Sets the current position of the visual feature
155 std::vector<vpFeaturePoint> s(4);
156 for (size_t i = 0; i < s.size(); ++i) {
157 vpFeatureBuilder::create(s[i], cam, dot[i]); // Retrieve x,y of the vpFeaturePoint structure
158 }
159
160 // Set the position of the square target in a frame which origin is
161 // centered in the middle of the square
162 vpPoint point[4];
163 point[0].setWorldCoordinates(-L, -L, 0);
164 point[1].setWorldCoordinates(+L, -L, 0);
165 point[2].setWorldCoordinates(+L, +L, 0);
166 point[3].setWorldCoordinates(-L, +L, 0);
167
168 // Initialise a desired pose to compute s*, the desired 2D point features
170 vpTranslationVector c_t_o(0, 0, 0.5); // tz = 0.5 meter
171 vpRxyzVector c_r_o(vpMath::rad(0), vpMath::rad(0), vpMath::rad(0)); // No rotations
172 vpRotationMatrix c_R_o(c_r_o); // Build the rotation matrix
173 c_M_o.buildFrom(c_t_o, c_R_o); // Build the homogeneous matrix
174
175 // Sets the desired position of the 2D visual feature
176 std::vector<vpFeaturePoint> s_d(4);
177 // Compute the desired position of the features from the desired pose
178 for (size_t i = 0; i < s_d.size(); ++i) {
179 vpColVector cP, p;
180 point[i].changeFrame(c_M_o, cP);
181 point[i].projection(cP, p);
182
183 s_d[i].set_x(p[0]);
184 s_d[i].set_y(p[1]);
185 s_d[i].set_Z(cP[2]);
186 }
187
188 // Define the task
189 // - we want an eye-in-hand control law
190 // - robot is controlled in the camera frame
191 // - Interaction matrix is computed with the desired visual features
194 task.setInteractionMatrixType(vpServo::DESIRED, vpServo::PSEUDO_INVERSE);
195
196 // We want to see a point on a point
197 for (size_t i = 0; i < s.size(); ++i) {
198 task.addFeature(s[i], s_d[i]);
199 }
200
201 // Set the proportional gain
202 task.setLambda(0.4);
203
204 // Display task information
205 task.print();
206
207 // Initialise the velocity control of the robot
208 robot.setRobotState(vpRobot::STATE_VELOCITY_CONTROL);
209
210 std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
211
212 bool quit = false;
213 while (!quit) {
214 // Acquire a new image from the camera
215 rs.acquire(I);
216
217 // Display this image
219
220 // For each point...
221 for (size_t i = 0; i < dot.size(); ++i) {
222 // Achieve the tracking of the dot in the image
223 dot[i].track(I);
224 // Update the point feature from the dot location
225 vpFeatureBuilder::create(s[i], cam, dot[i]);
226 }
227
228 // Compute the visual servoing skew vector
229 vpColVector v = task.computeControlLaw();
230
231 // Display the current and desired feature points in the image display
232 vpServoDisplay::display(task, cam, I);
233
234 // Apply the computed camera velocities to the robot
235 robot.setVelocity(vpRobot::CAMERA_FRAME, v);
236
237 // Save velocities applied to the robot in the log file
238 // v[0], v[1], v[2] correspond to camera translation velocities in m/s
239 // v[3], v[4], v[5] correspond to camera rotation velocities in rad/s
240 flog << v[0] << " " << v[1] << " " << v[2] << " " << v[3] << " " << v[4] << " " << v[5] << " ";
241
242 // Get the measured joint velocities of the robot
243 vpColVector qvel;
244 robot.getVelocity(vpRobot::CAMERA_FRAME, qvel);
245 // Save measured camera velocities of the robot in the log file:
246 // - qvel[0], qvel[1], qvel[2] correspond to measured camera translation
247 // velocities in m/s
248 // - qvel[3], qvel[4], qvel[5] correspond to measured camera rotation
249 // velocities in rad/s
250 flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " " << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
251
252 // Get the measured joint positions of the robot
253 vpColVector q;
254 robot.getPosition(vpRobot::ARTICULAR_FRAME, q);
255 // Save measured joint positions of the robot in the log file
256 // - q[0], q[1], q[2] correspond to measured joint translation
257 // positions in m
258 // - q[3], q[4], q[5] correspond to measured joint rotation
259 // positions in rad
260 flog << q[0] << " " << q[1] << " " << q[2] << " " << q[3] << " " << q[4] << " " << q[5] << " ";
261
262 // Save feature error (s-s*) for the 4 feature points. For each feature
263 // point, we have 2 errors (along x and y axis). This error is
264 // expressed in meters in the camera frame
265 flog << (task.getError()).t() << std::endl;
266
267 vpDisplay::displayText(I, 20, 20, "Click to quit...", vpColor::red);
268 if (vpDisplay::getClick(I, false)) {
269 quit = true;
270 }
271 // Flush the display
273 }
274
275 // Close the log file
276 flog.close();
277
278 // Display task information
279 task.print();
280
281 return EXIT_SUCCESS;
282 }
283 catch (const vpException &e) {
284 // Close the log file
285 flog.close();
286 std::cout << "Visual servo failed with exception: " << e << std::endl;
287 return EXIT_FAILURE;
288 }
289}
290
291#else
292int main()
293{
294 std::cout << "You do not have an afma6 robot connected to your computer..." << std::endl;
295 return EXIT_SUCCESS;
296}
297
298#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
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 projection(const vpColVector &_cP, vpColVector &_p) const VP_OVERRIDE
Definition vpPoint.cpp:252
void changeFrame(const vpHomogeneousMatrix &cMo, vpColVector &cP) const VP_OVERRIDE
Definition vpPoint.cpp:273
void setWorldCoordinates(double oX, double oY, double oZ)
Definition vpPoint.cpp:116
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
@ DESIRED
Definition vpServo.h:223
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.