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