GObs.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 ##
00004 # GObs.py is a General Observer to observer a topic and provide observation on /observations topic.
00005 # It needs three parameters Required frequency, frequency deviation and window size.
00006 # Copyright (c).2012. OWNER: Institute for Software Technology, TU Graz Austria.
00007 # Authors: Safdar Zaman, Gerald Steinbauer. (szaman@ist.tugraz.at, steinbauer@ist.tugraz.at)
00008 # All rights reserved.
00009 #    This program is free software: you can redistribute it and/or modify
00010 #    it under the terms of the GNU General Public License as published by
00011 #    the Free Software Foundation, either version 3 of the License, or
00012 #    (at your option) any later version.
00013 #
00014 #    This program is distributed in the hope that it will be useful,
00015 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 #    GNU General Public License for more details.
00018 #
00019 #    You should have received a copy of the GNU General Public License
00020 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
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                                 #print self.param_topic, self.param_frq, self.param_dev, self.param_ws
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                                                 #self.sum = self.sum + delta_t
00096                                                 #self.count = self.count + 1
00097                                                 self.delta_t.append(delta_t)
00098                                                 #avg_delta_t = self.average_delta_t()
00099                                                 #calculated_freq = 1/float(avg_delta_t)
00100                                                 #print calculated_freq, avg_delta_t
00101                                                 #diff_freq = abs(self.param_frq - calculated_freq )
00102                                                 #self.make_output(diff_freq)
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                                                 #c = self.count
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                                                 #self.sum = 0
00139                                                 #self.count = 0
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                                                 #print topicList
00156                                                 for item in topicList:
00157                                                         #print item[0][1:],string
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) #sleep for a specified amount of time.
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()


tug_ist_diagnosis_observers
Author(s): Safdar Zaman, Gerald Steinbauer
autogenerated on Mon Jan 6 2014 11:51:27