Visual Servoing Platform  version 3.6.1 under development (2024-05-08)
servoViper650FourPoints2DArtVelocityLs_cur.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 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 https://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 *****************************************************************************/
53 #include <fstream>
54 #include <iostream>
55 #include <sstream>
56 #include <stdio.h>
57 #include <stdlib.h>
58 
59 #include <visp3/core/vpConfig.h>
60 
61 #if defined(VISP_HAVE_VIPER650) && defined(VISP_HAVE_DC1394) && defined(VISP_HAVE_X11)
62 
63 #include <visp3/blob/vpDot2.h>
64 #include <visp3/core/vpHomogeneousMatrix.h>
65 #include <visp3/core/vpIoTools.h>
66 #include <visp3/core/vpPoint.h>
67 #include <visp3/gui/vpDisplayX.h>
68 #include <visp3/robot/vpRobotViper650.h>
69 #include <visp3/sensor/vp1394TwoGrabber.h>
70 #include <visp3/vision/vpPose.h>
71 #include <visp3/visual_features/vpFeatureBuilder.h>
72 #include <visp3/visual_features/vpFeaturePoint.h>
73 #include <visp3/vs/vpServo.h>
74 #include <visp3/vs/vpServoDisplay.h>
75 
76 #define L 0.05 // to deal with a 10cm by 10cm square
77 
96 void compute_pose(std::vector<vpPoint> &point, std::vector<vpDot2> &dot, vpCameraParameters cam,
97  vpHomogeneousMatrix &cMo, bool init)
98 {
99  vpPose pose;
100 
101  for (size_t i = 0; i < point.size(); i++) {
102 
103  double x = 0, y = 0;
104  vpImagePoint cog = dot[i].getCog();
106  y); // pixel to meter conversion
107  point[i].set_x(x); // projection perspective p
108  point[i].set_y(y);
109  pose.addPoint(point[i]);
110  }
111 
112  if (init == true) {
114  } else {
115  pose.computePose(vpPose::VIRTUAL_VS, cMo);
116  }
117 }
118 
119 int main()
120 {
121  // Log file creation in /tmp/$USERNAME/log.dat
122  // This file contains by line:
123  // - the 6 computed joint velocities (m/s, rad/s) to achieve the task
124  // - the 6 mesured joint velocities (m/s, rad/s)
125  // - the 6 mesured joint positions (m, rad)
126  // - the 8 values of s - s*
127  std::string username;
128  // Get the user login name
129  vpIoTools::getUserName(username);
130 
131  // Create a log filename to save velocities...
132  std::string logdirname;
133  logdirname = "/tmp/" + username;
134 
135  // Test if the output path exist. If no try to create it
136  if (vpIoTools::checkDirectory(logdirname) == false) {
137  try {
138  // Create the dirname
139  vpIoTools::makeDirectory(logdirname);
140  } catch (...) {
141  std::cerr << std::endl << "ERROR:" << std::endl;
142  std::cerr << " Cannot create " << logdirname << std::endl;
143  return EXIT_FAILURE;
144  }
145  }
146  std::string logfilename;
147  logfilename = logdirname + "/log.dat";
148 
149  // Open the log file name
150  std::ofstream flog(logfilename.c_str());
151 
152  try {
153  vpRobotViper650 robot;
154  // Load the end-effector to camera frame transformation obtained
155  // using a camera intrinsic model with distortion
159  robot.get_eMc(eMc);
160  std::cout << "Camera extrinsic parameters (eMc): \n" << eMc << std::endl;
161 
162  vpServo task;
163 
165 
166  bool reset = false;
167  vp1394TwoGrabber g(reset);
169  g.setFramerate(vp1394TwoGrabber::vpFRAMERATE_60);
170  g.open(I);
171 
172  g.acquire(I);
173 
174  vpDisplayX display(I, 100, 100, "Current image");
176  vpDisplay::flush(I);
177 
178  std::vector<vpDot2> dot(4);
179 
180  vpImagePoint cog;
181 
182  std::cout << "Click on the 4 dots clockwise starting from upper/left dot..." << std::endl;
183 
184  for (size_t i = 0; i < dot.size(); i++) {
185  dot[i].setGraphics(true);
186  dot[i].initTracking(I);
187  vpImagePoint cog = dot[i].getCog();
189  vpDisplay::flush(I);
190  }
191 
192  vpCameraParameters cam;
193 
194  // Update camera parameters
195  robot.getCameraParameters(cam, I);
196  std::cout << "Camera intrinsic parameters: \n" << cam << std::endl;
197 
198  // Sets the current position of the visual feature
199  vpFeaturePoint p[4];
200  for (size_t i = 0; i < dot.size(); i++)
201  vpFeatureBuilder::create(p[i], cam, dot[i]); // retrieve x,y of the vpFeaturePoint structure
202 
203  // Set the position of the square target in a frame which origin is
204  // centered in the middle of the square
205  std::vector<vpPoint> point(4);
206  point[0].setWorldCoordinates(-L, -L, 0);
207  point[1].setWorldCoordinates(L, -L, 0);
208  point[2].setWorldCoordinates(L, L, 0);
209  point[3].setWorldCoordinates(-L, L, 0);
210 
211  // Compute target initial pose
213  compute_pose(point, dot, cam, cMo, true);
214  std::cout << "Initial camera pose (cMo): \n" << cMo << std::endl;
215 
216  // Initialise a desired pose to compute s*, the desired 2D point features
217  vpHomogeneousMatrix cMo_d(vpTranslationVector(0, 0, 0.5), // tz = 0.5 meter
218  vpRotationMatrix()); // no rotation
219 
220  // Sets the desired position of the 2D visual feature
221  vpFeaturePoint pd[4];
222  // Compute the desired position of the features from the desired pose
223  for (int i = 0; i < 4; i++) {
224  vpColVector cP, p;
225  point[i].changeFrame(cMo_d, cP);
226  point[i].projection(cP, p);
227 
228  pd[i].set_x(p[0]);
229  pd[i].set_y(p[1]);
230  pd[i].set_Z(cP[2]);
231  }
232 
233  // We want to see a point on a point
234  for (size_t i = 0; i < dot.size(); i++)
235  task.addFeature(p[i], pd[i]);
236 
237  // Set the proportional gain
238  task.setLambda(0.3);
239 
240  // Define the task
241  // - we want an eye-in-hand control law
242  // - articular velocity are computed
245 
247  robot.get_cVe(cVe);
248  task.set_cVe(cVe);
249 
250  // Set the Jacobian (expressed in the end-effector frame)
251  vpMatrix eJe;
252  robot.get_eJe(eJe);
253  task.set_eJe(eJe);
254  task.print();
255 
256  // Initialise the velocity control of the robot
258 
259  std::cout << "\nHit CTRL-C or click in the image to stop the loop...\n" << std::flush;
260  for (;;) {
261  // Acquire a new image from the camera
262  g.acquire(I);
263 
264  // Display this image
266 
267  try {
268  // For each point...
269  for (size_t i = 0; i < dot.size(); i++) {
270  // Achieve the tracking of the dot in the image
271  dot[i].track(I);
272  // Display a green cross at the center of gravity position in the
273  // image
274  vpImagePoint cog = dot[i].getCog();
276  }
277  } catch (...) {
278  std::cout << "Error detected while tracking visual features.." << std::endl;
279  break;
280  }
281 
282  // During the servo, we compute the pose using LOWE method. For the
283  // initial pose used in the non linear minimization we use the pose
284  // computed at the previous iteration.
285  compute_pose(point, dot, cam, cMo, false);
286 
287  for (size_t i = 0; i < dot.size(); i++) {
288  // Update the point feature from the dot location
289  vpFeatureBuilder::create(p[i], cam, dot[i]);
290  // Set the feature Z coordinate from the pose
291  vpColVector cP;
292  point[i].changeFrame(cMo, cP);
293 
294  p[i].set_Z(cP[2]);
295  }
296 
297  // Get the jacobian of the robot
298  robot.get_eJe(eJe);
299  // Update this jacobian in the task structure. It will be used to
300  // compute the velocity skew (as an articular velocity) qdot = -lambda *
301  // L^+ * cVe * eJe * (s-s*)
302  task.set_eJe(eJe);
303 
304  // Compute the visual servoing skew vector
305  vpColVector v = task.computeControlLaw();
306 
307  // Display the current and desired feature points in the image display
308  vpServoDisplay::display(task, cam, I);
309 
310  // Apply the computed joint velocities to the robot
312 
313  // Save velocities applied to the robot in the log file
314  // v[0], v[1], v[2] correspond to joint translation velocities in m/s
315  // v[3], v[4], v[5] correspond to joint rotation velocities in rad/s
316  flog << v[0] << " " << v[1] << " " << v[2] << " " << v[3] << " " << v[4] << " " << v[5] << " ";
317 
318  // Get the measured joint velocities of the robot
319  vpColVector qvel;
321  // Save measured joint velocities of the robot in the log file:
322  // - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
323  // velocities in m/s
324  // - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
325  // velocities in rad/s
326  flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " " << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
327 
328  // Get the measured joint positions of the robot
329  vpColVector q;
330  robot.getPosition(vpRobot::ARTICULAR_FRAME, q);
331  // Save measured joint positions of the robot in the log file
332  // - q[0], q[1], q[2] correspond to measured joint translation
333  // positions in m
334  // - q[3], q[4], q[5] correspond to measured joint rotation
335  // positions in rad
336  flog << q[0] << " " << q[1] << " " << q[2] << " " << q[3] << " " << q[4] << " " << q[5] << " ";
337 
338  // Save feature error (s-s*) for the 4 feature points. For each feature
339  // point, we have 2 errors (along x and y axis). This error is
340  // expressed in meters in the camera frame
341  flog << (task.getError()).t() << std::endl;
342 
343  vpDisplay::displayText(I, 10, 10, "Click to quit...", vpColor::red);
344  if (vpDisplay::getClick(I, false))
345  break;
346 
347  // Flush the display
348  vpDisplay::flush(I);
349 
350  // std::cout << "\t\t || s - s* || = " << ( task.getError()
351  // ).sumSquare() << std::endl;
352  }
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
Class for firewire ieee1394 video devices using libdc1394-2.x api.
Generic class defining intrinsic camera parameters.
@ perspectiveProjWithDistortion
Perspective projection with distortion model.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:163
static const vpColor red
Definition: vpColor.h:211
static const vpColor blue
Definition: vpColor.h:217
static const vpColor green
Definition: vpColor.h:214
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:128
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 emitted by ViSP classes.
Definition: vpException.h:59
const char * getMessage() const
Definition: vpException.cpp:64
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:82
void init(unsigned int h, unsigned int w, Type value)
Definition: vpImage.h:619
static bool checkDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:832
static std::string getUserName()
Definition: vpIoTools.cpp:725
static void makeDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:981
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:146
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:78
void addPoint(const vpPoint &P)
Definition: vpPose.cpp:93
@ DEMENTHON_LAGRANGE_VIRTUAL_VS
Definition: vpPose.h:99
@ VIRTUAL_VS
Definition: vpPose.h:93
bool computePose(vpPoseMethodType method, vpHomogeneousMatrix &cMo, bool(*func)(const vpHomogeneousMatrix &)=nullptr)
Definition: vpPose.cpp:340
void get_eJe(vpMatrix &eJe) vp_override
void getVelocity(const vpRobot::vpControlFrameType frame, vpColVector &velocity)
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel) vp_override
Control of Irisa's Viper S650 robot named Viper650.
@ ARTICULAR_FRAME
Definition: vpRobot.h:78
@ STATE_VELOCITY_CONTROL
Initialize the velocity controller.
Definition: vpRobot.h:65
virtual vpRobotStateType setRobotState(const vpRobot::vpRobotStateType newState)
Definition: vpRobot.cpp:198
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:378
@ EYEINHAND_L_cVe_eJe
Definition: vpServo.h:162
void addFeature(vpBasicFeature &s_cur, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:329
void set_cVe(const vpVelocityTwistMatrix &cVe_)
Definition: vpServo.h:1028
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:169
void setLambda(double c)
Definition: vpServo.h:976
void set_eJe(const vpMatrix &eJe_)
Definition: vpServo.h:1091
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:132
vpColVector getError() const
Definition: vpServo.h:504
@ PSEUDO_INVERSE
Definition: vpServo.h:229
vpColVector computeControlLaw()
Definition: vpServo.cpp:703
@ CURRENT
Definition: vpServo.h:196
Class that consider the case of a translation vector.
vpVelocityTwistMatrix get_cVe() const
Definition: vpUnicycle.h:70
@ TOOL_PTGREY_FLEA2_CAMERA
Definition: vpViper650.h:120
void display(vpImage< unsigned char > &I, const std::string &title)
Display a gray-scale image.