handeye_target_aruco_test.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2019, Intel Corporation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 /* Author: Yu Yan */
36 
37 #include <fstream>
38 #include <gtest/gtest.h>
39 #include <ros/package.h>
40 #include <opencv2/core/core.hpp>
41 #include <tf2_eigen/tf2_eigen.h>
42 #include <sensor_msgs/CameraInfo.h>
44 #include <geometry_msgs/TransformStamped.h>
46 
47 class MoveItHandEyeTargetTester : public ::testing::Test
48 {
49 protected:
50  void SetUp() override
51  {
52  try
53  {
56  "moveit_calibration_plugins", "moveit_handeye_calibration::HandEyeTargetBase"));
57  target_ = target_plugins_loader_->createUniqueInstance("HandEyeTarget/Aruco");
58  ASSERT_TRUE(target_->setParameter("markers, X", 4));
59  ASSERT_TRUE(target_->setParameter("markers, Y", 3));
60  ASSERT_TRUE(target_->setParameter("marker size (px)", 200));
61  ASSERT_TRUE(target_->setParameter("marker separation (px)", 20));
62  ASSERT_TRUE(target_->setParameter("marker border (bits)", 1));
63  ASSERT_TRUE(target_->setParameter("ArUco dictionary", "DICT_4X4_250"));
64  ASSERT_TRUE(target_->setParameter("measured marker size (m)", 0.0256));
65  ASSERT_TRUE(target_->setParameter("measured separation (m)", 0.0066));
66 
67  ASSERT_TRUE(target_->initialize());
68  }
69  catch (const pluginlib::PluginlibException& ex)
70  {
71  ROS_ERROR_STREAM("Exception while creating handeye target plugin: " << ex.what());
72  return;
73  }
74 
75  std::string image_path = ros::package::getPath("moveit_calibration_plugins") +
76  "/handeye_calibration_target/test/test_aruco_marker_detection.png";
77 
78  image_ = cv::imread(image_path, cv::IMREAD_COLOR);
79 
80  resource_ok_ = false;
81  if (!image_.data)
82  ROS_ERROR_STREAM("Could not open or find the image file: " << image_path);
83  else
84  resource_ok_ = true;
85  }
86 
87  void TearDown() override
88  {
89  target_.reset();
90  target_plugins_loader_.reset();
91  }
92 
93 protected:
94  pluginlib::UniquePtr<moveit_handeye_calibration::HandEyeTargetBase> target_;
95  std::unique_ptr<pluginlib::ClassLoader<moveit_handeye_calibration::HandEyeTargetBase> > target_plugins_loader_;
96  bool resource_ok_;
97  cv::Mat image_;
98 };
99 
101 {
102  ASSERT_TRUE(resource_ok_);
103  ASSERT_EQ(image_.cols, 640);
104  ASSERT_EQ(image_.rows, 480);
105  ASSERT_TRUE(target_);
106 }
107 
108 TEST_F(MoveItHandEyeTargetTester, DetectArucoMarkerPose)
109 {
110  // Set camera intrinsic parameters
111  sensor_msgs::CameraInfoPtr camera_info(new sensor_msgs::CameraInfo());
112  camera_info->height = 480;
113  camera_info->width = 640;
114  camera_info->header.frame_id = "camera_color_optical_frame";
115  camera_info->distortion_model = "plumb_bob";
116  camera_info->D = std::vector<double>{ 0.0, 0.0, 0.0, 0.0, 0.0 };
117  camera_info->K = boost::array<double, 9>{
118  618.6002197265625, 0.0, 321.9837646484375, 0.0, 619.1103515625, 241.1459197998047, 0.0, 0.0, 1.0
119  };
120  camera_info->R = boost::array<double, 9>{ 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0 };
121  camera_info->P = boost::array<double, 12>{
122  618.6002197265625, 0.0, 321.9837646484375, 0.0, 0.0, 619.1103515625, 241.1459197998047, 0.0, 0.0, 0.0, 1.0, 0.0
123  };
124  ASSERT_TRUE(target_->setCameraIntrinsicParams(camera_info));
125 
126  // Check target image creation
127  cv::Mat target_image;
128  ASSERT_TRUE(target_->createTargetImage(target_image));
129 
130  // Get target pose
131  cv::Mat gray_image;
132  cv::cvtColor(image_, gray_image, cv::COLOR_RGB2GRAY);
133  ASSERT_TRUE(target_->detectTargetPose(gray_image));
134 
135  // Get translation and rotation part
136  geometry_msgs::TransformStamped camera_transform;
137  ros::Time::init();
138  camera_transform = target_->getTransformStamped(camera_info->header.frame_id);
139  Eigen::Affine3d ret = tf2::transformToEigen(camera_transform);
140  std::cout << "Translation:\n"
141  << ret.translation() << "\nRotation:\n"
142  << ret.rotation().eulerAngles(0, 1, 2) << std::endl;
143 #if CV_VERSION_MAJOR == 3 && CV_VERSION_MINOR == 2
144  // OpenCV 3.2.0 (which is the default on Ubuntu 18.04) has a buggy ArUco pose detection implementation
145  Eigen::Vector3d t(0.412949, -0.198895, 11.1761);
146  Eigen::Vector3d r(0.324172, -2.03144, 2.90114);
147 #else
148  Eigen::Vector3d t(0.0148984, 0.0123107, 0.58609);
149  Eigen::Vector3d r(2.12328, -1.50481, -1.29729);
150 #endif
151  ASSERT_TRUE(ret.translation().isApprox(t, 0.01));
152  ASSERT_TRUE(ret.rotation().eulerAngles(0, 1, 2).isApprox(r, 0.01));
153 }
154 
155 int main(int argc, char** argv)
156 {
157  testing::InitGoogleTest(&argc, argv);
158  return RUN_ALL_TESTS();
159 }
ros::package::getPath
ROSLIB_DECL std::string getPath(const std::string &package_name)
main
int main(int argc, char **argv)
Definition: handeye_target_aruco_test.cpp:155
ROS_ERROR_STREAM
#define ROS_ERROR_STREAM(args)
tf2_eigen.h
MoveItHandEyeTargetTester::target_
pluginlib::UniquePtr< moveit_handeye_calibration::HandEyeTargetBase > target_
Definition: handeye_target_aruco_test.cpp:126
TEST_F
TEST_F(MoveItHandEyeTargetTester, InitOK)
Definition: handeye_target_aruco_test.cpp:100
pluginlib::PluginlibException
MoveItHandEyeTargetTester::target_plugins_loader_
std::unique_ptr< pluginlib::ClassLoader< moveit_handeye_calibration::HandEyeTargetBase > > target_plugins_loader_
Definition: handeye_target_aruco_test.cpp:127
pluginlib::ClassLoader
class_loader.hpp
handeye_target_base.h
package.h
r
S r
tf2::transformToEigen
Eigen::Isometry3d transformToEigen(const geometry_msgs::Transform &t)
ros::Time::init
static void init()
MoveItHandEyeTargetTester
Definition: handeye_target_aruco_test.cpp:47
MoveItHandEyeTargetTester::TearDown
void TearDown() override
Definition: handeye_target_aruco_test.cpp:119
MoveItHandEyeTargetTester::SetUp
void SetUp() override
Definition: handeye_target_aruco_test.cpp:82
MoveItHandEyeTargetTester::resource_ok_
bool resource_ok_
Definition: handeye_target_aruco_test.cpp:128
t
geometry_msgs::TransformStamped t
MoveItHandEyeTargetTester::image_
cv::Mat image_
Definition: handeye_target_aruco_test.cpp:129


moveit_calibration_plugins
Author(s): Yu Yan
autogenerated on Fri Oct 18 2024 02:14:08