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