diagnosis_model_server.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 
00003 ##
00004 # Copyright (c).2012. OWNER: Institute for Software Technology, TU Graz Austria.
00005 # Authors: Safdar Zaman, Gerald Steinbauer. (szaman@ist.tugraz.at, steinbauer@ist.tugraz.at)
00006 # All rights reserved.
00007 #    This program is free software: you can redistribute it and/or modify
00008 #    it under the terms of the GNU General Public License as published by
00009 #    the Free Software Foundation, either version 3 of the License, or
00010 #    (at your option) any later version.
00011 #
00012 #    This program is distributed in the hope that it will be useful,
00013 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 #    GNU General Public License for more details.
00016 #
00017 #    You should have received a copy of the GNU General Public License
00018 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
00019 ##
00020 
00021 # The ModelActionServer calss acts like Diagnosis Model Server. It takes YAML
00022 # file as parameter. The YAML file is diagnosis_model.yaml file for the diagnosis system model.
00023 # and publishes data on /diagnosis_model topic compatible for our Model Based Diagnosis.
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 


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