Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
servoViper850Point2DArtVelocity.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 articular
35  *
36  * Authors:
37  * Eric Marchand
38  * Fabien Spindler
39  *
40  *****************************************************************************/
41 
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 
60 #if (defined(VISP_HAVE_VIPER850) && defined(VISP_HAVE_DC1394))
61 
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/visual_features/vpFeatureBuilder.h>
74 #include <visp3/visual_features/vpFeaturePoint.h>
75 #include <visp3/vs/vpServo.h>
76 
77 // Exception
78 #include <visp3/core/vpException.h>
79 #include <visp3/vs/vpServoDisplay.h>
80 
81 #include <visp3/blob/vpDot2.h>
82 
83 int main()
84 {
85  // Log file creation in /tmp/$USERNAME/log.dat
86  // This file contains by line:
87  // - the 6 computed joint velocities (m/s, rad/s) to achieve the task
88  // - the 6 mesured joint velocities (m/s, rad/s)
89  // - the 6 mesured joint positions (m, rad)
90  // - the 2 values of s - s*
91  std::string username;
92  // Get the user login name
93  vpIoTools::getUserName(username);
94 
95  // Create a log filename to save velocities...
96  std::string logdirname;
97  logdirname = "/tmp/" + username;
98 
99  // Test if the output path exist. If no try to create it
100  if (vpIoTools::checkDirectory(logdirname) == false) {
101  try {
102  // Create the dirname
103  vpIoTools::makeDirectory(logdirname);
104  } catch (...) {
105  std::cerr << std::endl << "ERROR:" << std::endl;
106  std::cerr << " Cannot create " << logdirname << std::endl;
107  exit(-1);
108  }
109  }
110  std::string logfilename;
111  logfilename = logdirname + "/log.dat";
112 
113  // Open the log file name
114  std::ofstream flog(logfilename.c_str());
115 
116  try {
117  vpRobotViper850 robot;
118 
119  vpServo task;
120 
122 
123  bool reset = false;
124  vp1394TwoGrabber g(reset);
126  g.setFramerate(vp1394TwoGrabber::vpFRAMERATE_60);
127  g.open(I);
128 
129  g.acquire(I);
130 
131 #ifdef VISP_HAVE_X11
132  vpDisplayX display(I, 800, 100, "Current image");
133 #elif defined(VISP_HAVE_OPENCV)
134  vpDisplayOpenCV display(I, 800, 100, "Current image");
135 #elif defined(VISP_HAVE_GTK)
136  vpDisplayGTK display(I, 800, 100, "Current image");
137 #endif
138 
140  vpDisplay::flush(I);
141  // exit(1) ;
142 
143  std::cout << std::endl;
144  std::cout << "-------------------------------------------------------" << std::endl;
145  std::cout << " Test program for vpServo " << std::endl;
146  std::cout << " Eye-in-hand task control, velocity computed in the joint space" << std::endl;
147  std::cout << " Use of the Afma6 robot " << std::endl;
148  std::cout << " task : servo a point " << std::endl;
149  std::cout << "-------------------------------------------------------" << std::endl;
150  std::cout << std::endl;
151 
152  vpDot2 dot;
153 
154  std::cout << "Click on a dot..." << std::endl;
155  dot.initTracking(I);
156  vpImagePoint cog = dot.getCog();
158  vpDisplay::flush(I);
159 
160  vpCameraParameters cam;
161  // Update camera parameters
162  robot.getCameraParameters(cam, I);
163 
164  vpTRACE("sets the current position of the visual feature ");
165  vpFeaturePoint p;
166  vpFeatureBuilder::create(p, cam, dot); // retrieve x,y and Z of the vpPoint structure
167 
168  p.set_Z(1);
169  vpTRACE("sets the desired position of the visual feature ");
170  vpFeaturePoint pd;
171  pd.buildFrom(0, 0, 1);
172 
173  vpTRACE("define the task");
174  vpTRACE("\t we want an eye-in-hand control law");
175  vpTRACE("\t articular velocity are computed");
178 
179  vpTRACE("Set the position of the camera in the end-effector frame ");
181  // robot.get_cMe(cMe) ;
182 
184  robot.get_cVe(cVe);
185  std::cout << cVe << std::endl;
186  task.set_cVe(cVe);
187 
188  // vpDisplay::getClick(I) ;
189  vpTRACE("Set the Jacobian (expressed in the end-effector frame)");
190  vpMatrix eJe;
191  robot.get_eJe(eJe);
192  task.set_eJe(eJe);
193 
194  vpTRACE("\t we want to see a point on a point..");
195  std::cout << std::endl;
196  task.addFeature(p, pd);
197 
198  vpTRACE("\t set the gain");
199  task.setLambda(0.8);
200 
201  vpTRACE("Display task information ");
202  task.print();
203 
205 
206  std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
207  for (;;) {
208  // Acquire a new image from the camera
209  g.acquire(I);
210 
211  // Display this image
213 
214  // Achieve the tracking of the dot in the image
215  dot.track(I);
216  cog = dot.getCog();
217 
218  // Display a green cross at the center of gravity position in the image
220 
221  // Update the point feature from the dot location
222  vpFeatureBuilder::create(p, cam, dot);
223 
224  // Get the jacobian of the robot
225  robot.get_eJe(eJe);
226  // Update this jacobian in the task structure. It will be used to
227  // compute the velocity skew (as an articular velocity) qdot = -lambda *
228  // L^+ * cVe * eJe * (s-s*)
229  task.set_eJe(eJe);
230 
231  // std::cout << (vpMatrix)cVe*eJe << std::endl ;
232 
233  vpColVector v;
234  // Compute the visual servoing skew vector
235  v = task.computeControlLaw();
236 
237  // Display the current and desired feature points in the image display
238  vpServoDisplay::display(task, cam, I);
239 
240  // Apply the computed joint velocities to the robot
242 
243  // Save velocities applied to the robot in the log file
244  // v[0], v[1], v[2] correspond to joint translation velocities in m/s
245  // v[3], v[4], v[5] correspond to joint rotation velocities in rad/s
246  flog << v[0] << " " << v[1] << " " << v[2] << " " << v[3] << " " << v[4] << " " << v[5] << " ";
247 
248  // Get the measured joint velocities of the robot
249  vpColVector qvel;
251  // Save measured joint velocities of the robot in the log file:
252  // - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
253  // velocities in m/s
254  // - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
255  // velocities in rad/s
256  flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " " << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
257 
258  // Get the measured joint positions of the robot
259  vpColVector q;
261  // Save measured joint positions of the robot in the log file
262  // - q[0], q[1], q[2] correspond to measured joint translation
263  // positions in m
264  // - q[3], q[4], q[5] correspond to measured joint rotation
265  // positions in rad
266  flog << q[0] << " " << q[1] << " " << q[2] << " " << q[3] << " " << q[4] << " " << q[5] << " ";
267 
268  // Save feature error (s-s*) for the feature point. For this feature
269  // point, we have 2 errors (along x and y axis). This error is
270  // expressed in meters in the camera frame
271  flog << (task.getError()).t() << std::endl; // s-s* for point
272 
273  vpDisplay::flush(I);
274 
275  // std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() <<
276  // std::endl;
277  }
278 
279  flog.close(); // Close the log file
280 
281  std::cout << "Display task information: " << std::endl;
282  task.print();
283  task.kill();
284  return EXIT_SUCCESS;
285  }
286  catch (const vpException &e) {
287  flog.close(); // Close the log file
288  std::cout << "Catch an exception: " << e.getMessage() << std::endl;
289  return EXIT_FAILURE;
290  }
291 }
292 
293 #else
294 int main()
295 {
296  std::cout << "You do not have an Viper 850 robot connected to your computer..." << std::endl;
297  return EXIT_SUCCESS;
298 }
299 #endif
void getPosition(const vpRobot::vpControlFrameType frame, vpColVector &position)
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:104
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:467
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:540
Control of Irisa&#39;s Viper S850 robot named Viper850.
void set_eJe(const vpMatrix &eJe_)
Definition: vpServo.h:508
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:151
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, const unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:497
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...
void get_eJe(vpMatrix &eJe)
static const vpColor green
Definition: vpColor.h:183
This tracker is meant to track a blob (connex pixels with same gray level) on a vpImage.
Definition: vpDot2.h:126
void track(const vpImage< unsigned char > &I)
Definition: vpDot2.cpp:438
static void flush(const vpImage< unsigned char > &I)
vpImagePoint getCog() const
Definition: vpDot2.h:161
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:597
void kill()
Definition: vpServo.cpp:192
Initialize the velocity controller.
Definition: vpRobot.h:67
vpColVector getError() const
Definition: vpServo.h:282
vpColVector computeControlLaw()
Definition: vpServo.cpp:935
#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...
Generic class defining intrinsic camera parameters.
void setLambda(double c)
Definition: vpServo.h:406
static std::string getUserName()
Definition: vpIoTools.cpp:298
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:138
const char * getMessage(void) const
Definition: vpException.cpp:90
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:574
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &velocity)
void buildFrom(const double x, const double y, const double Z)
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:72
void set_cVe(const vpVelocityTwistMatrix &cVe_)
Definition: vpServo.h:450
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:313
Class for firewire ieee1394 video devices using libdc1394-2.x api.
void set_Z(const double Z)
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 setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:223
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)
static const vpColor blue
Definition: vpColor.h:186