Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
servoViper850FourPointsKinect.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 */
44
45#include <visp3/core/vpConfig.h>
46#include <visp3/core/vpDebug.h> // Debug trace
47
48#include <fstream>
49#include <iostream>
50#include <sstream>
51#include <stdio.h>
52#include <stdlib.h>
53
54#if (defined(VISP_HAVE_VIPER850) && defined(VISP_HAVE_LIBFREENECT_AND_DEPENDENCIES))
55
56#include <visp3/core/vpDisplay.h>
57#include <visp3/core/vpHomogeneousMatrix.h>
58#include <visp3/core/vpImage.h>
59#include <visp3/core/vpImageConvert.h>
60#include <visp3/core/vpIoTools.h>
61#include <visp3/core/vpMath.h>
62#include <visp3/core/vpPoint.h>
63#include <visp3/gui/vpDisplayFactory.h>
64#include <visp3/robot/vpRobotViper850.h>
65#include <visp3/sensor/vp1394TwoGrabber.h>
66#include <visp3/sensor/vpKinect.h>
67#include <visp3/vision/vpPose.h>
68#include <visp3/visual_features/vpFeatureBuilder.h>
69#include <visp3/visual_features/vpFeaturePoint.h>
70#include <visp3/vs/vpServo.h>
71
72// Exception
73#include <visp3/core/vpException.h>
74#include <visp3/vs/vpServoDisplay.h>
75
76#include <visp3/blob/vpDot2.h>
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
104void compute_pose(vpPoint point[], vpDot2 dot[], int ndot, vpCameraParameters cam, vpHomogeneousMatrix &cMo, bool init)
105{
107 vpPose pose;
108 vpImagePoint cog;
109 for (int i = 0; i < ndot; i++) {
110
111 double x = 0, y = 0;
112 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) {
122 }
123 else { // init = false; use of the previous pose to initialise VIRTUAL_VS
125 }
126}
127
128int main()
129{
130 // Log file creation in /tmp/$USERNAME/log.dat
131 // This file contains by line:
132 // - the 6 computed joint velocities (m/s, rad/s) to achieve the task
133 // - the 6 measured joint velocities (m/s, rad/s)
134 // - the 6 measured joint positions (m, rad)
135 // - the 8 values of s - s*
136 std::string username;
137 // Get the user login name
138 vpIoTools::getUserName(username);
139
140 // Create a log filename to save velocities...
141 std::string logdirname;
142 logdirname = "/tmp/" + username;
143
144 // Test if the output path exist. If no try to create it
145 if (vpIoTools::checkDirectory(logdirname) == false) {
146 try {
147 // Create the dirname
148 vpIoTools::makeDirectory(logdirname);
149 }
150 catch (...) {
151 std::cerr << std::endl << "ERROR:" << std::endl;
152 std::cerr << " Cannot create " << logdirname << std::endl;
153 return EXIT_FAILURE;
154 }
155 }
156 std::string logfilename;
157 logfilename = logdirname + "/log.dat";
158
159 // Open the log file name
160 std::ofstream flog(logfilename.c_str());
161
162#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
163 std::shared_ptr<vpDisplay> display;
164#else
165 vpDisplay *display = nullptr;
166#endif
167
168 try {
169 vpRobotViper850 robot;
170 // Load the end-effector to camera frame transformation obtained
171 // using a camera intrinsic model with distortion
173 robot.init(vpRobotViper850::TOOL_GENERIC_CAMERA, projModel);
174
176
178 vpImage<vpRGBa> Irgb;
179 int i;
180
181#ifdef VISP_HAVE_LIBFREENECT_OLD
182 // This is the way to initialize Freenect with an old version of
183 // libfreenect packages under ubuntu lucid 10.04
184 Freenect::Freenect<vpKinect> freenect;
185 vpKinect &kinect = freenect.createDevice(0);
186#else
187 Freenect::Freenect freenect;
188 vpKinect &kinect = freenect.createDevice<vpKinect>(0);
189#endif
190
192 kinect.getRGB(Irgb);
194
195#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
196 display = vpDisplayFactory::createDisplay(I, 100, 100, "Current image");
197#else
198 display = vpDisplayFactory::allocateDisplay(I, 100, 100, "Current image");
199#endif
200
203
204 std::cout << std::endl;
205 std::cout << "-------------------------------------------------------" << std::endl;
206 std::cout << " Test program for vpServo " << std::endl;
207 std::cout << " Eye-in-hand task control, velocity computed in the camera space" << std::endl;
208 std::cout << " Use of the Viper850 robot " << std::endl;
209 std::cout << " task : servo 4 points on a square with dimension " << L << " meters" << std::endl;
210 std::cout << "-------------------------------------------------------" << std::endl;
211 std::cout << std::endl;
212
213 vpDot2 dot[4];
214 vpImagePoint cog;
215
216 std::cout << "Click on the 4 dots clockwise starting from upper/left dot..." << std::endl;
217
218 for (i = 0; i < 4; i++) {
219 dot[i].initTracking(I);
220 cog = dot[i].getCog();
223 }
224
225 // Get Kinect Camera Parameters
227 // kinect.getRGBCamParameters(cam);
228
229 robot.getCameraParameters(cam, I);
230
231 cam.printParameters();
232
233 // Sets the current position of the visual feature
234 vpFeaturePoint p[4];
235 for (i = 0; i < 4; i++)
236 vpFeatureBuilder::create(p[i], cam, dot[i]); // retrieve x,y of the vpFeaturePoint structure
237
238 // Set the position of the square target in a frame which origin is
239 // centered in the middle of the square
240 vpPoint point[4];
241 point[0].setWorldCoordinates(-L, -L, 0);
242 point[1].setWorldCoordinates(L, -L, 0);
243 point[2].setWorldCoordinates(L, L, 0);
244 point[3].setWorldCoordinates(-L, L, 0);
245
246 // Initialise a desired pose to compute s*, the desired 2D point features
248 vpTranslationVector cto(0, 0, 0.5); // tz = 0.5 meter
250 vpRotationMatrix cRo(cro); // Build the rotation matrix
251 cMo.buildFrom(cto, cRo); // Build the homogeneous matrix
252
253 // Sets the desired position of the 2D visual feature
254 vpFeaturePoint pd[4];
255 // Compute the desired position of the features from the desired pose
256 for (int i = 0; i < 4; i++) {
257 vpColVector cP, p;
258 point[i].changeFrame(cMo, cP);
259 point[i].projection(cP, p);
260
261 pd[i].set_x(p[0]);
262 pd[i].set_y(p[1]);
263 pd[i].set_Z(cP[2]);
264 }
265
266 // We want to see a point on a point
267 for (i = 0; i < 4; i++)
268 task.addFeature(p[i], pd[i]);
269
270 // Set the proportional gain
271 task.setLambda(0.5);
272
273 // Display task information
274 task.print();
275
276 // Define the task
277 // - we want an eye-in-hand control law
278 // - articular velocity are computed
280 task.setInteractionMatrixType(vpServo::CURRENT, vpServo::PSEUDO_INVERSE);
281 task.print();
282
283 // Initialise the velocity control of the robot
284 robot.setRobotState(vpRobot::STATE_VELOCITY_CONTROL);
285
286 std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
287 bool init_pose_from_linear_method = true;
288 for (;;) {
289 // Acquire a new image from the kinect
290 kinect.getRGB(Irgb);
292
293 // Display this image
295
296 try {
297 // For each point...
298 for (i = 0; i < 4; i++) {
299 // Achieve the tracking of the dot in the image
300 dot[i].track(I);
301 // Display a green cross at the center of gravity position in the
302 // image
303 cog = dot[i].getCog();
305 }
306 }
307 catch (...) {
308 flog.close(); // Close the log file
309 vpTRACE("Error detected while tracking visual features");
310 robot.stopMotion();
311 kinect.stop();
312#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
313 if (display != nullptr) {
314 delete display;
315 }
316#endif
317 return EXIT_FAILURE;
318 }
319
320 // At first iteration, we initialise non linear pose estimation with a linear approach.
321 // For the other iterations, non linear pose estimation is initialized with the pose estimated at previous
322 // iteration of the loop
323 compute_pose(point, dot, 4, cam, cMo, init_pose_from_linear_method);
324 if (init_pose_from_linear_method) {
325 init_pose_from_linear_method = false;
326 }
327 for (i = 0; i < 4; i++) {
328 // Update the point feature from the dot location
329 vpFeatureBuilder::create(p[i], cam, dot[i]);
330 // Set the feature Z coordinate from the pose
331 vpColVector cP;
332 point[i].changeFrame(cMo, cP);
333
334 p[i].set_Z(cP[2]);
335 }
336
338 // Compute the visual servoing skew vector
339 v = task.computeControlLaw();
340
341 // Display the current and desired feature points in the image display
342 vpServoDisplay::display(task, cam, I);
343
344 // Apply the computed joint velocities to the robot
345 robot.setVelocity(vpRobot::CAMERA_FRAME, v);
346
347 // Save velocities applied to the robot in the log file
348 // v[0], v[1], v[2] correspond to joint translation velocities in m/s
349 // v[3], v[4], v[5] correspond to joint rotation velocities in rad/s
350 flog << v[0] << " " << v[1] << " " << v[2] << " " << v[3] << " " << v[4] << " " << v[5] << " ";
351
352 // Get the measured joint velocities of the robot
353 vpColVector qvel;
354 robot.getVelocity(vpRobot::ARTICULAR_FRAME, qvel);
355 // Save measured joint velocities of the robot in the log file:
356 // - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
357 // velocities in m/s
358 // - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
359 // velocities in rad/s
360 flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " " << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
361
362 // Get the measured joint positions of the robot
363 vpColVector q;
364 robot.getPosition(vpRobot::ARTICULAR_FRAME, q);
365 // Save measured joint positions of the robot in the log file
366 // - q[0], q[1], q[2] correspond to measured joint translation
367 // positions in m
368 // - q[3], q[4], q[5] correspond to measured joint rotation
369 // positions in rad
370 flog << q[0] << " " << q[1] << " " << q[2] << " " << q[3] << " " << q[4] << " " << q[5] << " ";
371
372 // Save feature error (s-s*) for the 4 feature points. For each feature
373 // point, we have 2 errors (along x and y axis). This error is
374 // expressed in meters in the camera frame
375 flog << (task.getError()).t() << std::endl;
376
377 // Flush the display
379
380 // std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() <<
381 // std::endl;
382 }
383
384 kinect.stop();
385 std::cout << "Display task information: " << std::endl;
386 task.print();
387 flog.close(); // Close the log file
388#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
389 if (display != nullptr) {
390 delete display;
391 }
392#endif
393 return EXIT_SUCCESS;
394 }
395 catch (const vpException &e) {
396 flog.close(); // Close the log file
397 std::cout << "Catch an exception: " << e.getMessage() << std::endl;
398#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
399 if (display != nullptr) {
400 delete display;
401 }
402#endif
403 return EXIT_FAILURE;
404 }
405}
406
407#else
408int main()
409{
410 std::cout << "You do not have an Viper 850 robot connected to your computer..." << std::endl;
411 return EXIT_SUCCESS;
412}
413#endif
Generic class defining intrinsic camera parameters.
@ perspectiveProjWithDistortion
Perspective projection with distortion model.
Implementation of column vector and the associated operations.
vpRowVector t() const
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 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)
This tracker is meant to track a blob (connex pixels with same gray level) on a vpImage.
Definition vpDot2.h:127
void track(const vpImage< unsigned char > &I, bool canMakeTheWindowGrow=true)
Definition vpDot2.cpp:441
vpImagePoint getCog() const
Definition vpDot2.h:183
void initTracking(const vpImage< unsigned char > &I, unsigned int size=0)
Definition vpDot2.cpp:263
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.
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
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)
Driver for the Kinect-1 device.
Definition vpKinect.h:110
void stop()
Definition vpKinect.cpp:110
void start(vpKinect::vpDMResolution res=DMAP_LOW_RES)
Definition vpKinect.cpp:70
@ DMAP_LOW_RES
Definition vpKinect.h:126
bool getRGB(vpImage< vpRGBa > &IRGB)
Definition vpKinect.cpp:223
static double rad(double deg)
Definition vpMath.h:129
static void convertPoint(const vpCameraParameters &cam, const double &u, const double &v, double &x, double &y)
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition vpPoint.h:79
void set_x(double x)
Set the point x coordinate in the image plane.
Definition vpPoint.cpp:471
void projection(const vpColVector &_cP, vpColVector &_p) const VP_OVERRIDE
Definition vpPoint.cpp:252
void changeFrame(const vpHomogeneousMatrix &cMo, vpColVector &cP) const VP_OVERRIDE
Definition vpPoint.cpp:273
void setWorldCoordinates(double oX, double oY, double oZ)
Definition vpPoint.cpp:116
void set_y(double y)
Set the point y coordinate in the image plane.
Definition vpPoint.cpp:473
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
Control of Irisa's Viper S850 robot named Viper850.
@ 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.
Implementation of a rotation vector as Euler angle minimal representation.
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.
@ TOOL_GENERIC_CAMERA
Definition vpViper850.h:124
#define vpTRACE
Definition vpDebug.h:450
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.