ViSP
Main Page
Related Pages
Modules
Classes
Examples
All
Classes
Functions
Variables
Enumerations
Enumerator
Friends
Groups
Pages
servoAfma6FourPoints2DArtVelocity.cpp
1
/****************************************************************************
2
*
3
* $Id: servoAfma6FourPoints2DArtVelocity.cpp 4056 2013-01-05 13:04:42Z fspindle $
4
*
5
* This file is part of the ViSP software.
6
* Copyright (C) 2005 - 2013 by INRIA. All rights reserved.
7
*
8
* This software is free software; you can redistribute it and/or
9
* modify it under the terms of the GNU General Public License
10
* ("GPL") version 2 as published by the Free Software Foundation.
11
* See the file LICENSE.txt at the root directory of this source
12
* distribution for additional information about the GNU GPL.
13
*
14
* For using ViSP with software that can not be combined with the GNU
15
* GPL, please contact INRIA about acquiring a ViSP Professional
16
* Edition License.
17
*
18
* See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19
*
20
* This software was developed at:
21
* INRIA Rennes - Bretagne Atlantique
22
* Campus Universitaire de Beaulieu
23
* 35042 Rennes Cedex
24
* France
25
* http://www.irisa.fr/lagadic
26
*
27
* If you have questions regarding the use of this file, please contact
28
* INRIA at visp@inria.fr
29
*
30
* This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31
* WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32
*
33
*
34
* Description:
35
* tests the control law
36
* eye-in-hand control
37
* velocity computed in the articular frame
38
*
39
* Authors:
40
* Eric Marchand
41
* Fabien Spindler
42
*
43
*****************************************************************************/
66
#include <visp/vpConfig.h>
67
#include <visp/vpDebug.h>
// Debug trace
68
#include <stdio.h>
69
#include <iostream>
70
#include <fstream>
71
#include <sstream>
72
#include <stdlib.h>
73
#if (defined (VISP_HAVE_AFMA6) && defined (VISP_HAVE_DC1394_2))
74
75
#include <visp/vp1394TwoGrabber.h>
76
#include <visp/vpImage.h>
77
#include <visp/vpImagePoint.h>
78
#include <visp/vpDisplay.h>
79
#include <visp/vpDisplayX.h>
80
#include <visp/vpDisplayOpenCV.h>
81
#include <visp/vpDisplayGTK.h>
82
83
#include <visp/vpMath.h>
84
#include <visp/vpHomogeneousMatrix.h>
85
#include <visp/vpFeaturePoint.h>
86
#include <visp/vpPoint.h>
87
#include <visp/vpServo.h>
88
#include <visp/vpFeatureBuilder.h>
89
#include <visp/vpIoTools.h>
90
#include <visp/vpRobotAfma6.h>
91
92
// Exception
93
#include <visp/vpException.h>
94
#include <visp/vpMatrixException.h>
95
#include <visp/vpServoDisplay.h>
96
97
#include <visp/vpDot.h>
98
99
int
100
main()
101
{
102
// Log file creation in /tmp/$USERNAME/log.dat
103
// This file contains by line:
104
// - the 6 computed joint velocities (m/s, rad/s) to achieve the task
105
// - the 6 mesured joint velocities (m/s, rad/s)
106
// - the 6 mesured joint positions (m, rad)
107
// - the 8 values of s - s*
108
std::string username;
109
// Get the user login name
110
vpIoTools::getUserName
(username);
111
112
// Create a log filename to save velocities...
113
std::string logdirname;
114
logdirname =
"/tmp/"
+ username;
115
116
// Test if the output path exist. If no try to create it
117
if
(
vpIoTools::checkDirectory
(logdirname) ==
false
) {
118
try
{
119
// Create the dirname
120
vpIoTools::makeDirectory
(logdirname);
121
}
122
catch
(...) {
123
std::cerr << std::endl
124
<<
"ERROR:"
<< std::endl;
125
std::cerr <<
" Cannot create "
<< logdirname << std::endl;
126
exit(-1);
127
}
128
}
129
std::string logfilename;
130
logfilename = logdirname +
"/log.dat"
;
131
132
// Open the log file name
133
std::ofstream flog(logfilename.c_str());
134
135
try
{
136
// Define the square CAD model
137
// Square dimention
138
#define L 0.075
139
// Distance between the camera and the square at the desired
140
// position after visual servoing convergence
141
#define D 0.5
142
143
vpServo
task ;
144
145
vpImage<unsigned char>
I ;
146
int
i ;
147
148
vp1394TwoGrabber
g;
149
g.
setVideoMode
(
vp1394TwoGrabber::vpVIDEO_MODE_640x480_MONO8
);
150
g.
setFramerate
(
vp1394TwoGrabber::vpFRAMERATE_60
);
151
g.
open
(I) ;
152
153
g.
acquire
(I) ;
154
155
#ifdef VISP_HAVE_X11
156
vpDisplayX
display(I,100,100,
"Current image"
) ;
157
#elif defined(VISP_HAVE_OPENCV)
158
vpDisplayOpenCV
display(I,100,100,
"Current image"
) ;
159
#elif defined(VISP_HAVE_GTK)
160
vpDisplayGTK
display(I,100,100,
"Current image"
) ;
161
#endif
162
163
vpDisplay::display
(I) ;
164
vpDisplay::flush
(I) ;
165
166
std::cout << std::endl ;
167
std::cout <<
"-------------------------------------------------------"
<< std::endl ;
168
std::cout <<
" Test program for vpServo "
<<std::endl ;
169
std::cout <<
" Eye-in-hand task control, velocity computed in the joint space"
<< std::endl ;
170
std::cout <<
" Use of the Afma6 robot "
<< std::endl ;
171
std::cout <<
" task : servo 4 points on a square with dimention "
<< L <<
" meters"
<< std::endl ;
172
std::cout <<
"-------------------------------------------------------"
<< std::endl ;
173
std::cout << std::endl ;
174
175
176
vpDot
dot[4] ;
177
vpImagePoint
cog;
178
179
std::cout <<
"Click on the 4 dots clockwise starting from upper/left dot..."
180
<< std::endl;
181
182
for
(i=0 ; i < 4 ; i++) {
183
dot[i].
initTracking
(I) ;
184
cog = dot[i].
getCog
();
185
vpDisplay::displayCross
(I, cog, 10,
vpColor::blue
) ;
186
vpDisplay::flush
(I);
187
}
188
189
vpRobotAfma6
robot ;
190
191
vpCameraParameters
cam ;
192
193
// Update camera parameters
194
robot.
getCameraParameters
(cam, I);
195
196
// Sets the current position of the visual feature
197
vpFeaturePoint
p[4] ;
198
for
(i=0 ; i < 4 ; i++)
199
vpFeatureBuilder::create
(p[i],cam, dot[i]) ;
//retrieve x,y and Z of the vpPoint structure
200
201
// sets the desired position of the visual feature
202
vpFeaturePoint
pd[4] ;
203
204
pd[0].
buildFrom
(-L,-L,D) ;
205
pd[1].
buildFrom
(L,-L,D) ;
206
pd[2].
buildFrom
(L,L,D) ;
207
pd[3].
buildFrom
(-L,L,D) ;
208
209
// We want to see a point on a point
210
std::cout << std::endl ;
211
for
(i=0 ; i < 4 ; i++)
212
task.
addFeature
(p[i],pd[i]) ;
213
214
// Set the proportional gain
215
task.
setLambda
(0.2) ;
216
217
// Display task information
218
task.
print
() ;
219
220
// Define the task
221
// - we want an eye-in-hand control law
222
// - articular velocity are computed
223
task.
setServo
(
vpServo::EYEINHAND_L_cVe_eJe
) ;
224
task.
setInteractionMatrixType
(
vpServo::DESIRED
,
vpServo::PSEUDO_INVERSE
) ;
225
task.
print
() ;
226
227
vpVelocityTwistMatrix
cVe ;
228
robot.
get_cVe
(cVe) ;
229
task.
set_cVe
(cVe) ;
230
task.
print
() ;
231
232
// Set the Jacobian (expressed in the end-effector frame)
233
vpMatrix
eJe ;
234
robot.
get_eJe
(eJe) ;
235
task.
set_eJe
(eJe) ;
236
task.
print
() ;
237
238
// Initialise the velocity control of the robot
239
robot.
setRobotState
(
vpRobot::STATE_VELOCITY_CONTROL
) ;
240
241
std::cout <<
"\nHit CTRL-C to stop the loop...\n"
<< std::flush;
242
for
( ; ; ) {
243
// Acquire a new image from the camera
244
g.
acquire
(I) ;
245
246
// Display this image
247
vpDisplay::display
(I) ;
248
249
try
{
250
// For each point...
251
for
(i=0 ; i < 4 ; i++) {
252
// Achieve the tracking of the dot in the image
253
dot[i].
track
(I) ;
254
// Get the dot cog
255
cog = dot[i].
getCog
();
256
// Display a green cross at the center of gravity position in the
257
// image
258
vpDisplay::displayCross
(I, cog, 10,
vpColor::green
) ;
259
}
260
}
261
catch
(...) {
262
flog.close() ;
// Close the log file
263
vpTRACE
(
"Error detected while tracking visual features"
) ;
264
robot.
stopMotion
() ;
265
exit(1) ;
266
}
267
268
// Update the point feature from the dot location
269
for
(i=0 ; i < 4 ; i++)
270
vpFeatureBuilder::create
(p[i],cam, dot[i]);
271
272
// Get the jacobian of the robot
273
robot.
get_eJe
(eJe) ;
274
// Update this jacobian in the task structure. It will be used to compute
275
// the velocity skew (as an articular velocity)
276
// qdot = -lambda * L^+ * cVe * eJe * (s-s*)
277
task.
set_eJe
(eJe) ;
278
279
vpColVector
v ;
280
// Compute the visual servoing skew vector
281
v = task.
computeControlLaw
() ;
282
283
// Display the current and desired feature points in the image display
284
vpServoDisplay::display
(task,cam,I) ;
285
286
// Apply the computed joint velocities to the robot
287
robot.
setVelocity
(
vpRobot::ARTICULAR_FRAME
, v) ;
288
289
// Save velocities applied to the robot in the log file
290
// v[0], v[1], v[2] correspond to joint translation velocities in m/s
291
// v[3], v[4], v[5] correspond to joint rotation velocities in rad/s
292
flog << v[0] <<
" "
<< v[1] <<
" "
<< v[2] <<
" "
293
<< v[3] <<
" "
<< v[4] <<
" "
<< v[5] <<
" "
;
294
295
// Get the measured joint velocities of the robot
296
vpColVector
qvel;
297
robot.
getVelocity
(
vpRobot::ARTICULAR_FRAME
, qvel);
298
// Save measured joint velocities of the robot in the log file:
299
// - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
300
// velocities in m/s
301
// - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
302
// velocities in rad/s
303
flog << qvel[0] <<
" "
<< qvel[1] <<
" "
<< qvel[2] <<
" "
304
<< qvel[3] <<
" "
<< qvel[4] <<
" "
<< qvel[5] <<
" "
;
305
306
// Get the measured joint positions of the robot
307
vpColVector
q;
308
robot.
getPosition
(
vpRobot::ARTICULAR_FRAME
, q);
309
// Save measured joint positions of the robot in the log file
310
// - q[0], q[1], q[2] correspond to measured joint translation
311
// positions in m
312
// - q[3], q[4], q[5] correspond to measured joint rotation
313
// positions in rad
314
flog << q[0] <<
" "
<< q[1] <<
" "
<< q[2] <<
" "
315
<< q[3] <<
" "
<< q[4] <<
" "
<< q[5] <<
" "
;
316
317
// Save feature error (s-s*) for the 4 feature points. For each feature
318
// point, we have 2 errors (along x and y axis). This error is expressed
319
// in meters in the camera frame
320
flog << ( task.
getError
() ).t() << std::endl;
321
322
// Flush the display
323
vpDisplay::flush
(I) ;
324
325
// vpTRACE("\t\t || s - s* || = %f ", ( task.getError() ).sumSquare()) ;
326
}
327
328
vpTRACE
(
"Display task information "
) ;
329
task.
print
() ;
330
task.
kill
();
331
flog.close() ;
// Close the log file
332
return
0;
333
}
334
catch
(...)
335
{
336
flog.close() ;
// Close the log file
337
vpERROR_TRACE
(
" Test failed"
) ;
338
return
0;
339
}
340
}
341
342
#else
343
int
344
main()
345
{
346
vpERROR_TRACE
(
"You do not have an afma6 robot or a firewire framegrabber connected to your computer..."
);
347
348
}
349
350
#endif
example
servo-afma6
servoAfma6FourPoints2DArtVelocity.cpp
Generated on Thu Nov 7 2013 03:14:00 for ViSP by
1.8.4