ViSP  3.0.0
servoViper650Point2DCamVelocity.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * tests the control law
32  * eye-in-hand control
33  * velocity computed in camera frame
34  *
35  * Authors:
36  * Eric Marchand
37  * Fabien Spindler
38  *
39  *****************************************************************************/
40 
41 
52 #include <visp3/core/vpConfig.h>
53 #include <visp3/core/vpDebug.h> // Debug trace
54 
55 #include <stdlib.h>
56 #include <stdio.h>
57 #include <iostream>
58 #include <fstream>
59 #include <sstream>
60 
61 #if (defined (VISP_HAVE_VIPER650) && defined (VISP_HAVE_DC1394) && defined (VISP_HAVE_X11))
62 
63 #include <visp3/sensor/vp1394TwoGrabber.h>
64 #include <visp3/core/vpImage.h>
65 #include <visp3/core/vpMath.h>
66 #include <visp3/core/vpHomogeneousMatrix.h>
67 #include <visp3/visual_features/vpFeaturePoint.h>
68 #include <visp3/core/vpPoint.h>
69 #include <visp3/vs/vpServo.h>
70 #include <visp3/visual_features/vpFeatureBuilder.h>
71 #include <visp3/robot/vpRobotViper650.h>
72 #include <visp3/core/vpIoTools.h>
73 #include <visp3/core/vpException.h>
74 #include <visp3/vs/vpServoDisplay.h>
75 #include <visp3/io/vpImageIo.h>
76 #include <visp3/blob/vpDot2.h>
77 #include <visp3/core/vpDisplay.h>
78 #include <visp3/gui/vpDisplayX.h>
79 
80 int
81 main()
82 {
83  // Log file creation in /tmp/$USERNAME/log.dat
84  // This file contains by line:
85  // - the 6 computed joint velocities (m/s, rad/s) to achieve the task
86  // - the 6 mesured joint velocities (m/s, rad/s)
87  // - the 6 mesured joint positions (m, rad)
88  // - the 2 values of s - s*
89  std::string username;
90  // Get the user login name
91  vpIoTools::getUserName(username);
92 
93  // Create a log filename to save velocities...
94  std::string logdirname;
95  logdirname ="/tmp/" + username;
96 
97  // Test if the output path exist. If no try to create it
98  if (vpIoTools::checkDirectory(logdirname) == false) {
99  try {
100  // Create the dirname
101  vpIoTools::makeDirectory(logdirname);
102  }
103  catch (...) {
104  std::cerr << std::endl
105  << "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  vpRobotViper650 robot ;
118 
119  vpServo task ;
120 
122 
123  bool reset = false;
124  vp1394TwoGrabber g(reset);
125 
126 #if 1
128  g.setFramerate(vp1394TwoGrabber::vpFRAMERATE_60);
129 #else
131  g.setColorCoding(vp1394TwoGrabber::vpCOLOR_CODING_MONO8);
132 #endif
133  g.open(I) ;
134 
135 
136  vpDisplayX display(I, (int)(100+I.getWidth()+30), 200, "Current image") ;
137 
138  vpDisplay::display(I) ;
139  vpDisplay::flush(I) ;
140 
141  vpDot2 dot ;
142  vpImagePoint cog;
143 
144  dot.setGraphics(true);
145 
146  for (int i=0; i< 10; i++)
147  g.acquire(I) ;
148 
149  std::cout << "Click on a dot..." << std::endl;
150  dot.initTracking(I) ;
151 
152  cog = dot.getCog();
154  vpDisplay::flush(I);
155 
156  vpCameraParameters cam ;
157  // Update camera parameters
158  robot.getCameraParameters (cam, I);
159 
160  // sets the current position of the visual feature
161  vpFeaturePoint p ;
162  // retrieve x,y and Z of the vpPoint structure
163  vpFeatureBuilder::create(p,cam, dot);
164 
165  // sets the desired position of the visual feature
166  vpFeaturePoint pd ;
167  pd.buildFrom(0,0,1) ;
168 
169  // define the task
170  // - we want an eye-in-hand control law
171  // - robot is controlled in the camera frame
173 
174  // - we want to see a point on a point
175  task.addFeature(p,pd) ;
176 
177  // - set the constant gain
178  task.setLambda(0.8) ;
179 
180  // Display task information
181  task.print() ;
182 
183  // Now the robot will be controlled in velocity
185 
186  std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
187  vpColVector v ;
188  for ( ; ; ) {
189  try {
190  // Acquire a new image from the camera
191  g.acquire(I) ;
192 
193  // Display this image
194  vpDisplay::display(I) ;
195 
196  // Achieve the tracking of the dot in the image
197  dot.track(I) ;
198 
199  // Get the dot cog
200  cog = dot.getCog();
201 
202  // Display a green cross at the center of gravity position in the image
204 
205  // Update the point feature from the dot location
206  vpFeatureBuilder::create(p, cam, dot);
207 
208  // Compute the visual servoing skew vector
209  v = task.computeControlLaw() ;
210 
211  // Display the current and desired feature points in the image display
212  vpServoDisplay::display(task, cam, I) ;
213 
214  // Apply the computed camera velocities to the robot
216  }
217  catch(...) {
218  std::cout << "Tracking failed... Stop the robot." << std::endl;
219  v = 0;
220  // Stop robot
222  return 0;
223  }
224 
225  // Save velocities applied to the robot in the log file
226  // v[0], v[1], v[2] correspond to camera translation velocities in m/s
227  // v[3], v[4], v[5] correspond to camera rotation velocities in rad/s
228  flog << v[0] << " " << v[1] << " " << v[2] << " "
229  << v[3] << " " << v[4] << " " << v[5] << " ";
230 
231  // Get the measured joint velocities of the robot
232  vpColVector qvel;
234  // Save measured joint velocities of the robot in the log file:
235  // - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
236  // velocities in m/s
237  // - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
238  // velocities in rad/s
239  flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " "
240  << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
241 
242  // Get the measured joint positions of the robot
243  vpColVector q;
245  // Save measured joint positions of the robot in the log file
246  // - q[0], q[1], q[2] correspond to measured joint translation
247  // positions in m
248  // - q[3], q[4], q[5] correspond to measured joint rotation
249  // positions in rad
250  flog << q[0] << " " << q[1] << " " << q[2] << " "
251  << q[3] << " " << q[4] << " " << q[5] << " ";
252 
253  // Save feature error (s-s*) for the feature point. For this feature
254  // point, we have 2 errors (along x and y axis). This error is expressed
255  // in meters in the camera frame
256  flog << ( task.getError() ).t() << std::endl; // s-s* for point
257 
258  // Flush the display
259  vpDisplay::flush(I) ;
260 
261  }
262 
263  flog.close() ; // Close the log file
264 
265  // Display task information
266  task.print() ;
267 
268  // Kill the task
269  task.kill();
270 
271  return 0;
272  }
273  catch (...)
274  {
275  flog.close() ; // Close the log file
276  vpERROR_TRACE(" Test failed") ;
277  return 0;
278  }
279 }
280 
281 #else
282 int
283 main()
284 {
285  vpERROR_TRACE("You do not have a Viper robot or a firewire framegrabber connected to your computer...");
286 }
287 #endif
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &velocity)
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:335
unsigned int getWidth() const
Definition: vpImage.h:161
void getVelocity(const vpRobot::vpControlFrameType frame, vpColVector &velocity)
Control of Irisa&#39;s Viper S650 robot named Viper650.
#define vpERROR_TRACE
Definition: vpDebug.h:391
Define the X11 console to display images.
Definition: vpDisplayX.h:148
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, const unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:446
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
static const vpColor green
Definition: vpColor.h:166
This tracker is meant to track a blob (connex pixels with same gray level) on a vpImage.
Definition: vpDot2.h:124
void track(const vpImage< unsigned char > &I)
Definition: vpDot2.cpp:461
void getCameraParameters(vpCameraParameters &cam, const unsigned int &image_width, const unsigned int &image_height) const
Definition: vpViper650.cpp:660
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:2233
vpImagePoint getCog() const
Definition: vpDot2.h:160
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:404
void kill()
Definition: vpServo.cpp:186
vpRobot::vpRobotStateType setRobotState(vpRobot::vpRobotStateType newState)
Initialize the velocity controller.
Definition: vpRobot.h:68
vpColVector getError() const
Definition: vpServo.h:271
vpColVector computeControlLaw()
Definition: vpServo.cpp:899
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
virtual void displayCross(const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)=0
Generic class defining intrinsic camera parameters.
void setLambda(double c)
Definition: vpServo.h:390
static std::string getUserName()
Definition: vpIoTools.cpp:161
void buildFrom(const double x, const double y, const double Z)
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
void initTracking(const vpImage< unsigned char > &I, unsigned int size=0)
Definition: vpDot2.cpp:262
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:248
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 setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:217
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 setGraphics(const bool activate)
Definition: vpDot2.h:309
static const vpColor blue
Definition: vpColor.h:169
void getPosition(const vpRobot::vpControlFrameType frame, vpColVector &position)