Visual Servoing Platform  version 3.4.0
testRobotAfma6Pose.cpp
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  * Test for Afma 6 dof robot.
33  *
34  * Authors:
35  * Fabien Spindler
36  *
37  *****************************************************************************/
38 
49 #include <iostream>
50 #include <visp3/blob/vpDot.h>
51 #include <visp3/core/vpCameraParameters.h>
52 #include <visp3/core/vpDebug.h>
53 #include <visp3/core/vpImage.h>
54 #include <visp3/core/vpPixelMeterConversion.h>
55 #include <visp3/core/vpPoint.h>
56 #include <visp3/gui/vpDisplayGTK.h>
57 #include <visp3/gui/vpDisplayOpenCV.h>
58 #include <visp3/gui/vpDisplayX.h>
59 #include <visp3/robot/vpRobotAfma6.h>
60 #include <visp3/sensor/vp1394TwoGrabber.h>
61 #include <visp3/vision/vpPose.h>
62 #if defined(VISP_HAVE_AFMA6) && defined(VISP_HAVE_DC1394)
63 
64 int main()
65 {
66  try {
67  // Create an image B&W container
69 
70  // Create a firewire grabber based on libdc1394-2.x
72 
73  // Grab an image from the firewire camera
74  g.acquire(I);
75 
76 // Create an image viewer for the image
77 #ifdef VISP_HAVE_X11
78  vpDisplayX display(I, 100, 100, "Current image");
79 #elif defined(VISP_HAVE_OPENCV)
80  vpDisplayOpenCV display(I, 100, 100, "Current image");
81 #elif defined(VISP_HAVE_GTK)
82  vpDisplayGTK display(I, 100, 100, "Current image");
83 #endif
84 
85  // Display the image
88 
89  // Define a squared target
90  // The target is made of 4 planar points (square dim = 0.077m)
91  double sdim = 0.077; // square width and height
92  vpPoint target[4];
93  // Set the point world coordinates (x,y,z) in the object frame
94  // o ----> x
95  // |
96  // |
97  // \/
98  // y
99  target[0].setWorldCoordinates(-sdim / 2., -sdim / 2., 0);
100  target[1].setWorldCoordinates(sdim / 2., -sdim / 2., 0);
101  target[2].setWorldCoordinates(sdim / 2., sdim / 2., 0);
102  target[3].setWorldCoordinates(-sdim / 2., sdim / 2., 0);
103 
104  // Image processing to extract the 2D coordinates in sub-pixels of the 4
105  // points from the image acquired by the camera
106  // Creation of 4 trackers
107  vpDot dot[4];
108  vpImagePoint cog;
109  for (int i = 0; i < 4; i++) {
110  dot[i].setGraphics(true); // to display the tracking results
111  std::cout << "Click on dot " << i << std::endl;
112  dot[i].initTracking(I);
113  // The tracker computes the sub-pixels coordinates in the image
114  // i ----> u
115  // |
116  // |
117  // \/
118  // v
119  std::cout << " Coordinates: " << dot[i].getCog() << std::endl;
120  // Flush the tracking results in the viewer
121  vpDisplay::flush(I);
122  }
123 
124  // Create an intrinsic camera parameters structure
125  vpCameraParameters cam;
126 
127  // Create a robot access
128  vpRobotAfma6 robot;
129 
130  // Load the end-effector to camera frame transformation obtained
131  // using a camera intrinsic model with distortion
133 
134  // Get the intrinsic camera parameters associated to the image
135  robot.getCameraParameters(cam, I);
136 
137  // Using the camera parameters, compute the perspective projection
138  // (transform the dot sub-pixel coordinates into coordinates in the camera
139  // frame in meter)
140  for (int i = 0; i < 4; i++) {
141  double x = 0, y = 0; // coordinates of the dots in the camera frame
142  // c ----> x
143  // |
144  // |
145  // \/
146  // y
147  // pixel to meter conversion
148  cog = dot[i].getCog();
149  vpPixelMeterConversion::convertPoint(cam, cog, x, y);
150  target[i].set_x(x);
151  target[i].set_y(y);
152  }
153 
154  // From now, in target[i], we have the 3D coordinates of a point in the
155  // object frame, and their correspondances in the camera frame. We can now
156  // compute the pose cMo between the camera and the object.
157  vpPose pose;
158  // Add the 4 points to compute the pose
159  for (int i = 0; i < 4; i++) {
160  pose.addPoint(target[i]);
161  }
162  // Create an homogeneous matrix for the camera to object transformation
163  // computed just bellow
166  vpRxyzVector r;
167  // Compute the pose: initialisation is done by Lagrange method, and the
168  // final pose is computed by the more accurate Virtual Visual Servoing
169  // method.
171 
172  std::cout << "Pose cMo: " << std::endl << cMo;
173  cMo.extract(R);
174  r.buildFrom(R);
175  std::cout << " rotation: " << vpMath::deg(r[0]) << " " << vpMath::deg(r[1]) << " " << vpMath::deg(r[2]) << " deg"
176  << std::endl
177  << std::endl;
178 
179  // Get the robot position in the reference frame
181  vpColVector p; // position x,y,z,rx,ry,rz
183  std::cout << "Robot pose in reference frame: " << p << std::endl;
185  t[0] = p[0];
186  t[1] = p[1];
187  t[2] = p[2];
188  r[0] = p[3];
189  r[1] = p[4];
190  r[2] = p[5];
191  R.buildFrom(r);
192  rMc.buildFrom(t, R);
193  std::cout << "Pose rMc: " << std::endl << rMc;
194  rMc.extract(R);
195  r.buildFrom(R);
196  std::cout << " rotation: " << vpMath::deg(r[0]) << " " << vpMath::deg(r[1]) << " " << vpMath::deg(r[2]) << " deg"
197  << std::endl
198  << std::endl;
199 
201  std::cout << "Robot pose in articular: " << p << std::endl;
202 
203  robot.get_fMc(p, rMc);
204  std::cout << "Pose rMc from MGD: " << std::endl << rMc;
205  rMc.extract(R);
206  r.buildFrom(R);
207  std::cout << " rotation: " << vpMath::deg(r[0]) << " " << vpMath::deg(r[1]) << " " << vpMath::deg(r[2]) << " deg"
208  << std::endl
209  << std::endl;
210 
212  rMo = rMc * cMo;
213  std::cout << "Pose rMo = rMc * cMo: " << std::endl << rMo;
214  rMo.extract(R);
215  r.buildFrom(R);
216  std::cout << " rotation: " << vpMath::deg(r[0]) << " " << vpMath::deg(r[1]) << " " << vpMath::deg(r[2]) << " deg"
217  << std::endl
218  << std::endl;
219 
220  } catch (const vpException &e) {
221  std::cout << "Catch an exception: " << e << std::endl;
222  }
223  return 0;
224 }
225 #else
226 int main()
227 {
228  std::cout << "Sorry, test not valid. You should have an Afma6 robot..." << std::endl;
229  return 0;
230 }
231 
232 #endif
vpRxyzVector buildFrom(const vpRotationMatrix &R)
bool computePose(vpPoseMethodType method, vpHomogeneousMatrix &cMo, bool(*func)(const vpHomogeneousMatrix &)=NULL)
Definition: vpPose.cpp:374
void getCameraParameters(vpCameraParameters &cam, const unsigned int &image_width, const unsigned int &image_height) const
Definition: vpAfma6.cpp:1256
void setWorldCoordinates(double oX, double oY, double oZ)
Definition: vpPoint.cpp:113
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix get_fMc(const vpColVector &q) const
Definition: vpAfma6.cpp:775
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:150
error that can be emited by ViSP classes.
Definition: vpException.h:71
static void convertPoint(const vpCameraParameters &cam, const double &u, const double &v, double &x, double &y)
void acquire(vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
void getPosition(const vpRobot::vpControlFrameType frame, vpColVector &position)
Control of Irisa&#39;s gantry robot named Afma6.
Definition: vpRobotAfma6.h:211
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:81
void set_x(double x)
Set the point x coordinate in the image plane.
Definition: vpPoint.cpp:497
Implementation of a rotation matrix and operations on such kind of matrices.
void set_y(double y)
Set the point y coordinate in the image plane.
Definition: vpPoint.cpp:499
void init(void)
vpImagePoint getCog() const
Definition: vpDot.h:247
vpRotationMatrix buildFrom(const vpHomogeneousMatrix &M)
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Class used for pose computation from N points (pose from point only). Some of the algorithms implemen...
Definition: vpPose.h:80
Generic class defining intrinsic camera parameters.
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:134
void extract(vpRotationMatrix &R) const
void buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
static double deg(double rad)
Definition: vpMath.h:103
Implementation of column vector and the associated operations.
Definition: vpColVector.h:130
This tracker is meant to track a dot (connected pixels with same gray level) on a vpImage...
Definition: vpDot.h:115
Implementation of a rotation vector as Euler angle minimal representation.
Definition: vpRxyzVector.h:183
Class for firewire ieee1394 video devices using libdc1394-2.x api.
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:87
void setGraphics(bool activate)
Definition: vpDot.h:361
void addPoint(const vpPoint &P)
Definition: vpPose.cpp:149
void initTracking(const vpImage< unsigned char > &I)
Definition: vpDot.cpp:635
Class that consider the case of a translation vector.