Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 import roslib.message; roslib.load_manifest('tug_ist_diagnosis_observers')
00024 import rospy
00025 import sys
00026 import xmlrpclib
00027 import os
00028 from std_msgs.msg import String
00029 from tug_ist_diagnosis_msgs.msg import Observations
00030 import time
00031 from array import array
00032 import thread
00033 import re
00034 import traceback
00035 import numpy
00036
00037 class General_Observer(object):
00038
00039 def __init__(self):
00040 rospy.init_node('GObs_Node', anonymous=True)
00041 self.m = xmlrpclib.ServerProxy(os.environ['ROS_MASTER_URI'])
00042 self.caller_id = '/script'
00043 self.obs_msg = []
00044 self.topic_type = ""
00045 self.topic_name = ""
00046 self.msg = ""
00047 self.prev_t = time.time()
00048 self.pub = rospy.Publisher('/observations', Observations)
00049 self.param_topic = rospy.get_param('~topic', '/Topic')
00050 self.param_frq = rospy.get_param('~frq', 10)
00051 self.param_dev = rospy.get_param('~dev', 1)
00052 self.param_ws = rospy.get_param('~ws', 10)
00053 self.param_mismatch_th = rospy.get_param('~mismatch_th', 5)
00054 self.circular_queu = [0.1 for i in xrange(self.param_ws)]
00055 self.delta_t = [0 for i in xrange(self.param_ws)]
00056 self.sum = 1
00057 self.count = 0
00058 self.mismatch_th_counter = 0;
00059 if self.param_topic[0] == '/':
00060 self.param_topic = self.param_topic[1:len(self.param_topic)]
00061 thread.start_new_thread(self.check_topic,(self.param_topic,1))
00062 thread.start_new_thread(self.make_output,(self.param_topic,0.5))
00063
00064
00065 def start(self):
00066 if self.param_topic[0] != '/':
00067 self.param_topic = "/%s" % (self.param_topic)
00068 self.topic_name = self.param_topic[1:len(self.param_topic)]
00069
00070 topic_found = False
00071 firstcheck = True
00072 while firstcheck:
00073 pubcode, statusMessage, topicList = self.m.getPublishedTopics(self.caller_id, "")
00074 for item in topicList:
00075 if item[0] == self.param_topic:
00076 self.param_topic = item[0]
00077 self.topic_type = item[1]
00078 topic_found = True
00079 if topic_found == True:
00080 firstcheck = False
00081 msg_class = roslib.message.get_message_class(self.topic_type)
00082 rospy.Subscriber(self.param_topic, msg_class, self.callback)
00083 rospy.spin()
00084 else:
00085 self.msg = '~ok('+self.topic_name+')'
00086 self.pub.publish(Observations(time.time(),[self.msg]))
00087 time.sleep(1)
00088
00089
00090
00091 def callback(self,data):
00092 self.delta_t.pop(0)
00093 curr_t = time.time()
00094 delta_t = curr_t - self.prev_t
00095
00096
00097 self.delta_t.append(delta_t)
00098
00099
00100
00101
00102
00103 self.prev_t = curr_t
00104
00105
00106
00107 def make_output(self,diff_freq,delay,*args):
00108 self.sum = 1
00109 while not rospy.is_shutdown():
00110 time.sleep(delay)
00111 s = numpy.sum(self.delta_t);
00112
00113 T = float(s)/self.param_ws
00114 if T == 0:
00115 T = 1
00116 f = 1/T
00117 print f,self.param_frq,abs(self.param_frq - f),self.param_dev,self.param_dev-abs(self.param_frq - f)
00118 obs_msg = []
00119 if abs(self.param_frq - f) < self.param_dev:
00120 if self.mismatch_th_counter > 0:
00121 self.mismatch_th_counter = self.mismatch_th_counter -1
00122 self.msg = 'ok('+self.topic_name+')'
00123 rospy.loginfo('ok('+self.topic_name+')')
00124 obs_msg.append(self.msg)
00125 self.pub.publish(Observations(time.time(),obs_msg))
00126 else:
00127 self.mismatch_th_counter = self.mismatch_th_counter + 1
00128 if self.mismatch_th_counter > self.param_mismatch_th:
00129 self.msg = '~ok('+self.topic_name+')'
00130 rospy.loginfo('~ok('+self.topic_name+')')
00131 obs_msg.append(self.msg)
00132 self.pub.publish(Observations(time.time(),obs_msg))
00133 else:
00134 self.msg = 'ok('+self.topic_name+')'
00135 rospy.loginfo('ok('+self.topic_name+')')
00136 obs_msg.append(self.msg)
00137 self.pub.publish(Observations(time.time(),obs_msg))
00138
00139
00140
00141
00142
00143 def average_delta_t(self):
00144 s = 0
00145 for val in self.circular_queu:
00146 s = s + val
00147 return s/float(self.param_ws)
00148
00149 def check_topic(self,string,sleeptime,*args):
00150 try:
00151 while not rospy.is_shutdown():
00152 t = 0
00153 m = xmlrpclib.ServerProxy(os.environ['ROS_MASTER_URI'])
00154 pubcode, statusMessage, topicList = m.getPublishedTopics(self.caller_id, "")
00155
00156 for item in topicList:
00157
00158 if item[0][1:] == string:
00159 t = 1
00160 if time.time() - self.prev_t > 2 :
00161 rospy.loginfo('~ok('+self.topic_name+')')
00162 self.pub.publish(Observations(time.time(),['~ok('+self.topic_name+')']))
00163 break
00164 if t == 0:
00165 t = 1
00166 self.msg = '~ok('+self.topic_name+')'
00167 rospy.loginfo('~ok('+self.topic_name+')')
00168 self.pub.publish(Observations(time.time(),[self.msg]))
00169 time.sleep(sleeptime)
00170 except:
00171 print "An unhandled exception occured, here's the traceback!"
00172 traceback.print_exc()
00173
00174
00175 def throws():
00176 raise RuntimeError('this is the error message')
00177
00178 def report_error(self):
00179 print '\nrosrun tug_ist_diagnosis_observers GObs.py <Topic_name> <Frequency> <FreqDeviation> <WindowSize>'
00180 print 'e.g rosrun tug_ist_diagnosis_observers GObs.py _topic:=scan _frq:=10 _dev:=1 _ws:=10'
00181 sys.exit(os.EX_USAGE)
00182
00183 if __name__ == '__main__':
00184 GObs = General_Observer()
00185 GObs.start()