Visual Servoing Platform  version 3.4.0
servoViper850FourPoints2DArtVelocityLs_cur.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 articular frame
35  *
36  * Authors:
37  * Fabien Spindler
38  *
39  *****************************************************************************/
51 #include <visp3/core/vpConfig.h>
52 #include <visp3/core/vpDebug.h> // Debug trace
53 
54 #include <fstream>
55 #include <iostream>
56 #include <sstream>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #if (defined(VISP_HAVE_VIPER850) && defined(VISP_HAVE_DC1394))
60 
61 #include <visp3/blob/vpDot2.h>
62 #include <visp3/core/vpDisplay.h>
63 #include <visp3/core/vpHomogeneousMatrix.h>
64 #include <visp3/core/vpImage.h>
65 #include <visp3/core/vpIoTools.h>
66 #include <visp3/core/vpMath.h>
67 #include <visp3/core/vpPoint.h>
68 #include <visp3/gui/vpDisplayGTK.h>
69 #include <visp3/gui/vpDisplayOpenCV.h>
70 #include <visp3/gui/vpDisplayX.h>
71 #include <visp3/robot/vpRobotViper850.h>
72 #include <visp3/sensor/vp1394TwoGrabber.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 
106 void compute_pose(vpPoint point[], vpDot2 dot[], int ndot, vpCameraParameters cam, vpHomogeneousMatrix &cMo,
107  vpTranslationVector &cto, vpRxyzVector &cro, bool init)
108 {
109  vpHomogeneousMatrix cMo_dementhon; // computed pose with dementhon
110  vpHomogeneousMatrix cMo_lagrange; // computed pose with dementhon
111  vpRotationMatrix cRo;
112  vpPose pose;
113  vpImagePoint cog;
114  for (int i = 0; i < ndot; i++) {
115 
116  double x = 0, y = 0;
117  cog = dot[i].getCog();
119  y); // pixel to meter conversion
120  point[i].set_x(x); // projection perspective p
121  point[i].set_y(y);
122  pose.addPoint(point[i]);
123  }
124 
125  if (init == true) {
126  pose.computePose(vpPose::DEMENTHON, cMo_dementhon);
127  // Compute and return the residual expressed in meter for the pose matrix
128  // 'cMo'
129  double residual_dementhon = pose.computeResidual(cMo_dementhon);
130  pose.computePose(vpPose::LAGRANGE, cMo_lagrange);
131  double residual_lagrange = pose.computeResidual(cMo_lagrange);
132 
133  // Select the best pose to initialize the lowe pose computation
134  if (residual_lagrange < residual_dementhon)
135  cMo = cMo_lagrange;
136  else
137  cMo = cMo_dementhon;
138 
139  } else { // init = false; use of the previous pose to initialise LOWE
140  cRo.buildFrom(cro);
141  cMo.buildFrom(cto, cRo);
142  }
143  pose.computePose(vpPose::LOWE, cMo);
144  cMo.extract(cto);
145  cMo.extract(cRo);
146  cro.buildFrom(cRo);
147 }
148 
149 int main()
150 {
151  // Log file creation in /tmp/$USERNAME/log.dat
152  // This file contains by line:
153  // - the 6 computed joint velocities (m/s, rad/s) to achieve the task
154  // - the 6 mesured joint velocities (m/s, rad/s)
155  // - the 6 mesured joint positions (m, rad)
156  // - the 8 values of s - s*
157  std::string username;
158  // Get the user login name
159  vpIoTools::getUserName(username);
160 
161  // Create a log filename to save velocities...
162  std::string logdirname;
163  logdirname = "/tmp/" + username;
164 
165  // Test if the output path exist. If no try to create it
166  if (vpIoTools::checkDirectory(logdirname) == false) {
167  try {
168  // Create the dirname
169  vpIoTools::makeDirectory(logdirname);
170  } catch (...) {
171  std::cerr << std::endl << "ERROR:" << std::endl;
172  std::cerr << " Cannot create " << logdirname << std::endl;
173  return (-1);
174  }
175  }
176  std::string logfilename;
177  logfilename = logdirname + "/log.dat";
178 
179  // Open the log file name
180  std::ofstream flog(logfilename.c_str());
181 
182  try {
183  vpRobotViper850 robot;
184  // Load the end-effector to camera frame transformation obtained
185  // using a camera intrinsic model with distortion
188 
189  vpServo task;
190 
192  int i;
193 
194  bool reset = false;
195  vp1394TwoGrabber g(reset);
197  g.setFramerate(vp1394TwoGrabber::vpFRAMERATE_60);
198  g.open(I);
199 
200  g.acquire(I);
201 
202 #ifdef VISP_HAVE_X11
203  vpDisplayX display(I, 100, 100, "Current image");
204 #elif defined(VISP_HAVE_OPENCV)
205  vpDisplayOpenCV display(I, 100, 100, "Current image");
206 #elif defined(VISP_HAVE_GTK)
207  vpDisplayGTK display(I, 100, 100, "Current image");
208 #endif
209 
211  vpDisplay::flush(I);
212 
213  std::cout << std::endl;
214  std::cout << "-------------------------------------------------------" << std::endl;
215  std::cout << " Test program for vpServo " << std::endl;
216  std::cout << " Eye-in-hand task control, velocity computed in the joint space" << std::endl;
217  std::cout << " Use of the Afma6 robot " << std::endl;
218  std::cout << " task : servo 4 points on a square with dimention " << L << " meters" << std::endl;
219  std::cout << "-------------------------------------------------------" << std::endl;
220  std::cout << std::endl;
221 
222  vpDot2 dot[4];
223  vpImagePoint cog;
224 
225  std::cout << "Click on the 4 dots clockwise starting from upper/left dot..." << std::endl;
226 
227  for (i = 0; i < 4; i++) {
228  dot[i].setGraphics(true);
229  dot[i].initTracking(I);
230  cog = dot[i].getCog();
232  vpDisplay::flush(I);
233  }
234 
235  vpCameraParameters cam;
236 
237  // Update camera parameters
238  robot.getCameraParameters(cam, I);
239 
240  cam.printParameters();
241 
242  // Sets the current position of the visual feature
243  vpFeaturePoint p[4];
244  for (i = 0; i < 4; i++)
245  vpFeatureBuilder::create(p[i], cam, dot[i]); // retrieve x,y of the vpFeaturePoint structure
246 
247  // Set the position of the square target in a frame which origin is
248  // centered in the middle of the square
249  vpPoint point[4];
250  point[0].setWorldCoordinates(-L, -L, 0);
251  point[1].setWorldCoordinates(L, -L, 0);
252  point[2].setWorldCoordinates(L, L, 0);
253  point[3].setWorldCoordinates(-L, L, 0);
254 
255  // Initialise a desired pose to compute s*, the desired 2D point features
257  vpTranslationVector cto(0, 0, 0.5); // tz = 0.5 meter
259  vpRotationMatrix cRo(cro); // Build the rotation matrix
260  cMo.buildFrom(cto, cRo); // Build the homogeneous matrix
261 
262  // Sets the desired position of the 2D visual feature
263  vpFeaturePoint pd[4];
264  // Compute the desired position of the features from the desired pose
265  for (int i = 0; i < 4; i++) {
266  vpColVector cP, p;
267  point[i].changeFrame(cMo, cP);
268  point[i].projection(cP, p);
269 
270  pd[i].set_x(p[0]);
271  pd[i].set_y(p[1]);
272  pd[i].set_Z(cP[2]);
273  }
274 
275  // We want to see a point on a point
276  for (i = 0; i < 4; i++)
277  task.addFeature(p[i], pd[i]);
278 
279  // Set the proportional gain
280  task.setLambda(0.3);
281 
282  // Display task information
283  task.print();
284 
285  // Define the task
286  // - we want an eye-in-hand control law
287  // - articular velocity are computed
290  task.print();
291 
293  robot.get_cVe(cVe);
294  task.set_cVe(cVe);
295  task.print();
296 
297  // Set the Jacobian (expressed in the end-effector frame)
298  vpMatrix eJe;
299  robot.get_eJe(eJe);
300  task.set_eJe(eJe);
301  task.print();
302 
303  // Initialise the velocity control of the robot
305 
306  std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
307  for (;;) {
308  // Acquire a new image from the camera
309  g.acquire(I);
310 
311  // Display this image
313 
314  try {
315  // For each point...
316  for (i = 0; i < 4; i++) {
317  // Achieve the tracking of the dot in the image
318  dot[i].track(I);
319  // Display a green cross at the center of gravity position in the
320  // image
321  cog = dot[i].getCog();
323  }
324  } catch (...) {
325  flog.close(); // Close the log file
326  vpTRACE("Error detected while tracking visual features");
327  robot.stopMotion();
328  return (1);
329  }
330 
331  // During the servo, we compute the pose using LOWE method. For the
332  // initial pose used in the non linear minimisation we use the pose
333  // computed at the previous iteration.
334  compute_pose(point, dot, 4, cam, cMo, cto, cro, false);
335 
336  for (i = 0; i < 4; i++) {
337  // Update the point feature from the dot location
338  vpFeatureBuilder::create(p[i], cam, dot[i]);
339  // Set the feature Z coordinate from the pose
340  vpColVector cP;
341  point[i].changeFrame(cMo, cP);
342 
343  p[i].set_Z(cP[2]);
344  }
345 
346  // Get the jacobian of the robot
347  robot.get_eJe(eJe);
348  // Update this jacobian in the task structure. It will be used to
349  // compute the velocity skew (as an articular velocity) qdot = -lambda *
350  // L^+ * cVe * eJe * (s-s*)
351  task.set_eJe(eJe);
352 
353  vpColVector v;
354  // Compute the visual servoing skew vector
355  v = task.computeControlLaw();
356 
357  // Display the current and desired feature points in the image display
358  vpServoDisplay::display(task, cam, I);
359 
360  // Apply the computed joint velocities to the robot
362 
363  // Save velocities applied to the robot in the log file
364  // v[0], v[1], v[2] correspond to joint translation velocities in m/s
365  // v[3], v[4], v[5] correspond to joint rotation velocities in rad/s
366  flog << v[0] << " " << v[1] << " " << v[2] << " " << v[3] << " " << v[4] << " " << v[5] << " ";
367 
368  // Get the measured joint velocities of the robot
369  vpColVector qvel;
371  // Save measured joint velocities of the robot in the log file:
372  // - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
373  // velocities in m/s
374  // - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
375  // velocities in rad/s
376  flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " " << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
377 
378  // Get the measured joint positions of the robot
379  vpColVector q;
381  // Save measured joint positions of the robot in the log file
382  // - q[0], q[1], q[2] correspond to measured joint translation
383  // positions in m
384  // - q[3], q[4], q[5] correspond to measured joint rotation
385  // positions in rad
386  flog << q[0] << " " << q[1] << " " << q[2] << " " << q[3] << " " << q[4] << " " << q[5] << " ";
387 
388  // Save feature error (s-s*) for the 4 feature points. For each feature
389  // point, we have 2 errors (along x and y axis). This error is
390  // expressed in meters in the camera frame
391  flog << (task.getError()).t() << std::endl;
392 
393  // Flush the display
394  vpDisplay::flush(I);
395 
396  // std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() <<
397  // std::endl;
398  }
399 
400  std::cout << "Display task information: " << std::endl;
401  task.print();
402  flog.close(); // Close the log file
403  return EXIT_SUCCESS;
404  }
405  catch (const vpException &e) {
406  flog.close(); // Close the log file
407  std::cout << "Catch an exception: " << e.getMessage() << std::endl;
408  return EXIT_FAILURE;
409  }
410 }
411 
412 #else
413 int main()
414 {
415  std::cout << "You do not have an Viper 850 robot connected to your computer..." << std::endl;
416  return EXIT_SUCCESS;
417 }
418 #endif
void getPosition(const vpRobot::vpControlFrameType frame, vpColVector &position)
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:153
vpRxyzVector buildFrom(const vpRotationMatrix &R)
static void makeDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:482
bool computePose(vpPoseMethodType method, vpHomogeneousMatrix &cMo, bool(*func)(const vpHomogeneousMatrix &)=NULL)
Definition: vpPose.cpp:374
void setWorldCoordinates(double oX, double oY, double oZ)
Definition: vpPoint.cpp:113
Implementation of an homogeneous matrix and operations on such kind of matrices.
void getCameraParameters(vpCameraParameters &cam, const unsigned int &image_width, const unsigned int &image_height) const
Definition: vpViper850.cpp:558
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:490
void changeFrame(const vpHomogeneousMatrix &cMo, vpColVector &cP) const
Definition: vpPoint.cpp:239
void set_eJe(const vpMatrix &eJe_)
Definition: vpServo.h:506
void setGraphics(bool activate)
Definition: vpDot2.h:314
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:150
vpRobot::vpRobotStateType setRobotState(vpRobot::vpRobotStateType newState)
error that can be emited by ViSP classes.
Definition: vpException.h:71
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
static void convertPoint(const vpCameraParameters &cam, const double &u, const double &v, double &x, double &y)
void set_y(double y)
void get_eJe(vpMatrix &eJe)
static const vpColor green
Definition: vpColor.h:220
This tracker is meant to track a blob (connex pixels with same gray level) on a vpImage.
Definition: vpDot2.h:126
void set_x(double x)
static void flush(const vpImage< unsigned char > &I)
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:81
void set_x(double x)
Set the point x coordinate in the image plane.
Definition: vpPoint.cpp:497
Implementation of a rotation matrix and operations on such kind of matrices.
static bool checkDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:332
void set_y(double y)
Set the point y coordinate in the image plane.
Definition: vpPoint.cpp:499
vpImagePoint getCog() const
Definition: vpDot2.h:180
Initialize the velocity controller.
Definition: vpRobot.h:66
vpColVector getError() const
Definition: vpServo.h:278
vpColVector computeControlLaw()
Definition: vpServo.cpp:929
void set_Z(double Z)
vpRotationMatrix buildFrom(const vpHomogeneousMatrix &M)
#define vpTRACE
Definition: vpDebug.h:416
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Class used for pose computation from N points (pose from point only). Some of the algorithms implemen...
Definition: vpPose.h:80
Generic class defining intrinsic camera parameters.
void setLambda(double c)
Definition: vpServo.h:404
static std::string getUserName()
Definition: vpIoTools.cpp:228
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:134
void extract(vpRotationMatrix &R) const
void buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
void track(const vpImage< unsigned char > &I, bool canMakeTheWindowGrow=true)
Definition: vpDot2.cpp:441
void projection(const vpColVector &_cP, vpColVector &_p) const
Definition: vpPoint.cpp:222
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:567
static double rad(double deg)
Definition: vpMath.h:110
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &velocity)
void get_cVe(vpVelocityTwistMatrix &cVe) const
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
void getVelocity(const vpRobot::vpControlFrameType frame, vpColVector &velocity)
Implementation of column vector and the associated operations.
Definition: vpColVector.h:130
void set_cVe(const vpVelocityTwistMatrix &cVe_)
Definition: vpServo.h:448
void initTracking(const vpImage< unsigned char > &I, unsigned int size=0)
Definition: vpDot2.cpp:253
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:306
Implementation of a rotation vector as Euler angle minimal representation.
Definition: vpRxyzVector.h:183
Class for firewire ieee1394 video devices using libdc1394-2.x api.
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:87
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
void addPoint(const vpPoint &P)
Definition: vpPose.cpp:149
const char * getMessage() const
Definition: vpException.cpp:90
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:218
Class that consider the case of a translation vector.
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)
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
static const vpColor blue
Definition: vpColor.h:223