silverhammer_lowspeed_streamer.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 from jsk_network_tools.msg import FC2OCS, OCS2FC
5 from threading import Lock, Thread
6 from socket import *
7 from struct import Struct
8 import diagnostic_updater
9 from diagnostic_msgs.msg import DiagnosticStatus
10 import os
11 import rospy
12 import signal
13 import sys
14 
15 import roslib
16 from roslib.message import get_message_class
17 from std_msgs.msg import Time
18 from jsk_network_tools.srv import SetSendRate, SetSendRateResponse
19 
21  def __init__(self):
22  message_class_str = rospy.get_param("~message",
23  "jsk_network_tools/FC2OCS")
24  try:
25  self.send_message = get_message_class(message_class_str)
26  except:
27  raise Exception("invalid topic type: %s"%message_class_str)
28  self.lock = Lock()
29  self.launched_time = rospy.Time.now()
31  self.diagnostic_updater.setHardwareID("none")
32  self.diagnostic_updater.add("LowspeedStreamer", self.diagnosticCallback)
33  self.send_num = 0
34  self.last_send_time = rospy.Time(0)
35  self.last_input_received_time = rospy.Time(0)
36  self.last_send_time_pub = rospy.Publisher("~last_send_time", Time)
37  self.last_input_received_time_pub = rospy.Publisher(
38  "~last_input_received_time", Time)
39  self.to_port = rospy.get_param("~to_port", 1024)
40  self.to_ip = rospy.get_param("~to_ip", "127.0.0.1")
41  self.send_rate = rospy.get_param("~send_rate", 1.0)
42  self.event_driven = rospy.get_param("~event_driven", False)
43  self.latest_message = None
44  self.socket_client = socket(AF_INET, SOCK_DGRAM)
46  self.sub = rospy.Subscriber("~input",
47  self.send_message, self.messageCallback)
48  if not self.event_driven:
49  self.send_timer = rospy.Timer(rospy.Duration(1.0 / self.send_rate),
50  self.sendTimerCallback)
51  self.diagnostic_timer = rospy.Timer(rospy.Duration(1.0 / 10),
53  self.send_rate_service = rospy.Service('~set_send_rate', SetSendRate, self.setSendRate)
54 
55  def setSendRate(self, req):
56  try:
57  if self.event_driven:
58  rospy.logerr("failed to change send_rate. event_driven is enabled.")
59  return SetSendRateResponse(ok=False)
60  if self.send_timer.is_alive():
61  self.send_timer.shutdown()
62  self.send_rate = req.rate
63  rospy.set_param("~send_rate", self.send_rate)
64 # self.send_rate = rospy.get_param("~send_rate", 1.0)
65  rospy.loginfo("send_rate is set to %f" % self.send_rate)
66  self.send_timer = rospy.Timer(rospy.Duration(1.0 / self.send_rate),
67  self.sendTimerCallback)
68  return SetSendRateResponse(ok=True)
69  except Exception as e:
70  rospy.logerr("failed to set send_rate: %s" % e)
71  return SetSendRateResponse(ok=False)
72 
73  def diagnosticTimerCallback(self, event):
74  self.diagnostic_updater.update()
75  def diagnosticCallback(self, stat):
76  # always OK
77  stat.summary(DiagnosticStatus.OK, "OK")
78  with self.lock:
79  now = rospy.Time.now()
80  stat.add("Uptime [sec]",
81  (now - self.launched_time).to_sec())
82  stat.add("Time from the last sending [sec]",
83  (now - self.last_send_time).to_sec())
84  stat.add("Number of transmission", self.send_num)
85  stat.add("Time from the last input [sec]",
86  (now - self.last_input_received_time).to_sec())
87  # properties
88  stat.add("UDP address", self.to_ip)
89  stat.add("UDP port", self.to_port)
90  stat.add("EventDrivenMode", self.event_driven)
91  self.last_send_time_pub.publish(self.last_send_time)
92  self.last_input_received_time_pub.publish(self.last_input_received_time)
93  return stat
94  def messageCallback(self, msg):
95  with self.lock:
96  self.latest_message = msg
97  self.last_input_received_time = rospy.Time.now()
98  if self.event_driven:
99  self.sendMessage(msg)
100  def sendMessage(self, msg):
101  packed_data = packMessage(msg, self.send_format)
102  self.socket_client.sendto(packed_data, (self.to_ip, self.to_port))
103  self.last_send_time = rospy.Time.now()
104  self.send_num = self.send_num + 1
105  def sendTimerCallback(self, event):
106  with self.lock:
107  if self.latest_message:
108  rospy.logdebug("sending message")
109  self.sendMessage(self.latest_message)
110  self.latest_message = None
111  else:
112  rospy.loginfo("no message is available")
113 
114 if __name__ == "__main__":
115  rospy.init_node("silverhammer_lowspeed_streamer")
117  rospy.spin()
118 
119 
120 


jsk_network_tools
Author(s): Yusuke Furuta
autogenerated on Tue Feb 6 2018 03:45:07