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
42 /*!
43 \example servoViper850Point2DCamVelocity.cpp
44
45 Example of eye-in-hand control law. We control here a real robot, the
46 ADEPT Viper 850 robot (arm, with 6 degrees of freedom). The velocity is
47 computed in the camera frame. The visual feature is the center of gravity of
48 a point.
49
50 */
51
52 #include <visp3/core/vpConfig.h>
53 #include <visp3/core/vpDebug.h> // Debug trace
54
55 #include <fstream>
56 #include <iostream>
57 #include <sstream>
58 #include <stdio.h>
59 #include <stdlib.h>
60
61 #if (defined(VISP_HAVE_VIPER850) && defined(VISP_HAVE_DC1394))
62
63 #include <visp3/blob/vpDot2.h>
64 #include <visp3/core/vpDisplay.h>
65 #include <visp3/core/vpException.h>
66 #include <visp3/core/vpHomogeneousMatrix.h>
67 #include <visp3/core/vpImage.h>
68 #include <visp3/core/vpIoTools.h>
69 #include <visp3/core/vpMath.h>
70 #include <visp3/core/vpPoint.h>
71 #include <visp3/gui/vpDisplayGTK.h>
72 #include <visp3/gui/vpDisplayOpenCV.h>
73 #include <visp3/gui/vpDisplayX.h>
74 #include <visp3/io/vpImageIo.h>
75 #include <visp3/robot/vpRobotViper850.h>
76 #include <visp3/sensor/vp1394TwoGrabber.h>
77 #include <visp3/visual_features/vpFeatureBuilder.h>
78 #include <visp3/visual_features/vpFeaturePoint.h>
79 #include <visp3/vs/vpServo.h>
80 #include <visp3/vs/vpServoDisplay.h>
81
main()82 int main()
83 {
84 // Log file creation in /tmp/$USERNAME/log.dat
85 // This file contains by line:
86 // - the 6 computed joint velocities (m/s, rad/s) to achieve the task
87 // - the 6 mesured joint velocities (m/s, rad/s)
88 // - the 6 mesured joint positions (m, rad)
89 // - the 2 values of s - s*
90 std::string username;
91 // Get the user login name
92 vpIoTools::getUserName(username);
93
94 // Create a log filename to save velocities...
95 std::string logdirname;
96 logdirname = "/tmp/" + username;
97
98 // Test if the output path exist. If no try to create it
99 if (vpIoTools::checkDirectory(logdirname) == false) {
100 try {
101 // Create the dirname
102 vpIoTools::makeDirectory(logdirname);
103 } catch (...) {
104 std::cerr << std::endl << "ERROR:" << std::endl;
105 std::cerr << " Cannot create " << logdirname << std::endl;
106 exit(-1);
107 }
108 }
109 std::string logfilename;
110 logfilename = logdirname + "/log.dat";
111
112 // Open the log file name
113 std::ofstream flog(logfilename.c_str());
114
115 try {
116 vpRobotViper850 robot;
117
118 vpServo task;
119
120 vpImage<unsigned char> I;
121
122 bool reset = false;
123 vp1394TwoGrabber g(reset);
124
125 #if 1
126 g.setVideoMode(vp1394TwoGrabber::vpVIDEO_MODE_640x480_MONO8);
127 g.setFramerate(vp1394TwoGrabber::vpFRAMERATE_60);
128 #else
129 g.setVideoMode(vp1394TwoGrabber::vpVIDEO_MODE_FORMAT7_0);
130 g.setColorCoding(vp1394TwoGrabber::vpCOLOR_CODING_MONO8);
131 #endif
132 g.open(I);
133
134 #ifdef VISP_HAVE_X11
135 vpDisplayX display(I, (int)(100 + I.getWidth() + 30), 200, "Current image");
136 #elif defined(VISP_HAVE_OPENCV)
137 vpDisplayOpenCV display(I, (int)(100 + I.getWidth() + 30), 200, "Current image");
138 #elif defined(VISP_HAVE_GTK)
139 vpDisplayGTK display(I, (int)(100 + I.getWidth() + 30), 200, "Current image");
140 #endif
141
142 vpDisplay::display(I);
143 vpDisplay::flush(I);
144
145 vpDot2 dot;
146 vpImagePoint cog;
147
148 dot.setGraphics(true);
149
150 for (int i = 0; i < 10; i++)
151 g.acquire(I);
152
153 std::cout << "Click on a dot..." << std::endl;
154 dot.initTracking(I);
155
156 cog = dot.getCog();
157 vpDisplay::displayCross(I, cog, 10, vpColor::blue);
158 vpDisplay::flush(I);
159
160 vpCameraParameters cam;
161 // Update camera parameters
162 robot.getCameraParameters(cam, I);
163
164 // sets the current position of the visual feature
165 vpFeaturePoint p;
166 // retrieve x,y and Z of the vpPoint structure
167 vpFeatureBuilder::create(p, cam, dot);
168
169 // sets the desired position of the visual feature
170 vpFeaturePoint pd;
171 pd.buildFrom(0, 0, 1);
172
173 // define the task
174 // - we want an eye-in-hand control law
175 // - robot is controlled in the camera frame
176 task.setServo(vpServo::EYEINHAND_CAMERA);
177
178 // - we want to see a point on a point
179 task.addFeature(p, pd);
180
181 // - set the constant gain
182 task.setLambda(0.8);
183
184 // Display task information
185 task.print();
186
187 // Now the robot will be controlled in velocity
188 robot.setRobotState(vpRobot::STATE_VELOCITY_CONTROL);
189
190 std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
191 vpColVector v;
192 for (;;) {
193 try {
194 // Acquire a new image from the camera
195 g.acquire(I);
196
197 // Display this image
198 vpDisplay::display(I);
199
200 // Achieve the tracking of the dot in the image
201 dot.track(I);
202
203 // Get the dot cog
204 cog = dot.getCog();
205
206 // Display a green cross at the center of gravity position in the
207 // image
208 vpDisplay::displayCross(I, cog, 10, vpColor::green);
209
210 // Update the point feature from the dot location
211 vpFeatureBuilder::create(p, cam, dot);
212
213 // Compute the visual servoing skew vector
214 v = task.computeControlLaw();
215
216 // Display the current and desired feature points in the image display
217 vpServoDisplay::display(task, cam, I);
218
219 // Apply the computed camera velocities to the robot
220 robot.setVelocity(vpRobot::CAMERA_FRAME, v);
221 } catch (...) {
222 std::cout << "Tracking failed... Stop the robot." << std::endl;
223 v = 0;
224 // Stop robot
225 robot.setVelocity(vpRobot::CAMERA_FRAME, v);
226 return 0;
227 }
228
229 // Save velocities applied to the robot in the log file
230 // v[0], v[1], v[2] correspond to camera translation velocities in m/s
231 // v[3], v[4], v[5] correspond to camera rotation velocities in rad/s
232 flog << v[0] << " " << v[1] << " " << v[2] << " " << v[3] << " " << v[4] << " " << v[5] << " ";
233
234 // Get the measured joint velocities of the robot
235 vpColVector qvel;
236 robot.getVelocity(vpRobot::ARTICULAR_FRAME, qvel);
237 // Save measured joint velocities of the robot in the log file:
238 // - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
239 // velocities in m/s
240 // - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
241 // velocities in rad/s
242 flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " " << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
243
244 // Get the measured joint positions of the robot
245 vpColVector q;
246 robot.getPosition(vpRobot::ARTICULAR_FRAME, q);
247 // Save measured joint positions of the robot in the log file
248 // - q[0], q[1], q[2] correspond to measured joint translation
249 // positions in m
250 // - q[3], q[4], q[5] correspond to measured joint rotation
251 // positions in rad
252 flog << q[0] << " " << q[1] << " " << q[2] << " " << q[3] << " " << q[4] << " " << q[5] << " ";
253
254 // Save feature error (s-s*) for the feature point. For this feature
255 // point, we have 2 errors (along x and y axis). This error is
256 // expressed in meters in the camera frame
257 flog << (task.getError()).t() << std::endl; // s-s* for point
258
259 // Flush the display
260 vpDisplay::flush(I);
261 }
262
263 flog.close(); // Close the log file
264
265 // Display task information
266 task.print();
267
268 return EXIT_SUCCESS;
269 }
270 catch (const vpException &e) {
271 flog.close(); // Close the log file
272 std::cout << "Catch an exception: " << e.getMessage() << std::endl;
273 return EXIT_FAILURE;
274 }
275 }
276
277 #else
main()278 int main()
279 {
280 std::cout << "You do not have an Viper 850 robot connected to your computer..." << std::endl;
281 return EXIT_SUCCESS;
282 }
283 #endif
284