SmartVehicle-Basis
SmartFactory
HoistCtrl.cpp
Go to the documentation of this file.
1 
14 #include "HoistCtrl.h"
15 //=====PUBLIC====================================================================================
16 HoistCtrl::HoistCtrl() : currentState(State::low), doActionFPtr(&HoistCtrl::doAction_low) {
17 }
18 
20  DBFUNCCALLln("HoistCtrl::loop()");
21  process((this->*doActionFPtr)()); //do actions
22 }
23 
24 void HoistCtrl::loop(Event currentEvent) {
25  DBFUNCCALLln("HoistCtrl::loop(Event)");
27  process((this->*doActionFPtr)()); //do actions
28 }
29 
31  return currentState;
32 }
33 //=====PRIVATE====================================================================================
35  DBFUNCCALL("HoistCtrl::process ")
36  DBEVENTln(decodeEvent(e));
37  switch (currentState) {
38  case State::low:
39  if (Event::Raise == e) {
40  exitAction_low(); // Exit-action current state
41  entryAction_raising(); // Entry-actions next state
42  } else if (Event::Error == e) {
43  exitAction_low(); // Exit-action current state
44  entryAction_errorState(); // Entry-actions next state
45  }
46  break;
47  case State::raising:
48  if (Event::PosReached == e) {
49  exitAction_raising(); // Exit-action current state
50  entryAction_high(); // Entry-actions next state
51  } else if (Event::Error == e) {
52  exitAction_raising(); // Exit-action current state
53  entryAction_errorState(); // Entry-actions next state
54  }
55  break;
56  case State::high:
57  if (Event::Lower == e) {
58  exitAction_high(); // Exit-action current state
59  entryAction_lowering(); // Entry-actions next state
60  } else if (Event::Error == e) {
61  exitAction_high(); // Exit-action current state
62  entryAction_errorState(); // Entry-actions next state
63  }
64  break;
65  case State::lowering:
66  if (Event::PosReached == e) {
67  exitAction_lowering(); // Exit-action current state
68  entryAction_low(); // Entry-actions next state
69  } else if (Event::Error == e) {
70  exitAction_lowering(); // Exit-action current state
71  entryAction_errorState(); // Entry-actions next state
72  }
73  break;
74  case State::errorState:
75  if (Event::Resume == e) {
76  exitAction_errorState(); // Exit-action current state
77  switch (lastStateBevorError) {
78  case State::low:
79  entryAction_low(); // Entry-actions next state
80  break;
81  case State::raising:
82  entryAction_raising(); // Entry-actions next state
83  break;
84  case State::high:
85  entryAction_high(); // Entry-actions next state
86  break;
87  case State::lowering:
88  entryAction_lowering(); // Entry-actions next state
89  break;
90  default:
91  break;
92  }
93  }
94  if (Event::Reset == e) {
95  exitAction_errorState(); // Exit-action current state
96  entryAction_resetState(); // Entry-actions next state
97  }
98  break;
99  case State::resetState:
100  if (Event::Resume == e) {
101  exitAction_resetState(); // Exit-action current state
102  entryAction_low(); // Entry-actions next state
103  }
104  break;
105  default:
106  break;
107  }
108 }
109 //==low==========================================================
111  DBSTATUSln("Hoist Entering State: low");
112  currentState = State::low; // state transition
114  //Entry-Action
115 }
116 
118  DBINFO1ln("Hoist State: low");
119  //Generate the Event
120  return Event::NoEvent;
121 }
122 
124  DBSTATUSln("Hoist Leaving State: low");
125 }
126 
127 //==raising==========================================================
129  DBSTATUSln("Hoist Entering State: raising");
130  currentState = State::raising; // state transition
132  //Entry-Action
133  pHoist.attach();
134 }
135 
137  DBINFO1ln("Hoist State: raising");
138  //Generate the Event
139  if (pHoist.raise()) {
140  return Event::PosReached;
141  }
142  return Event::NoEvent;
143 }
144 
146  DBSTATUSln("Hoist Leaving State: raising");
147  pHoist.detach();
148 }
149 
150 //==high==========================================================
152  DBSTATUSln("Hoist Entering State: high");
153  currentState = State::high; // state transition
155  //Entry-Action
156 }
157 
159  DBINFO1ln("Hoist State: high");
160  //Generate Event
161  return Event::NoEvent;
162 }
163 
165  DBSTATUSln("Hoist Leaving State: high");
166 }
167 
168 //==lowering==========================================================
170  DBSTATUSln("Hoist Entering State: lowering");
171  currentState = State::lowering; // state transition
173  //Entry-Action
174  pHoist.attach();
175 }
176 
178  DBINFO1ln("Hoist State: lowering");
179  //Generate the Event
180  if (pHoist.lower()) {
181  return Event::PosReached;
182  }
183  return Event::NoEvent;
184 }
185 
187  DBSTATUSln("Hoist Leaving State: lowering");
188  pHoist.detach();
189 }
190 
191 //==errorState========================================================
193  DBSTATUSln("Hoist Entering State: errorState");
195  currentState = State::errorState; // state transition
197  //Entry-Action
198 }
199 
201  DBINFO1ln("Hoist State: errorState");
202  //Generate the Event
203  return Event::NoEvent;
204 }
205 
207  DBSTATUSln("Hoist Leaving State: errorState");
208 }
209 
210 //==resetState========================================================
212  DBERROR("Entering State: resetState");
213  currentState = State::resetState; // state transition
215  pHoist.attach();
216 }
217 
219  DBINFO1ln("State: resetState");
220  //Generate the Event
221  if (pHoist.lower()) {
222  pHoist.detach();
223  }
224  return Event::NoEvent;
225 }
226 
228  DBSTATUSln("Leaving State: resetState");
229 }
230 
231 //============================================================================
232 //==Aux-Function==============================================================
234  switch (state) {
235  case State::low:
236  return "State::low";
237  break;
238  case State::raising:
239  return "State::raising";
240  case State::high:
241  return "State::high";
242  break;
243  case State::lowering:
244  return "State::lowering";
245  break;
246  case State::errorState:
247  return "State::errorState";
248  break;
249  case State::resetState:
250  return "State::resetState";
251  break;
252  default:
253  return "ERROR: No matching state";
254  break;
255  }
256 }
257 
259  switch (event) {
260  case Event::Raise:
261  return "Event::Raise";
262  break;
263  case Event::Lower:
264  return "Event::Lower";
265  break;
266  case Event::PosReached:
267  return "Event::PosReached";
268  break;
269  case Event::Error:
270  return "Event::Error";
271  break;
272  case Event::Resume:
273  return "Event::Resume";
274  break;
275  case Event::Reset:
276  return "Event::Reset";
277  break;
278  case Event::NoEvent:
279  return "Event::NoEvent";
280  break;
281  default:
282  return "ERROR: No matching event";
283  break;
284  }
285 }
Ext: Start Lower.
void exitAction_errorState()
exit action of the errorState state.
Definition: HoistCtrl.cpp:206
HoistCtrl::Event doAction_resetState()
main action of the resetState
Definition: HoistCtrl.cpp:218
void process(Event e)
changes the state of the FSM based on the event
Definition: HoistCtrl.cpp:34
void entryAction_high()
executes the entry action of the high state.
Definition: HoistCtrl.cpp:151
void detach()
detach servo from servopin
Definition: Hoist.h:76
Event currentEvent
holds the current event of the FSM
Definition: HoistCtrl.h:86
HoistCtrl::Event doAction_lowering()
executes the main action of the lowering state.
Definition: HoistCtrl.cpp:177
Contains the FSM for the Hoist.
Definition: HoistCtrl.h:27
String decodeEvent(Event event)
Decodes the Event-Enum and returns a description.
Definition: HoistCtrl.cpp:258
HoistCtrl()
Construct a new Hoist Ctrl object and initailize the currentState with low state.
Definition: HoistCtrl.cpp:16
void exitAction_high()
executes the exit action of the high state.
Definition: HoistCtrl.cpp:164
Ext: Start Raise.
HoistCtrl::Event doAction_high()
executes the main action of the high state.
Definition: HoistCtrl.cpp:158
const State getcurrentState()
Get the current State.
Definition: HoistCtrl.cpp:30
bool lower()
Lower the Hoist 1° per servodelay/call until it's at positionMin.
Definition: Hoist.cpp:59
void entryAction_lowering()
executes the entry action of the lowering state.
Definition: HoistCtrl.cpp:169
Ext.: Reset after Error occured.
Signal: Position reached.
void entryAction_raising()
executes the entry action of the raising state.
Definition: HoistCtrl.cpp:128
No event generated.
State currentState
holds the current state of the FSM
Definition: HoistCtrl.h:85
HoistCtrl::Event doAction_errorState()
main action of the errorState state.
Definition: HoistCtrl.cpp:200
void entryAction_errorState()
entry action of the errorState state.
Definition: HoistCtrl.cpp:192
Ext.: Resume after Error occured.
HoistCtrl::Event doAction_raising()
executes the main action of the raising state.
Definition: HoistCtrl.cpp:136
void exitAction_raising()
executes the exit action of the raising state.
Definition: HoistCtrl.cpp:145
String decodeState(State state)
Decodes the State-Enum and returns a description.
Definition: HoistCtrl.cpp:233
State lastStateBevorError
holds the last state of the FSM so it's possible to resume after error
Definition: HoistCtrl.h:84
Event
Enum holds all possible events.
Definition: HoistCtrl.h:34
void exitAction_resetState()
exit action of the resetState
Definition: HoistCtrl.cpp:227
bool raise()
Raise the Hoist 1° per servodelay/call until it's at positionMax.
Definition: Hoist.cpp:42
The Hoist-Controll class contains the FSM for the Hoist.
State
Enum holds all possible states for the Hoist.
Definition: HoistCtrl.h:47
Hoist pHoist
Hoist Object.
Definition: HoistCtrl.h:95
void loop()
Calls the do-function of the active state and hence generates Events.
Definition: HoistCtrl.cpp:19
Event(HoistCtrl::* doActionFPtr)(void)
Functionpointer to call the current states do-function.
Definition: HoistCtrl.h:93
void exitAction_lowering()
executes the exit action of the lowering state.
Definition: HoistCtrl.cpp:186
void entryAction_low()
executes the entry action of the low state.
Definition: HoistCtrl.cpp:110
void attach()
attache servo to servopin
Definition: Hoist.h:67
void exitAction_low()
executes the exit action of the low state.
Definition: HoistCtrl.cpp:123
HoistCtrl::Event doAction_low()
executes the main action of the low state.
Definition: HoistCtrl.cpp:117
void entryAction_resetState()
entry action of the resetState
Definition: HoistCtrl.cpp:211