Visual Servoing Platform version 3.5.0
servoViper650FourPoints2DCamVelocityLs_cur-SR300.cpp
1/****************************************************************************
2 *
3 * ViSP, open source Visual Servoing Platform software.
4 * Copyright (C) 2005 - 2019 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 http://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 * Authors:
37 * Fabien Spindler
38 *
39 *****************************************************************************/
59#include <fstream>
60#include <iostream>
61#include <sstream>
62#include <stdio.h>
63#include <stdlib.h>
64
65#include <visp3/core/vpConfig.h>
66
67#if defined(VISP_HAVE_VIPER650) && defined(VISP_HAVE_REALSENSE) && defined(VISP_HAVE_X11)
68
69#include <visp3/blob/vpDot2.h>
70#include <visp3/core/vpHomogeneousMatrix.h>
71#include <visp3/core/vpIoTools.h>
72#include <visp3/core/vpPoint.h>
73#include <visp3/gui/vpDisplayX.h>
74#include <visp3/robot/vpRobotViper650.h>
75#include <visp3/sensor/vpRealSense.h>
76#include <visp3/vision/vpPose.h>
77#include <visp3/visual_features/vpFeatureBuilder.h>
78#include <visp3/visual_features/vpFeaturePoint.h>
79#include <visp3/vs/vpServo.h>
80#include <visp3/vs/vpServoDisplay.h>
81
82#define L 0.05 // to deal with a 10cm by 10cm square
83
102void compute_pose(std::vector<vpPoint> &point, std::vector<vpDot2> &dot, vpCameraParameters cam,
103 vpHomogeneousMatrix &cMo, bool init)
104{
105 vpHomogeneousMatrix cMo_dementhon; // computed pose with dementhon method
106 vpHomogeneousMatrix cMo_lagrange; // computed pose with lagrange method
107 vpPose pose;
108
109 for (size_t i = 0; i < point.size(); i++) {
110
111 double x = 0, y = 0;
112 vpImagePoint cog = dot[i].getCog();
114 y); // pixel to meter conversion
115 point[i].set_x(x); // projection perspective p
116 point[i].set_y(y);
117 pose.addPoint(point[i]);
118 }
119
120 if (init == true) {
121 pose.computePose(vpPose::DEMENTHON, cMo_dementhon);
122 // Compute and return the residual expressed in meter for the pose matrix
123 double residual_dementhon = pose.computeResidual(cMo_dementhon);
124 pose.computePose(vpPose::LAGRANGE, cMo_lagrange);
125 double residual_lagrange = pose.computeResidual(cMo_lagrange);
126
127 // Select the best pose to initialize the lowe pose computation
128 if (residual_lagrange < residual_dementhon)
129 cMo = cMo_lagrange;
130 else
131 cMo = cMo_dementhon;
132 }
133
134 pose.computePose(vpPose::LOWE, cMo);
135}
136
137int main()
138{
139 // Log file creation in /tmp/$USERNAME/log.dat
140 // This file contains by line:
141 // - the 6 computed camera velocities (m/s, rad/s) to achieve the task
142 // - the 6 mesured joint velocities (m/s, rad/s)
143 // - the 6 mesured joint positions (m, rad)
144 // - the 8 values of s - s*
145 std::string username;
146 // Get the user login name
147 vpIoTools::getUserName(username);
148
149 // Create a log filename to save velocities...
150 std::string logdirname;
151 logdirname = "/tmp/" + username;
152
153 // Test if the output path exist. If no try to create it
154 if (vpIoTools::checkDirectory(logdirname) == false) {
155 try {
156 // Create the dirname
157 vpIoTools::makeDirectory(logdirname);
158 } catch (...) {
159 std::cerr << std::endl << "ERROR:" << std::endl;
160 std::cerr << " Cannot create " << logdirname << std::endl;
161 return (-1);
162 }
163 }
164 std::string logfilename;
165 logfilename = logdirname + "/log.dat";
166
167 // Open the log file name
168 std::ofstream flog(logfilename.c_str());
169
170 try {
171 vpRobotViper650 robot;
172
173 // Load the end-effector to camera frame transformation from SR300-eMc.cnf
174 // file
175 robot.init(vpRobotViper650::TOOL_CUSTOM, "./SR300-eMc.cnf");
177 robot.get_eMc(eMc);
178 std::cout << "Camera extrinsic parameters (eMc): \n" << eMc << std::endl;
179
180 vpServo task;
181
183
184 vpRealSense g;
185 // Enable the RealSense device to acquire only color images with size
186 // 640x480
187 g.setEnableStream(rs::stream::color, true);
188 g.setEnableStream(rs::stream::depth, false);
189 g.setEnableStream(rs::stream::infrared, false);
190 g.setEnableStream(rs::stream::infrared2, false);
191 g.setStreamSettings(rs::stream::color, vpRealSense::vpRsStreamParams(640, 480, rs::format::rgba8, 30));
192 g.open();
193
194 // Update camera parameters
197 std::cout << "Camera intrinsic parameters: \n" << cam << std::endl;
198
199 g.acquire(I);
200
201 vpDisplayX display(I, 100, 100, "Current image");
204
205 std::vector<vpDot2> dot(4);
206
207 std::cout << "Click on the 4 dots clockwise starting from upper/left dot..." << std::endl;
208
209 for (size_t i = 0; i < dot.size(); i++) {
210 dot[i].setGraphics(true);
211 dot[i].initTracking(I);
212 vpImagePoint cog = dot[i].getCog();
215 }
216
217 // Sets the current position of the visual feature
218 vpFeaturePoint p[4];
219 for (size_t i = 0; i < dot.size(); i++)
220 vpFeatureBuilder::create(p[i], cam, dot[i]); // retrieve x,y of the vpFeaturePoint structure
221
222 // Set the position of the square target in a frame which origin is
223 // centered in the middle of the square
224 std::vector<vpPoint> point(4);
225 point[0].setWorldCoordinates(-L, -L, 0);
226 point[1].setWorldCoordinates(L, -L, 0);
227 point[2].setWorldCoordinates(L, L, 0);
228 point[3].setWorldCoordinates(-L, L, 0);
229
230 // Compute target initial pose
232 compute_pose(point, dot, cam, cMo, true);
233 std::cout << "Initial camera pose (cMo): \n" << cMo << std::endl;
234
235 // Initialise a desired pose to compute s*, the desired 2D point features
236 vpHomogeneousMatrix cMo_d(vpTranslationVector(0, 0, 0.5), // tz = 0.5 meter
237 vpRotationMatrix()); // no rotation
238
239 // Sets the desired position of the 2D visual feature
240 vpFeaturePoint pd[4];
241 // Compute the desired position of the features from the desired pose
242 for (int i = 0; i < 4; i++) {
243 vpColVector cP, p;
244 point[i].changeFrame(cMo_d, cP);
245 point[i].projection(cP, p);
246
247 pd[i].set_x(p[0]);
248 pd[i].set_y(p[1]);
249 pd[i].set_Z(cP[2]);
250 }
251
252 // We want to see a point on a point
253 for (size_t i = 0; i < dot.size(); i++)
254 task.addFeature(p[i], pd[i]);
255
256 // Set the proportional gain
257 task.setLambda(0.3);
258
259 // Define the task
260 // - we want an eye-in-hand control law
261 // - camera velocities are computed
264 task.print();
265
266 // Initialise the velocity control of the robot
268
269 std::cout << "\nHit CTRL-C or click in the image to stop the loop...\n" << std::flush;
270 for (;;) {
271 // Acquire a new image from the camera
272 g.acquire(I);
273
274 // Display this image
276
277 try {
278 // For each point...
279 for (size_t i = 0; i < dot.size(); i++) {
280 // Achieve the tracking of the dot in the image
281 dot[i].track(I);
282 // Display a green cross at the center of gravity position in the
283 // image
284 vpImagePoint cog = dot[i].getCog();
286 }
287 } catch (...) {
288 std::cout << "Error detected while tracking visual features.." << std::endl;
289 break;
290 }
291
292 // During the servo, we compute the pose using a non linear method. For
293 // the initial pose used in the non linear minimisation we use the pose
294 // computed at the previous iteration.
295 compute_pose(point, dot, cam, cMo, false);
296
297 for (size_t i = 0; i < dot.size(); i++) {
298 // Update the point feature from the dot location
299 vpFeatureBuilder::create(p[i], cam, dot[i]);
300 // Set the feature Z coordinate from the pose
301 vpColVector cP;
302 point[i].changeFrame(cMo, cP);
303
304 p[i].set_Z(cP[2]);
305 }
306
307 // Compute the visual servoing skew vector
309
310 // Display the current and desired feature points in the image display
311 vpServoDisplay::display(task, cam, I);
312
313 // Apply the computed joint velocities to the robot
315
316 // Save velocities applied to the robot in the log file
317 // v[0], v[1], v[2] correspond to camera translation velocities in m/s
318 // v[3], v[4], v[5] correspond to camera rotation velocities in rad/s
319 flog << v[0] << " " << v[1] << " " << v[2] << " " << v[3] << " " << v[4] << " " << v[5] << " ";
320
321 // Get the measured joint velocities of the robot
322 vpColVector qvel;
324 // Save measured joint velocities of the robot in the log file:
325 // - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
326 // velocities in m/s
327 // - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
328 // velocities in rad/s
329 flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " " << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
330
331 // Get the measured joint positions of the robot
332 vpColVector q;
333 robot.getPosition(vpRobot::ARTICULAR_FRAME, q);
334 // Save measured joint positions of the robot in the log file
335 // - q[0], q[1], q[2] correspond to measured joint translation
336 // positions in m
337 // - q[3], q[4], q[5] correspond to measured joint rotation
338 // positions in rad
339 flog << q[0] << " " << q[1] << " " << q[2] << " " << q[3] << " " << q[4] << " " << q[5] << " ";
340
341 // Save feature error (s-s*) for the 4 feature points. For each feature
342 // point, we have 2 errors (along x and y axis). This error is
343 // expressed in meters in the camera frame
344 flog << task.getError() << std::endl;
345
346 vpDisplay::displayText(I, 10, 10, "Click to quit...", vpColor::red);
347 if (vpDisplay::getClick(I, false))
348 break;
349
350 // Flush the display
352
353 // std::cout << "\t\t || s - s* || = " << ( task.getError()
354 // ).sumSquare() << std::endl;
355 }
356
357 std::cout << "Display task information: " << std::endl;
358 task.print();
359 flog.close(); // Close the log file
360 return EXIT_SUCCESS;
361 } catch (const vpException &e) {
362 flog.close(); // Close the log file
363 std::cout << "Catch an exception: " << e.getMessage() << std::endl;
364 return EXIT_FAILURE;
365 }
366}
367
368#else
369int main()
370{
371 std::cout << "You do not have an Viper 650 robot connected to your computer..." << std::endl;
372 return EXIT_SUCCESS;
373}
374#endif
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:131
static const vpColor red
Definition: vpColor.h:217
static const vpColor blue
Definition: vpColor.h:223
static const vpColor green
Definition: vpColor.h:220
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:135
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 emited by ViSP classes.
Definition: vpException.h:72
const char * getMessage() const
Definition: vpException.cpp:90
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: vpImagePoint.h:88
void init(unsigned int h, unsigned int w, Type value)
Definition: vpImage.h:631
static bool checkDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:420
static std::string getUserName()
Definition: vpIoTools.cpp:316
static void makeDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:570
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:149
@ DEMENTHON
Definition: vpPose.h:86
@ LAGRANGE
Definition: vpPose.h:85
@ LOWE
Definition: vpPose.h:87
double computeResidual(const vpHomogeneousMatrix &cMo) const
Compute and return the sum of squared residuals expressed in meter^2 for the pose matrix cMo.
Definition: vpPose.cpp:336
bool computePose(vpPoseMethodType method, vpHomogeneousMatrix &cMo, bool(*func)(const vpHomogeneousMatrix &)=NULL)
Definition: vpPose.cpp:374
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:78
@ CAMERA_FRAME
Definition: vpRobot.h:82
@ STATE_VELOCITY_CONTROL
Initialize the velocity controller.
Definition: vpRobot.h:66
virtual vpRobotStateType setRobotState(const vpRobot::vpRobotStateType newState)
Definition: vpRobot.cpp:201
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:567
@ EYEINHAND_CAMERA
Definition: vpServo.h:155
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:306
void setLambda(double c)
Definition: vpServo.h:404
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:218
vpColVector getError() const
Definition: vpServo.h:278
@ PSEUDO_INVERSE
Definition: vpServo.h:202
vpColVector computeControlLaw()
Definition: vpServo.cpp:929
@ CURRENT
Definition: vpServo.h:182
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:490
Class that consider the case of a translation vector.