packml_state_machine.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (Apache License)
3  *
4  * Copyright (c) 2018 Plus One Robotics
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
20 
21 namespace packml_sm
22 {
23 template <typename T>
25 {
26  auto state_change_notifier = dynamic_cast<StateChangeNotifier*>(&boost_fsm_);
27  if (state_change_notifier != nullptr)
28  {
29  state_change_notifier->stateChangedEvent.bind_member_func(this, &PackmlStateMachine<T>::handleStateChanged);
30  }
31 
32  event_loop_.updateTickEvent.bind_member_func(this, &PackmlStateMachine<T>::update);
33 }
34 
35 template <typename T>
37 {
38  auto state_change_notifier = dynamic_cast<StateChangeNotifier*>(&boost_fsm_);
39  if (state_change_notifier != nullptr)
40  {
41  state_change_notifier->stateChangedEvent.unbind_member_func(this, &PackmlStateMachine<T>::handleStateChanged);
42  }
43 
44  event_loop_.updateTickEvent.unbind_member_func(this, &PackmlStateMachine<T>::update);
45 }
46 
47 template <typename T>
49 {
50  is_active_ = true;
51  event_loop_.start();
52  boost_fsm_.start();
53  return true;
54 }
55 
56 template <typename T>
58 {
59  is_active_ = false;
60  event_loop_.stop();
61  boost_fsm_.stop();
62  return true;
63 }
64 
65 template <typename T>
66 bool PackmlStateMachine<T>::setStarting(std::function<int()> state_method)
67 {
68  this->setStateMethod(StatesEnum::STARTING, state_method);
69 }
70 
71 template <typename T>
72 bool PackmlStateMachine<T>::setExecute(std::function<int()> state_method)
73 {
74  this->setStateMethod(StatesEnum::EXECUTE, state_method);
75 }
76 
77 template <typename T>
78 bool PackmlStateMachine<T>::setCompleting(std::function<int()> state_method)
79 {
80  this->setStateMethod(StatesEnum::COMPLETING, state_method);
81 }
82 
83 template <typename T>
84 bool PackmlStateMachine<T>::setAborting(std::function<int()> state_method)
85 {
86  this->setStateMethod(StatesEnum::ABORTING, state_method);
87 }
88 
89 template <typename T>
90 bool PackmlStateMachine<T>::setClearing(std::function<int()> state_method)
91 {
92  this->setStateMethod(StatesEnum::CLEARING, state_method);
93 }
94 
95 template <typename T>
96 bool PackmlStateMachine<T>::setStopping(std::function<int()> state_method)
97 {
98  this->setStateMethod(StatesEnum::STOPPING, state_method);
99 }
100 
101 template <typename T>
102 bool PackmlStateMachine<T>::setResetting(std::function<int()> state_method)
103 {
104  this->setStateMethod(StatesEnum::RESETTING, state_method);
105 }
106 
107 template <typename T>
108 bool PackmlStateMachine<T>::setSuspending(std::function<int()> state_method)
109 {
110  this->setStateMethod(StatesEnum::SUSPENDING, state_method);
111 }
112 
113 template <typename T>
114 bool PackmlStateMachine<T>::setUnsuspending(std::function<int()> state_method)
115 {
116  this->setStateMethod(StatesEnum::UNSUSPENDING, state_method);
117 }
118 
119 template <typename T>
120 bool PackmlStateMachine<T>::setHolding(std::function<int()> state_method)
121 {
122  this->setStateMethod(StatesEnum::HOLDING, state_method);
123 }
124 
125 template <typename T>
126 bool PackmlStateMachine<T>::setUnholding(std::function<int()> state_method)
127 {
128  this->setStateMethod(StatesEnum::UNHOLDING, state_method);
129 }
130 
131 template <typename T>
133 {
134  return is_active_;
135 }
136 
137 template <typename T>
139 {
140  sendCommand(CmdEnum::START);
141 }
142 
143 template <typename T>
145 {
146  sendCommand(CmdEnum::CLEAR);
147 }
148 
149 template <typename T>
151 {
152  sendCommand(CmdEnum::RESET);
153 }
154 
155 template <typename T>
157 {
158  sendCommand(CmdEnum::HOLD);
159 }
160 
161 template <typename T>
163 {
164  sendCommand(CmdEnum::UNHOLD);
165 }
166 
167 template <typename T>
169 {
170  sendCommand(CmdEnum::SUSPEND);
171 }
172 
173 template <typename T>
175 {
176  sendCommand(CmdEnum::UNSUSPEND);
177 }
178 
179 template <typename T>
181 {
182  sendCommand(CmdEnum::STOP);
183 }
184 
185 template <typename T>
187 {
188  sendCommand(CmdEnum::ABORT);
189 }
190 
191 template <typename T>
193 {
194  switch (command)
195  {
196  case CmdEnum::CLEAR:
197  boost_fsm_.enqueue_event(clear_event());
198  break;
199  case CmdEnum::START:
200  boost_fsm_.enqueue_event(start_event());
201  break;
202  case CmdEnum::STOP:
203  boost_fsm_.enqueue_event(stop_event());
204  break;
205  case CmdEnum::HOLD:
206  boost_fsm_.enqueue_event(hold_event());
207  break;
208  case CmdEnum::ABORT:
209  boost_fsm_.enqueue_event(abort_event());
210  break;
211  case CmdEnum::RESET:
212  boost_fsm_.enqueue_event(reset_event());
213  break;
214  case CmdEnum::SUSPEND:
215  boost_fsm_.enqueue_event(suspend_event());
216  break;
217  case CmdEnum::UNSUSPEND:
218  boost_fsm_.enqueue_event(unsuspend_event());
219  break;
220  case CmdEnum::UNHOLD:
221  boost_fsm_.enqueue_event(unhold_event());
222  break;
223  default:
224  DLog::LogError("Unsupported command requested.");
225  }
226 }
227 
228 template <typename T>
230 {
231  PackmlState* state_machine_state = nullptr;
232 
233  switch (state)
234  {
235  case StatesEnum::STOPPED:
236  state_machine_state = static_cast<PackmlState*>(boost_fsm_.template get_state<Stopped_impl*>());
237  break;
239  state_machine_state = static_cast<PackmlState*>(boost_fsm_.template get_state<Starting_impl*>());
240  break;
241  case StatesEnum::IDLE:
242  state_machine_state = static_cast<PackmlState*>(boost_fsm_.template get_state<Idle_impl*>());
243  break;
245  state_machine_state = static_cast<PackmlState*>(boost_fsm_.template get_state<Suspended_impl*>());
246  break;
247  case StatesEnum::EXECUTE:
248  state_machine_state = static_cast<PackmlState*>(boost_fsm_.template get_state<Execute_impl*>());
249  break;
251  state_machine_state = static_cast<PackmlState*>(boost_fsm_.template get_state<Stopping_impl*>());
252  break;
254  state_machine_state = static_cast<PackmlState*>(boost_fsm_.template get_state<Aborting_impl*>());
255  break;
256  case StatesEnum::ABORTED:
257  state_machine_state = static_cast<PackmlState*>(boost_fsm_.template get_state<Aborted_impl*>());
258  break;
259  case StatesEnum::HOLDING:
260  state_machine_state = static_cast<PackmlState*>(boost_fsm_.template get_state<Holding_impl*>());
261  break;
262  case StatesEnum::HELD:
263  state_machine_state = static_cast<PackmlState*>(boost_fsm_.template get_state<Held_impl*>());
264  break;
266  state_machine_state = static_cast<PackmlState*>(boost_fsm_.template get_state<Resetting_impl*>());
267  break;
269  state_machine_state = static_cast<PackmlState*>(boost_fsm_.template get_state<Suspending_impl*>());
270  break;
272  state_machine_state = static_cast<PackmlState*>(boost_fsm_.template get_state<UnSuspending_impl*>());
273  break;
275  state_machine_state = static_cast<PackmlState*>(boost_fsm_.template get_state<Clearing_impl*>());
276  break;
278  state_machine_state = static_cast<PackmlState*>(boost_fsm_.template get_state<UnHolding_impl*>());
279  break;
281  state_machine_state = static_cast<PackmlState*>(boost_fsm_.template get_state<Completing_impl*>());
282  break;
284  state_machine_state = static_cast<PackmlState*>(boost_fsm_.template get_state<Complete_impl*>());
285  break;
286  default:
287  DLog::LogError("Invalid state for setting state method");
288  }
289 
290  return state_machine_state;
291 }
292 
293 template <typename T>
294 bool PackmlStateMachine<T>::setStateMethod(StatesEnum state, std::function<int()> state_method)
295 {
296  PackmlState* state_machine_state = getPackmlState(state);
297 
298  if (state_machine_state != nullptr)
299  {
300  state_machine_state->setStateMethod(state_method);
301  return true;
302  }
303 
304  return false;
305 }
306 
307 template <typename T>
309 {
310  invokeStateChangedEvent(args.name, args.value);
311 }
312 
313 template <typename T>
315 {
316  if (boost_fsm_.get_message_queue_size() > 0)
317  {
318  boost_fsm_.execute_queued_events();
319  }
320 }
321 }
virtual bool setStarting(std::function< int()> state_method) override
Override to handle setting the starting state method.
virtual void _start() override
Override to call implementations version of start command.
void update(StateMachineEventLoop &event_loop, const EventArgs &args)
virtual bool setHolding(std::function< int()> state_method) override
Override to handle setting the holding state method.
PackmlState * getPackmlState(StatesEnum state)
virtual bool setCompleting(std::function< int()> state_method) override
Override to handle setting the completing state method.
virtual bool isActive() override
Override to provide an accessor to whether the state machine is currently active. ...
virtual void _reset() override
Override to call implementations version of the reset command.
virtual bool setClearing(std::function< int()> state_method) override
Override to handle setting the clearing state method.
virtual bool setStopping(std::function< int()> state_method) override
Override to handle setting the stopping state method.
virtual void _unsuspend() override
Override to call implementations version of the unsuspend command.
virtual bool activate() override
Override to handle activate command.
virtual bool setSuspending(std::function< int()> state_method) override
Override to handle setting the suspending state method.
virtual void _hold() override
Override to call implementations version of the hold command.
virtual bool setUnsuspending(std::function< int()> state_method) override
Override to handle setting the un-suspending state method.
virtual bool setResetting(std::function< int()> state_method) override
Override to handle setting the resetting state method.
void setStateMethod(std::function< int()> state_method)
Definition: packml_states.h:37
virtual void _abort() override
Override to call implementations version of the abort command.
StatesEnum
Definition: common.h:35
virtual void _stop() override
Override to call implementations version of the stop command.
virtual bool setAborting(std::function< int()> state_method) override
Override to handle setting the aborting state method.
void handleStateChanged(StateChangeNotifier &state_machine, const StateChangedEventArgs &args)
bool setStateMethod(StatesEnum state, std::function< int()> state_method)
virtual bool deactivate() override
Override to handle deactivate command.
virtual void _unhold() override
Override to call implementations version of the unhold command.
virtual void _clear() override
Override to call implementations version of clear command.
virtual void _suspend() override
Override to call implementations version of the suspend command.
static void LogError(const char *format,...)
Definition: dlog.cpp:37
virtual bool setExecute(std::function< int()> state_method) override
Override to handle setting the execute state method.
EventHandler< StateChangeNotifier, StateChangedEventArgs > stateChangedEvent
virtual bool setUnholding(std::function< int()> state_method) override
Override to handle setting unholding state method.


packml_sm
Author(s): Shaun Edwards
autogenerated on Fri Jul 12 2019 03:30:55