WUT Velma robot API
gazebo_publish_ros_tf_object.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 
6 
7 # Copyright (c) 2017, Robot Control and Pattern Recognition Group,
8 # Institute of Control and Computation Engineering
9 # Warsaw University of Technology
10 #
11 # All rights reserved.
12 #
13 # Redistribution and use in source and binary forms, with or without
14 # modification, are permitted provided that the following conditions are met:
15 # * Redistributions of source code must retain the above copyright
16 # notice, this list of conditions and the following disclaimer.
17 # * Redistributions in binary form must reproduce the above copyright
18 # notice, this list of conditions and the following disclaimer in the
19 # documentation and/or other materials provided with the distribution.
20 # * Neither the name of the Warsaw University of Technology nor the
21 # names of its contributors may be used to endorse or promote products
22 # derived from this software without specific prior written permission.
23 #
24 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
25 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 # DISCLAIMED. IN NO EVENT SHALL <COPYright HOLDER> BE LIABLE FOR ANY
28 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #
35 # Author: Dawid Seredynski
36 #
37 
38 import roslib; roslib.load_manifest('rcprg_gazebo_utils')
39 
40 import sys
41 import rospy
42 import tf
43 import tf2_ros
44 
45 from geometry_msgs.msg import *
46 from gazebo_msgs.srv import *
47 
49  def __init__(self, link_name, frame_id, retry):
50  self._link_name = link_name
51  self._frame_id = frame_id
52  self._retry = retry
53 
54  self._service_name = '/gazebo/get_link_state'
55  self.get_link_state = None
56  self._br = tf2_ros.TransformBroadcaster()
57 
58  def spin(self):
59  while not rospy.is_shutdown():
60  req = GetLinkStateRequest()
61  req.link_name = self._link_name
62  req.reference_frame = "world"
63  if self.get_link_state == None:
64  try:
65  rospy.wait_for_service(self._service_name, 4.0)
66  self.get_link_state = rospy.ServiceProxy(self._service_name, GetLinkState)
67  print "connected to ROS service " + self._service_name
68  except rospy.exceptions.ROSException as e:
69  print e
70  print "Could not connect to ROS service " + self._service_name + ", retrying..."
71  if not self._retry:
72  print('GazeboTfPublisher: Terminating')
73  return
74  continue
75  try:
76  resp = self.get_link_state(req)
77  except rospy.service.ServiceException as e:
78  self.get_link_state = None
79  print e
80  print "Could not call to ROS service " + self._service_name + ", retrying..."
81  continue
82 
83  if resp.success == False:
84  print "success:", resp.success, ", status:", resp.status_message
85  raise Exception(self._service_name)
86  t = TransformStamped()
87  t.header.stamp = rospy.Time.now()
88  t.header.frame_id = "world"
89  t.child_frame_id = self._frame_id
90  t.transform.translation.x = resp.link_state.pose.position.x
91  t.transform.translation.y = resp.link_state.pose.position.y
92  t.transform.translation.z = resp.link_state.pose.position.z
93  t.transform.rotation = resp.link_state.pose.orientation
94  self._br.sendTransform(t)
95  try:
96  rospy.sleep(0.1)
97  except:
98  break
99 
101  print "usage: gazebo_publish_ros_tf_object model_name::link_name frame_id"
102 
103 if __name__ == "__main__":
104 
105  rospy.init_node('gazebo_publish_ros_tf_object', anonymous=True)
106  rospy.sleep(0.5)
107 
108  try:
109  link_name = rospy.get_param("~link_name")
110  frame_id = rospy.get_param("~frame_id")
111  retry = bool(rospy.get_param("~retry", True))
112  except KeyError as e:
113  print "Some ROS parameters are not provided:"
114  print e
115  exit(1)
116 
117  if len(sys.argv) != 3:
118  printUsage()
119  exit(1)
120 
121  pub = GazeboTfPublisher(link_name, frame_id, retry)
122 
123  pub.spin()
124