ViSP
Main Page
Related Pages
Modules
Classes
Examples
All
Classes
Functions
Variables
Enumerations
Enumerator
Friends
Groups
Pages
servoViper850FourPoints2DCamVelocityInteractionCurrent.cpp
1
/****************************************************************************
2
*
3
* $Id: servoViper850FourPoints2DCamVelocityInteractionCurrent.cpp 4065 2013-01-11 13:32:47Z 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 camera frame
38
*
39
* Authors:
40
* Fabien Spindler
41
*
42
*****************************************************************************/
56
#include <visp/vpConfig.h>
57
#include <visp/vpDebug.h>
// Debug trace
58
59
#include <stdio.h>
60
#include <iostream>
61
#include <fstream>
62
#include <sstream>
63
#include <stdlib.h>
64
#if (defined (VISP_HAVE_VIPER850) && defined (VISP_HAVE_DC1394_2))
65
66
#include <visp/vp1394TwoGrabber.h>
67
#include <visp/vpDisplay.h>
68
#include <visp/vpDisplayGTK.h>
69
#include <visp/vpDisplayX.h>
70
#include <visp/vpDisplayOpenCV.h>
71
#include <visp/vpDot2.h>
72
#include <visp/vpFeatureBuilder.h>
73
#include <visp/vpFeaturePoint.h>
74
#include <visp/vpHomogeneousMatrix.h>
75
#include <visp/vpImage.h>
76
#include <visp/vpIoTools.h>
77
#include <visp/vpMath.h>
78
#include <visp/vpPoint.h>
79
#include <visp/vpPose.h>
80
#include <visp/vpRobotViper850.h>
81
#include <visp/vpServo.h>
82
#include <visp/vpServoDisplay.h>
83
84
#define L 0.05 // to deal with a 10cm by 10cm square
85
86
112
void
compute_pose(
vpPoint
point[],
vpDot2
dot[],
int
ndot,
113
vpCameraParameters
cam,
114
vpHomogeneousMatrix
&cMo,
115
vpTranslationVector
&cto,
116
vpRxyzVector
&cro,
bool
init)
117
{
118
vpHomogeneousMatrix
cMo_dementhon;
// computed pose with dementhon
119
vpHomogeneousMatrix
cMo_lagrange;
// computed pose with dementhon
120
vpRotationMatrix
cRo;
121
vpPose
pose;
122
vpImagePoint
cog;
123
for
(
int
i=0; i < ndot; i ++) {
124
125
double
x=0, y=0;
126
cog = dot[i].
getCog
();
127
vpPixelMeterConversion::convertPoint
(cam, cog, x, y) ;
//pixel to meter conversion
128
point[i].
set_x
(x) ;
//projection perspective p
129
point[i].
set_y
(y) ;
130
pose.
addPoint
(point[i]) ;
131
}
132
133
if
(init ==
true
) {
134
pose.
computePose
(
vpPose::DEMENTHON
, cMo_dementhon) ;
135
// Compute and return the residual expressed in meter for the pose matrix
136
// 'cMo'
137
double
residual_dementhon = pose.
computeResidual
(cMo_dementhon);
138
pose.
computePose
(
vpPose::LAGRANGE
, cMo_lagrange) ;
139
double
residual_lagrange = pose.
computeResidual
(cMo_lagrange);
140
141
// Select the best pose to initialize the lowe pose computation
142
if
(residual_lagrange < residual_dementhon)
143
cMo = cMo_lagrange;
144
else
145
cMo = cMo_dementhon;
146
147
}
148
else
{
// init = false; use of the previous pose to initialise LOWE
149
cRo.
buildFrom
(cro);
150
cMo.
buildFrom
(cto, cRo);
151
}
152
pose.
computePose
(
vpPose::LOWE
, cMo) ;
153
cMo.
extract
(cto);
154
cMo.
extract
(cRo);
155
cro.
buildFrom
(cRo);
156
}
157
158
int
159
main()
160
{
161
// Log file creation in /tmp/$USERNAME/log.dat
162
// This file contains by line:
163
// - the 6 computed joint velocities (m/s, rad/s) to achieve the task
164
// - the 6 mesured joint velocities (m/s, rad/s)
165
// - the 6 mesured joint positions (m, rad)
166
// - the 8 values of s - s*
167
std::string username;
168
// Get the user login name
169
vpIoTools::getUserName
(username);
170
171
// Create a log filename to save velocities...
172
std::string logdirname;
173
logdirname =
"/tmp/"
+ username;
174
175
// Test if the output path exist. If no try to create it
176
if
(
vpIoTools::checkDirectory
(logdirname) ==
false
) {
177
try
{
178
// Create the dirname
179
vpIoTools::makeDirectory
(logdirname);
180
}
181
catch
(...) {
182
std::cerr << std::endl
183
<<
"ERROR:"
<< std::endl;
184
std::cerr <<
" Cannot create "
<< logdirname << std::endl;
185
return
(-1);
186
}
187
}
188
std::string logfilename;
189
logfilename = logdirname +
"/log.dat"
;
190
191
// Open the log file name
192
std::ofstream flog(logfilename.c_str());
193
194
try
{
195
vpRobotViper850
robot ;
196
// Load the end-effector to camera frame transformation obtained
197
// using a camera intrinsic model with distortion
198
vpCameraParameters::vpCameraParametersProjType
projModel =
199
vpCameraParameters::perspectiveProjWithDistortion
;
200
robot.
init
(
vpRobotViper850::TOOL_PTGREY_FLEA2_CAMERA
, projModel);
201
202
vpServo
task ;
203
204
vpImage<unsigned char>
I ;
205
int
i ;
206
207
bool
reset =
false
;
208
vp1394TwoGrabber
g(reset);
209
g.setVideoMode(
vp1394TwoGrabber::vpVIDEO_MODE_640x480_MONO8
);
210
g.setFramerate(
vp1394TwoGrabber::vpFRAMERATE_60
);
211
g.open(I) ;
212
213
g.acquire(I) ;
214
215
#ifdef VISP_HAVE_X11
216
vpDisplayX
display(I,100,100,
"Current image"
) ;
217
#elif defined(VISP_HAVE_OPENCV)
218
vpDisplayOpenCV
display(I,100,100,
"Current image"
) ;
219
#elif defined(VISP_HAVE_GTK)
220
vpDisplayGTK
display(I,100,100,
"Current image"
) ;
221
#endif
222
223
vpDisplay::display
(I) ;
224
vpDisplay::flush
(I) ;
225
226
std::cout << std::endl ;
227
std::cout <<
"-------------------------------------------------------"
<< std::endl ;
228
std::cout <<
" Test program for vpServo "
<<std::endl ;
229
std::cout <<
" Eye-in-hand task control, velocity computed in the camera space"
<< std::endl ;
230
std::cout <<
" Use of the Viper850 robot "
<< std::endl ;
231
std::cout <<
" task : servo 4 points on a square with dimention "
<< L <<
" meters"
<< std::endl ;
232
std::cout <<
"-------------------------------------------------------"
<< std::endl ;
233
std::cout << std::endl ;
234
235
236
vpDot2
dot[4] ;
237
vpImagePoint
cog;
238
239
std::cout <<
"Click on the 4 dots clockwise starting from upper/left dot..."
240
<< std::endl;
241
242
for
(i=0 ; i < 4 ; i++) {
243
dot[i].
setGraphics
(
true
) ;
244
dot[i].
initTracking
(I) ;
245
cog = dot[i].
getCog
();
246
vpDisplay::displayCross
(I, cog, 10,
vpColor::blue
) ;
247
vpDisplay::flush
(I);
248
}
249
250
vpCameraParameters
cam ;
251
252
// Update camera parameters
253
robot.
getCameraParameters
(cam, I);
254
255
cam.
printParameters
();
256
257
258
// Sets the current position of the visual feature
259
vpFeaturePoint
p[4] ;
260
for
(i=0 ; i < 4 ; i++)
261
vpFeatureBuilder::create
(p[i], cam, dot[i]);
//retrieve x,y of the vpFeaturePoint structure
262
263
// Set the position of the square target in a frame which origin is
264
// centered in the middle of the square
265
vpPoint
point[4] ;
266
point[0].
setWorldCoordinates
(-L, -L, 0) ;
267
point[1].
setWorldCoordinates
( L, -L, 0) ;
268
point[2].
setWorldCoordinates
( L, L, 0) ;
269
point[3].
setWorldCoordinates
(-L, L, 0) ;
270
271
// Initialise a desired pose to compute s*, the desired 2D point features
272
vpHomogeneousMatrix
cMo;
273
vpTranslationVector
cto(0, 0, 0.5);
// tz = 0.5 meter
274
vpRxyzVector
cro(
vpMath::rad
(10),
vpMath::rad
(30),
vpMath::rad
(20));
275
vpRotationMatrix
cRo(cro);
// Build the rotation matrix
276
cMo.
buildFrom
(cto, cRo);
// Build the homogeneous matrix
277
278
// Sets the desired position of the 2D visual feature
279
vpFeaturePoint
pd[4] ;
280
// Compute the desired position of the features from the desired pose
281
for
(
int
i=0; i < 4; i ++) {
282
vpColVector
cP, p ;
283
point[i].
changeFrame
(cMo, cP) ;
284
point[i].
projection
(cP, p) ;
285
286
pd[i].
set_x
(p[0]) ;
287
pd[i].
set_y
(p[1]) ;
288
pd[i].
set_Z
(cP[2]);
289
}
290
291
// We want to see a point on a point
292
for
(i=0 ; i < 4 ; i++)
293
task.
addFeature
(p[i],pd[i]) ;
294
295
// Set the proportional gain
296
task.
setLambda
(0.3) ;
297
298
// Display task information
299
task.
print
() ;
300
301
// Define the task
302
// - we want an eye-in-hand control law
303
// - articular velocity are computed
304
task.
setServo
(
vpServo::EYEINHAND_CAMERA
) ;
305
task.
setInteractionMatrixType
(
vpServo::CURRENT
,
vpServo::PSEUDO_INVERSE
) ;
306
task.
print
() ;
307
308
309
// Initialise the velocity control of the robot
310
robot.
setRobotState
(
vpRobot::STATE_VELOCITY_CONTROL
) ;
311
312
std::cout <<
"\nHit CTRL-C to stop the loop...\n"
<< std::flush;
313
for
( ; ; ) {
314
// Acquire a new image from the camera
315
g.acquire(I) ;
316
317
// Display this image
318
vpDisplay::display
(I) ;
319
320
try
{
321
// For each point...
322
for
(i=0 ; i < 4 ; i++) {
323
// Achieve the tracking of the dot in the image
324
dot[i].
track
(I) ;
325
// Display a green cross at the center of gravity position in the
326
// image
327
cog = dot[i].
getCog
();
328
vpDisplay::displayCross
(I, cog, 10,
vpColor::green
) ;
329
}
330
}
331
catch
(...) {
332
flog.close() ;
// Close the log file
333
vpTRACE
(
"Error detected while tracking visual features"
) ;
334
robot.
stopMotion
() ;
335
return
(1) ;
336
}
337
338
// During the servo, we compute the pose using LOWE method. For the
339
// initial pose used in the non linear minimisation we use the pose
340
// computed at the previous iteration.
341
compute_pose(point, dot, 4, cam, cMo, cto, cro,
false
);
342
343
for
(i=0 ; i < 4 ; i++) {
344
// Update the point feature from the dot location
345
vpFeatureBuilder::create
(p[i], cam, dot[i]);
346
// Set the feature Z coordinate from the pose
347
vpColVector
cP;
348
point[i].
changeFrame
(cMo, cP) ;
349
350
p[i].set_Z(cP[2]);
351
}
352
353
vpColVector
v ;
354
// Compute the visual servoing skew vector
355
v = task.
computeControlLaw
() ;
356
357
// Display the current and desired feature points in the image display
358
vpServoDisplay::display
(task,cam,I) ;
359
360
// Apply the computed joint velocities to the robot
361
robot.
setVelocity
(
vpRobot::CAMERA_FRAME
, v) ;
362
363
// Save velocities applied to the robot in the log file
364
// v[0], v[1], v[2] correspond to joint translation velocities in m/s
365
// v[3], v[4], v[5] correspond to joint rotation velocities in rad/s
366
flog << v[0] <<
" "
<< v[1] <<
" "
<< v[2] <<
" "
367
<< v[3] <<
" "
<< v[4] <<
" "
<< v[5] <<
" "
;
368
369
// Get the measured joint velocities of the robot
370
vpColVector
qvel;
371
robot.
getVelocity
(
vpRobot::ARTICULAR_FRAME
, qvel);
372
// Save measured joint velocities of the robot in the log file:
373
// - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
374
// velocities in m/s
375
// - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
376
// velocities in rad/s
377
flog << qvel[0] <<
" "
<< qvel[1] <<
" "
<< qvel[2] <<
" "
378
<< qvel[3] <<
" "
<< qvel[4] <<
" "
<< qvel[5] <<
" "
;
379
380
// Get the measured joint positions of the robot
381
vpColVector
q;
382
robot.
getPosition
(
vpRobot::ARTICULAR_FRAME
, q);
383
// Save measured joint positions of the robot in the log file
384
// - q[0], q[1], q[2] correspond to measured joint translation
385
// positions in m
386
// - q[3], q[4], q[5] correspond to measured joint rotation
387
// positions in rad
388
flog << q[0] <<
" "
<< q[1] <<
" "
<< q[2] <<
" "
389
<< q[3] <<
" "
<< q[4] <<
" "
<< q[5] <<
" "
;
390
391
// Save feature error (s-s*) for the 4 feature points. For each feature
392
// point, we have 2 errors (along x and y axis). This error is expressed
393
// in meters in the camera frame
394
flog << task.
getError
() << std::endl;
395
396
// Flush the display
397
vpDisplay::flush
(I) ;
398
399
// std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() << std::endl;
400
}
401
402
std::cout <<
"Display task information: "
<< std::endl;
403
task.
print
() ;
404
task.
kill
();
405
flog.close() ;
// Close the log file
406
return
0;
407
}
408
catch
(...)
409
{
410
flog.close() ;
// Close the log file
411
vpERROR_TRACE
(
" Test failed"
) ;
412
return
0;
413
}
414
}
415
416
#else
417
int
418
main()
419
{
420
vpERROR_TRACE
(
"You do not have an afma6 robot or a firewire framegrabber connected to your computer..."
);
421
}
422
423
#endif
example
servo-viper850
servoViper850FourPoints2DCamVelocityInteractionCurrent.cpp
Generated on Thu Oct 24 2013 14:47:35 for ViSP by
1.8.4