NimbRo ROS Soccer Package
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
statecontroller::StateController Class Reference

Base class for all state controllers. More...

#include <state_controller.h>

Inherited by sitting_demo::SittingDemoSC, standing_demo::StandingDemoSC, and statecontrollertest::DemoSC.

Public Member Functions

 StateController (const std::string &name)
 Default constructor.
 
virtual ~StateController ()
 Destructor.
 
action_t goToState (StatePtr state)
 Transition to the specified next state (for callback) More...
 
action_t terminate ()
 Terminate the execution of the state controller (for callback) More...
 
bool belongsToMe (StateConstPtr state) const
 Check whether a state is owned by the calling state controller.
 
bool isActive () const
 Return whether the state controller is active or not.
 
cycle_t getCycle () const
 Return the current cycle count (zero after reset)
 
StateConstPtr getCurState () const
 Return a constant reference to the current state.
 
ret_t init (StatePtr state)
 Reset and initialise the state controller with a particular state (callback-protected) More...
 
ret_t forceState (StatePtr state)
 Force a change of state of the state controller (callback-protected) More...
 
ret_t step ()
 Execute a single step of the state controller (callback-protected) More...
 
ret_t loop ()
 Execute state controller steps until either an error or clean termination occurs (callback-protected) More...
 

Public Attributes

const std::string name
 The human-friendly name of the state controller.
 

Protected Member Functions

virtual bool preStepCallback ()
 A callback that is executed at the very beginning of the step() function, right after the cycle count is incremented, whenever the current state is not null. More...
 
virtual void preActivateCallback (bool willCallActivate)
 A callback that is executed in the step() function immediately prior to the activation of a state (called with willCallActivate true if there is a new state to activate)
 
virtual void preExecuteCallback ()
 A callback that is executed in the step() function immediately prior to the execution of the current state (called without exception whenever there is a state to be executed)
 
virtual bool postExecuteCallback ()
 A callback that is executed in the step() function immediately after the execution of the current state (called without exception whenever there is a state that was just executed) More...
 
virtual void onTerminateCallback ()
 A callback that is executed in the step() function (before the resources are freed) when a state controller termination condition is encountered.
 
virtual void postStepCallback ()
 A callback that is executed at the very end of the step() function whenever the current state is not null (in particular, it is NOT called after onTerminateCallback() in the case of a termination condition, as termination resets the current state)
 

Friends

class State
 

Detailed Description

Base class for all state controllers.

All state controllers implemented using this library need to derive from this abstract base class, which provides the underlying functionality.

Member Function Documentation

ret_t StateController::forceState ( StatePtr  state)

Force a change of state of the state controller (callback-protected)

In current implementation, the only difference between the forceState() and init() functions is that forceState() leaves the internal cycle count of the state controller untouched. In particular, the current state queue is cleared, and all contained states are deactivated, including the current state.

This function should almost never be required. Perform all state and queue changes within the states as a general rule - only call forceState() if special circumstances mandate an external invasion on the state of the state controller. Use this function with care!

Parameters
stateThe state to transition the state controller into.
Returns
A ret_t return ID specifying whether the function was successful and performed the required action. Calling this function from a callback (SCR_BAD_CALLBACK), passing a null state (SCR_NULL_STATE), and/or passing a state that belongs to another state controller (SCR_NOT_MY_STATE) results in return values other than SCR_OK.
action_t StateController::goToState ( StatePtr  state)

Transition to the specified next state (for callback)

The transition itself occurs in the next step. This function sets state as the next and only element in the state queue. If state is null then this function is equivalent to calling terminate(). This function ignores calls that do not originate from a state callback.

Parameters
stateAn class instance of the new state to enter.
Returns
Always returns PROCEED_NEXT_STATE so that the following usage has the desired effect.
return sc->goToState(NewStateInstance<MyStateClass>(sc, args)); // In an execute() callback...
ret_t StateController::init ( StatePtr  state)

Reset and initialise the state controller with a particular state (callback-protected)

The init() function can be called at any time and be guaranteed to bring the state controller to a well-defined initial state, while releasing all the states and resources that it owned. It is recommended to always use this function with an inline NewStateInstance call, as opposed to creating a state, assigning it to a variable in the calling function and then passing this variable as the argument to the init() function. For example:

ret_t ret;
MyStateController sc;
ret = sc.init(NewStateInstance<MyState>(&sc, arg2, ...)); // Pass the newly created state directly to init() function (recommended)
Parameters
stateThe state instance to initialise the state controller with.
Returns
A ret_t return ID specifying whether the function was successful and performed the required action. Calling this function from a callback (SCR_BAD_CALLBACK), passing a null state (SCR_NULL_STATE), and/or passing a state that belongs to another state controller (SCR_NOT_MY_STATE) results in return values other than SCR_OK.
ret_t StateController::loop ( )

Execute state controller steps until either an error or clean termination occurs (callback-protected)

Returns
A ret_t return ID indicating the success of the function. If the state controller terminated cleanly then the function returns SCR_OK. Otherwise, this function echos the return code received from the offending internal call to step(). For more information refer to the statecontroller::SCReturnID enumeration.
virtual bool statecontroller::StateController::postExecuteCallback ( )
inlineprotectedvirtual

A callback that is executed in the step() function immediately after the execution of the current state (called without exception whenever there is a state that was just executed)

Returns
Return true if you want to force a state transition to the state at the head of the queue (which you can modify using the goToState() function for example), otherwise return false.
virtual bool statecontroller::StateController::preStepCallback ( )
inlineprotectedvirtual

A callback that is executed at the very beginning of the step() function, right after the cycle count is incremented, whenever the current state is not null.

Returns
Return true if you want to force a state transition to the state at the head of the queue (which you can modify using the goToState() function for example), otherwise return false.
ret_t StateController::step ( )

Execute a single step of the state controller (callback-protected)

Returns
A ret_t return ID indicating the success of the function and/or the resulting state of the state controller (refer to the statecontroller::SCReturnID enumeration).
action_t StateController::terminate ( )

Terminate the execution of the state controller (for callback)

Termination occurs in the calling step, and irrespective of the state of the queue and the action that is possibly subsequently returned from the execute() callback. This function only has an effect when used from within either the activate() or execute() callbacks of a state.

Returns
Always returns PROCEED_NEXT_STATE so that the following usage has the desired effect.
return sc->terminate(); // In an execute() callback...

The documentation for this class was generated from the following files: