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
00024
00025
00026 import roslib; roslib.load_manifest('tug_ist_diagnosis_model')
00027 import rospy
00028 import actionlib
00029 import tug_ist_diagnosis_msgs.msg
00030 import time
00031 import sys
00032 import os
00033 import yaml
00034
00035 class ModelActionServer(object):
00036
00037 def __init__(self):
00038 self._feedback = tug_ist_diagnosis_msgs.msg.SystemModelFeedback()
00039 self._result = tug_ist_diagnosis_msgs.msg.SystemModelResult()
00040
00041 def start(self):
00042 rospy.init_node('diag_model_server_node', anonymous=True)
00043 self.param_file = rospy.get_param('~model', 'diagnosis_model.yaml')
00044 self._as = actionlib.SimpleActionServer('diagnosis_model_server', tug_ist_diagnosis_msgs.msg.SystemModelAction, execute_cb=self.execute_cb, auto_start = False)
00045 self.pub = rospy.Publisher('diagnosis_model', tug_ist_diagnosis_msgs.msg.SystemModelResult)
00046 print 'Diagnosis Model Server is up......'
00047 self._as.start()
00048
00049 def execute_cb(self, goal):
00050 try:
00051 file_ptr = open(self.param_file)
00052 except IOError:
00053 self.report_error()
00054 self.sys_modl = yaml.load(file_ptr)
00055 r = rospy.Rate(1)
00056 success = True
00057 rospy.loginfo('Request for System Model received:')
00058 time.sleep(2)
00059 if success:
00060 self._result.ab = self.sys_modl["ab"]
00061 self._result.nab = self.sys_modl["nab"]
00062 self._result.neg_prefix = self.sys_modl["neg_prefix"]
00063 self._result.rules = self.sys_modl["rules"]
00064 self._result.props = self.sys_modl["props"]
00065 r = self.sys_modl["rules"]
00066 p = self.sys_modl["props"]
00067 print "Rules:\n",r
00068 no_of_rules = len(r)
00069 print "Nos of Rules:",no_of_rules
00070 print "Propositions:\n",p
00071 no_of_props = len(p)
00072 print "Nos of Props:",no_of_props
00073 self.pub.publish(self._result)
00074 self._as.set_succeeded(self._result)
00075 rospy.loginfo('System Model sent successfully.')
00076
00077 def report_error(self):
00078 print '\nError:'
00079 print 'Either \'diagnosis_model.yaml\' does not exist or the path is wrong.'
00080 print 'Syntax: rosrun tug_ist_diagnosis_model diagnosis_model_server.py <Path_of_the_Yaml_file_parameter>'
00081 print 'e.g. rosrun tug_ist_diagnosis_model diagnosis_model_server.py _model:=path_to_file/diagnosis_model.yaml'
00082 print 'NOTE: \'tug_ist_diagnosis_model\' directory already contains \'diagnosis_model.yaml\' .'
00083 sys.exit(os.EX_USAGE)
00084
00085 if __name__ == '__main__':
00086 mdl = ModelActionServer()
00087 mdl.start()
00088 rospy.spin()
00089