ViSP  3.0.0
servoViper850Point2DCamVelocity.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_VIPER850) && defined (VISP_HAVE_DC1394))
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/vpRobotViper850.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 #include <visp3/gui/vpDisplayOpenCV.h>
80 #include <visp3/gui/vpDisplayGTK.h>
81 
82 
83 int
84 main()
85 {
86  // Log file creation in /tmp/$USERNAME/log.dat
87  // This file contains by line:
88  // - the 6 computed joint velocities (m/s, rad/s) to achieve the task
89  // - the 6 mesured joint velocities (m/s, rad/s)
90  // - the 6 mesured joint positions (m, rad)
91  // - the 2 values of s - s*
92  std::string username;
93  // Get the user login name
94  vpIoTools::getUserName(username);
95 
96  // Create a log filename to save velocities...
97  std::string logdirname;
98  logdirname ="/tmp/" + username;
99 
100  // Test if the output path exist. If no try to create it
101  if (vpIoTools::checkDirectory(logdirname) == false) {
102  try {
103  // Create the dirname
104  vpIoTools::makeDirectory(logdirname);
105  }
106  catch (...) {
107  std::cerr << std::endl
108  << "ERROR:" << std::endl;
109  std::cerr << " Cannot create " << logdirname << std::endl;
110  exit(-1);
111  }
112  }
113  std::string logfilename;
114  logfilename = logdirname + "/log.dat";
115 
116  // Open the log file name
117  std::ofstream flog(logfilename.c_str());
118 
119  try {
120  vpRobotViper850 robot ;
121 
122  vpServo task ;
123 
125 
126  bool reset = false;
127  vp1394TwoGrabber g(reset);
128 
129 #if 1
131  g.setFramerate(vp1394TwoGrabber::vpFRAMERATE_60);
132 #else
134  g.setColorCoding(vp1394TwoGrabber::vpCOLOR_CODING_MONO8);
135 #endif
136  g.open(I) ;
137 
138 #ifdef VISP_HAVE_X11
139  vpDisplayX display(I, (int)(100+I.getWidth()+30), 200, "Current image") ;
140 #elif defined(VISP_HAVE_OPENCV)
141  vpDisplayOpenCV display(I, (int)(100+I.getWidth()+30), 200, "Current image") ;
142 #elif defined(VISP_HAVE_GTK)
143  vpDisplayGTK display(I, (int)(100+I.getWidth()+30), 200, "Current image") ;
144 #endif
145 
146  vpDisplay::display(I) ;
147  vpDisplay::flush(I) ;
148 
149  vpDot2 dot ;
150  vpImagePoint cog;
151 
152  dot.setGraphics(true);
153 
154  for (int i=0; i< 10; i++)
155  g.acquire(I) ;
156 
157  std::cout << "Click on a dot..." << std::endl;
158  dot.initTracking(I) ;
159 
160  cog = dot.getCog();
162  vpDisplay::flush(I);
163 
164  vpCameraParameters cam ;
165  // Update camera parameters
166  robot.getCameraParameters (cam, I);
167 
168  // sets the current position of the visual feature
169  vpFeaturePoint p ;
170  // retrieve x,y and Z of the vpPoint structure
171  vpFeatureBuilder::create(p,cam, dot);
172 
173  // sets the desired position of the visual feature
174  vpFeaturePoint pd ;
175  pd.buildFrom(0,0,1) ;
176 
177  // define the task
178  // - we want an eye-in-hand control law
179  // - robot is controlled in the camera frame
181 
182  // - we want to see a point on a point
183  task.addFeature(p,pd) ;
184 
185  // - set the constant gain
186  task.setLambda(0.8) ;
187 
188  // Display task information
189  task.print() ;
190 
191  // Now the robot will be controlled in velocity
193 
194  std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
195  vpColVector v ;
196  for ( ; ; ) {
197  try {
198  // Acquire a new image from the camera
199  g.acquire(I) ;
200 
201  // Display this image
202  vpDisplay::display(I) ;
203 
204  // Achieve the tracking of the dot in the image
205  dot.track(I) ;
206 
207  // Get the dot cog
208  cog = dot.getCog();
209 
210  // Display a green cross at the center of gravity position in the image
212 
213  // Update the point feature from the dot location
214  vpFeatureBuilder::create(p, cam, dot);
215 
216  // Compute the visual servoing skew vector
217  v = task.computeControlLaw() ;
218 
219  // Display the current and desired feature points in the image display
220  vpServoDisplay::display(task, cam, I) ;
221 
222  // Apply the computed camera velocities to the robot
224  }
225  catch(...) {
226  std::cout << "Tracking failed... Stop the robot." << std::endl;
227  v = 0;
228  // Stop robot
230  return 0;
231  }
232 
233  // Save velocities applied to the robot in the log file
234  // v[0], v[1], v[2] correspond to camera translation velocities in m/s
235  // v[3], v[4], v[5] correspond to camera rotation velocities in rad/s
236  flog << v[0] << " " << v[1] << " " << v[2] << " "
237  << v[3] << " " << v[4] << " " << v[5] << " ";
238 
239  // Get the measured joint velocities of the robot
240  vpColVector qvel;
242  // Save measured joint velocities of the robot in the log file:
243  // - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
244  // velocities in m/s
245  // - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
246  // velocities in rad/s
247  flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " "
248  << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
249 
250  // Get the measured joint positions of the robot
251  vpColVector q;
253  // Save measured joint positions of the robot in the log file
254  // - q[0], q[1], q[2] correspond to measured joint translation
255  // positions in m
256  // - q[3], q[4], q[5] correspond to measured joint rotation
257  // positions in rad
258  flog << q[0] << " " << q[1] << " " << q[2] << " "
259  << q[3] << " " << q[4] << " " << q[5] << " ";
260 
261  // Save feature error (s-s*) for the feature point. For this feature
262  // point, we have 2 errors (along x and y axis). This error is expressed
263  // in meters in the camera frame
264  flog << ( task.getError() ).t() << std::endl; // s-s* for point
265 
266  // Flush the display
267  vpDisplay::flush(I) ;
268  }
269 
270  flog.close() ; // Close the log file
271 
272  // Display task information
273  task.print() ;
274 
275  // Kill the task
276  task.kill();
277 
278  return 0;
279  }
280  catch (...)
281  {
282  flog.close() ; // Close the log file
283  vpERROR_TRACE(" Test failed") ;
284  return 0;
285  }
286 }
287 
288 #else
289 int
290 main()
291 {
292  vpERROR_TRACE("You do not have a Viper robot or a firewire framegrabber connected to your computer...");
293 }
294 #endif
void getPosition(const vpRobot::vpControlFrameType frame, vpColVector &position)
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:335
unsigned int getWidth() const
Definition: vpImage.h:161
void getCameraParameters(vpCameraParameters &cam, const unsigned int &image_width, const unsigned int &image_height) const
Definition: vpViper850.cpp:584
Control of Irisa's Viper S850 robot named Viper850.
#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
vpRobot::vpRobotStateType setRobotState(vpRobot::vpRobotStateType newState)
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
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
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
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
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &velocity)
void buildFrom(const double x, const double y, const double Z)
void getVelocity(const vpRobot::vpControlFrameType frame, vpColVector &velocity)
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