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