ViSP  3.0.0
servoAfma6Segment2DCamVelocity.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 the camera frame
34  *
35  * Authors:
36  * Filip Novotny
37  *
38  *****************************************************************************/
39 
50 #include <visp3/core/vpConfig.h>
51 #include <visp3/core/vpDebug.h> // Debug trace
52 #include <stdlib.h>
53 #include <vector>
54 #if (defined (VISP_HAVE_AFMA6) && defined (VISP_HAVE_DC1394))
55 
56 #include <visp3/sensor/vp1394TwoGrabber.h>
57 #include <visp3/core/vpImage.h>
58 #include <visp3/core/vpImagePoint.h>
59 #include <visp3/core/vpMath.h>
60 #include <visp3/core/vpHomogeneousMatrix.h>
61 #include <visp3/visual_features/vpFeatureSegment.h>
62 #include <visp3/core/vpPoint.h>
63 #include <visp3/vs/vpServo.h>
64 #include <visp3/visual_features/vpFeatureBuilder.h>
65 #include <visp3/robot/vpRobotAfma6.h>
66 #include <visp3/core/vpIoTools.h>
67 #include <visp3/core/vpException.h>
68 #include <visp3/vs/vpServoDisplay.h>
69 #include <visp3/blob/vpDot.h>
70 #include <visp3/core/vpDisplay.h>
71 #include <visp3/gui/vpDisplayX.h>
72 #include <visp3/gui/vpDisplayOpenCV.h>
73 #include <visp3/gui/vpDisplayGTK.h>
74 
75 int
76 main()
77 {
78  // Log file creation in /tmp/$USERNAME/log.dat
79  // This file contains by line:
80  // - the 6 computed cam velocities (m/s, rad/s) to achieve the task
81  // - the 6 mesured joint velocities (m/s, rad/s)
82  // - the 6 mesured joint positions (m, rad)
83  // - the 2 values of s - s*
84  std::string username;
85  // Get the user login name
86  vpIoTools::getUserName(username);
87 
88  // Create a log filename to save velocities...
89  std::string logdirname;
90  logdirname ="/tmp/" + username;
91 
92  // Test if the output path exist. If no try to create it
93  if (vpIoTools::checkDirectory(logdirname) == false) {
94  try {
95  // Create the dirname
96  vpIoTools::makeDirectory(logdirname);
97  }
98  catch (...) {
99  std::cerr << std::endl
100  << "ERROR:" << std::endl;
101  std::cerr << " Cannot create " << logdirname << std::endl;
102  exit(-1);
103  }
104  }
105  std::string logfilename;
106  logfilename = logdirname + "/log.dat";
107 
108  // Open the log file name
109  std::ofstream flog(logfilename.c_str());
110 
111  try
112  {
113  vpServo task ;
114 
116 
120  g.open(I) ;
121 
122  g.acquire(I) ;
123 
124 #ifdef VISP_HAVE_X11
125  vpDisplayX display(I,100,100,"Current image") ;
126 #elif defined(VISP_HAVE_OPENCV)
127  vpDisplayOpenCV display(I,100,100,"Current image") ;
128 #elif defined(VISP_HAVE_GTK)
129  vpDisplayGTK display(I,100,100,"Current image") ;
130 #endif
131 
132  vpDisplay::display(I) ;
133  vpDisplay::flush(I) ;
134 
135  std::vector<vpDot> dot_d(2), dot(2) ;
136  vpFeatureSegment seg_d,seg ;
137  vpImagePoint cog;
138  vpRobotAfma6 robot ;
139  vpCameraParameters cam ;
140 
141  // Update camera parameters
142  robot.getCameraParameters (cam, I);
143  std::cout << "define the initial segment" << std::endl;
144 
145  for(std::vector<vpDot>::iterator i = dot.begin();i!=dot.end();i++){
146  std::cout << "Click on a dot..." << std::endl;
147  i->initTracking(I) ;
148  cog = i->getCog();
150  vpDisplay::flush(I);
151  }
152  vpFeatureBuilder::create(seg, cam, dot[0], dot[1]);
153  seg.display(cam,I,vpColor::red);
154  vpDisplay::flush(I);
155  std::cout << "define the destination segment" << std::endl;
156  for(std::vector<vpDot>::iterator i = dot_d.begin();i!=dot_d.end();i++){
157  vpImagePoint ip;
158  vpDisplay::getClick(I,ip);
159  *i = vpDot(ip);
161  }
162  vpFeatureBuilder::create(seg_d, cam, dot_d[0], dot_d[1]);
163  seg_d.setZ1( 1. );
164  seg_d.setZ2( 1. );
165 
166  seg_d.display(cam,I);
167  vpDisplay::flush(I);
168  // define the task
169  // - we want an eye-in-hand control law
170  // - robot is controlled in the camera frame
173 
174  // - we want to see both segments
175  task.addFeature(seg, seg_d) ;
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  for ( ; ; ) {
188  // Acquire a new image from the camera
189  g.acquire(I) ;
190 
191  // Display this image
192  vpDisplay::display(I) ;
193 
194  // Achieve the tracking of the dot in the image
195  for(std::vector<vpDot>::iterator i = dot.begin();i!=dot.end();i++){
196  i->track(I) ;
197  }
198 
199  // Update the segment feature from the dot locations
200  vpFeatureBuilder::create(seg, cam, dot[0], dot[1]);
201 
202  vpColVector v ;
203  // Compute the visual servoing skew vector
204  v = task.computeControlLaw() ;
205 
206  // Display the current and desired feature segments in the image display
207  vpServoDisplay::display(task, cam, I) ;
208 
209  // Apply the computed joint velocities to the robot
211 
212 
213  // Save feature error (s-s*) for the feature segment. For this feature
214  // segments, we have 4 errors (Xc,Yc,l,alpha).
215  flog << ( task.getError() ).t() << std::endl;
216 
217  // Flush the display
218  vpDisplay::flush(I) ;
219 
220  }
221 
222  flog.close() ; // Close the log file
223 
224  // Display task information
225  task.print() ;
226 
227  // Kill the task
228  task.kill();
229 
230  return 0;
231  }
232  catch (...)
233  {
234  flog.close() ; // Close the log file
235  vpERROR_TRACE(" Test failed") ;
236  return 0;
237  }
238 }
239 
240 
241 #else
242 int
243 main()
244 {
245  vpERROR_TRACE("You do not have an afma6 robot or a firewire framegrabber connected to your computer...");
246 }
247 #endif
void getCameraParameters(vpCameraParameters &cam, const unsigned int &image_width, const unsigned int &image_height) const
Definition: vpAfma6.cpp:1244
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:335
#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
static const vpColor green
Definition: vpColor.h:166
void acquire(vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:2233
void setZ1(const double val)
Control of Irisa&#39;s gantry robot named Afma6.
Definition: vpRobotAfma6.h:210
static const vpColor red
Definition: vpColor.h:163
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:404
void open(vpImage< unsigned char > &I)
void kill()
Definition: vpServo.cpp:186
Initialize the velocity controller.
Definition: vpRobot.h:68
vpColVector getError() const
Definition: vpServo.h:271
vpColVector computeControlLaw()
Definition: vpServo.cpp:899
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:206
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.
void setLambda(double c)
Definition: vpServo.h:390
static std::string getUserName()
Definition: vpIoTools.cpp:161
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
Definition: vpDisplayGTK.h:141
vpRobot::vpRobotStateType setRobotState(vpRobot::vpRobotStateType newState)
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:519
Implementation of column vector and the associated operations.
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:248
This tracker is meant to track a dot (connected pixels with same gray level) on a vpImage...
Definition: vpDot.h:115
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: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 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:169