Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
servoAfma6Point2DArtVelocity.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 articular
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
62int main()
63{
64#ifdef ENABLE_VISP_NAMESPACE
65 using namespace VISP_NAMESPACE_NAME;
66#endif
67
68 // Log file creation in /tmp/$USERNAME/log.dat
69 // This file contains by line:
70 // - the 6 computed joint velocities (m/s, rad/s) to achieve the task
71 // - the 6 measured joint velocities (m/s, rad/s)
72 // - the 6 measured joint positions (m, rad)
73 // - the 2 values of s - s*
74
75 // Get the user login name
76 std::string username = vpIoTools::getUserName();
77
78 // Create a log filename to save velocities...
79 std::string logdirname = "/tmp/" + username;
80
81 // Test if the output path exist. If no try to create it
82 if (vpIoTools::checkDirectory(logdirname) == false) {
83 try {
84 // Create the dirname
85 vpIoTools::makeDirectory(logdirname);
86 }
87 catch (...) {
88 std::cerr << std::endl << "ERROR:" << std::endl;
89 std::cerr << " Cannot create " << logdirname << std::endl;
90 return EXIT_FAILURE;
91 }
92 }
93 std::string logfilename = logdirname + "/log.dat";
94
95 // Open the log file name
96 std::ofstream flog(logfilename.c_str());
97
98 try {
99 vpRealSense2 rs;
100 rs2::config config;
101 unsigned int width = 640, height = 480, fps = 60;
102 config.enable_stream(RS2_STREAM_COLOR, width, height, RS2_FORMAT_RGBA8, fps);
103 config.enable_stream(RS2_STREAM_DEPTH, width, height, RS2_FORMAT_Z16, fps);
104 config.enable_stream(RS2_STREAM_INFRARED, width, height, RS2_FORMAT_Y8, fps);
105 rs.open(config);
106
108
109 // Warm up camera
110 for (size_t i = 0; i < 10; ++i) {
111 rs.acquire(I);
112 }
113
114 std::shared_ptr<vpDisplay> d = vpDisplayFactory::createDisplay(I, 100, 100, "Current image");
115
118
119 std::cout << "-------------------------------------------------------" << std::endl;
120 std::cout << " Test program for vpServo " << std::endl;
121 std::cout << " Eye-in-hand task control, velocity computed in the joint space" << std::endl;
122 std::cout << " Use of the Afma6 robot " << std::endl;
123 std::cout << " task : servo a point " << std::endl;
124 std::cout << "-------------------------------------------------------" << std::endl;
125
126 vpDot dot;
127 dot.setGraphics(true);
128
129 std::cout << "Click on a dot..." << std::endl;
130 dot.initTracking(I);
131
132 // Get the dot cog
133 vpImagePoint cog = dot.getCog();
136
137 vpRobotAfma6 robot;
139
140 // Get camera intrinsics
142 robot.getCameraParameters(cam, I);
143
144 // Sets the current position of the visual feature
146 // Update visual feature from camera parameters and blob center of gravity
147 vpFeatureBuilder::create(s, cam, dot);
148
149 // Sets the desired position of the visual feature
150 vpFeaturePoint s_d;
151 s_d.buildFrom(0, 0, 1); // Here we consider the center of the image (x=y=0 and Z=1 meter)
152
153 // Define the task
154 // - we want an eye-in-hand control law
155 // - joint velocities are computed
158 task.setInteractionMatrixType(vpServo::DESIRED, vpServo::PSEUDO_INVERSE);
159
160 // - set the camera to end-effector velocity twist matrix transformation
162 robot.get_cVe(c_V_e);
163 task.set_cVe(c_V_e);
164
165 // - set the Jacobian (expressed in the end-effector frame)
166 vpMatrix e_J_e;
167 robot.get_eJe(e_J_e);
168 task.set_eJe(e_J_e);
169
170 // - we want to see a point on a point
171 task.addFeature(s, s_d);
172
173 // - set the gain
174 task.setLambda(0.4);
175
176 // Display task information
177 task.print();
178
179 robot.setRobotState(vpRobot::STATE_VELOCITY_CONTROL);
180
181 std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
182
183 bool quit = false;
184 while (!quit) {
185 // Acquire a new image from the camera
186 rs.acquire(I);
187
188 // Display this image
190
191 // Achieve the tracking of the dot in the image
192 dot.track(I);
193
194 // Update the point feature from the dot location
195 vpFeatureBuilder::create(s, cam, dot);
196
197 // Get the Jacobian of the robot
198 robot.get_eJe(e_J_e);
199
200 // Update this jacobian in the task structure. It will be used to
201 task.set_eJe(e_J_e);
202
203 // Compute the visual servoing skew vector (as a joint velocity)
204 // qdot = -lambda * L^+ * cVe * eJe * (s-s*)
205 vpColVector qdot = task.computeControlLaw();
206
207 // Display the current and desired feature points in the image display
208 vpServoDisplay::display(task, cam, I);
209
210 // Apply the computed joint velocities to the robot
211 robot.setVelocity(vpRobot::JOINT_STATE, qdot);
212
213 // Save velocities applied to the robot in the log file
214 // qdot[0], qdot[1], qdot[2] correspond to joint translation velocities in m/s
215 // qdot[3], qdot[4], qdot[5] correspond to joint rotation velocities in rad/s
216 flog << qdot[0] << " " << qdot[1] << " " << qdot[2] << " " << qdot[3] << " " << qdot[4] << " " << qdot[5] << " ";
217
218 // Get the measured joint velocities of the robot
219 vpColVector qdot_mes;
220 robot.getVelocity(vpRobot::JOINT_STATE, qdot_mes);
221 // Save measured joint velocities of the robot in the log file:
222 // - qdot_mes[0], qdot_mes[1], qdot_mes[2] correspond to measured joint translation velocities in m/s
223 // - qdot_mes[3], qdot_mes[4], qdot_mes[5] correspond to measured joint rotation velocities in rad/s
224 flog << qdot_mes[0] << " " << qdot_mes[1] << " " << qdot_mes[2] << " " << qdot_mes[3] << " " << qdot_mes[4] << " " << qdot_mes[5] << " ";
225
226 // Get the measured joint positions of the robot
227 vpColVector q;
228 robot.getPosition(vpRobot::JOINT_STATE, q);
229 // Save measured joint positions of the robot in the log file
230 // - q[0], q[1], q[2] correspond to measured joint translation
231 // positions in m
232 // - q[3], q[4], q[5] correspond to measured joint rotation
233 // positions in rad
234 flog << q[0] << " " << q[1] << " " << q[2] << " " << q[3] << " " << q[4] << " " << q[5] << " ";
235
236 // Save feature error (s-s*) for the 4 feature points. For each feature
237 // point, we have 2 errors (along x and y axis). This error is
238 // expressed in meters in the camera frame
239 flog << (task.getError()).t() << std::endl;
240
241 vpDisplay::displayText(I, 20, 20, "Click to quit...", vpColor::red);
242 if (vpDisplay::getClick(I, false)) {
243 quit = true;
244 }
245 // Flush the display
247 }
248
249 // Close the log file
250 flog.close();
251
252 // Display task information
253 task.print();
254
255 return EXIT_SUCCESS;
256 }
257 catch (const vpException &e) {
258 // Close the log file
259 flog.close();
260 std::cout << "Visual servo failed with exception: " << e << std::endl;
261 return EXIT_FAILURE;
262 }
263}
264
265#else
266int main()
267{
268 std::cout << "You do not have an afma6 robot connected to your computer..." << std::endl;
269 return EXIT_SUCCESS;
270}
271#endif
@ TOOL_INTEL_D435_CAMERA
Definition vpAfma6.h:129
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 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 initTracking(const vpImage< unsigned char > &I)
Definition vpDot.cpp:630
void setGraphics(bool activate)
Definition vpDot.h:362
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 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.
void init(void)
@ 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.