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