ds4_twist_node.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import rospy
4 from geometry_msgs.msg import Twist, TwistStamped
5 from ds4_driver.msg import Status
6 
7 
8 class StatusToTwist(object):
9  def __init__(self):
10  self._stamped = rospy.get_param('~stamped', False)
11  if self._stamped:
12  self._cls = TwistStamped
13  self._frame_id = rospy.get_param('~frame_id', 'base_link')
14  else:
15  self._cls = Twist
16  self._inputs = rospy.get_param('~inputs')
17  self._scales = rospy.get_param('~scales')
18 
19  self._attrs = []
20  for attr in Status.__slots__:
21  if attr.startswith('axis_') or attr.startswith('button_'):
22  self._attrs.append(attr)
23 
24  self._pub = rospy.Publisher('cmd_vel', self._cls, queue_size=1)
25  rospy.Subscriber('status', Status, self.cb_status, queue_size=1)
26 
27  def cb_status(self, msg):
28  """
29  :param msg:
30  :type msg: Status
31  :return:
32  """
33  input_vals = {}
34  for attr in self._attrs:
35  input_vals[attr] = getattr(msg, attr)
36 
37  to_pub = self._cls()
38  if self._stamped:
39  to_pub.header.stamp = rospy.Time.now()
40  to_pub.header.frame_id = self._frame_id
41  twist = to_pub.twist
42  else:
43  twist = to_pub
44 
45  for vel_type in self._inputs:
46  vel_vec = getattr(twist, vel_type)
47  for k, expr in self._inputs[vel_type].items():
48  scale = self._scales[vel_type].get(k, 1.0)
49  val = eval(expr, {}, input_vals)
50  setattr(vel_vec, k, scale * val)
51 
52  self._pub.publish(to_pub)
53 
54 
55 def main():
56  rospy.init_node('ds4_twist')
57 
59 
60  rospy.spin()
61 
62 
63 if __name__ == '__main__':
64  main()


ds4_driver
Author(s): Naoki Mizuno
autogenerated on Fri May 1 2020 03:25:46