Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
calibrateTsai.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Tsai calibration example to estimate hand to eye transformation.
32  *
33  * Authors:
34  * Fabien Spindler
35  *
36  *****************************************************************************/
37 
38 
44 #include <stdio.h>
45 #include <sstream>
46 #include <iomanip>
47 #include <vector>
48 
49 #include <visp3/core/vpDebug.h>
50 #include <visp3/io/vpParseArgv.h>
51 #include <visp3/core/vpIoTools.h>
52 #include <visp3/vision/vpCalibration.h>
53 #include <visp3/core/vpExponentialMap.h>
54 
55 int main()
56 {
57  try {
58  // We want to calibrate the hand to eye extrinsic camera parameters from 6 couple of poses: cMo and wMe
59  const unsigned int N = 6;
60  // Input: six couple of poses used as input in the calibration proces
61  std::vector<vpHomogeneousMatrix> cMo(N) ; // eye (camera) to object transformation. The object frame is attached to the calibrartion grid
62  std::vector<vpHomogeneousMatrix> wMe(N) ; // world to hand (end-effector) transformation
63  // Output: Result of the calibration
64  vpHomogeneousMatrix eMc; // hand (end-effector) to eye (camera) transformation
65 
66  // Initialize an eMc transformation used to produce the simulated input transformations cMo and wMe
67  vpTranslationVector etc(0.1, 0.2, 0.3);
68  vpThetaUVector erc;
69  erc[0] = vpMath::rad(10); // 10 deg
70  erc[1] = vpMath::rad(-10); // -10 deg
71  erc[2] = vpMath::rad(25); // 25 deg
72 
73  eMc.buildFrom(etc, erc);
74  std::cout << "Simulated hand to eye transformation: eMc " << std::endl ;
75  std::cout << eMc << std::endl ;
76  std::cout << "Theta U rotation: " << vpMath::deg(erc[0]) << " " << vpMath::deg(erc[1]) << " " << vpMath::deg(erc[2]) << std::endl;
77 
78  vpColVector v_c(6) ; // camera velocity used to produce 6 simulated poses
79  for (unsigned int i=0 ; i < N ; i++)
80  {
81  v_c = 0 ;
82  if (i==0) {
83  // Initialize first poses
84  cMo[0].buildFrom(0, 0, 0.5, 0, 0, 0); // z=0.5 m
85  wMe[0].buildFrom(0, 0, 0, 0, 0, 0); // Id
86  }
87  else if (i==1)
88  v_c[3] = M_PI/8 ;
89  else if (i==2)
90  v_c[4] = M_PI/8 ;
91  else if (i==3)
92  v_c[5] = M_PI/10 ;
93  else if (i==4)
94  v_c[0] = 0.5 ;
95  else if (i==5)
96  v_c[1] = 0.8 ;
97 
98  vpHomogeneousMatrix cMc; // camera displacement
99  cMc = vpExponentialMap::direct(v_c) ; // Compute the camera displacement due to the velocity applied to the camera
100  if (i > 0) {
101  // From the camera displacement cMc, compute the wMe and cMo matrices
102  cMo[i] = cMc.inverse() * cMo[i-1];
103  wMe[i] = wMe[i-1] * eMc * cMc * eMc.inverse();
104  }
105  }
106 
107  if (0) {
108  for (unsigned int i=0 ; i < N ; i++) {
110  wMo = wMe[i] * eMc * cMo[i];
111  std::cout << std::endl << "wMo[" << i << "] " << std::endl ;
112  std::cout << wMo << std::endl ;
113  std::cout << "cMo[" << i << "] " << std::endl ;
114  std::cout << cMo[i] << std::endl ;
115  std::cout << "wMe[" << i << "] " << std::endl ;
116  std::cout << wMe[i] << std::endl ;
117  }
118  }
119 
120  // Reset the eMc matrix to eye
121  eMc.eye();
122 
123  // Compute the eMc hand to eye transformation from six poses
124  // - cMo[6]: camera to object poses as six homogeneous transformations
125  // - wMe[6]: world to hand (end-effector) poses as six homogeneous transformations
126  vpCalibration::calibrationTsai(cMo, wMe, eMc) ;
127 
128  std::cout << std::endl << "Output: hand to eye calibration result: eMc estimated " << std::endl ;
129  std::cout << eMc << std::endl ;
130  eMc.extract(erc);
131  std::cout << "Theta U rotation: " << vpMath::deg(erc[0]) << " " << vpMath::deg(erc[1]) << " " << vpMath::deg(erc[2]) << std::endl;
132  return 0 ;
133  }
134  catch(vpException &e) {
135  std::cout << "Catch an exception: " << e << std::endl;
136  return 1 ;
137  }
138 }
139 
Implementation of an homogeneous matrix and operations on such kind of matrices.
error that can be emited by ViSP classes.
Definition: vpException.h:73
void extract(vpRotationMatrix &R) const
void buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
static double rad(double deg)
Definition: vpMath.h:104
static void calibrationTsai(std::vector< vpHomogeneousMatrix > &cMo, std::vector< vpHomogeneousMatrix > &rMe, vpHomogeneousMatrix &eMc)
calibration method of effector-camera from R. Tsai and R. Lorenz .
static double deg(double rad)
Definition: vpMath.h:97
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
vpHomogeneousMatrix inverse() const
static vpHomogeneousMatrix direct(const vpColVector &v)
Class that consider the case of a translation vector.
Implementation of a rotation vector as axis-angle minimal representation.