00001 /* 00002 * Software License Agreement (Apache License) 00003 * 00004 * Copyright (c) 2016 Shaun Edwards 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 00020 #ifndef STATE_MACHINE_H 00021 #define STATE_MACHINE_H 00022 00023 #include <functional> 00024 00025 #include <QtGui> 00026 #include "QEvent" 00027 #include "QAbstractTransition" 00028 00029 #include "packml_sm/state.h" 00030 #include "packml_sm/transitions.h" 00031 00032 #include "ros/console.h" 00033 00034 namespace packml_sm 00035 { 00036 00041 class StateMachineInterface 00042 { 00043 public: 00044 00045 virtual bool activate()=0; 00046 virtual bool setStarting(std::function<int()> state_method)=0; 00047 virtual bool setExecute(std::function<int()> state_method)=0; 00048 virtual bool setCompleting(std::function<int()> state_method)=0; 00049 virtual bool setAborting(std::function<int()> state_method)=0; 00050 virtual bool setClearing(std::function<int()> state_method)=0; 00051 virtual bool setStopping(std::function<int()> state_method)=0; 00052 virtual bool setResetting(std::function<int()> state_method)=0; 00053 virtual bool setSuspending(std::function<int()> state_method)=0; 00054 virtual bool setUnsuspending(std::function<int()> state_method)=0; 00055 virtual bool setHolding(std::function<int()> state_method)=0; 00056 virtual bool setUnholding(std::function<int()> state_method)=0; 00057 virtual bool isActive()=0; 00058 virtual int getCurrentState()=0; 00059 00060 virtual bool start(); 00061 virtual bool clear(); 00062 virtual bool reset(); 00063 virtual bool hold(); 00064 virtual bool unhold(); 00065 virtual bool suspend(); 00066 virtual bool unsuspend(); 00067 virtual bool stop(); 00068 virtual bool abort(); 00069 00070 protected: 00071 00072 virtual void _start()=0; 00073 virtual void _clear()=0; 00074 virtual void _reset()=0; 00075 virtual void _hold()=0; 00076 virtual void _unhold()=0; 00077 virtual void _suspend()=0; 00078 virtual void _unsuspend()=0; 00079 virtual void _stop()=0; 00080 virtual void _abort()=0; 00081 00082 }; 00083 00084 00085 void init(int argc, char *argv[]); 00086 00087 00088 class StateMachine : public QObject, public StateMachineInterface 00089 { 00090 Q_OBJECT 00091 00092 public: 00093 00094 static std::shared_ptr<StateMachine> singleCyleSM(); 00095 static std::shared_ptr<StateMachine> continuousCycleSM(); 00096 00097 bool activate(); 00098 bool deactivate(); 00099 bool setIdle(std::function<int()> state_method); 00100 bool setStarting(std::function<int()> state_method); 00101 bool setExecute(std::function<int()> state_method); 00102 bool setCompleting(std::function<int()> state_method); 00103 bool setAborting(std::function<int()> state_method); 00104 bool setClearing(std::function<int()> state_method); 00105 bool setStopping(std::function<int()> state_method); 00106 bool setResetting(std::function<int()> state_method); 00107 bool setSuspending(std::function<int()> state_method); 00108 bool setUnsuspending(std::function<int()> state_method); 00109 bool setHolding(std::function<int()> state_method); 00110 bool setUnholding(std::function<int()> state_method); 00111 00112 bool isActive() 00113 { 00114 return sm_internal_.isRunning(); 00115 } 00116 00117 int getCurrentState() 00118 { 00119 return state_value_; 00120 } 00121 00122 virtual ~StateMachine(); 00123 00124 00125 protected: 00126 StateMachine(); 00127 virtual void _start(); 00128 virtual void _clear(); 00129 virtual void _reset(); 00130 virtual void _hold(); 00131 virtual void _unhold(); 00132 virtual void _suspend(); 00133 virtual void _unsuspend(); 00134 virtual void _stop(); 00135 virtual void _abort(); 00136 00137 int state_value_; 00138 QString state_name_; 00139 00140 WaitState* abortable_; 00141 WaitState* stoppable_; 00142 WaitState* held_; 00143 WaitState* idle_; 00144 WaitState* suspended_; 00145 WaitState* stopped_; 00146 WaitState* complete_; 00147 WaitState* aborted_; 00148 00149 ActingState* unholding_; 00150 ActingState* holding_; 00151 ActingState* starting_; 00152 ActingState* completing_; 00153 ActingState* resetting_; 00154 ActingState* unsuspending_; 00155 ActingState* suspending_; 00156 ActingState* stopping_; 00157 ActingState* clearing_; 00158 ActingState* aborting_; 00159 00160 DualState* execute_; 00161 00162 QStateMachine sm_internal_; 00163 00164 00165 00166 protected slots: 00167 void setState(int value, QString name); 00168 00169 signals: 00170 void stateChanged(int value, QString name); 00171 00172 }; 00173 00174 class ContinuousCycle : public StateMachine 00175 { 00176 Q_OBJECT 00177 00178 public: 00179 00180 ContinuousCycle(); 00181 virtual ~ContinuousCycle() {} 00182 00183 }; 00184 00185 class SingleCycle : public StateMachine 00186 { 00187 Q_OBJECT 00188 00189 public: 00190 00191 SingleCycle(); 00192 virtual ~SingleCycle() {} 00193 00194 }; 00195 00196 } 00197 00198 #endif //STATE_MACHINE_H