geometry_util.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 # FSRobo-R Package BSDL
5 # ---------
6 # Copyright (C) 2019 FUJISOFT. All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without modification,
9 # are permitted provided that the following conditions are met:
10 # 1. Redistributions of source code must retain the above copyright notice,
11 # this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright notice,
13 # this list of conditions and the following disclaimer in the documentation and/or
14 # other materials provided with the distribution.
15 # 3. Neither the name of the copyright holder nor the names of its contributors
16 # may be used to endorse or promote products derived from this software without
17 # specific prior written permission.
18 #
19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 # IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 # --------
30 
31 import rospy
32 import time
33 import tf
34 import fnmatch
35 from geometry_msgs.msg import PoseStamped
36 
38  def __init__(self, init_node=False):
39  if init_node:
40  rospy.init_node('tf_listener', anonymous=True)
42  rospy.sleep(0.5)
43 
44  def transform_pose(self, target_frame, source_frame, position, orientation):
45  pose_stamped = PoseStamped()
46  pose_stamped.header.stamp = rospy.Time.now() - rospy.Duration(0.1)
47  pose_stamped.header.frame_id = source_frame
48  pose_stamped.pose.position.x = position[0]
49  pose_stamped.pose.position.y = position[1]
50  pose_stamped.pose.position.z = position[2]
51  pose_stamped.pose.orientation.x = orientation[0]
52  pose_stamped.pose.orientation.y = orientation[1]
53  pose_stamped.pose.orientation.z = orientation[2]
54  pose_stamped.pose.orientation.w = orientation[3]
55 
56  self._listener.waitForTransform(target_frame, source_frame, rospy.Time(0), rospy.Duration(4))
57 
58  trans_result = self._listener.transformPose(target_frame, pose_stamped)
59 
60  return ([trans_result.pose.position.x,
61  trans_result.pose.position.y,
62  trans_result.pose.position.z],
63  [trans_result.pose.orientation.x,
64  trans_result.pose.orientation.y,
65  trans_result.pose.orientation.z,
66  trans_result.pose.orientation.w])
67 
68  def get_current_pose(self, target_frame, source_frame, timeout=4.0):
69  t = time.time()
70  while time.time() - t < timeout:
71  try:
72  now = rospy.Time.now()
73  self._listener.waitForTransform(target_frame, source_frame, now, rospy.Duration(0.01))
74  trans_result = self._listener.lookupTransform(target_frame, source_frame, now)
75  return trans_result
76  except tf.Exception as e:
77  pass
78 
79  return None
80 
81  def get_frame_names(self, pattern=None):
82  names = self._listener.getFrameStrings()
83  if pattern is None:
84  return names
85  else:
86  return fnmatch.filter(names, pattern)
87 
88 #if __name__ == '__main__':
89 # rospy.init_node('tf_test', anonymous=True)
90 # rospy.loginfo('node started')
91 # util = GeometryUtil()
92 # print util.transform_pose('arm2/base_link', 'world', \
93 # [0.1003, 0.0, 0.8325], [0.0, 0.0, -0.707, 0.707])
94 # print util.transform_pose('world', 'arm2/base_link', \
95 # [0.1003, 0.0, 0.8325], [0.0, 0.0, -0.707, 0.707])
96 # print util.transform_pose('arm1/base_link', 'arm2/base_link', \
97 # [0.1003, 0.0, 0.8325], [0.0, 0.0, -0.707, 0.707])
98 # print util.get_current_pose('arm2/base_link', 'arm2/Link6')
99 # print util.get_current_pose('arm1/base_link', 'arm1/Link6')
100 # print util.get_current_pose('arm1/Link6', 'arm1/base_link')
101 # print util.get_current_pose('world', 'arm2/Link6')
102 #
103 # #listener = tf.TransformListener()
104 # #listener.waitForTransform('world', 'arm2/Link6', rospy.Time(0), rospy.Duration(4))
105 # #listener.waitForTransform('arm2/base_link', 'arm2/Link6', rospy.Time(0), rospy.Duration(4))
106 # #print listener.allFramesAsString()
107 # #print listener.transformPose('world', pose)
108 # #print listener.lookupTransform('arm2/base_link', 'arm2/Link6', rospy.Time(0))
109 
110 if __name__ == '__main__':
111  rospy.init_node('tf_test', anonymous=True)
112  rospy.loginfo('node started')
113  util = GeometryUtil()
114  #ret = util.get_current_pose('camera_color_optical_frame', 'object_22_0', timeout=0.5)
115  #print(ret)
116  while not rospy.is_shutdown():
117  ret = util.get_current_pose('camera_color_optical_frame', 'object_22_0', timeout=0.1)
118  print(ret)
def __init__(self, init_node=False)
def transform_pose(self, target_frame, source_frame, position, orientation)
def get_current_pose(self, target_frame, source_frame, timeout=4.0)


fsrobo_r_driver
Author(s): F-ROSROBO
autogenerated on Sun Feb 9 2020 03:58:29