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