Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
servoViper650FourPoints2DCamVelocityLs_cur-SR300.cpp
1/****************************************************************************
2 *
3 * ViSP, open source Visual Servoing Platform software.
4 * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
5 *
6 * This software is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 * See the file LICENSE.txt at the root directory of this source
11 * distribution for additional information about the GNU GPL.
12 *
13 * For using ViSP with software that can not be combined with the GNU
14 * GPL, please contact Inria about acquiring a ViSP Professional
15 * Edition License.
16 *
17 * See https://visp.inria.fr for more information.
18 *
19 * This software was developed at:
20 * Inria Rennes - Bretagne Atlantique
21 * Campus Universitaire de Beaulieu
22 * 35042 Rennes Cedex
23 * France
24 *
25 * If you have questions regarding the use of this file, please contact
26 * Inria at visp@inria.fr
27 *
28 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30 *
31 * Description:
32 * tests the control law
33 * eye-in-hand control
34 * velocity computed in the camera frame
35 *
36*****************************************************************************/
56#include <fstream>
57#include <iostream>
58#include <sstream>
59#include <stdio.h>
60#include <stdlib.h>
61
62#include <visp3/core/vpConfig.h>
63
64#if defined(VISP_HAVE_VIPER650) && defined(VISP_HAVE_REALSENSE) && defined(VISP_HAVE_X11)
65
66#include <visp3/blob/vpDot2.h>
67#include <visp3/core/vpHomogeneousMatrix.h>
68#include <visp3/core/vpIoTools.h>
69#include <visp3/core/vpPoint.h>
70#include <visp3/gui/vpDisplayX.h>
71#include <visp3/robot/vpRobotViper650.h>
72#include <visp3/sensor/vpRealSense.h>
73#include <visp3/vision/vpPose.h>
74#include <visp3/visual_features/vpFeatureBuilder.h>
75#include <visp3/visual_features/vpFeaturePoint.h>
76#include <visp3/vs/vpServo.h>
77#include <visp3/vs/vpServoDisplay.h>
78
79#define L 0.05 // to deal with a 10cm by 10cm square
80
99void compute_pose(std::vector<vpPoint> &point, std::vector<vpDot2> &dot, vpCameraParameters cam,
100 vpHomogeneousMatrix &cMo, bool init)
101{
102 vpPose pose;
103
104 for (size_t i = 0; i < point.size(); i++) {
105
106 double x = 0, y = 0;
107 vpImagePoint cog = dot[i].getCog();
109 y); // pixel to meter conversion
110 point[i].set_x(x); // projection perspective p
111 point[i].set_y(y);
112 pose.addPoint(point[i]);
113 }
114
115 if (init == true) {
117 } else {
119 }
120}
121
122int main()
123{
124 // Log file creation in /tmp/$USERNAME/log.dat
125 // This file contains by line:
126 // - the 6 computed camera velocities (m/s, rad/s) to achieve the task
127 // - the 6 mesured joint velocities (m/s, rad/s)
128 // - the 6 mesured joint positions (m, rad)
129 // - the 8 values of s - s*
130 std::string username;
131 // Get the user login name
132 vpIoTools::getUserName(username);
133
134 // Create a log filename to save velocities...
135 std::string logdirname;
136 logdirname = "/tmp/" + username;
137
138 // Test if the output path exist. If no try to create it
139 if (vpIoTools::checkDirectory(logdirname) == false) {
140 try {
141 // Create the dirname
142 vpIoTools::makeDirectory(logdirname);
143 } catch (...) {
144 std::cerr << std::endl << "ERROR:" << std::endl;
145 std::cerr << " Cannot create " << logdirname << std::endl;
146 return EXIT_FAILURE;
147 }
148 }
149 std::string logfilename;
150 logfilename = logdirname + "/log.dat";
151
152 // Open the log file name
153 std::ofstream flog(logfilename.c_str());
154
155 try {
156 vpRobotViper650 robot;
157
158 // Load the end-effector to camera frame transformation from SR300-eMc.cnf
159 // file
160 robot.init(vpRobotViper650::TOOL_CUSTOM, "./SR300-eMc.cnf");
162 robot.get_eMc(eMc);
163 std::cout << "Camera extrinsic parameters (eMc): \n" << eMc << std::endl;
164
165 vpServo task;
166
168
169 vpRealSense g;
170 // Enable the RealSense device to acquire only color images with size
171 // 640x480
172 g.setEnableStream(rs::stream::color, true);
173 g.setEnableStream(rs::stream::depth, false);
174 g.setEnableStream(rs::stream::infrared, false);
175 g.setEnableStream(rs::stream::infrared2, false);
176 g.setStreamSettings(rs::stream::color, vpRealSense::vpRsStreamParams(640, 480, rs::format::rgba8, 30));
177 g.open();
178
179 // Update camera parameters
182 std::cout << "Camera intrinsic parameters: \n" << cam << std::endl;
183
184 g.acquire(I);
185
186 vpDisplayX display(I, 100, 100, "Current image");
189
190 std::vector<vpDot2> dot(4);
191
192 std::cout << "Click on the 4 dots clockwise starting from upper/left dot..." << std::endl;
193
194 for (size_t i = 0; i < dot.size(); i++) {
195 dot[i].setGraphics(true);
196 dot[i].initTracking(I);
197 vpImagePoint cog = dot[i].getCog();
200 }
201
202 // Sets the current position of the visual feature
203 vpFeaturePoint p[4];
204 for (size_t i = 0; i < dot.size(); i++)
205 vpFeatureBuilder::create(p[i], cam, dot[i]); // retrieve x,y of the vpFeaturePoint structure
206
207 // Set the position of the square target in a frame which origin is
208 // centered in the middle of the square
209 std::vector<vpPoint> point(4);
210 point[0].setWorldCoordinates(-L, -L, 0);
211 point[1].setWorldCoordinates(L, -L, 0);
212 point[2].setWorldCoordinates(L, L, 0);
213 point[3].setWorldCoordinates(-L, L, 0);
214
215 // Compute target initial pose
217 compute_pose(point, dot, cam, cMo, true);
218 std::cout << "Initial camera pose (cMo): \n" << cMo << std::endl;
219
220 // Initialise a desired pose to compute s*, the desired 2D point features
221 vpHomogeneousMatrix cMo_d(vpTranslationVector(0, 0, 0.5), // tz = 0.5 meter
222 vpRotationMatrix()); // no rotation
223
224 // Sets the desired position of the 2D visual feature
225 vpFeaturePoint pd[4];
226 // Compute the desired position of the features from the desired pose
227 for (int i = 0; i < 4; i++) {
228 vpColVector cP, p;
229 point[i].changeFrame(cMo_d, cP);
230 point[i].projection(cP, p);
231
232 pd[i].set_x(p[0]);
233 pd[i].set_y(p[1]);
234 pd[i].set_Z(cP[2]);
235 }
236
237 // We want to see a point on a point
238 for (size_t i = 0; i < dot.size(); i++)
239 task.addFeature(p[i], pd[i]);
240
241 // Set the proportional gain
242 task.setLambda(0.3);
243
244 // Define the task
245 // - we want an eye-in-hand control law
246 // - camera velocities are computed
249 task.print();
250
251 // Initialise the velocity control of the robot
253
254 std::cout << "\nHit CTRL-C or click in the image to stop the loop...\n" << std::flush;
255 for (;;) {
256 // Acquire a new image from the camera
257 g.acquire(I);
258
259 // Display this image
261
262 try {
263 // For each point...
264 for (size_t i = 0; i < dot.size(); i++) {
265 // Achieve the tracking of the dot in the image
266 dot[i].track(I);
267 // Display a green cross at the center of gravity position in the
268 // image
269 vpImagePoint cog = dot[i].getCog();
271 }
272 } catch (...) {
273 std::cout << "Error detected while tracking visual features.." << std::endl;
274 break;
275 }
276
277 // During the servo, we compute the pose using a non linear method. For
278 // the initial pose used in the non linear minimization we use the pose
279 // computed at the previous iteration.
280 compute_pose(point, dot, cam, cMo, false);
281
282 for (size_t i = 0; i < dot.size(); i++) {
283 // Update the point feature from the dot location
284 vpFeatureBuilder::create(p[i], cam, dot[i]);
285 // Set the feature Z coordinate from the pose
286 vpColVector cP;
287 point[i].changeFrame(cMo, cP);
288
289 p[i].set_Z(cP[2]);
290 }
291
292 // Compute the visual servoing skew vector
294
295 // Display the current and desired feature points in the image display
296 vpServoDisplay::display(task, cam, I);
297
298 // Apply the computed joint velocities to the robot
300
301 // Save velocities applied to the robot in the log file
302 // v[0], v[1], v[2] correspond to camera translation velocities in m/s
303 // v[3], v[4], v[5] correspond to camera rotation velocities in rad/s
304 flog << v[0] << " " << v[1] << " " << v[2] << " " << v[3] << " " << v[4] << " " << v[5] << " ";
305
306 // Get the measured joint velocities of the robot
307 vpColVector qvel;
309 // Save measured joint velocities of the robot in the log file:
310 // - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
311 // velocities in m/s
312 // - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
313 // velocities in rad/s
314 flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " " << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
315
316 // Get the measured joint positions of the robot
317 vpColVector q;
318 robot.getPosition(vpRobot::ARTICULAR_FRAME, q);
319 // Save measured joint positions of the robot in the log file
320 // - q[0], q[1], q[2] correspond to measured joint translation
321 // positions in m
322 // - q[3], q[4], q[5] correspond to measured joint rotation
323 // positions in rad
324 flog << q[0] << " " << q[1] << " " << q[2] << " " << q[3] << " " << q[4] << " " << q[5] << " ";
325
326 // Save feature error (s-s*) for the 4 feature points. For each feature
327 // point, we have 2 errors (along x and y axis). This error is
328 // expressed in meters in the camera frame
329 flog << task.getError() << std::endl;
330
331 vpDisplay::displayText(I, 10, 10, "Click to quit...", vpColor::red);
332 if (vpDisplay::getClick(I, false))
333 break;
334
335 // Flush the display
337
338 // std::cout << "\t\t || s - s* || = " << ( task.getError()
339 // ).sumSquare() << std::endl;
340 }
341
342 std::cout << "Display task information: " << std::endl;
343 task.print();
344 flog.close(); // Close the log file
345 return EXIT_SUCCESS;
346 } catch (const vpException &e) {
347 flog.close(); // Close the log file
348 std::cout << "Catch an exception: " << e.getMessage() << std::endl;
349 return EXIT_FAILURE;
350 }
351}
352
353#else
354int main()
355{
356 std::cout << "You do not have an Viper 650 robot connected to your computer..." << std::endl;
357 return EXIT_SUCCESS;
358}
359#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:211
static const vpColor blue
Definition vpColor.h:217
static const vpColor green
Definition vpColor.h:214
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition vpDisplayX.h:132
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:59
const char * getMessage() const
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:135
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:81
void addPoint(const vpPoint &P)
Definition vpPose.cpp:140
@ DEMENTHON_LAGRANGE_VIRTUAL_VS
Definition vpPose.h:102
@ VIRTUAL_VS
Definition vpPose.h:96
bool computePose(vpPoseMethodType method, vpHomogeneousMatrix &cMo, bool(*func)(const vpHomogeneousMatrix &)=NULL)
Definition vpPose.cpp:469
void setStreamSettings(const rs::stream &stream, const rs::preset &preset)
vpCameraParameters getCameraParameters(const rs::stream &stream, vpCameraParameters::vpCameraParametersProjType type=vpCameraParameters::perspectiveProjWithDistortion) const
void acquire(std::vector< vpColVector > &pointcloud)
void setEnableStream(const rs::stream &stream, bool status)
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
void getVelocity(const vpRobot::vpControlFrameType frame, vpColVector &velocity)
Control of Irisa's Viper S650 robot named Viper650.
@ ARTICULAR_FRAME
Definition vpRobot.h:76
@ CAMERA_FRAME
Definition vpRobot.h:80
@ STATE_VELOCITY_CONTROL
Initialize the velocity controller.
Definition vpRobot.h:64
virtual vpRobotStateType setRobotState(const vpRobot::vpRobotStateType newState)
Definition vpRobot.cpp:198
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)
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition vpServo.cpp:564
@ EYEINHAND_CAMERA
Definition vpServo.h:151
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition vpServo.cpp:299
void setLambda(double c)
Definition vpServo.h:403
void setServo(const vpServoType &servo_type)
Definition vpServo.cpp:210
vpColVector getError() const
Definition vpServo.h:276
@ PSEUDO_INVERSE
Definition vpServo.h:199
vpColVector computeControlLaw()
Definition vpServo.cpp:930
@ CURRENT
Definition vpServo.h:179
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition vpServo.cpp:487
Class that consider the case of a translation vector.