ssm_main.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # Copyright 2015 Airbus
00004 # Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00005 #
00006 # Licensed under the Apache License, Version 2.0 (the "License");
00007 # you may not use this file except in compliance with the License.
00008 # You may obtain a copy of the License at
00009 #
00010 #   http://www.apache.org/licenses/LICENSE-2.0
00011 #
00012 # Unless required by applicable law or agreed to in writing, software
00013 # distributed under the License is distributed on an "AS IS" BASIS,
00014 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 # See the License for the specific language governing permissions and
00016 # limitations under the License.
00017 
00018 
00019 
00020 import ssm_scxml_interpreter
00021 import ssm_introspection
00022 import ssm_graphviz
00023 import airbus_ssm_core.srv
00024 from std_msgs.msg import Int8, Empty, Bool
00025 
00026 import rospy
00027 import sys
00028 import os
00029 import smach_ros
00030 
00031 class ssmMain:
00032     
00033     def __init__(self):
00034         self._SSM = None
00035         self._introspection = None
00036         self._server_name = rospy.get_param('ssm_server_name', '/ssm')
00037         self._init_srv = rospy.Service(self._server_name + '/srv/init',airbus_ssm_core.srv.SSM_init, self._init_SSM_srv)
00038         self._start_sub = rospy.Subscriber(self._server_name + '/start',Empty,self.start, queue_size=1)
00039         self._init_sub = rospy.Subscriber(self._server_name + '/init', Empty, self._init_SSM_cb, queue_size=1)
00040         self._preempt_sub = rospy.Subscriber(self._server_name + '/preempt',Empty, self._preempt_cb, queue_size=1)
00041         self._pause_sub = rospy.Subscriber(self._server_name + '/pause', Bool, self._pause_cb, queue_size=1)
00042         self._status_pub = rospy.Publisher(self._server_name + '/status', Int8, queue_size=1)
00043         self._preempt = False
00044         self._loading = False
00045         rospy.loginfo("SSM is now ready !")
00046         
00047     def _init_SSM_cb(self, msg):
00048         if(self._loading == False):
00049             self._loading = True
00050             self._preempt = False
00051             self._init_SSM()
00052             
00053     def _init_SSM_srv(self, msg):
00054         rospy.set_param('/ssm_node/scxml_file', str(msg.file_scxml.data))
00055         response = Bool()
00056         response.data = False
00057         if(self._loading == False):
00058             self._loading = True
00059             self._preempt = False
00060             response.data = self._init_SSM()
00061              
00062         return response
00063         
00064     def _init_SSM(self):
00065         result = self.readSCXML()
00066         if(result):
00067             self._status_pub.publish(1)
00068         else:
00069             self._status_pub.publish(-10)
00070         self._loading = False
00071         return result
00072         
00073     def readSCXML(self):
00074         
00075         ##test file existence if the whole path has been given
00076         file = ssm_scxml_interpreter.get_pkg_dir_from_prefix(rospy.get_param('/ssm_node/scxml_file'))
00077         if(os.path.isfile(file) == False):
00078             ##test file existence if only the name of the file was given (suppose to be in the resources dir of airbus_ssm_core
00079             path = "${airbus_ssm_core}/resources/"+rospy.get_param('/ssm_node/scxml_file')+".scxml"
00080             file = ssm_scxml_interpreter.get_pkg_dir_from_prefix(path)
00081             if(os.path.isfile(file) == False):
00082                 rospy.logerr("[SSM] %s not found. Either give only the name of file without the scxml extension and put it in the resource folder.\n Or give the full path (${pkg}/dir/file.scxml)" 
00083                              %rospy.get_param('/ssm_node/scxml_file'))
00084                 return False
00085         try:
00086             interpreter = ssm_scxml_interpreter.ssmInterpreter(file)
00087             self._SSM = interpreter.convertSCXML()
00088         except Exception as e:  
00089             rospy.logerr("[SSM] error during interpretation of the SCXML ")
00090             rospy.logerr(e)
00091             self._SSM = None
00092             return False
00093 
00094         if self._SSM is not None:
00095             try:
00096                 self._SSM.check_consistency()
00097             except Exception as e:  
00098                 rospy.logerr("[SSM] error during consistency checks.")
00099                 rospy.logerr(e)
00100                 self._SSM = None
00101                 return False
00102         else:
00103             return False
00104         
00105         self._graphviz = ssm_graphviz.ssmGraph(self._SSM)
00106         self._introspection = ssm_introspection.ssmIntrospection(self._SSM)
00107         self._introspection.start()
00108         self._graphviz.start()
00109         rospy.sleep(1)##temp fix
00110         rospy.loginfo("[SSM] : %s file loaded and created." %file)
00111         return True      
00112     
00113     def start(self, msg):
00114         if(self._SSM is not None):
00115             self._status_pub.publish(2)
00116             try:
00117                 self._SSM.execute()
00118             except Exception as e:
00119                 self._status_pub.publish(-2)
00120                 rospy.logerr(e)
00121                 self._introspection.stop()
00122                 self._graphviz.stop()
00123                 return
00124             if(self._preempt == False):
00125                 self._status_pub.publish(10)
00126                 rospy.loginfo("[SSM] : Finished without error")
00127             self._introspection.stop()
00128             self._graphviz.stop()
00129         else:
00130             rospy.logwarn("[SSM] : Start requested but there is no state machine loaded !")
00131             
00132     def _preempt_cb(self, msg):
00133         self._preempt = True
00134         self._status_pub.publish(-2)
00135         
00136     def _pause_cb(self, msg):
00137         if(msg.data):
00138             self._status_pub.publish(-1)
00139         else:
00140             self._status_pub.publish(2)
00141     
00142     
00143     
00144         
00145     
00146 
00147         


airbus_ssm_core
Author(s): Ludovic Delval
autogenerated on Thu Jun 6 2019 17:59:28