state.h
Go to the documentation of this file.
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 #ifndef PACKML_STATE_H
00020 #define PACKML_STATE_H
00021 
00022 #include <functional>
00023 
00024 #include <QtGui>
00025 #include "QState"
00026 #include "QEvent"
00027 #include "QAbstractTransition"
00028 
00029 #include "ros/console.h"
00030 #include "packml_sm/common.h"
00031 
00032 namespace packml_sm
00033 {
00034 
00035 struct PackmlState : public QState
00036 {
00037   Q_OBJECT
00038 
00039 public:
00040   PackmlState(StatesEnum state_value, QString name_value) :
00041     state_(state_value),
00042     name_(name_value),
00043     cummulative_time_(0) {}
00044 
00045   PackmlState(StatesEnum state_value, QString name_value, QState* super_state) :
00046     QState(super_state),
00047     state_(state_value),
00048     name_(name_value),
00049     cummulative_time_(0) {}
00050 
00051   StatesEnum state() const {return state_;}
00052   const QString name() const {return name_;}
00053 
00054 signals:
00055   void stateEntered(int value, QString name);
00056 
00057 protected:
00058   StatesEnum state_;
00059   QString name_;
00060 
00061   ros::Time enter_time_;
00062   ros::Time exit_time_;
00063   ros::Duration cummulative_time_;
00064 
00065   virtual void onEntry(QEvent *e);
00066   virtual void operation() {}
00067   virtual void onExit(QEvent *e);
00068 };
00069 
00070 
00071 
00072 struct WaitState : public PackmlState
00073 {
00074 public:
00075 
00076   static WaitState* Abortable()
00077   {
00078     return new WaitState(StatesEnum::ABORTABLE, CmdEnum::ABORT, "Abortable");
00079   }
00080   static WaitState* Stoppable(QState* abortable)
00081   {
00082     return new WaitState(StatesEnum::STOPPABLE, CmdEnum::ABORT, "Stoppable", abortable);
00083   }
00084   static WaitState* Idle(QState* stoppable)
00085   {
00086     return new WaitState(StatesEnum::IDLE, CmdEnum::START, "Idle", stoppable);
00087   }
00088   static WaitState* Held(QState* stoppable)
00089   {
00090     return new WaitState(StatesEnum::HELD, CmdEnum::UNHOLD, "Held", stoppable);
00091   }
00092   static WaitState* Complete(QState* stoppable)
00093   {
00094     return new WaitState(StatesEnum::COMPLETE, CmdEnum::RESET, "Complete", stoppable);
00095   }
00096   static WaitState* Suspended(QState* stoppable)
00097   {
00098     return new WaitState(StatesEnum::SUSPENDED, CmdEnum::UNSUSPEND, "Suspended", stoppable);
00099   }
00100   static WaitState* Stopped(QState* abortable)
00101   {
00102     return new WaitState(StatesEnum::STOPPED, CmdEnum::RESET, "Stopped", abortable);
00103   }
00104   static WaitState* Aborted()
00105   {
00106     return new WaitState(StatesEnum::ABORTED, CmdEnum::CLEAR, "Aborted");
00107   }
00108 
00109   WaitState(StatesEnum state_value, CmdEnum exit_cmd_value, QString name_value) :
00110     PackmlState(state_value, name_value),
00111     exit_cmd(exit_cmd_value)
00112   {}
00113 
00114   WaitState(StatesEnum state_value, CmdEnum exit_cmd_value, QString name_value, QState* super_state) :
00115     PackmlState(state_value, name_value, super_state),
00116     exit_cmd(exit_cmd_value)
00117   {}
00118 
00119 private:
00120   CmdEnum exit_cmd;
00121 };
00122 
00123 
00124 struct ActingState : public PackmlState
00125 {
00126 public:
00127 
00128   static ActingState* Resetting(QState* stoppable, int delay_ms_value = 200)
00129   {
00130     return new ActingState(StatesEnum::RESETTING, "Resetting", stoppable, delay_ms_value);
00131   }
00132   static ActingState* Starting(QState* stoppable, int delay_ms_value = 200)
00133   {
00134     return new ActingState(StatesEnum::STARTING, "Starting", stoppable, delay_ms_value);
00135   }
00136   static ActingState* Unholding(QState* stoppable, int delay_ms_value = 200)
00137   {
00138     return new ActingState(StatesEnum::UNHOLDING, "Un-Holding", stoppable, delay_ms_value);
00139   }
00140   static ActingState* Unsuspending(QState* stoppable, int delay_ms_value = 200)
00141   {
00142     return new ActingState(StatesEnum::UNSUSPENDING, "Un-Suspending", stoppable, delay_ms_value);
00143   }
00144   static ActingState* Holding(QState* stoppable, int delay_ms_value = 200)
00145   {
00146     return new ActingState(StatesEnum::HOLDING, "Holding", stoppable, delay_ms_value);
00147   }
00148   static ActingState* Suspending(QState* stoppable, int delay_ms_value = 200)
00149   {
00150     return new ActingState(StatesEnum::SUSPENDING, "Suspending", stoppable, delay_ms_value);
00151   }
00152   static ActingState* Execute(QState* stoppable, int delay_ms_value = 200)
00153   {
00154     return new ActingState(StatesEnum::EXECUTE, "Execute", stoppable, delay_ms_value);
00155   }
00156   static ActingState* Execute(QState* stoppable, std::function<int()> function_value)
00157   {
00158     return new ActingState(StatesEnum::EXECUTE, "Execute", stoppable, function_value);
00159   }
00160   static ActingState* Completing(QState* stoppable, int delay_ms_value = 200)
00161   {
00162     return new ActingState(StatesEnum::COMPLETING, "Completing", stoppable, delay_ms_value);
00163   }
00164   static ActingState* Aborting(int delay_ms_value = 200)
00165   {
00166     return new ActingState(StatesEnum::ABORTING, "Aborting", delay_ms_value);
00167   }
00168   static ActingState* Clearing(QState* abortable, int delay_ms_value = 200)
00169   {
00170     return new ActingState(StatesEnum::CLEARING, "Clearing", abortable, delay_ms_value);
00171   }
00172   static ActingState* Stopping(QState* abortable, int delay_ms_value = 200)
00173   {
00174     return new ActingState(StatesEnum::STOPPING, "Aborting", abortable, delay_ms_value);
00175   }
00176 
00177   ActingState(StatesEnum state_value, const char* name_value, int delay_ms_value = 200) :
00178     PackmlState(state_value, QString(name_value)),
00179     delay_ms(delay_ms_value)
00180   {}
00181 
00182   ActingState(StatesEnum state_value, const QString & name_value, QState* super_state, int delay_ms_value = 200) :
00183     PackmlState(state_value, name_value, super_state),
00184     delay_ms(delay_ms_value)
00185   {}
00186 
00187   ActingState(StatesEnum state_value, const char* name_value, QState* super_state, std::function<int()> function_value) :
00188     PackmlState(state_value, QString(name_value), super_state),
00189     function_(function_value)
00190   {}
00191 
00192   bool setOperationMethod(std::function<int()> function_value)
00193   {
00194     function_ = function_value;
00195     return true;
00196   }
00197 
00198   virtual void operation();
00199 
00200 protected:
00201 
00202   virtual void onEntry(QEvent *e);
00203   virtual void onExit(QEvent *e);
00204 
00205 private:
00206   int delay_ms;
00207   std::function<int()> function_;
00208   QFuture<void> function_state_;
00209 };
00210 
00211 typedef ActingState DualState;
00212 
00213 
00214 }
00215 
00216 #endif // PACKML_STATE_H


packml_sm
Author(s): Shaun Edwards
autogenerated on Sat Jun 8 2019 20:13:34