In Smach, state is an important part of the state machine, and understanding the principle and implementation of States is helpful for using Smach, especially understanding
__init__ (), execute (), preempt is especially critical.
__init__ (): Initializes the function, initializes the parameter when the state is initialized
Execute (): function executed by State runtime
Preempt: Paused state
The following explains the state class with the following code:
1 ImportThreading2 ImportTraceback3 4 ImportSmach5 6 __all__= [' State','cbstate']7 8 classState (object):9 """state base class for SmachTen One There are two ways to interact with Smach states and Smach containers. The first is its output identifier outcome. A The second is run (execute), which is used to read in and output the user data (UserData). In the Execute () - before calling, you need to declare outcome and UserData in the constructor (__init__ ()) and make the appropriate checksum. - """ the def __init__(self, outcomes=[], input_keys=[], output_keys=[], io_keys=[]): - """State Constructors - @type Outcomes: array of strings - @param outcomes: Defines the output (outcomes) for this state. + - @type Input_keys: Array of strings + @param input_keys: At run time, the user data entered from the external keys. A at @type Output_keys: Array of strings - @param output_keys: At run time, the user data is output from the external keys. - - @type Io_keys: Array of strings - @param io_keys: At run time, the user data from the external input/output keys. - """ in #Store output results (outcomes) -Self._outcomes =Set (outcomes) to + #description of the storage user data interface -Self._input_keys = Set (Input_keys +Io_keys) theSelf._output_keys = Set (Output_keys +Io_keys) * $ #declaring a paused identifierPanax Notoginsengself._preempt_requested =False - the ## # Meat + defExecute (Self, UD): A """called when executing a state. Called when the status is executed the in the base class, the function throws an exception notimplementederror. + - @type UD: User data structure Body $ @param ud: The user data passed while the state is executing $ """ - RaiseNotimplementederror () - the ## # Smach Interface API - defregister_outcomes (Self, new_outcomes):Wuyi """adds a result label to the result set.""" theSelf._outcomes =self._outcomes.union (new_outcomes) - Wu defget_registered_outcomes (self): - """Gets the result set that has been registered to hit. About @rtype: Array of strings $ @return: An array that has been registered to hit the result string. - """ - returntuple (self._outcomes) - A ## # User Data API + defRegister_io_keys (self, keys): the """add keys to the Io_keys collection. - @type Keys: array of strings $ @param keys: Keys for input and output. the """ theSelf._input_keys =self._input_keys.union (keys) theSelf._output_keys =self._output_keys.union (keys) the - defRegister_input_keys (self, keys): in """add keys to the Input_keys collection. the @type Keys: List of strings the @param keys: The keys entered. About """ theSelf._input_keys =self._input_keys.union (keys) the the defGet_registered_input_keys (self): + """gets an array of input_keys that have already been registered.""" - returntuple (Self._input_keys) the Bayi defRegister_output_keys (self, keys): the """add keys to the Output_keys collection. the @type Keys: List of strings - @param keys: The keys for the output. - """ theSelf._output_keys =self._output_keys.union (keys) the the defGet_registered_output_keys (self): the """get an array that has been registered to hit the output keys.""" - returntuple (Self._output_keys) the the ## # Paused interface the defrequest_preempt (self):94 """set pause Request preempt_requested to True, when the state machine is paused, you need to set the state to False when the runtime the It needs to be set to true, otherwise it will stop running to that state. the """ theself._preempt_requested =True98 About defservice_preempt (self): - """set pause request preempt_requested to False"""101self._preempt_requested =False102 103 defrecall_preempt (self):104 """set pause request preempt_requested to False""" theself._preempt_requested =False106 107 defpreempt_requested (self):108 """returns True if paused."""109 returnself._preempt_requested the 111 classcbstate (state): the def __init__(Self, CB, cb_args=[], cb_kwargs={}, outcomes=[], input_keys=[], output_keys=[], io_keys=[]):113 """create a state from a function. the the @type Outcomes: array of strings the @param outcomes: The outcomes defined for the state.117 118 @type Input_keys: Array of strings119 @param input_keys: At run time, the user data entered from the external keys. - 121 @type Output_keys: Array of strings122 @param output_keys: At run time, the user data is output from the external keys.123 124 @type Io_keys: Array of strings the @param io_keys: At run time, the user data from the external input/output keys.126 """127State.__init__(self, outcomes, Input_keys, Output_keys, Io_keys) -SELF._CB =CB129Self._cb_args =Cb_args theSelf._cb_kwargs =Cb_kwargs131 the ifSmach.util.has_smach_interface (CB):133Self._cb_input_keys =Cb.get_registered_input_keys ()134Self._cb_output_keys =Cb.get_registered_output_keys ()135Self._cb_outcomes =cb.get_registered_outcomes ()136 137 Self.register_input_keys (Self._cb_input_keys)138 Self.register_output_keys (Self._cb_output_keys)139 self.register_outcomes (self._cb_outcomes) $ 141 defExecute (Self, UD):142 returnSELF._CB (UD, *self._cb_args, **self._cb_kwargs)
Smach----State class implementation and Chinese annotation