Visual Servoing Platform  version 3.1.0
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 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  * Tsai calibration example to estimate hand to eye transformation.
33  *
34  * Authors:
35  * Fabien Spindler
36  *
37  *****************************************************************************/
38 
45 #include <iomanip>
46 #include <sstream>
47 #include <stdio.h>
48 #include <vector>
49 
50 #include <visp3/core/vpDebug.h>
51 #include <visp3/core/vpExponentialMap.h>
52 #include <visp3/core/vpIoTools.h>
53 #include <visp3/io/vpParseArgv.h>
54 #include <visp3/vision/vpCalibration.h>
55 
56 int main()
57 {
58  try {
59  // We want to calibrate the hand to eye extrinsic camera parameters from 6
60  // couple of poses: cMo and wMe
61  const unsigned int N = 6;
62  // Input: six couple of poses used as input in the calibration proces
63  std::vector<vpHomogeneousMatrix> cMo(N); // eye (camera) to object
64  // transformation. The object
65  // frame is attached to the
66  // calibrartion grid
67  std::vector<vpHomogeneousMatrix> wMe(N); // world to hand (end-effector) transformation
68  // Output: Result of the calibration
69  vpHomogeneousMatrix eMc; // hand (end-effector) to eye (camera) transformation
70 
71  // Initialize an eMc transformation used to produce the simulated input
72  // transformations cMo and wMe
73  vpTranslationVector etc(0.1, 0.2, 0.3);
74  vpThetaUVector erc;
75  erc[0] = vpMath::rad(10); // 10 deg
76  erc[1] = vpMath::rad(-10); // -10 deg
77  erc[2] = vpMath::rad(25); // 25 deg
78 
79  eMc.buildFrom(etc, erc);
80  std::cout << "Simulated hand to eye transformation: eMc " << std::endl;
81  std::cout << eMc << std::endl;
82  std::cout << "Theta U rotation: " << vpMath::deg(erc[0]) << " " << vpMath::deg(erc[1]) << " " << vpMath::deg(erc[2])
83  << std::endl;
84 
85  vpColVector v_c(6); // camera velocity used to produce 6 simulated poses
86  for (unsigned int i = 0; i < N; i++) {
87  v_c = 0;
88  if (i == 0) {
89  // Initialize first poses
90  cMo[0].buildFrom(0, 0, 0.5, 0, 0, 0); // z=0.5 m
91  wMe[0].buildFrom(0, 0, 0, 0, 0, 0); // Id
92  } else if (i == 1)
93  v_c[3] = M_PI / 8;
94  else if (i == 2)
95  v_c[4] = M_PI / 8;
96  else if (i == 3)
97  v_c[5] = M_PI / 10;
98  else if (i == 4)
99  v_c[0] = 0.5;
100  else if (i == 5)
101  v_c[1] = 0.8;
102 
103  vpHomogeneousMatrix cMc; // camera displacement
104  cMc = vpExponentialMap::direct(v_c); // Compute the camera displacement
105  // due to the velocity applied to
106  // the camera
107  if (i > 0) {
108  // From the camera displacement cMc, compute the wMe and cMo matrices
109  cMo[i] = cMc.inverse() * cMo[i - 1];
110  wMe[i] = wMe[i - 1] * eMc * cMc * eMc.inverse();
111  }
112  }
113 
114  if (0) {
115  for (unsigned int i = 0; i < N; i++) {
117  wMo = wMe[i] * eMc * cMo[i];
118  std::cout << std::endl << "wMo[" << i << "] " << std::endl;
119  std::cout << wMo << std::endl;
120  std::cout << "cMo[" << i << "] " << std::endl;
121  std::cout << cMo[i] << std::endl;
122  std::cout << "wMe[" << i << "] " << std::endl;
123  std::cout << wMe[i] << std::endl;
124  }
125  }
126 
127  // Reset the eMc matrix to eye
128  eMc.eye();
129 
130  // Compute the eMc hand to eye transformation from six poses
131  // - cMo[6]: camera to object poses as six homogeneous transformations
132  // - wMe[6]: world to hand (end-effector) poses as six homogeneous
133  // transformations
134  vpCalibration::calibrationTsai(cMo, wMe, eMc);
135 
136  std::cout << std::endl << "Output: hand to eye calibration result: eMc estimated " << std::endl;
137  std::cout << eMc << std::endl;
138  eMc.extract(erc);
139  std::cout << "Theta U rotation: " << vpMath::deg(erc[0]) << " " << vpMath::deg(erc[1]) << " " << vpMath::deg(erc[2])
140  << std::endl;
141  return 0;
142  } catch (vpException &e) {
143  std::cout << "Catch an exception: " << e << std::endl;
144  return 1;
145  }
146 }
Implementation of an homogeneous matrix and operations on such kind of matrices.
error that can be emited by ViSP classes.
Definition: vpException.h:71
vpHomogeneousMatrix inverse() const
void extract(vpRotationMatrix &R) const
void buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
static double rad(double deg)
Definition: vpMath.h:102
static double deg(double rad)
Definition: vpMath.h:95
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
static void calibrationTsai(const std::vector< vpHomogeneousMatrix > &cMo, const std::vector< vpHomogeneousMatrix > &rMe, vpHomogeneousMatrix &eMc)
calibration method of effector-camera from R. Tsai and R. Lorenz .
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.