magic_button_relay.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 ##############################################################################
4 # Description
5 ##############################################################################
6 
7 """
8 Catches the output from buttons on a joystick and converts these
9 to customisable ros publishers (Empty, Time Timestamped Bool) for use
10 as pure events, timestamped events, or true/false triggers.
11 
12 Publishing only occurs when joystick button signals change state.
13 """
14 
15 ##############################################################################
16 # Imports
17 ##############################################################################
18 
19 import rospy
20 from yocs_msgs.msg import MagicButton
21 from std_msgs.msg import Empty
22 from std_msgs.msg import Time
23 from sensor_msgs.msg import Joy
24 
25 ##############################################################################
26 # Imports
27 ##############################################################################
28 
29 
30 class EventRelay(object):
31  def __init__(self, id, name):
32  self.button_id = id
33  self.publisher = rospy.Publisher(name, Empty, queue_size=10)
34  rospy.loginfo("MagicButtonRelay : adding event relay for button '%s' to publisher at '%s'" % (self.button_id, self.publisher.name))
35  self.last_state = False
36 
37  def update(self, pressed, unused_timestamp):
38  if not pressed and self.last_state: # only when released
39  self.publisher.publish(Empty())
40  rospy.logdebug("MagicButtonRelay : event for button '%s' published at '%s'" % (self.button_id, self.publisher.name))
41  self.last_state = pressed
42 
43 class StampedEventRelay(object):
44  def __init__(self, id, name):
45  self.button_id = id
46  self.publisher = rospy.Publisher(name, Time, queue_size=10)
47  rospy.loginfo("MagicButtonRelay : adding stamped event relay for button '%s' to publisher at '%s'" % (self.button_id, self.publisher.name))
48  self.last_state = False
49 
50  def update(self, pressed, timestamp):
51  if not pressed and self.last_state: # only when released
52  rospy.logdebug("MagicButtonRelay : stamped event for button '%s' published at '%s'" % (self.button_id, self.publisher.name))
53  self.publisher.publish(Time(timestamp))
54  self.last_state = pressed
55 
56 class StampedButtonRelay(object):
57  def __init__(self, id, name):
58  self.button_id = id
59  self.publisher = rospy.Publisher(name, MagicButton, queue_size=10)
60  rospy.loginfo("MagicButtonRelay : adding stamped button relay for button '%s' to publisher at '%s'" % (self.button_id, self.publisher.name))
61  self.last_state = False
62 
63  def update(self, pressed, timestamp):
64  if pressed != self.last_state: # whenever state changes
65  msg = MagicButton()
66  msg.header.stamp = timestamp
67  msg.pressed = pressed
68  rospy.logdebug("MagicButtonRelay : stamped button event for button '%s' published at '%s'" % (self.button_id, self.publisher.name))
69  self.publisher.publish(msg)
70  self.last_state = pressed
71 
72 class MagicButtonRelay(object):
73 
74  def __init__(self, relay_specifications):
75  self.relays = []
76  self.subscriber = rospy.Subscriber("joy", Joy, self.joy_callback)
77  for spec in relay_specifications:
78  try:
79  if spec["type"] == "event":
80  self.relays.append(EventRelay(spec["id"], spec["name"]))
81  elif spec["type"] == "stamped_event":
82  self.relays.append(StampedEventRelay(spec["id"], spec["name"]))
83  elif spec["type"] == "stamped_button":
84  self.relays.append(StampedButtonRelay(spec["id"], spec["name"]))
85  else:
86  rospy.logerr("MagicButtonRelay : invalid type (%s) for the magic button ['event', 'timestamp', 'button']" % spec["type"])
87  except KeyError:
88  rospy.logerr("MagicButtonRelay : no 'type' provided for the magic button ['event', 'timestamp', 'button']")
89 
90  def joy_callback(self, msg):
91  """
92  Processes the joy topic.
93  """
94  timestamp = msg.header.stamp
95  for relay in self.relays:
96  relay.update(msg.buttons[relay.button_id], msg.header.stamp)
97 
98 if __name__ == '__main__':
99  rospy.init_node('magic_button_relay')
100  relay_specifications = rospy.get_param("~relays")
101  magic_button_relay = MagicButtonRelay(relay_specifications)
102  rospy.spin()
def update(self, pressed, timestamp)
def __init__(self, relay_specifications)
def update(self, pressed, timestamp)
def update(self, pressed, unused_timestamp)


yocs_joyop
Author(s): Marcus Liebhardt
autogenerated on Mon Jun 10 2019 15:53:33