patlite_node.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 try: # catkin does not requires load_manifest
00003     import rospatlite
00004 except:
00005     import roslib; roslib.load_manifest('rospatlite')
00006 
00007 import rospy
00008 from std_msgs.msg import Int8
00009 
00010 from patlite import Patlite
00011 from patlite import PatliteState
00012 import socket
00013 
00014 class PatliteNode(object):
00015     def set_state(self, target, state):
00016         self.pstate.set_from_int(target, state)
00017         self.send_flag = True
00018 
00019     def callback_r(self, msg):
00020         self.set_state(self.pstate.Target.LIGHT_RED, msg.data)
00021 
00022     def callback_y(self, msg):
00023         self.set_state(self.pstate.Target.LIGHT_YELLOW, msg.data)
00024 
00025     def callback_g(self, msg):
00026         self.set_state(self.pstate.Target.LIGHT_GREEN, msg.data)
00027 
00028     def callback_b(self, msg):
00029         self.set_state(self.pstate.Target.LIGHT_BLUE, msg.data)
00030 
00031     def callback_w(self, msg):
00032         self.set_state(self.pstate.Target.LIGHT_WHITE, msg.data)
00033 
00034     def callback_buzz(self, msg):
00035         self.set_state(self.pstate.Target.BUZZER, msg.data)
00036 
00037     def run(self):
00038         rospy.init_node('patlite_node')
00039         patlite_host = rospy.get_param('~host', "10.68.0.10")
00040         patlite_port = rospy.get_param('~port', 10000)
00041         timeout = rospy.get_param('~timeout', 0.5)
00042         self.p = Patlite(patlite_host, port=patlite_port, timeout=timeout)
00043         self.pstate = PatliteState()
00044         self.send_flag = False
00045 
00046         rospy.Subscriber("~set/red", Int8, self.callback_r)
00047         rospy.Subscriber("~set/yellow", Int8, self.callback_y)
00048         rospy.Subscriber("~set/green", Int8, self.callback_g)
00049         rospy.Subscriber("~set/blue", Int8, self.callback_b)
00050         rospy.Subscriber("~set/white", Int8, self.callback_w)
00051         rospy.Subscriber("~set/buzzer", Int8, self.callback_buzz)
00052         rospy.loginfo("[patlite_node] started")
00053 
00054         while not rospy.is_shutdown():
00055             if self.send_flag:
00056                 self.send_flag = False
00057                 try:
00058                     with self.p:
00059                         self.p.write(self.pstate)
00060                         self.pstate.clear()
00061                 except socket.timeout:
00062                     rospy.logerr("[patlite_node] Error: socket timeout (%s:%d)",
00063                             patlite_host, patlite_port)
00064                 except socket.error as e:
00065                     rospy.logerr("[patlite_node] Error: %s (%s:%d)",
00066                             e, patlite_host, patlite_port)
00067             rospy.sleep(0.1)
00068         rospy.loginfo("[patlite_node] shutdown")
00069         return
00070 
00071 if __name__ == '__main__':
00072     try:
00073         pn = PatliteNode()
00074         pn.run()
00075     except rospy.ROSInterruptException:
00076         pass
00077 


rospatlite
Author(s): Takuya Nakaoka
autogenerated on Mon Oct 6 2014 10:57:56