Visual Servoing Platform  version 3.3.0 under development (2020-02-17)
servoViper650FourPoints2DArtVelocityLs_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  *****************************************************************************/
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_DC1394) && 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/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 
99 void compute_pose(std::vector<vpPoint> &point, std::vector<vpDot2> &dot, vpCameraParameters cam,
100  vpHomogeneousMatrix &cMo, bool init)
101 {
102  vpHomogeneousMatrix cMo_dementhon; // computed pose with dementhon method
103  vpHomogeneousMatrix cMo_lagrange; // computed pose with lagrange method
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) {
118  pose.computePose(vpPose::DEMENTHON, cMo_dementhon);
119  // Compute and return the residual expressed in meter for the pose matrix
120  double residual_dementhon = pose.computeResidual(cMo_dementhon);
121  pose.computePose(vpPose::LAGRANGE, cMo_lagrange);
122  double residual_lagrange = pose.computeResidual(cMo_lagrange);
123 
124  // Select the best pose to initialize the lowe pose computation
125  if (residual_lagrange < residual_dementhon)
126  cMo = cMo_lagrange;
127  else
128  cMo = cMo_dementhon;
129  }
130 
131  pose.computePose(vpPose::LOWE, cMo);
132 }
133 
134 int main()
135 {
136  // Log file creation in /tmp/$USERNAME/log.dat
137  // This file contains by line:
138  // - the 6 computed joint velocities (m/s, rad/s) to achieve the task
139  // - the 6 mesured joint velocities (m/s, rad/s)
140  // - the 6 mesured joint positions (m, rad)
141  // - the 8 values of s - s*
142  std::string username;
143  // Get the user login name
144  vpIoTools::getUserName(username);
145 
146  // Create a log filename to save velocities...
147  std::string logdirname;
148  logdirname = "/tmp/" + username;
149 
150  // Test if the output path exist. If no try to create it
151  if (vpIoTools::checkDirectory(logdirname) == false) {
152  try {
153  // Create the dirname
154  vpIoTools::makeDirectory(logdirname);
155  } catch (...) {
156  std::cerr << std::endl << "ERROR:" << std::endl;
157  std::cerr << " Cannot create " << logdirname << std::endl;
158  return (-1);
159  }
160  }
161  std::string logfilename;
162  logfilename = logdirname + "/log.dat";
163 
164  // Open the log file name
165  std::ofstream flog(logfilename.c_str());
166 
167  try {
168  vpRobotViper650 robot;
169  // Load the end-effector to camera frame transformation obtained
170  // using a camera intrinsic model with distortion
174  robot.get_eMc(eMc);
175  std::cout << "Camera extrinsic parameters (eMc): \n" << eMc << std::endl;
176 
177  vpServo task;
178 
180 
181  bool reset = false;
182  vp1394TwoGrabber g(reset);
184  g.setFramerate(vp1394TwoGrabber::vpFRAMERATE_60);
185  g.open(I);
186 
187  g.acquire(I);
188 
189  vpDisplayX display(I, 100, 100, "Current image");
191  vpDisplay::flush(I);
192 
193  std::vector<vpDot2> dot(4);
194 
195  vpImagePoint cog;
196 
197  std::cout << "Click on the 4 dots clockwise starting from upper/left dot..." << std::endl;
198 
199  for (size_t i = 0; i < dot.size(); i++) {
200  dot[i].setGraphics(true);
201  dot[i].initTracking(I);
202  vpImagePoint cog = dot[i].getCog();
204  vpDisplay::flush(I);
205  }
206 
207  vpCameraParameters cam;
208 
209  // Update camera parameters
210  robot.getCameraParameters(cam, I);
211  std::cout << "Camera intrinsic parameters: \n" << cam << std::endl;
212 
213  // Sets the current position of the visual feature
214  vpFeaturePoint p[4];
215  for (size_t i = 0; i < dot.size(); i++)
216  vpFeatureBuilder::create(p[i], cam, dot[i]); // retrieve x,y of the vpFeaturePoint structure
217 
218  // Set the position of the square target in a frame which origin is
219  // centered in the middle of the square
220  std::vector<vpPoint> point(4);
221  point[0].setWorldCoordinates(-L, -L, 0);
222  point[1].setWorldCoordinates(L, -L, 0);
223  point[2].setWorldCoordinates(L, L, 0);
224  point[3].setWorldCoordinates(-L, L, 0);
225 
226  // Compute target initial pose
228  compute_pose(point, dot, cam, cMo, true);
229  std::cout << "Initial camera pose (cMo): \n" << cMo << std::endl;
230 
231  // Initialise a desired pose to compute s*, the desired 2D point features
232  vpHomogeneousMatrix cMo_d(vpTranslationVector(0, 0, 0.5), // tz = 0.5 meter
233  vpRotationMatrix()); // no rotation
234 
235  // Sets the desired position of the 2D visual feature
236  vpFeaturePoint pd[4];
237  // Compute the desired position of the features from the desired pose
238  for (int i = 0; i < 4; i++) {
239  vpColVector cP, p;
240  point[i].changeFrame(cMo_d, cP);
241  point[i].projection(cP, p);
242 
243  pd[i].set_x(p[0]);
244  pd[i].set_y(p[1]);
245  pd[i].set_Z(cP[2]);
246  }
247 
248  // We want to see a point on a point
249  for (size_t i = 0; i < dot.size(); i++)
250  task.addFeature(p[i], pd[i]);
251 
252  // Set the proportional gain
253  task.setLambda(0.3);
254 
255  // Define the task
256  // - we want an eye-in-hand control law
257  // - articular velocity are computed
260 
262  robot.get_cVe(cVe);
263  task.set_cVe(cVe);
264 
265  // Set the Jacobian (expressed in the end-effector frame)
266  vpMatrix eJe;
267  robot.get_eJe(eJe);
268  task.set_eJe(eJe);
269  task.print();
270 
271  // Initialise the velocity control of the robot
273 
274  std::cout << "\nHit CTRL-C or click in the image to stop the loop...\n" << std::flush;
275  for (;;) {
276  // Acquire a new image from the camera
277  g.acquire(I);
278 
279  // Display this image
281 
282  try {
283  // For each point...
284  for (size_t i = 0; i < dot.size(); i++) {
285  // Achieve the tracking of the dot in the image
286  dot[i].track(I);
287  // Display a green cross at the center of gravity position in the
288  // image
289  vpImagePoint cog = dot[i].getCog();
291  }
292  } catch (...) {
293  std::cout << "Error detected while tracking visual features.." << std::endl;
294  break;
295  }
296 
297  // During the servo, we compute the pose using LOWE method. For the
298  // initial pose used in the non linear minimisation we use the pose
299  // computed at the previous iteration.
300  compute_pose(point, dot, cam, cMo, false);
301 
302  for (size_t i = 0; i < dot.size(); i++) {
303  // Update the point feature from the dot location
304  vpFeatureBuilder::create(p[i], cam, dot[i]);
305  // Set the feature Z coordinate from the pose
306  vpColVector cP;
307  point[i].changeFrame(cMo, cP);
308 
309  p[i].set_Z(cP[2]);
310  }
311 
312  // Get the jacobian of the robot
313  robot.get_eJe(eJe);
314  // Update this jacobian in the task structure. It will be used to
315  // compute the velocity skew (as an articular velocity) qdot = -lambda *
316  // L^+ * cVe * eJe * (s-s*)
317  task.set_eJe(eJe);
318 
319  // Compute the visual servoing skew vector
320  vpColVector v = task.computeControlLaw();
321 
322  // Display the current and desired feature points in the image display
323  vpServoDisplay::display(task, cam, I);
324 
325  // Apply the computed joint velocities to the robot
327 
328  // Save velocities applied to the robot in the log file
329  // v[0], v[1], v[2] correspond to joint translation velocities in m/s
330  // v[3], v[4], v[5] correspond to joint rotation velocities in rad/s
331  flog << v[0] << " " << v[1] << " " << v[2] << " " << v[3] << " " << v[4] << " " << v[5] << " ";
332 
333  // Get the measured joint velocities of the robot
334  vpColVector qvel;
336  // Save measured joint velocities of the robot in the log file:
337  // - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
338  // velocities in m/s
339  // - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
340  // velocities in rad/s
341  flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " " << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
342 
343  // Get the measured joint positions of the robot
344  vpColVector q;
346  // Save measured joint positions of the robot in the log file
347  // - q[0], q[1], q[2] correspond to measured joint translation
348  // positions in m
349  // - q[3], q[4], q[5] correspond to measured joint rotation
350  // positions in rad
351  flog << q[0] << " " << q[1] << " " << q[2] << " " << q[3] << " " << q[4] << " " << q[5] << " ";
352 
353  // Save feature error (s-s*) for the 4 feature points. For each feature
354  // point, we have 2 errors (along x and y axis). This error is
355  // expressed in meters in the camera frame
356  flog << (task.getError()).t() << std::endl;
357 
358  vpDisplay::displayText(I, 10, 10, "Click to quit...", vpColor::red);
359  if (vpDisplay::getClick(I, false))
360  break;
361 
362  // Flush the display
363  vpDisplay::flush(I);
364 
365  // std::cout << "\t\t || s - s* || = " << ( task.getError()
366  // ).sumSquare() << std::endl;
367  }
368 
369  std::cout << "Display task information: " << std::endl;
370  task.print();
371  task.kill();
372  flog.close(); // Close the log file
373  return EXIT_SUCCESS;
374  } catch (const vpException &e) {
375  flog.close(); // Close the log file
376  std::cout << "Catched an exception: " << e.getMessage() << std::endl;
377  return EXIT_FAILURE;
378  }
379 }
380 
381 #else
382 int main()
383 {
384  std::cout << "You do not have an Viper 650 robot connected to your computer..." << std::endl;
385  return EXIT_SUCCESS;
386 }
387 #endif
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:164
static void makeDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:572
bool computePose(vpPoseMethodType method, vpHomogeneousMatrix &cMo, bool(*func)(const vpHomogeneousMatrix &)=NULL)
Definition: vpPose.cpp:374
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &velocity)
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
void getVelocity(const vpRobot::vpControlFrameType frame, vpColVector &velocity)
Implementation of an homogeneous matrix and operations on such kind of matrices.
Control of Irisa&#39;s Viper S650 robot named Viper650.
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:497
void set_eJe(const vpMatrix &eJe_)
Definition: vpServo.h:508
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:150
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 get_eMc(vpHomogeneousMatrix &eMc) const
Definition: vpViper.cpp:894
void set_y(double y)
void getCameraParameters(vpCameraParameters &cam, const unsigned int &image_width, const unsigned int &image_height) const
Definition: vpViper650.cpp:538
static const vpColor green
Definition: vpColor.h:182
void set_x(double x)
static void flush(const vpImage< unsigned char > &I)
static const vpColor red
Definition: vpColor.h:179
Implementation of a rotation matrix and operations on such kind of matrices.
static bool checkDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:422
void kill()
Definition: vpServo.cpp:192
vpRobot::vpRobotStateType setRobotState(vpRobot::vpRobotStateType newState)
Initialize the velocity controller.
Definition: vpRobot.h:66
vpColVector computeControlLaw()
Definition: vpServo.cpp:935
void set_Z(double Z)
static void display(const vpImage< unsigned char > &I)
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:406
static std::string getUserName()
Definition: vpIoTools.cpp:318
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:574
const char * getMessage(void) const
Definition: vpException.cpp:90
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
Implementation of column vector and the associated operations.
Definition: vpColVector.h:130
void set_cVe(const vpVelocityTwistMatrix &cVe_)
Definition: vpServo.h:450
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:313
vpColVector getError() const
Definition: vpServo.h:282
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:88
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
void get_cVe(vpVelocityTwistMatrix &cVe) const
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
void addPoint(const vpPoint &P)
Definition: vpPose.cpp:149
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:223
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)
void get_eJe(vpMatrix &eJe)
static const vpColor blue
Definition: vpColor.h:185
void getPosition(const vpRobot::vpControlFrameType frame, vpColVector &position)