hand_eye_calibration.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import rospy
3 import rostest
4 
5 import glob
6 import os
7 import unittest
8 
9 import actionlib
10 import tf
11 
12 from actionlib_msgs.msg import GoalStatus
13 from ensenso_camera_msgs.msg import CalibrateHandEyeAction, CalibrateHandEyeGoal
14 
15 from helper import Pose
16 
17 DATA_SET_DIRECTORY = "../data/hand_eye_calibration/"
18 
19 ROBOT_BASE_FRAME = "robot_base"
20 ROBOT_WRIST_FRAME = "robot_wrist"
21 
22 # The indices of the data set frames that do not contain any visible patterns.
23 FRAMES_WITHOUT_PATTERNS = [2, 3]
24 
25 
26 class TestHandEyeCalibration(unittest.TestCase):
27  def setUp(self):
29 
30  self.calibration_client = actionlib.SimpleActionClient("calibrate_hand_eye", CalibrateHandEyeAction)
31  self.calibration_client.wait_for_server()
32 
34  self.assertEqual(self.calibration_client.get_state(), GoalStatus.SUCCEEDED)
35  self.assertEqual(self.calibration_client.get_result().error.code, 0)
36 
37  def send_calibration_goal(self, goal, feedback_cb=None, wait=True):
38  self.calibration_client.send_goal(goal, feedback_cb=feedback_cb)
39 
40  if wait:
41  self.calibration_client.wait_for_result()
43  self.assertEqual(self.calibration_client.get_result().command, goal.command)
44 
45  def _broadcast_tf(self, pose, child_frame, parent_frame):
46  self.tf_broadcaster.sendTransform(pose.position, pose.orientation, rospy.Time.now(), child_frame, parent_frame)
47 
49  self.send_calibration_goal(CalibrateHandEyeGoal(command=CalibrateHandEyeGoal.RESET))
50 
51  result = self.calibration_client.get_result()
52  self.assertEqual(result.command, CalibrateHandEyeGoal.RESET)
53  self.assertEqual(result.error_message, "")
54  self.assertEqual(result.error.code, 0)
55 
56  link_files = sorted(glob.glob(os.path.join(DATA_SET_DIRECTORY, "links/links*")))
57  self.assertEqual(len(link_files), 22)
58  for i, link_file in enumerate(link_files):
59  current_robot_pose = Pose.from_json(link_file)
60 
61  self._broadcast_tf(current_robot_pose, ROBOT_WRIST_FRAME, ROBOT_BASE_FRAME)
62  self.send_calibration_goal(CalibrateHandEyeGoal(command=CalibrateHandEyeGoal.CAPTURE_PATTERN))
63 
64  result = self.calibration_client.get_result()
65  self.assertEqual(result.command, CalibrateHandEyeGoal.CAPTURE_PATTERN)
66  self.assertEqual(result.error_message, "")
67  self.assertEqual(result.error.code, 0)
68 
69  if not result.found_pattern:
70  self.assertIn(i, FRAMES_WITHOUT_PATTERNS)
71  else:
72  self.assertEqual(result.pattern.thickness, 0.001)
73  self.assertEqual(result.pattern.grid_spacing, 0.01875)
74  self.assertEqual(result.pattern.grid_size_x, 7)
75  self.assertEqual(result.pattern.grid_size_y, 7)
76  self.assertEqual(len(result.pattern.left_points), 49)
77  self.assertEqual(len(result.pattern.right_points), 49)
78 
79  robot_pose = Pose.from_message(result.robot_pose)
80  self.assertTrue(robot_pose.equals(current_robot_pose))
81 
82  pattern_pose = Pose.from_message(result.pattern_pose)
83  self.assertTrue(all([x != 0 for x in pattern_pose.position]))
84 
85  self.last_feedback = None
86 
87  def on_feedback(feedback):
88  self.last_feedback = feedback
89 
90  self.send_calibration_goal(CalibrateHandEyeGoal(command=CalibrateHandEyeGoal.START_CALIBRATION),
91  feedback_cb=on_feedback)
92 
93  self.assertIsNotNone(self.last_feedback)
94  self.assertNotEqual(self.last_feedback.number_of_iterations, 0)
95  self.assertNotEqual(self.last_feedback.residual, 0)
96 
97  result = self.calibration_client.get_result()
98  self.assertEqual(result.command, CalibrateHandEyeGoal.START_CALIBRATION)
99  self.assertEqual(result.error_message, "")
100  self.assertEqual(result.error.code, 0)
101 
102  self.assertNotEqual(result.calibration_time, 0)
103  self.assertNotEqual(result.number_of_iterations, 0)
104  self.assertNotEqual(result.residual, 0)
105 
106  final_link = Pose.from_json(os.path.join(DATA_SET_DIRECTORY, "final_link.json")).inverse()
107  final_pattern_pose = Pose.from_json(os.path.join(DATA_SET_DIRECTORY, "final_pattern_pose.json"))
108  self.assertTrue(final_link.equals(Pose.from_message(result.link)))
109  self.assertTrue(final_pattern_pose.equals(Pose.from_message(result.pattern_pose)))
110 
111 
112 if __name__ == "__main__":
113  try:
114  rospy.init_node("test_hand_eye_calibration")
115  rostest.rosrun("ensenso_camera_test", "test_hand_eye_calibration", TestHandEyeCalibration)
116  except rospy.ROSInterruptException:
117  pass
def send_calibration_goal(self, goal, feedback_cb=None, wait=True)
def _broadcast_tf(self, pose, child_frame, parent_frame)


ensenso_camera_test
Author(s): Ensenso
autogenerated on Thu May 6 2021 02:50:10