patlite_node.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 try: # catkin does not requires load_manifest
3  import rospatlite
4 except:
5  import roslib; roslib.load_manifest('rospatlite')
6 
7 import rospy
8 from std_msgs.msg import Int8
9 
10 from patlite import Patlite
11 from patlite import PatliteState
12 import socket
13 
14 class PatliteNode(object):
15  def set_state(self, target, state):
16  self.pstate.set_from_int(target, state)
17  self.send_flag = True
18 
19  def callback_r(self, msg):
20  self.set_state(self.pstate.Target.LIGHT_RED, msg.data)
21 
22  def callback_y(self, msg):
23  self.set_state(self.pstate.Target.LIGHT_YELLOW, msg.data)
24 
25  def callback_g(self, msg):
26  self.set_state(self.pstate.Target.LIGHT_GREEN, msg.data)
27 
28  def callback_b(self, msg):
29  self.set_state(self.pstate.Target.LIGHT_BLUE, msg.data)
30 
31  def callback_w(self, msg):
32  self.set_state(self.pstate.Target.LIGHT_WHITE, msg.data)
33 
34  def callback_buzz(self, msg):
35  self.set_state(self.pstate.Target.BUZZER, msg.data)
36 
37  def run(self):
38  rospy.init_node('patlite_node')
39  patlite_host = rospy.get_param('~host', "10.68.0.10")
40  patlite_port = rospy.get_param('~port', 10000)
41  timeout = rospy.get_param('~timeout', 0.5)
42  self.p = Patlite(patlite_host, port=patlite_port, timeout=timeout)
44  self.send_flag = False
45 
46  rospy.Subscriber("~set/red", Int8, self.callback_r)
47  rospy.Subscriber("~set/yellow", Int8, self.callback_y)
48  rospy.Subscriber("~set/green", Int8, self.callback_g)
49  rospy.Subscriber("~set/blue", Int8, self.callback_b)
50  rospy.Subscriber("~set/white", Int8, self.callback_w)
51  rospy.Subscriber("~set/buzzer", Int8, self.callback_buzz)
52  rospy.loginfo("[patlite_node] started")
53 
54  while not rospy.is_shutdown():
55  if self.send_flag:
56  self.send_flag = False
57  try:
58  with self.p:
59  self.p.write(self.pstate)
60  self.pstate.clear()
61  except socket.timeout:
62  rospy.logerr("[patlite_node] Error: socket timeout (%s:%d)",
63  patlite_host, patlite_port)
64  except socket.error as e:
65  rospy.logerr("[patlite_node] Error: %s (%s:%d)",
66  e, patlite_host, patlite_port)
67  rospy.sleep(0.1)
68  rospy.loginfo("[patlite_node] shutdown")
69  return
70 
71 if __name__ == '__main__':
72  try:
73  pn = PatliteNode()
74  pn.run()
75  except rospy.ROSInterruptException:
76  pass
77 
def callback_g(self, msg)
Definition: patlite_node.py:25
def set_state(self, target, state)
Definition: patlite_node.py:15
def callback_w(self, msg)
Definition: patlite_node.py:31
def callback_r(self, msg)
Definition: patlite_node.py:19
def callback_y(self, msg)
Definition: patlite_node.py:22
def callback_buzz(self, msg)
Definition: patlite_node.py:34
def callback_b(self, msg)
Definition: patlite_node.py:28


rospatlite
Author(s): Takuya Nakaoka
autogenerated on Wed Jul 10 2019 03:47:15