Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
servoViper650FourPoints2DCamVelocityLs_cur-SR300.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 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 */
53
54#include <fstream>
55#include <iostream>
56#include <sstream>
57#include <stdio.h>
58#include <stdlib.h>
59
60#include <visp3/core/vpConfig.h>
61
62#if defined(VISP_HAVE_VIPER650) && defined(VISP_HAVE_REALSENSE2) && defined(VISP_HAVE_X11)
63
64#include <visp3/blob/vpDot2.h>
65#include <visp3/core/vpHomogeneousMatrix.h>
66#include <visp3/core/vpIoTools.h>
67#include <visp3/core/vpPoint.h>
68#include <visp3/gui/vpDisplayFactory.h>
69#include <visp3/robot/vpRobotViper650.h>
70#include <visp3/sensor/vpRealSense2.h>
71#include <visp3/vision/vpPose.h>
72#include <visp3/visual_features/vpFeatureBuilder.h>
73#include <visp3/visual_features/vpFeaturePoint.h>
74#include <visp3/vs/vpServo.h>
75#include <visp3/vs/vpServoDisplay.h>
76
77#define L 0.05 // to deal with a 10cm by 10cm square
78
79#ifdef ENABLE_VISP_NAMESPACE
80using namespace VISP_NAMESPACE_NAME;
81#endif
82
101void compute_pose(std::vector<vpPoint> &point, std::vector<vpDot2> &dot, vpCameraParameters cam,
102 vpHomogeneousMatrix &cMo, bool init)
103{
104 vpPose pose;
105
106 for (size_t i = 0; i < point.size(); i++) {
107
108 double x = 0, y = 0;
109 vpImagePoint cog = dot[i].getCog();
111 y); // pixel to meter conversion
112 point[i].set_x(x); // projection perspective p
113 point[i].set_y(y);
114 pose.addPoint(point[i]);
115 }
116
117 if (init == true) {
119 }
120 else {
122 }
123}
124
125int main()
126{
127 // Log file creation in /tmp/$USERNAME/log.dat
128 // This file contains by line:
129 // - the 6 computed camera velocities (m/s, rad/s) to achieve the task
130 // - the 6 measured joint velocities (m/s, rad/s)
131 // - the 6 measured joint positions (m, rad)
132 // - the 8 values of s - s*
133 std::string username;
134 // Get the user login name
135 vpIoTools::getUserName(username);
136
137 // Create a log filename to save velocities...
138 std::string logdirname;
139 logdirname = "/tmp/" + username;
140
141 // Test if the output path exist. If no try to create it
142 if (vpIoTools::checkDirectory(logdirname) == false) {
143 try {
144 // Create the dirname
145 vpIoTools::makeDirectory(logdirname);
146 }
147 catch (...) {
148 std::cerr << std::endl << "ERROR:" << std::endl;
149 std::cerr << " Cannot create " << logdirname << std::endl;
150 return EXIT_FAILURE;
151 }
152 }
153 std::string logfilename;
154 logfilename = logdirname + "/log.dat";
155
156 // Open the log file name
157 std::ofstream flog(logfilename.c_str());
158
159#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
160 std::shared_ptr<vpDisplay> display;
161#else
162 vpDisplay *display = nullptr;
163#endif
164 try {
165 vpRobotViper650 robot;
166
167 // Load the end-effector to camera frame transformation from SR300-eMc.cnf
168 // file
169 robot.init(vpRobotViper650::TOOL_CUSTOM, "./SR300-eMc.cnf");
171 robot.get_eMc(eMc);
172 std::cout << "Camera extrinsic parameters (eMc): \n" << eMc << std::endl;
173
175
177
178 // Enable the RealSense device to acquire only color images with size 640x480
179 vpRealSense2 g;
180 rs2::config config;
181 config.disable_stream(RS2_STREAM_DEPTH);
182 config.disable_stream(RS2_STREAM_INFRARED);
183 config.enable_stream(RS2_STREAM_COLOR, 640, 480, RS2_FORMAT_RGBA8, 30);
184 g.open(config);
185
186 // Update camera parameters
188 std::cout << "Camera intrinsic parameters: \n" << cam << std::endl;
189
190 g.acquire(I);
191
192#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
193 display = vpDisplayFactory::createDisplay(I, 100, 100, "Current image");
194#else
195 display = vpDisplayFactory::allocateDisplay(I, 100, 100, "Current image");
196#endif
199
200 std::vector<vpDot2> dot(4);
201
202 std::cout << "Click on the 4 dots clockwise starting from upper/left dot..." << std::endl;
203
204 for (size_t i = 0; i < dot.size(); i++) {
205 dot[i].setGraphics(true);
206 dot[i].initTracking(I);
207 vpImagePoint cog = dot[i].getCog();
210 }
211
212 // Sets the current position of the visual feature
213 vpFeaturePoint p[4];
214 for (size_t i = 0; i < dot.size(); i++)
215 vpFeatureBuilder::create(p[i], cam, dot[i]); // retrieve x,y of the vpFeaturePoint structure
216
217 // Set the position of the square target in a frame which origin is
218 // centered in the middle of the square
219 std::vector<vpPoint> point(4);
220 point[0].setWorldCoordinates(-L, -L, 0);
221 point[1].setWorldCoordinates(L, -L, 0);
222 point[2].setWorldCoordinates(L, L, 0);
223 point[3].setWorldCoordinates(-L, L, 0);
224
225 // Compute target initial pose
227 compute_pose(point, dot, cam, cMo, true);
228 std::cout << "Initial camera pose (cMo): \n" << cMo << std::endl;
229
230 // Initialise a desired pose to compute s*, the desired 2D point features
231 vpHomogeneousMatrix cMo_d(vpTranslationVector(0, 0, 0.5), // tz = 0.5 meter
232 vpRotationMatrix()); // no rotation
233
234 // Sets the desired position of the 2D visual feature
235 vpFeaturePoint pd[4];
236 // Compute the desired position of the features from the desired pose
237 for (int i = 0; i < 4; i++) {
238 vpColVector cP, p;
239 point[i].changeFrame(cMo_d, cP);
240 point[i].projection(cP, p);
241
242 pd[i].set_x(p[0]);
243 pd[i].set_y(p[1]);
244 pd[i].set_Z(cP[2]);
245 }
246
247 // We want to see a point on a point
248 for (size_t i = 0; i < dot.size(); i++)
249 task.addFeature(p[i], pd[i]);
250
251 // Set the proportional gain
252 task.setLambda(0.3);
253
254 // Define the task
255 // - we want an eye-in-hand control law
256 // - camera velocities are computed
258 task.setInteractionMatrixType(vpServo::CURRENT, vpServo::PSEUDO_INVERSE);
259 task.print();
260
261 // Initialise the velocity control of the robot
262 robot.setRobotState(vpRobot::STATE_VELOCITY_CONTROL);
263
264 std::cout << "\nHit CTRL-C or click in the image to stop the loop...\n" << std::flush;
265 for (;;) {
266 // Acquire a new image from the camera
267 g.acquire(I);
268
269 // Display this image
271
272 try {
273 // For each point...
274 for (size_t i = 0; i < dot.size(); i++) {
275 // Achieve the tracking of the dot in the image
276 dot[i].track(I);
277 // Display a green cross at the center of gravity position in the
278 // image
279 vpImagePoint cog = dot[i].getCog();
281 }
282 }
283 catch (...) {
284 std::cout << "Error detected while tracking visual features.." << std::endl;
285 break;
286 }
287
288 // During the servo, we compute the pose using a non linear method. For
289 // the initial pose used in the non linear minimization we use the pose
290 // computed at the previous iteration.
291 compute_pose(point, dot, cam, cMo, false);
292
293 for (size_t i = 0; i < dot.size(); i++) {
294 // Update the point feature from the dot location
295 vpFeatureBuilder::create(p[i], cam, dot[i]);
296 // Set the feature Z coordinate from the pose
297 vpColVector cP;
298 point[i].changeFrame(cMo, cP);
299
300 p[i].set_Z(cP[2]);
301 }
302
303 // Compute the visual servoing skew vector
304 vpColVector v = task.computeControlLaw();
305
306 // Display the current and desired feature points in the image display
307 vpServoDisplay::display(task, cam, I);
308
309 // Apply the computed joint velocities to the robot
310 robot.setVelocity(vpRobot::CAMERA_FRAME, v);
311
312 // Save velocities applied to the robot in the log file
313 // v[0], v[1], v[2] correspond to camera translation velocities in m/s
314 // v[3], v[4], v[5] correspond to camera rotation velocities in rad/s
315 flog << v[0] << " " << v[1] << " " << v[2] << " " << v[3] << " " << v[4] << " " << v[5] << " ";
316
317 // Get the measured joint velocities of the robot
318 vpColVector qvel;
319 robot.getVelocity(vpRobot::ARTICULAR_FRAME, qvel);
320 // Save measured joint velocities of the robot in the log file:
321 // - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
322 // velocities in m/s
323 // - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
324 // velocities in rad/s
325 flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " " << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
326
327 // Get the measured joint positions of the robot
328 vpColVector q;
329 robot.getPosition(vpRobot::ARTICULAR_FRAME, q);
330 // Save measured joint positions of the robot in the log file
331 // - q[0], q[1], q[2] correspond to measured joint translation
332 // positions in m
333 // - q[3], q[4], q[5] correspond to measured joint rotation
334 // positions in rad
335 flog << q[0] << " " << q[1] << " " << q[2] << " " << q[3] << " " << q[4] << " " << q[5] << " ";
336
337 // Save feature error (s-s*) for the 4 feature points. For each feature
338 // point, we have 2 errors (along x and y axis). This error is
339 // expressed in meters in the camera frame
340 flog << task.getError() << std::endl;
341
342 vpDisplay::displayText(I, 10, 10, "Click to quit...", vpColor::red);
343 if (vpDisplay::getClick(I, false))
344 break;
345
346 // Flush the display
348
349 // std::cout << "\t\t || s - s* || = " << ( task.getError()
350 // ).sumSquare() << std::endl;
351 }
352
353 std::cout << "Display task information: " << std::endl;
354 task.print();
355 flog.close(); // Close the log file
356#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
357 if (display != nullptr) {
358 delete display;
359 }
360#endif
361 return EXIT_SUCCESS;
362 }
363 catch (const vpException &e) {
364 flog.close(); // Close the log file
365 std::cout << "Catch an exception: " << e.getMessage() << std::endl;
366#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
367 if (display != nullptr) {
368 delete display;
369 }
370#endif
371 return EXIT_FAILURE;
372 }
373}
374
375#else
376int main()
377{
378 std::cout << "You do not have an Viper 650 robot connected to your computer..." << std::endl;
379 return EXIT_SUCCESS;
380}
381#endif
Generic class defining intrinsic camera parameters.
@ perspectiveProjWithDistortion
Perspective projection with distortion model.
Implementation of column vector and the associated operations.
static const vpColor red
Definition vpColor.h:198
static const vpColor blue
Definition vpColor.h:204
static const vpColor green
Definition vpColor.h:201
Class that defines generic functionalities for display.
Definition vpDisplay.h:171
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...
void set_y(double y)
void set_x(double x)
void set_Z(double Z)
Implementation of an homogeneous matrix and operations on such kind of matrices.
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 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
vpCameraParameters getCameraParameters(const rs2_stream &stream, vpCameraParameters::vpCameraParametersProjType type=vpCameraParameters::perspectiveProjWithDistortion, int index=-1) const
void acquire(vpImage< unsigned char > &grey, double *ts=nullptr)
bool open(const rs2::config &cfg=rs2::config())
Control of Irisa's Viper S650 robot named Viper650.
@ 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.
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.
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.