ssm_state.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 import traceback
00020 import rospy
00021 import smach
00022 
00023 __all__ = ['State','CBState']
00024 
00025 class ssmState(smach.State):
00026     
00027     def __init__(self, outcomes=[], io_keys=[]):
00028         io_keys.append("logfile")
00029         io_keys.append("skill")
00030         smach.State.__init__(self, outcomes, io_keys=io_keys)
00031         self.register_outcomes(["preempt"])
00032         self._datamodel = {}
00033         self._onEntry = None
00034         self._onExit  = None
00035     
00036     def execute(self, ud):
00037         
00038         if self.preempt_requested():
00039             self.service_preempt()
00040             return "preempt"
00041         
00042         ## Copy the datamodel's value into the userData
00043         for data in self._datamodel:
00044             if(self._datamodel[data] != ""):
00045                 ud[data] = self._datamodel[data]
00046         
00047         ## Do the <onentry>
00048         if(self._onEntry is not None):
00049             try:
00050                 self._onEntry.execute(ud)
00051             except Exception as ex:
00052                 rospy.logerr('%s::onEntry::execute() raised | %s'
00053                              %(self.__class__.__name__,str(ex)))
00054                 return "preempt"
00055         
00056         ## Execution    
00057         try:
00058             outcome = self.execution(ud)
00059         except Exception as ex:
00060             rospy.logerr('%s::execute() raised | %s'
00061                              %(self.__class__.__name__,str(ex)))
00062             return "preempt"
00063         finally:
00064             if self.preempt_requested():
00065                 self.service_preempt()
00066                 return "preempt"
00067         
00068         ## Do the <onexit>    
00069         if(self._onExit is not None):
00070             try:
00071                 outcome = self._onExit.execute(ud, outcome)
00072             except Exception as ex:
00073                 rospy.logerr('%s::onExit::execute() raised | %s'
00074                              %(self.__class__.__name__,str(ex)))
00075                 return "preempt"
00076         
00077         return outcome
00078     
00079     def execution(self, ud):
00080         """Called when executing a state.
00081         In the base class this raises a NotImplementedError.
00082 
00083         @type ud: L{UserData} structure
00084         @param ud: Userdata for the scope in which this state is executing
00085         """
00086         raise NotImplementedError()
00087     
00088 class EmptyState(ssmState):
00089     def __init__(self):
00090         ssmState.__init__(self,outcomes=["next"])
00091         
00092     def execution(self, ud):
00093         rospy.sleep(2)
00094         ud.skill = "Empty"
00095         return "next"


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