Public Member Functions |
| StateMachine (int num_of_state) |
| Constructor.
|
virtual | ~StateMachine () |
void | setNOP (Callback call_back) |
| Set NOP function.
|
void | setListener (Listener *listener) |
| Set Listener Object.
|
bool | setEntryAction (State state, Callback call_back) |
| Set Entry action function.
|
bool | setPreDoAction (State state, Callback call_back) |
| Set PreDo action function.
|
bool | setDoAction (State state, Callback call_back) |
| Set Do action function.
|
bool | setPostDoAction (State state, Callback call_back) |
| Set PostDo action function.
|
bool | setExitAction (State state, Callback call_back) |
| Set Exit action function.
|
bool | setTransitionAction (Callback call_back) |
| Set state transition action function.
|
void | setStartState (States states) |
| Set the initial state.
|
States | getStates () |
| Get states.
|
State | getState () |
| Get current state.
|
bool | isIn (State state) |
| Check current state.
|
void | goTo (State state) |
| Transit State.
|
void | worker () |
| Worker function.
|
template<class State, class Listener, class States = StateHolder<State>, class Callback = void (Listener::*)(const States& states)>
class RTC_Utils::StateMachine< State, Listener, States, Callback >
State machine class.
StateMachine class is a class to realize a state machine.
Example: ActiveObject assumes to be an active object that has the state machine. There are three states such as INACTIVE, ACTIVE and ERROR state, and if you want to define Entry or Exit action, this class will realize as follows:
class ActiveObject
{
public:
enum MyState { INACTIVE, ACTIVE, ERROR };
typedef States<MyState> MyStates;
ActiveObject()
: m_sm(3)
{
m_sm.setNOP(&ActiveObject::nullAction);
m_sm.setListener(this);
m_sm.setExitAction(INACTIVE, &ActiveObject::inactiveExit);
:
m_sm.setPostDoAction(ERROR, &ActiveObject::errorPostDo);
m_sm.setTransitionAction(&ActiveObject:transition);
};
bool nullAction(MyStates st) {};
bool inactiveExit(MyStates st) {};
:
bool errorPostDo(MyStates st) {};
bool transition(MyStates st) {};
private:
StateMachine<MyState, bool, ActiveObject> m_sm;
};
If you want to give a class to some states, you must implement the class to satisfy the following conditions:
-
You must define states by enum.
-
Template arguments of StateMachine must be <type of state(MyState), listener object, state holder,callback function>
-
Constructor arguments of StateMachine must be the number of the states.
-
You must set the following action functions as a function of (Return function_name(States))
-
You must define a function that does not do anything and give with setNOP.
-
You must set actions to each state by set(Entry|PreDo|Do|PostDo|Exit)Action.
-
You should set actions at the state transition by setTransitionAction().
-
You must implement action at the transition based on given states, such as current state, next state and previous state.
-
You should change states by goTo() and check the state by isIn(state).
-
goTo() is a function that sets next state forcibly, therefore, to determine the next state, you must get current state and implement that logic.
In this class, you can define the following five actions for one state:
-
Entry action
-
PreDo action
-
Do action
-
PostDo action
-
Exit action
Transition action is an action invoked at the transition between any states, and you must define its behavior.
This class executes each action according to the following timing.
-
If the state is changed and transits(A->B) state,
(A:Exit)->|(state update:A->B)->(B:Entry)->(B:PreDo)->(B:Do)->(B:PostDo)
-
If the state is not changed and remains B state, (| shows a step's break) (B(n-1):PostDo)->|(B(n):PreDo)->(B(n):Do)->(B(n):PostDo)->|(B(n+1):PreDo) PreDo, Do and PostDo are executed over and over again.
-
If the state transits to itself
(B(n-1):PostDo)->(B(n-1):Exit)->|(B(n):Entry)->(B(n):PreDo)
Once Exit is invoked, Entry is executed, and then the same operation described above will be done from here on.
- Parameters
-
State | Type of the state |
Listener | Listener object for action |
States | State holder |
Callback | Callback function for action |
- Since
- 0.4.0