ViSP  2.8.0
servoAfma6Segment2DCamVelocity.cpp
1 /****************************************************************************
2  *
3  * $Id: servoAfma6Point2DCamVelocity.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 the camera frame
38  *
39  * Authors:
40  * Filip Novotny
41  *
42  *****************************************************************************/
43 
54 #include <visp/vpConfig.h>
55 #include <visp/vpDebug.h> // Debug trace
56 #include <stdlib.h>
57 #include <vector>
58 #if (defined (VISP_HAVE_AFMA6) && defined (VISP_HAVE_DC1394_2))
59 
60 #include <visp/vp1394TwoGrabber.h>
61 #include <visp/vpImage.h>
62 #include <visp/vpImagePoint.h>
63 #include <visp/vpMath.h>
64 #include <visp/vpHomogeneousMatrix.h>
65 #include <visp/vpFeatureSegment.h>
66 #include <visp/vpPoint.h>
67 #include <visp/vpServo.h>
68 #include <visp/vpFeatureBuilder.h>
69 #include <visp/vpRobotAfma6.h>
70 #include <visp/vpIoTools.h>
71 #include <visp/vpException.h>
72 #include <visp/vpMatrixException.h>
73 #include <visp/vpServoDisplay.h>
74 #include <visp/vpDot.h>
75 #include <visp/vpDisplay.h>
76 #include <visp/vpDisplayX.h>
77 #include <visp/vpDisplayOpenCV.h>
78 #include <visp/vpDisplayGTK.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 cam 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  {
118  vpServo task ;
119 
121 
125  g.open(I) ;
126 
127  g.acquire(I) ;
128 
129 #ifdef VISP_HAVE_X11
130  vpDisplayX display(I,100,100,"Current image") ;
131 #elif defined(VISP_HAVE_OPENCV)
132  vpDisplayOpenCV display(I,100,100,"Current image") ;
133 #elif defined(VISP_HAVE_GTK)
134  vpDisplayGTK display(I,100,100,"Current image") ;
135 #endif
136 
137  vpDisplay::display(I) ;
138  vpDisplay::flush(I) ;
139 
140  std::vector<vpDot> dot_d(2), dot(2) ;
141  vpFeatureSegment seg_d,seg ;
142  vpImagePoint cog;
143  vpRobotAfma6 robot ;
144  vpCameraParameters cam ;
145 
146  // Update camera parameters
147  robot.getCameraParameters (cam, I);
148  std::cout << "define the initial segment" << std::endl;
149 
150  for(std::vector<vpDot>::iterator i = dot.begin();i!=dot.end();i++){
151  std::cout << "Click on a dot..." << std::endl;
152  i->initTracking(I) ;
153  cog = i->getCog();
155  vpDisplay::flush(I);
156  }
157  vpFeatureBuilder::create(seg, cam, dot[0], dot[1]);
158  seg.display(cam,I,vpColor::red);
159  vpDisplay::flush(I);
160  std::cout << "define the destination segment" << std::endl;
161  for(std::vector<vpDot>::iterator i = dot_d.begin();i!=dot_d.end();i++){
162  vpImagePoint ip;
163  vpDisplay::getClick(I,ip);
164  *i = vpDot(ip);
166  }
167  vpFeatureBuilder::create(seg_d, cam, dot_d[0], dot_d[1]);
168  seg_d.setZ1( 1. );
169  seg_d.setZ2( 1. );
170 
171  seg_d.display(cam,I);
172  vpDisplay::flush(I);
173  // define the task
174  // - we want an eye-in-hand control law
175  // - robot is controlled in the camera frame
178 
179  // - we want to see both segments
180  task.addFeature(seg, seg_d) ;
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  for ( ; ; ) {
193  // Acquire a new image from the camera
194  g.acquire(I) ;
195 
196  // Display this image
197  vpDisplay::display(I) ;
198 
199  // Achieve the tracking of the dot in the image
200  for(std::vector<vpDot>::iterator i = dot.begin();i!=dot.end();i++){
201  i->track(I) ;
202  }
203 
204  // Update the segment feature from the dot locations
205  vpFeatureBuilder::create(seg, cam, dot[0], dot[1]);
206 
207  vpColVector v ;
208  // Compute the visual servoing skew vector
209  v = task.computeControlLaw() ;
210 
211  // Display the current and desired feature segments in the image display
212  vpServoDisplay::display(task, cam, I) ;
213 
214  // Apply the computed joint velocities to the robot
216 
217 
218  // Save feature error (s-s*) for the feature segment. For this feature
219  // segments, we have 4 errors (Xc,Yc,l,alpha).
220  flog << ( task.getError() ).t() << std::endl;
221 
222  // Flush the display
223  vpDisplay::flush(I) ;
224 
225  }
226 
227  flog.close() ; // Close the log file
228 
229  // Display task information
230  task.print() ;
231 
232  // Kill the task
233  task.kill();
234 
235  return 0;
236  }
237  catch (...)
238  {
239  flog.close() ; // Close the log file
240  vpERROR_TRACE(" Test failed") ;
241  return 0;
242  }
243 }
244 
245 
246 #else
247 int
248 main()
249 {
250  vpERROR_TRACE("You do not have an afma6 robot or a firewire framegrabber connected to your computer...");
251 }
252 #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)
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:335
#define vpERROR_TRACE
Definition: vpDebug.h:379
void getCameraParameters(vpCameraParameters &cam, const unsigned int &image_width, const unsigned int &image_height)
Definition: vpAfma6.cpp:1234
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
static const vpColor green
Definition: vpColor.h:170
void acquire(vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1991
void setZ1(const double val)
Control of Irisa's gantry robot named Afma6.
Definition: vpRobotAfma6.h:214
static const vpColor red
Definition: vpColor.h:167
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:404
void open(vpImage< unsigned char > &I)
void kill()
destruction (memory deallocation if required)
Definition: vpServo.cpp:177
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
void setZ2(const double val)
Class that defines a 2D segment visual features. This class allow to consider two sets of visual feat...
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:203
The vpDisplayOpenCV allows to display image using the opencv library.
virtual void displayCross(const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)=0
Generic class defining intrinsic camera parameters.
static std::string getUserName()
Definition: vpIoTools.cpp:140
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
Definition: vpDisplayGTK.h:145
vpRobot::vpRobotStateType setRobotState(vpRobot::vpRobotStateType newState)
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Set the type of the interaction matrix (current, mean, desired, user).
Definition: vpServo.cpp:509
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 setFramerate(vp1394TwoFramerateType fps)
void setVideoMode(vp1394TwoVideoModeType videomode)
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:258
This tracker is meant to track a dot (connected pixels with same gray level) on a vpImage...
Definition: vpDot.h:118
Class for firewire ieee1394 video devices using libdc1394-2.x api.
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &velocity)
virtual bool getClick(bool blocking=true)=0
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 display(const vpCameraParameters &cam, const vpImage< unsigned char > &I, const vpColor &color=vpColor::green, unsigned int thickness=1) const
static const vpColor blue
Definition: vpColor.h:173