silverhammer_lowspeed_streamer.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 diagnostic_updater
00009 from diagnostic_msgs.msg import DiagnosticStatus
00010 import os
00011 import rospy
00012 import signal
00013 import sys
00014 
00015 import roslib
00016 from roslib.message import get_message_class
00017 from std_msgs.msg import Time
00018 from jsk_network_tools.srv import SetSendRate, SetSendRateResponse
00019 
00020 class SilverHammerLowspeedStreamer():
00021     def __init__(self):
00022         message_class_str = rospy.get_param("~message", 
00023                                             "jsk_network_tools/FC2OCS")
00024         try:
00025             self.send_message = get_message_class(message_class_str)
00026         except:
00027             raise Exception("invalid topic type: %s"%message_class_str)
00028         self.lock = Lock()
00029         self.launched_time = rospy.Time.now()
00030         self.diagnostic_updater = diagnostic_updater.Updater()
00031         self.diagnostic_updater.setHardwareID("none")
00032         self.diagnostic_updater.add("LowspeedStreamer", self.diagnosticCallback)
00033         self.send_num = 0
00034         self.last_send_time = rospy.Time(0)
00035         self.last_input_received_time = rospy.Time(0)
00036         self.last_send_time_pub = rospy.Publisher("~last_send_time", Time)
00037         self.last_input_received_time_pub = rospy.Publisher(
00038             "~last_input_received_time", Time)
00039         self.to_port = rospy.get_param("~to_port", 1024)
00040         self.to_ip = rospy.get_param("~to_ip", "127.0.0.1")
00041         self.send_rate = rospy.get_param("~send_rate", 1.0)
00042         self.event_driven = rospy.get_param("~event_driven", False)
00043         self.latest_message = None
00044         self.socket_client = socket(AF_INET, SOCK_DGRAM)
00045         self.send_format = msgToStructFormat(self.send_message())
00046         self.sub = rospy.Subscriber("~input", 
00047                                     self.send_message, self.messageCallback)
00048         if not self.event_driven:
00049             self.send_timer = rospy.Timer(rospy.Duration(1.0 / self.send_rate),
00050                                           self.sendTimerCallback)
00051         self.diagnostic_timer = rospy.Timer(rospy.Duration(1.0 / 10),
00052                                             self.diagnosticTimerCallback)
00053         self.send_rate_service = rospy.Service('~set_send_rate', SetSendRate, self.setSendRate)
00054 
00055     def setSendRate(self, req):
00056         try:
00057             if self.event_driven:
00058                 rospy.logerr("failed to change send_rate. event_driven is enabled.")
00059                 return SetSendRateResponse(ok=False)
00060             if self.send_timer.is_alive():
00061                 self.send_timer.shutdown()
00062             self.send_rate = req.rate
00063             rospy.set_param("~send_rate", self.send_rate)
00064 #            self.send_rate = rospy.get_param("~send_rate", 1.0)
00065             rospy.loginfo("send_rate is set to %f" % self.send_rate)
00066             self.send_timer = rospy.Timer(rospy.Duration(1.0 / self.send_rate),
00067                                           self.sendTimerCallback)
00068             return SetSendRateResponse(ok=True)
00069         except Exception as e:
00070             rospy.logerr("failed to set send_rate: %s" % e)
00071             return SetSendRateResponse(ok=False)
00072 
00073     def diagnosticTimerCallback(self, event):
00074         self.diagnostic_updater.update()
00075     def diagnosticCallback(self, stat):
00076         # always OK
00077         stat.summary(DiagnosticStatus.OK, "OK")
00078         with self.lock:
00079             now = rospy.Time.now()
00080             stat.add("Uptime [sec]",
00081                      (now - self.launched_time).to_sec())
00082             stat.add("Time from the last sending [sec]",
00083                      (now - self.last_send_time).to_sec())
00084             stat.add("Number of transmission", self.send_num)
00085             stat.add("Time from the last input [sec]",
00086                      (now - self.last_input_received_time).to_sec())
00087             # properties
00088             stat.add("UDP address", self.to_ip)
00089             stat.add("UDP port", self.to_port)
00090             stat.add("EventDrivenMode", self.event_driven)
00091             self.last_send_time_pub.publish(self.last_send_time)
00092             self.last_input_received_time_pub.publish(self.last_input_received_time)
00093         return stat
00094     def messageCallback(self, msg):
00095         with self.lock:
00096             self.latest_message = msg
00097             self.last_input_received_time = rospy.Time.now()
00098             if self.event_driven:
00099                 self.sendMessage(msg)
00100     def sendMessage(self, msg):
00101         packed_data = packMessage(msg, self.send_format)
00102         self.socket_client.sendto(packed_data, (self.to_ip, self.to_port))
00103         self.last_send_time = rospy.Time.now()
00104         self.send_num = self.send_num + 1
00105     def sendTimerCallback(self, event):
00106         with self.lock:
00107             if self.latest_message:
00108                 rospy.logdebug("sending message")
00109                 self.sendMessage(self.latest_message)
00110                 self.latest_message = None
00111             else:
00112                 rospy.loginfo("no message is available")
00113 
00114 if __name__ == "__main__":
00115     rospy.init_node("silverhammer_lowspeed_streamer")
00116     st = SilverHammerLowspeedStreamer()
00117     rospy.spin()
00118 
00119 
00120 


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