Visual Servoing Platform  version 3.4.0
servoViper650Point2DCamVelocity.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 camera frame
35  *
36  * Authors:
37  * Eric Marchand
38  * Fabien Spindler
39  *
40  *****************************************************************************/
41 
57 #include <fstream>
58 #include <iostream>
59 #include <sstream>
60 #include <stdio.h>
61 #include <stdlib.h>
62 
63 #include <visp3/core/vpConfig.h>
64 
65 #if defined(VISP_HAVE_VIPER650) && defined(VISP_HAVE_DC1394) && defined(VISP_HAVE_X11)
66 
67 #include <visp3/blob/vpDot2.h>
68 #include <visp3/core/vpHomogeneousMatrix.h>
69 #include <visp3/core/vpIoTools.h>
70 #include <visp3/core/vpPoint.h>
71 #include <visp3/gui/vpDisplayX.h>
72 #include <visp3/robot/vpRobotViper650.h>
73 #include <visp3/sensor/vp1394TwoGrabber.h>
74 #include <visp3/vision/vpPose.h>
75 #include <visp3/visual_features/vpFeatureBuilder.h>
76 #include <visp3/visual_features/vpFeaturePoint.h>
77 #include <visp3/vs/vpServo.h>
78 #include <visp3/vs/vpServoDisplay.h>
79 
80 int main()
81 {
82  // Log file creation in /tmp/$USERNAME/log.dat
83  // This file contains by line:
84  // - the 6 computed camera velocities (m/s, rad/s) to achieve the task
85  // - the 6 mesured joint velocities (m/s, rad/s)
86  // - the 6 mesured joint positions (m, rad)
87  // - the 2 values of s - s*
88  std::string username;
89  // Get the user login name
90  vpIoTools::getUserName(username);
91 
92  // Create a log filename to save velocities...
93  std::string logdirname;
94  logdirname = "/tmp/" + username;
95 
96  // Test if the output path exist. If no try to create it
97  if (vpIoTools::checkDirectory(logdirname) == false) {
98  try {
99  // Create the dirname
100  vpIoTools::makeDirectory(logdirname);
101  } catch (...) {
102  std::cerr << std::endl << "ERROR:" << std::endl;
103  std::cerr << " Cannot create " << logdirname << std::endl;
104  exit(-1);
105  }
106  }
107  std::string logfilename;
108  logfilename = logdirname + "/log.dat";
109 
110  // Open the log file name
111  std::ofstream flog(logfilename.c_str());
112 
113  try {
114  vpRobotViper650 robot;
115 
116  vpServo task;
117 
119 
120  bool reset = false;
121  vp1394TwoGrabber g(reset);
122 
123 #if 1
125  g.setFramerate(vp1394TwoGrabber::vpFRAMERATE_60);
126 #else
128  g.setColorCoding(vp1394TwoGrabber::vpCOLOR_CODING_MONO8);
129 #endif
130  g.open(I);
131 
132  vpDisplayX display(I, 100, 100, "Current image");
134  vpDisplay::flush(I);
135 
136  vpDot2 dot;
137 
138  dot.setGraphics(true);
139 
140  for (int i = 0; i < 10; i++) // warm up the camera
141  g.acquire(I);
142 
143  std::cout << "Click on a dot..." << std::endl;
144  dot.initTracking(I);
145 
146  vpImagePoint cog = dot.getCog();
148  vpDisplay::flush(I);
149 
150  vpCameraParameters cam;
151  // Update camera parameters
152  robot.getCameraParameters(cam, I);
153 
154  // sets the current position of the visual feature
155  vpFeaturePoint p;
156  // retrieve x,y and Z of the vpPoint structure
157  vpFeatureBuilder::create(p, cam, dot);
158 
159  // sets the desired position of the visual feature
160  vpFeaturePoint pd;
161  pd.buildFrom(0, 0, 1);
162 
163  // define the task
164  // - we want an eye-in-hand control law
165  // - robot is controlled in the camera frame
167 
168  // - we want to see a point on a point
169  task.addFeature(p, pd);
170 
171  // - set the constant gain
172  task.setLambda(0.8);
173 
174  // Display task information
175  task.print();
176 
177  // Now the robot will be controlled in velocity
179 
180  std::cout << "\nHit CTRL-C or click in the image to stop the loop...\n" << std::flush;
181 
182  for (;;) {
183  // Acquire a new image from the camera
184  g.acquire(I);
185 
186  // Display this image
188 
189  try {
190  // Achieve the tracking of the dot in the image
191  dot.track(I);
192  } catch (...) {
193  std::cout << "Error detected while tracking visual features.." << std::endl;
194  break;
195  }
196 
197  // Get the dot cog
198  cog = dot.getCog();
199 
200  // Display a green cross at the center of gravity position in the image
202 
203  // Update the point feature from the dot location
204  vpFeatureBuilder::create(p, cam, dot);
205 
206  // Compute the visual servoing skew vector
207  vpColVector v = task.computeControlLaw();
208 
209  // Display the current and desired feature points in the image display
210  vpServoDisplay::display(task, cam, I);
211 
212  // Apply the computed camera velocities to the robot
214 
215  // Save velocities applied to the robot in the log file
216  // v[0], v[1], v[2] correspond to camera translation velocities in m/s
217  // v[3], v[4], v[5] correspond to camera rotation velocities in rad/s
218  flog << v[0] << " " << v[1] << " " << v[2] << " " << v[3] << " " << v[4] << " " << v[5] << " ";
219 
220  // Get the measured joint velocities of the robot
221  vpColVector qvel;
223  // Save measured joint velocities of the robot in the log file:
224  // - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
225  // velocities in m/s
226  // - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
227  // velocities in rad/s
228  flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " " << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
229 
230  // Get the measured joint positions of the robot
231  vpColVector q;
233  // Save measured joint positions of the robot in the log file
234  // - q[0], q[1], q[2] correspond to measured joint translation
235  // positions in m
236  // - q[3], q[4], q[5] correspond to measured joint rotation
237  // positions in rad
238  flog << q[0] << " " << q[1] << " " << q[2] << " " << q[3] << " " << q[4] << " " << q[5] << " ";
239 
240  // Save feature error (s-s*) for the feature point. For this feature
241  // point, we have 2 errors (along x and y axis). This error is
242  // expressed in meters in the camera frame
243  flog << (task.getError()).t() << std::endl; // s-s* for point
244 
245  vpDisplay::displayText(I, 10, 10, "Click to quit...", vpColor::red);
246  if (vpDisplay::getClick(I, false))
247  break;
248 
249  // Flush the display
250  vpDisplay::flush(I);
251  }
252 
253  robot.stopMotion();
254 
255  flog.close(); // Close the log file
256 
257  // Display task information
258  task.print();
259 
260  return EXIT_SUCCESS;
261  } catch (const vpException &e) {
262  flog.close(); // Close the log file
263  std::cout << "Catched an exception: " << e.getMessage() << std::endl;
264  return EXIT_FAILURE;
265  }
266 }
267 
268 #else
269 int main()
270 {
271  std::cout << "You do not have an Viper 650 robot connected to your computer..." << std::endl;
272  return EXIT_SUCCESS;
273 }
274 #endif
static void makeDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:482
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &velocity)
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
void buildFrom(double x, double y, double Z)
void getVelocity(const vpRobot::vpControlFrameType frame, vpColVector &velocity)
Control of Irisa&#39;s Viper S650 robot named Viper650.
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:490
void setGraphics(bool activate)
Definition: vpDot2.h:314
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:150
error that can be emited by ViSP classes.
Definition: vpException.h:71
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:220
This tracker is meant to track a blob (connex pixels with same gray level) on a vpImage.
Definition: vpDot2.h:126
void getCameraParameters(vpCameraParameters &cam, const unsigned int &image_width, const unsigned int &image_height) const
Definition: vpViper650.cpp:557
static void flush(const vpImage< unsigned char > &I)
static const vpColor red
Definition: vpColor.h:217
static bool checkDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:332
vpImagePoint getCog() const
Definition: vpDot2.h:180
vpRobot::vpRobotStateType setRobotState(vpRobot::vpRobotStateType newState)
Initialize the velocity controller.
Definition: vpRobot.h:66
vpColVector getError() const
Definition: vpServo.h:278
vpColVector computeControlLaw()
Definition: vpServo.cpp:929
static void display(const vpImage< unsigned char > &I)
Generic class defining intrinsic camera parameters.
void setLambda(double c)
Definition: vpServo.h:404
static std::string getUserName()
Definition: vpIoTools.cpp:228
void track(const vpImage< unsigned char > &I, bool canMakeTheWindowGrow=true)
Definition: vpDot2.cpp:441
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
Implementation of column vector and the associated operations.
Definition: vpColVector.h:130
void initTracking(const vpImage< unsigned char > &I, unsigned int size=0)
Definition: vpDot2.cpp:253
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:306
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:87
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
const char * getMessage() const
Definition: vpException.cpp:90
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:218
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)
static const vpColor blue
Definition: vpColor.h:223
void getPosition(const vpRobot::vpControlFrameType frame, vpColVector &position)