ssm_action_server.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 import rospy
00019 import os
00020 import ssm_scxml_interpreter
00021 import ssm_introspection
00022 
00023 import actionlib
00024 import airbus_ssm_core.msg
00025 from std_msgs.msg import Empty, Bool 
00026 
00027 class ssmActionServer(object):
00028     _feedback = airbus_ssm_core.msg.SSMFeedback()
00029     _result = airbus_ssm_core.msg.SSMResult()
00030     
00031     def __init__(self):
00032         
00033         ##SSM variables
00034         self._SSM = None
00035         self._introspection = None
00036         self._server_name = rospy.get_param('~/ssm_server_name', '/ssm')
00037         self._preempt = False
00038         self._running = False
00039 
00040         self._as = actionlib.SimpleActionServer(self._server_name, airbus_ssm_core.msg.SSMAction,execute_cb=self.execute_cb, auto_start=False)
00041         self._as.register_preempt_callback(self.as_preempt_cb)
00042         self._as.start()
00043         
00044         rospy.loginfo("SSM Action Server is now ready !")
00045         
00046     def as_preempt_cb(self):
00047         self._SSM.request_preempt()
00048   
00049     def readSCXML(self, file):
00050         file_ = ssm_scxml_interpreter.get_pkg_dir_from_prefix(file)
00051         if(os.path.isfile(file_) == False):
00052             ##test file existence if only the name of the file was given (suppose to be in the resources dir of airbus_ssm_core
00053             path = "${airbus_ssm_core}/resources/"+file_+".scxml"
00054             file_ = ssm_scxml_interpreter.get_pkg_dir_from_prefix(path)
00055             if(os.path.isfile(file_) == False):
00056                 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)" 
00057                              %rospy.get_param('/ssm_node/scxml_file'))
00058                 return False
00059             
00060         try:
00061             interpreter = ssm_scxml_interpreter.ssmInterpreter(file_)
00062             self._SSM = interpreter.convertSCXML()
00063         except Exception as e:  
00064             rospy.logerr("[SSM] error during interpretation of the SCXML ")
00065             rospy.logerr(e)
00066             self._SSM = None
00067             return False
00068         try:
00069             self._SSM.check_consistency()
00070         except Exception as e:  
00071             rospy.logerr("[SSM] error during consistency checks.")
00072             rospy.logerr(e)
00073             self._SSM = None
00074             return False
00075 
00076         self._introspection = ssm_introspection.ssmIntrospection(self._SSM, self._as)
00077         self._introspection.start()
00078         rospy.loginfo("[SSM] : %s file loaded and created." %file)
00079         return True      
00080 
00081     def runSSM(self):
00082         if(self._SSM is not None):
00083             try:
00084                 outcome = self._SSM.execute()
00085             except Exception as e:
00086                 rospy.logerr(e)
00087                 self._introspection.stop()
00088                 self._running = False
00089                 self._SSM = None
00090                 outcome = "aborted"
00091             self._introspection.stop()
00092 
00093         self._SSM = None
00094         self._running = False
00095         return outcome
00096         
00097     def execute_cb(self, file):
00098         self._SSM = None
00099         self._feedback.current_active_states = "no one"
00100         self._result.outcome = "aborted"
00101         #loading the file
00102         rospy.loginfo("[SSM ActionServer] Received a new request : %s"%file.scxml_file)
00103         result = self.readSCXML(file.scxml_file)
00104         if(result == False):
00105             self._as.set_aborted(self._result,text="Error while loading the file : %s"%file.scxml_file)
00106             return
00107         
00108         rospy.loginfo("[SSM ActionServer] %s file loaded and state machine created. Will start the execution." %file.scxml_file)
00109         self._result.outcome = self.runSSM() 
00110         if(self._result.outcome == 'aborted'):
00111             self._as.set_aborted(self._result,text="Error during the execution.")
00112         if(self._result.outcome == 'preempt'):
00113             self._as.set_preempted(self._result,text="State Machine preempted.")
00114         else:
00115             self._as.set_succeeded(self._result,"SSM has been executed without error.")
00116         
00117 
00118     
00119     
00120         
00121     
00122 
00123         


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