silverhammer_lowspeed_receiver.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 os
9 import rospy
10 import signal
11 import sys
12 import roslib
13 from roslib.message import get_message_class
14 from std_msgs.msg import Time
15 import diagnostic_updater
16 from diagnostic_msgs.msg import DiagnosticStatus
17 
19  def __init__(self, server, buffer_size, format, message, pub):
20  self.server = server
21  self.format = format
22  self.pub = pub
23  self.message = message
24  self.buffer_size = buffer_size
25  def run(self):
26  recv_data, addr = self.server.recvfrom(self.buffer_size)
27  msg = unpackMessage(recv_data, self.format, self.message)
28  self.pub.publish(msg)
29  print "received:", msg
30 
32  def __init__(self):
33  message_class_str = rospy.get_param("~message",
34  "jsk_network_tools/FC2OCS")
35  try:
36  self.receive_message = get_message_class(message_class_str)
37  except:
38  raise Exception("invalid topic type: %s"%message_class_str)
39  self.lock = Lock()
40  self.launched_time = rospy.Time.now()
42  self.diagnostic_updater.setHardwareID("none")
43  self.diagnostic_updater.add("LowspeedReceiver", self.diagnosticCallback)
44  self.received_num = 0
45  self.receive_port = rospy.get_param("~receive_port", 1024)
46  self.receive_ip = rospy.get_param("~receive_ip", "127.0.0.1")
47  self.receive_buffer = rospy.get_param("~receive_buffer_size", 250)
48  self.socket_server = socket(AF_INET, SOCK_DGRAM)
49  self.socket_server.settimeout(None)
50  self.socket_server.bind((self.receive_ip, self.receive_port))
52  self.pub = rospy.Publisher("~output", self.receive_message)
53  self.last_received_time = rospy.Time(0)
54  self.last_received_time_pub = rospy.Publisher(
55  "~last_received_time", Time)
56  self.last_publish_output_time = rospy.Time(0)
57  self.last_publish_output_time_pub = rospy.Publisher(
58  "~last_publish_output_time", Time)
59  self.diagnostic_timer = rospy.Timer(rospy.Duration(1.0 / 10),
61  def diagnosticTimerCallback(self, event):
62  self.diagnostic_updater.update()
63  # and publish time
64  with self.lock:
65  self.last_publish_output_time_pub.publish(self.last_publish_output_time)
66  self.last_received_time_pub.publish(self.last_received_time)
67  def diagnosticCallback(self, stat):
68  # always OK
69  stat.summary(DiagnosticStatus.OK, "OK")
70  with self.lock:
71  now = rospy.Time.now()
72  stat.add("Uptime [sec]",
73  (now - self.launched_time).to_sec())
74  stat.add("Time from the last reception [sec]",
75  (now - self.last_received_time).to_sec())
76  stat.add("Time from the last publish ~output [sec]",
77  (now - self.last_publish_output_time).to_sec())
78  stat.add("UDP address", self.receive_ip)
79  stat.add("UDP port", self.receive_port)
80  return stat
81  def run(self):
82  while not rospy.is_shutdown():
83  recv_data, addr = self.socket_server.recvfrom(self.receive_buffer)
84  msg = unpackMessage(recv_data, self.receive_format,
85  self.receive_message)
86  with self.lock:
87  self.last_received_time = rospy.Time.now()
88  self.pub.publish(msg)
89  with self.lock:
90  self.last_publish_output_time = rospy.Time.now()
91  rospy.logdebug("received:", msg)
92 
93 if __name__ == "__main__":
94  rospy.init_node("silverhammer_lowspeed_receiver")
96  rc.run()
def unpackMessage(data, fmt, message_class)
def __init__(self, server, buffer_size, format, message, pub)


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