Visual Servoing Platform  version 3.3.0
servoAfma6Segment2DCamVelocity.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * tests the control law
33  * eye-in-hand control
34  * velocity computed in the camera frame
35  *
36  * Authors:
37  * Filip Novotny
38  *
39  *****************************************************************************/
40 
50 #include <stdlib.h>
51 #include <vector>
52 #include <visp3/core/vpConfig.h>
53 #include <visp3/core/vpDebug.h> // Debug trace
54 #if (defined(VISP_HAVE_AFMA6) && defined(VISP_HAVE_DC1394))
55 
56 #include <visp3/blob/vpDot.h>
57 #include <visp3/core/vpDisplay.h>
58 #include <visp3/core/vpException.h>
59 #include <visp3/core/vpHomogeneousMatrix.h>
60 #include <visp3/core/vpImage.h>
61 #include <visp3/core/vpImagePoint.h>
62 #include <visp3/core/vpIoTools.h>
63 #include <visp3/core/vpMath.h>
64 #include <visp3/core/vpPoint.h>
65 #include <visp3/gui/vpDisplayGTK.h>
66 #include <visp3/gui/vpDisplayOpenCV.h>
67 #include <visp3/gui/vpDisplayX.h>
68 #include <visp3/robot/vpRobotAfma6.h>
69 #include <visp3/sensor/vp1394TwoGrabber.h>
70 #include <visp3/visual_features/vpFeatureBuilder.h>
71 #include <visp3/visual_features/vpFeatureSegment.h>
72 #include <visp3/vs/vpServo.h>
73 #include <visp3/vs/vpServoDisplay.h>
74 
75 int main()
76 {
77  // Log file creation in /tmp/$USERNAME/log.dat
78  // This file contains by line:
79  // - the 6 computed cam velocities (m/s, rad/s) to achieve the task
80  // - the 6 mesured joint velocities (m/s, rad/s)
81  // - the 6 mesured joint positions (m, rad)
82  // - the 2 values of s - s*
83  std::string username;
84  // Get the user login name
85  vpIoTools::getUserName(username);
86 
87  // Create a log filename to save velocities...
88  std::string logdirname;
89  logdirname = "/tmp/" + username;
90 
91  // Test if the output path exist. If no try to create it
92  if (vpIoTools::checkDirectory(logdirname) == false) {
93  try {
94  // Create the dirname
95  vpIoTools::makeDirectory(logdirname);
96  } catch (...) {
97  std::cerr << std::endl << "ERROR:" << std::endl;
98  std::cerr << " Cannot create " << logdirname << std::endl;
99  exit(-1);
100  }
101  }
102  std::string logfilename;
103  logfilename = logdirname + "/log.dat";
104 
105  // Open the log file name
106  std::ofstream flog(logfilename.c_str());
107 
108  try {
109  vpServo task;
110 
112 
116  g.open(I);
117 
118  g.acquire(I);
119 
120 #ifdef VISP_HAVE_X11
121  vpDisplayX display(I, 100, 100, "Current image");
122 #elif defined(VISP_HAVE_OPENCV)
123  vpDisplayOpenCV display(I, 100, 100, "Current image");
124 #elif defined(VISP_HAVE_GTK)
125  vpDisplayGTK display(I, 100, 100, "Current image");
126 #endif
127 
129  vpDisplay::flush(I);
130 
131  std::vector<vpDot> dot_d(2), dot(2);
132  vpFeatureSegment seg_d, seg;
133  vpImagePoint cog;
134  vpRobotAfma6 robot;
135  vpCameraParameters cam;
136 
137  // Update camera parameters
138  robot.getCameraParameters(cam, I);
139  std::cout << "define the initial segment" << std::endl;
140 
141  for (std::vector<vpDot>::iterator i = dot.begin(); i != dot.end(); ++i) {
142  std::cout << "Click on a dot..." << std::endl;
143  i->initTracking(I);
144  cog = i->getCog();
146  vpDisplay::flush(I);
147  }
148  vpFeatureBuilder::create(seg, cam, dot[0], dot[1]);
149  seg.display(cam, I, vpColor::red);
150  vpDisplay::flush(I);
151  std::cout << "define the destination segment" << std::endl;
152  for (std::vector<vpDot>::iterator i = dot_d.begin(); i != dot_d.end(); ++i) {
153  vpImagePoint ip;
154  vpDisplay::getClick(I, ip);
155  *i = vpDot(ip);
157  }
158  vpFeatureBuilder::create(seg_d, cam, dot_d[0], dot_d[1]);
159  seg_d.setZ1(1.);
160  seg_d.setZ2(1.);
161 
162  seg_d.display(cam, I);
163  vpDisplay::flush(I);
164  // define the task
165  // - we want an eye-in-hand control law
166  // - robot is controlled in the camera frame
169 
170  // - we want to see both segments
171  task.addFeature(seg, seg_d);
172 
173  // - set the constant gain
174  task.setLambda(0.8);
175 
176  // Display task information
177  task.print();
178 
179  // Now the robot will be controlled in velocity
181 
182  std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
183  for (;;) {
184  // Acquire a new image from the camera
185  g.acquire(I);
186 
187  // Display this image
189 
190  // Achieve the tracking of the dot in the image
191  for (std::vector<vpDot>::iterator i = dot.begin(); i != dot.end(); ++i) {
192  i->track(I);
193  }
194 
195  // Update the segment feature from the dot locations
196  vpFeatureBuilder::create(seg, cam, dot[0], dot[1]);
197 
198  vpColVector v;
199  // Compute the visual servoing skew vector
200  v = task.computeControlLaw();
201 
202  // Display the current and desired feature segments in the image display
203  vpServoDisplay::display(task, cam, I);
204 
205  // Apply the computed joint velocities to the robot
207 
208  // Save feature error (s-s*) for the feature segment. For this feature
209  // segments, we have 4 errors (Xc,Yc,l,alpha).
210  flog << (task.getError()).t() << std::endl;
211 
212  // Flush the display
213  vpDisplay::flush(I);
214  }
215 
216  flog.close(); // Close the log file
217 
218  // Display task information
219  task.print();
220 
221  // Kill the task
222  task.kill();
223 
224  return EXIT_SUCCESS;
225  }
226  catch (const vpException &e) {
227  flog.close(); // Close the log file
228  std::cout << "Test failed with exception: " << e << std::endl;
229  return EXIT_FAILURE;
230  }
231 }
232 
233 #else
234 int main()
235 {
236  std::cout << "You do not have an afma6 robot connected to your computer..." << std::endl;
237  return EXIT_SUCCESS;
238 }
239 #endif
vpRobot::STATE_VELOCITY_CONTROL
@ STATE_VELOCITY_CONTROL
Initialize the velocity controller.
Definition: vpRobot.h:66
vpIoTools::getUserName
static std::string getUserName()
Definition: vpIoTools.cpp:318
vpDisplayX
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:150
vp1394TwoGrabber::setVideoMode
void setVideoMode(vp1394TwoVideoModeType videomode)
Definition: vp1394TwoGrabber.cpp:445
vpIoTools::checkDirectory
static bool checkDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:422
vpServo::kill
void kill()
Definition: vpServo.cpp:192
vpFeatureSegment::setZ1
void setZ1(double val)
Definition: vpFeatureSegment.h:238
vpCameraParameters
Generic class defining intrinsic camera parameters.
Definition: vpCameraParameters.h:233
vpRobotAfma6
Control of Irisa's gantry robot named Afma6.
Definition: vpRobotAfma6.h:211
vpServo::setLambda
void setLambda(double c)
Definition: vpServo.h:406
vp1394TwoGrabber::setFramerate
void setFramerate(vp1394TwoFramerateType fps)
Definition: vp1394TwoGrabber.cpp:680
vp1394TwoGrabber::vpVIDEO_MODE_640x480_MONO8
@ vpVIDEO_MODE_640x480_MONO8
Definition: vp1394TwoGrabber.h:215
vpServo::EYEINHAND_CAMERA
@ EYEINHAND_CAMERA
Definition: vpServo.h:159
vpFeatureBuilder::create
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
Definition: vpFeatureBuilderPoint.cpp:93
vpFeatureSegment
Class that defines a 2D segment visual features. This class allow to consider two sets of visual feat...
Definition: vpFeatureSegment.h:73
vpColVector
Implementation of column vector and the associated operations.
Definition: vpColVector.h:130
vp1394TwoGrabber::vpFRAMERATE_60
@ vpFRAMERATE_60
Definition: vp1394TwoGrabber.h:254
vpDisplayOpenCV
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Definition: vpDisplayOpenCV.h:141
vpServo::setServo
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:223
vpRobot::setRobotState
virtual vpRobotStateType setRobotState(const vpRobot::vpRobotStateType newState)
Definition: vpRobot.cpp:201
vpColor::green
static const vpColor green
Definition: vpColor.h:182
vpServo::print
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:313
vpDisplay::display
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay_uchar.cpp:740
vpDisplayGTK
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:137
vpIoTools::makeDirectory
static void makeDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:572
vpServo::getError
vpColVector getError() const
Definition: vpServo.h:282
vpServo::DESIRED
@ DESIRED
Definition: vpServo.h:190
vp1394TwoGrabber
Class for firewire ieee1394 video devices using libdc1394-2.x api.
Definition: vp1394TwoGrabber.h:184
vpImagePoint
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:88
vpFeatureSegment::setZ2
void setZ2(double val)
Definition: vpFeatureSegment.h:271
vpRobot::CAMERA_FRAME
@ CAMERA_FRAME
Definition: vpRobot.h:82
vpServo::addFeature
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:497
vp1394TwoGrabber::acquire
void acquire(vpImage< unsigned char > &I)
Definition: vp1394TwoGrabber.cpp:2491
vpServo::setInteractionMatrixType
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:574
vpColor::blue
static const vpColor blue
Definition: vpColor.h:185
vpServo
Definition: vpServo.h:150
vpServo::computeControlLaw
vpColVector computeControlLaw()
Definition: vpServo.cpp:935
vpDisplay::flush
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay_uchar.cpp:716
vpDisplay::displayCross
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
Definition: vpDisplay_uchar.cpp:180
vpImage< unsigned char >
vp1394TwoGrabber::open
void open(vpImage< unsigned char > &I)
Definition: vp1394TwoGrabber.cpp:2066
vpDisplay::getClick
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
Definition: vpDisplay_uchar.cpp:765
vpSimulatorCamera::setVelocity
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
Definition: vpSimulatorCamera.cpp:198
vpServoDisplay::display
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)
Definition: vpServoDisplay.cpp:80
vpException
error that can be emited by ViSP classes.
Definition: vpException.h:71
vpDot
This tracker is meant to track a dot (connected pixels with same gray level) on a vpImage.
Definition: vpDot.h:115
vpColor::red
static const vpColor red
Definition: vpColor.h:179
vpFeatureSegment::display
void display(const vpCameraParameters &cam, const vpImage< unsigned char > &I, const vpColor &color=vpColor::green, unsigned int thickness=1) const
Definition: vpFeatureSegment.cpp:483