hand_eye_calib.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Copyright 2020 Roboception GmbH
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are met:
7 #
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 #
11 # * Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
14 #
15 # * Neither the name of the {copyright_holder} nor the names of its
16 # contributors may be used to endorse or promote products derived from
17 # this software without specific prior written permission.
18 #
19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 # POSSIBILITY OF SUCH DAMAGE.
30 
31 from __future__ import absolute_import
32 
33 import rospy
34 
35 from tf2_msgs.msg import TFMessage
36 from geometry_msgs.msg import TransformStamped
37 
38 from rc_reason_msgs.srv import HandEyeCalibration, HandEyeCalibrationRequest
39 from rc_reason_msgs.srv import HandEyeCalibrationTrigger
40 from rc_reason_msgs.srv import SetHandEyeCalibration
41 from rc_reason_msgs.srv import SetHandEyeCalibrationPose
42 
43 from .rest_client import RestClient
44 
45 
47 
48  def __init__(self):
49  super(HandEyeCalibClient, self).__init__('rc_hand_eye_calibration')
50 
51  self.camera_frame_id = rospy.get_param("~camera_frame_id", "camera")
52  self.end_effector_frame_id = rospy.get_param("~end_effector_frame_id", "end_effector")
53  self.base_frame_id = rospy.get_param("~base_frame_id", "base_link")
54 
55  self.pub_tf = rospy.Publisher('/tf_static', TFMessage, queue_size=1, latch=True)
56 
57  self.add_rest_service(HandEyeCalibration, 'calibrate', self.pub_cb)
58  self.add_rest_service(HandEyeCalibration, 'get_calibration', self.pub_cb)
59  self.add_rest_service(SetHandEyeCalibration, 'set_calibration', self.pub_cb)
60  self.add_rest_service(HandEyeCalibrationTrigger, 'save_calibration', self.generic_cb)
61  self.add_rest_service(HandEyeCalibrationTrigger, 'delete_calibration', self.generic_cb)
62  self.add_rest_service(HandEyeCalibrationTrigger, 'reset_calibration', self.generic_cb)
63  self.add_rest_service(SetHandEyeCalibrationPose, 'set_pose', self.generic_cb)
64 
65  # get initial calibration from sensor
66  self.pub_cb('get_calibration', HandEyeCalibration, HandEyeCalibrationRequest())
67 
68  def generic_cb(self, srv_name, srv_type, request):
69  response = self.call_rest_service(srv_name, srv_type, request)
70  if not response.success:
71  rospy.logwarn("service %s: %s", srv_name, response.message)
72  return response
73 
74  def pub_cb(self, srv_name, srv_type, request):
75  """Handle service call and publish hand-eye-calib if successful"""
76  response = self.call_rest_service(srv_name, srv_type, request)
77  if response.success:
78  self.pub_hand_eye(response.pose, response.robot_mounted)
79  else:
80  rospy.logwarn("service %s: %s", srv_name, response.message)
81  return response
82 
83  def pub_hand_eye(self, pose, robot_mounted):
84  transform = TransformStamped()
85  transform.transform.translation.x = pose.position.x
86  transform.transform.translation.y = pose.position.y
87  transform.transform.translation.z = pose.position.z
88  transform.transform.rotation = pose.orientation
89  transform.header.stamp = rospy.Time.now()
90  if robot_mounted:
91  transform.header.frame_id = self.end_effector_frame_id
92  else:
93  transform.header.frame_id = self.base_frame_id
94  transform.child_frame_id = self.camera_frame_id
95  rospy.loginfo("publishing hand-eye calibration from {} to {}".format(transform.header.frame_id, transform.child_frame_id))
96  self.pub_tf.publish(TFMessage(transforms=[transform]))
97 
98 
99 def main():
100  client = HandEyeCalibClient()
101 
102  try:
103  rospy.spin()
104  except KeyboardInterrupt:
105  pass
106 
107 
108 if __name__ == '__main__':
109  main()
rc_reason_clients.hand_eye_calib.HandEyeCalibClient.pub_cb
def pub_cb(self, srv_name, srv_type, request)
Definition: hand_eye_calib.py:74
rc_reason_clients.rest_client.RestClient.add_rest_service
def add_rest_service(self, srv_type, srv_name, callback)
Definition: rest_client.py:193
rc_reason_clients.hand_eye_calib.HandEyeCalibClient.pub_hand_eye
def pub_hand_eye(self, pose, robot_mounted)
Definition: hand_eye_calib.py:83
rc_reason_clients.hand_eye_calib.HandEyeCalibClient.pub_tf
pub_tf
Definition: hand_eye_calib.py:55
rc_reason_clients.hand_eye_calib.HandEyeCalibClient.__init__
def __init__(self)
Definition: hand_eye_calib.py:48
rc_reason_clients.hand_eye_calib.HandEyeCalibClient.generic_cb
def generic_cb(self, srv_name, srv_type, request)
Definition: hand_eye_calib.py:68
rc_reason_clients.hand_eye_calib.HandEyeCalibClient.end_effector_frame_id
end_effector_frame_id
Definition: hand_eye_calib.py:52
rc_reason_clients.rest_client.RestClient
Definition: rest_client.py:70
rc_reason_clients.hand_eye_calib.HandEyeCalibClient.camera_frame_id
camera_frame_id
Definition: hand_eye_calib.py:51
rc_reason_clients.rest_client.RestClient.call_rest_service
def call_rest_service(self, name, srv_type=None, request=None)
Definition: rest_client.py:162
rc_reason_clients.hand_eye_calib.main
def main()
Definition: hand_eye_calib.py:99
rc_reason_clients.hand_eye_calib.HandEyeCalibClient.base_frame_id
base_frame_id
Definition: hand_eye_calib.py:53
rc_reason_clients.hand_eye_calib.HandEyeCalibClient
Definition: hand_eye_calib.py:46


rc_reason_clients
Author(s):
autogenerated on Sat Nov 23 2024 03:19:24