1 """This module contains utilities for interacting with Gazebo."""
6 from gazebo_msgs.srv
import ApplyBodyWrench, GetLinkState
7 from geometry_msgs.msg
import Wrench
8 from tf.transformations
import euler_from_quaternion
10 from dynamic_graph_bridge_msgs.msg
import Vector
as VectorMsg
14 list_of_topics = rospy.get_published_topics()
15 for a_topic
in list_of_topics:
16 if a_topic[0].startswith(
"/gazebo"):
21 def apply_force(force, duration, body_name="talos::torso_2_link
"):
22 """Gazebo service call for applying a force on a body."""
23 rospy.wait_for_service(
"/gazebo/apply_body_wrench")
24 apply_body_wrench_proxy = rospy.ServiceProxy(
25 "/gazebo/apply_body_wrench", ApplyBodyWrench
28 wrench.force.x = force[0]
29 wrench.force.y = force[1]
30 wrench.force.z = force[2]
35 body_name=body_name, wrench=wrench, duration=rospy.Duration(duration)
40 return [v.x, v.y, v.z]
44 return [v.x, v.y, v.z, v.w]
48 """Utility class reading the state of a given link from Gazebo and publishing."""
50 def __init__(self, link_name, rate, euler="sxyz", local_frame=False, prefix="/sot"):
51 super(GazeboLinkStatePublisher, self).
__init__(name=link_name +
"_publisher")
62 rospy.init_node(self.name, anonymous=
True)
74 pub_pos = rospy.Publisher(topic_pos, VectorMsg, queue_size=10)
75 pub_vel = rospy.Publisher(topic_vel, VectorMsg, queue_size=10)
77 rospy.wait_for_service(
"/gazebo/get_link_state")
78 get_link_state = rospy.ServiceProxy(
"/gazebo/get_link_state", GetLinkState)
80 rate = rospy.Rate(self.
rate)
81 while (
not self.
stopped())
and (
not rospy.is_shutdown()):
82 link_state_msg = get_link_state(
85 link_state = link_state_msg.link_state
87 cartesian =
vec2list(link_state.pose.position)
88 orientation =
quat2list(link_state.pose.orientation)
90 orientation = list(euler_from_quaternion(orientation, self.
euler))
91 position = cartesian + orientation
94 link_state.twist.angular
96 pub_pos.publish(position)
97 pub_vel.publish(velocity)