handeye_target_charuco_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, John Stechschulte */
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/Charuco");
58  ASSERT_TRUE(target_->setParameter("squares, X", 5));
59  ASSERT_TRUE(target_->setParameter("squares, Y", 7));
60  ASSERT_TRUE(target_->setParameter("marker size (px)", 50));
61  ASSERT_TRUE(target_->setParameter("square size (px)", 80));
62  ASSERT_TRUE(target_->setParameter("margin size (px)", 2));
63  ASSERT_TRUE(target_->setParameter("marker border (bits)", 1));
64  ASSERT_TRUE(target_->setParameter("ArUco dictionary", "DICT_5X5_250"));
65  ASSERT_TRUE(target_->setParameter("longest board side (m)", 0.1971));
66  ASSERT_TRUE(target_->setParameter("measured marker size (m)", 0.0176));
67 
68  ASSERT_TRUE(target_->initialize());
69  }
70  catch (const pluginlib::PluginlibException& ex)
71  {
72  ROS_ERROR_STREAM("Exception while creating handeye target plugin: " << ex.what());
73  return;
74  }
75 
76  std::string image_path = ros::package::getPath("moveit_calibration_plugins") +
77  "/handeye_calibration_target/test/test_charuco_board_detection.jpg";
78 
79  image_ = cv::imread(image_path, cv::IMREAD_COLOR);
80 
81  resource_ok_ = false;
82  if (!image_.data)
83  ROS_ERROR_STREAM("Could not open or find the image file: " << image_path);
84  else
85  resource_ok_ = true;
86  }
87 
88  void TearDown() override
89  {
90  target_.reset();
91  target_plugins_loader_.reset();
92  }
93 
94 protected:
95  pluginlib::UniquePtr<moveit_handeye_calibration::HandEyeTargetBase> target_;
96  std::unique_ptr<pluginlib::ClassLoader<moveit_handeye_calibration::HandEyeTargetBase> > target_plugins_loader_;
97  bool resource_ok_;
98  cv::Mat image_;
99 };
100 
102 {
103  ASSERT_TRUE(resource_ok_);
104  ASSERT_EQ(image_.cols, 640);
105  ASSERT_EQ(image_.rows, 480);
106  ASSERT_TRUE(target_);
107 }
108 
109 TEST_F(MoveItHandEyeTargetTester, DetectCharucoMarkerPose)
110 {
111  // Set camera intrinsic parameters
112  sensor_msgs::CameraInfoPtr camera_info(new sensor_msgs::CameraInfo());
113  camera_info->height = 480;
114  camera_info->width = 640;
115  camera_info->header.frame_id = "camera_color_optical_frame";
116  camera_info->distortion_model = "plumb_bob";
117  camera_info->D = std::vector<double>{ 0.15405498, -0.24916842, 0.00350791, -0.00110041, 0.0 };
118  camera_info->K =
119  boost::array<double, 9>{ 590.6972346, 0.0, 322.33104773, 0.0, 592.84676713, 247.40030325, 0.0, 0.0, 1.0 };
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>{ 590.6972346, 0.0, 322.33104773, 0.0, 0.0, 592.84676713,
122  247.40030325, 0.0, 0.0, 0.0, 1.0, 0.0 };
123  ASSERT_TRUE(target_->setCameraIntrinsicParams(camera_info));
124 
125  // Check target image creation
126  cv::Mat target_image;
127  ASSERT_TRUE(target_->createTargetImage(target_image));
128 
129  // Get target pose
130  cv::Mat gray_image;
131  cv::cvtColor(image_, gray_image, cv::COLOR_RGB2GRAY);
132  ASSERT_TRUE(target_->detectTargetPose(gray_image));
133 
134  // Get translation and rotation part
135  geometry_msgs::TransformStamped camera_transform;
136  ros::Time::init();
137  camera_transform = target_->getTransformStamped(camera_info->header.frame_id);
138  Eigen::Affine3d ret = tf2::transformToEigen(camera_transform);
139  std::cout << "Translation:\n"
140  << ret.translation() << "\nRotation:\n"
141  << ret.rotation().eulerAngles(0, 1, 2) << std::endl;
142  Eigen::Vector3d t(0.09752, 0.102848, 0.325596);
143  Eigen::Vector3d r(2.88409, -0.147996, 1.13757);
144  // Running this detection in Python gives this result:
145  // Eigen::Vector3d r(2.38276, -1.5543, 0.029617);
146  ASSERT_TRUE(ret.translation().isApprox(t, 0.01));
147  ASSERT_TRUE(ret.rotation().eulerAngles(0, 1, 2).isApprox(r, 0.01));
148 }
149 
150 int main(int argc, char** argv)
151 {
152  testing::InitGoogleTest(&argc, argv);
153  return RUN_ALL_TESTS();
154 }
ros::package::getPath
ROSLIB_DECL std::string getPath(const std::string &package_name)
ROS_ERROR_STREAM
#define ROS_ERROR_STREAM(args)
tf2_eigen.h
TEST_F
TEST_F(MoveItHandEyeTargetTester, InitOK)
Definition: handeye_target_charuco_test.cpp:101
MoveItHandEyeTargetTester::target_
pluginlib::UniquePtr< moveit_handeye_calibration::HandEyeTargetBase > target_
Definition: handeye_target_aruco_test.cpp:126
main
int main(int argc, char **argv)
Definition: handeye_target_charuco_test.cpp:150
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