silverhammer_lowspeed_receiver.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 from jsk_network_tools.msg import FC2OCS, OCS2FC
00004 from jsk_network_tools.silverhammer_util import *
00005 from threading import Lock, Thread
00006 from socket import *
00007 from struct import Struct
00008 import os
00009 import rospy
00010 import signal
00011 import sys
00012 import roslib
00013 from roslib.message import get_message_class
00014 from std_msgs.msg import Time
00015 import diagnostic_updater
00016 from diagnostic_msgs.msg import DiagnosticStatus
00017 
00018 class SilverHammerUDPListener():
00019     def __init__(self, server, buffer_size, format, message, pub):
00020         self.server = server
00021         self.format = format
00022         self.pub = pub
00023         self.message = message
00024         self.buffer_size = buffer_size
00025     def run(self):
00026         recv_data, addr = self.server.recvfrom(self.buffer_size)
00027         msg = unpackMessage(recv_data, self.format, self.message)
00028         self.pub.publish(msg)
00029         print "received:", msg
00030 
00031 class SilverHammerLowspeedReceiver():
00032     def __init__(self):
00033         message_class_str = rospy.get_param("~message",
00034                                             "jsk_network_tools/FC2OCS")
00035         try:
00036             self.receive_message = get_message_class(message_class_str)
00037         except:
00038             raise Exception("invalid topic type: %s"%message_class_str)
00039         self.lock = Lock()
00040         self.launched_time = rospy.Time.now()
00041         self.diagnostic_updater = diagnostic_updater.Updater()
00042         self.diagnostic_updater.setHardwareID("none")
00043         self.diagnostic_updater.add("LowspeedReceiver", self.diagnosticCallback)
00044         self.received_num = 0
00045         self.receive_port = rospy.get_param("~receive_port", 1024)
00046         self.receive_ip = rospy.get_param("~receive_ip", "127.0.0.1")
00047         self.receive_buffer = rospy.get_param("~receive_buffer_size", 250)
00048         self.socket_server = socket(AF_INET, SOCK_DGRAM)
00049         self.socket_server.settimeout(None)
00050         self.socket_server.bind((self.receive_ip, self.receive_port))
00051         self.receive_format = msgToStructFormat(self.receive_message())
00052         self.pub = rospy.Publisher("~output", self.receive_message)
00053         self.last_received_time = rospy.Time(0)
00054         self.last_received_time_pub = rospy.Publisher(
00055             "~last_received_time", Time)
00056         self.last_publish_output_time = rospy.Time(0)
00057         self.last_publish_output_time_pub = rospy.Publisher(
00058             "~last_publish_output_time", Time)
00059         self.diagnostic_timer = rospy.Timer(rospy.Duration(1.0 / 10),
00060                                             self.diagnosticTimerCallback)
00061     def diagnosticTimerCallback(self, event):
00062         self.diagnostic_updater.update()
00063         # and publish time
00064         with self.lock:
00065             self.last_publish_output_time_pub.publish(self.last_publish_output_time)
00066             self.last_received_time_pub.publish(self.last_received_time)
00067     def diagnosticCallback(self, stat):
00068         # always OK
00069         stat.summary(DiagnosticStatus.OK, "OK")
00070         with self.lock:
00071             now = rospy.Time.now()
00072             stat.add("Uptime [sec]",
00073                      (now - self.launched_time).to_sec())
00074             stat.add("Time from the last reception [sec]",
00075                      (now - self.last_received_time).to_sec())
00076             stat.add("Time from the last publish ~output [sec]",
00077                      (now - self.last_publish_output_time).to_sec())
00078             stat.add("UDP address", self.receive_ip)
00079             stat.add("UDP port", self.receive_port)
00080         return stat
00081     def run(self):
00082         while not rospy.is_shutdown():
00083             recv_data, addr = self.socket_server.recvfrom(self.receive_buffer)
00084             msg = unpackMessage(recv_data, self.receive_format,
00085                                 self.receive_message)
00086             with self.lock:
00087                 self.last_received_time = rospy.Time.now()
00088             self.pub.publish(msg)
00089             with self.lock:
00090                 self.last_publish_output_time = rospy.Time.now()
00091             rospy.logdebug("received:", msg)
00092 
00093 if __name__ == "__main__":
00094     rospy.init_node("silverhammer_lowspeed_receiver")
00095     rc = SilverHammerLowspeedReceiver()
00096     rc.run()


jsk_network_tools
Author(s): Yusuke Furuta
autogenerated on Fri Sep 8 2017 03:38:47