packml_states.h
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  */
18 #pragma once
19 
20 #include "packml_sm/common.h"
21 #include "packml_sm/dlog.h"
24 
25 #include <boost/msm/front/state_machine_def.hpp>
26 #include <boost/msm/back/state_machine.hpp>
27 #include <future>
28 
29 namespace packml_sm
30 {
31 struct PackmlState : public boost::msm::front::state<>
32 {
33 public:
34  virtual std::string stateName() = 0;
35  virtual StatesEnum stateId() = 0;
36 
37  void setStateMethod(std::function<int()> state_method)
38  {
39  state_method_ = state_method;
40  }
41 
42  template <class FSM>
43  void runStateMethod(FSM* state_machine_ptr)
44  {
45  if (state_method_ != nullptr)
46  {
47  auto result = state_method_();
48  if (result == 0)
49  {
50  if (!is_exiting_)
51  {
52  state_machine_ptr->enqueue_event(state_complete_event());
53  }
54  }
55  else
56  {
57  DLog::LogInfo("Error running state method: %s", state_name_.c_str());
58  state_machine_ptr->enqueue_event(error_event());
59  }
60  }
61  else
62  {
63  state_machine_ptr->enqueue_event(state_complete_event());
64  }
65  }
66 
67  template <class Event, class FSM>
68  void on_entry(Event const& event, FSM& state_machine)
69  {
70  start_time_ = std::chrono::steady_clock::now();
71 
72  is_exiting_ = false;
73  is_running_ = true;
74  auto smi = dynamic_cast<StateChangeNotifier*>(&state_machine);
75  if (smi != nullptr)
76  {
78  }
79 
80  DLog::LogInfo("Entering: %s", stateName().c_str());
81 
82  state_method_future_ = std::async(
83  std::launch::async, std::bind(&PackmlState::runStateMethod<FSM>, this, std::placeholders::_1), &state_machine);
84  }
85 
86  template <class Event, class FSM>
87  void on_exit(Event const& event, FSM& state_machine)
88  {
89  is_exiting_ = true;
90  if (state_method_future_.valid())
91  {
93  }
94 
95  is_running_ = false;
96  DLog::LogInfo("Leaving: %s", stateName().c_str());
97  }
98 
99 private:
100  std::atomic<bool> is_running_;
101  std::atomic<bool> is_exiting_;
102  std::string state_name_;
103  std::function<int()> state_method_;
104  std::future<void> state_method_future_;
105  std::chrono::steady_clock::time_point start_time_;
106  double cummulative_time_ = 0.0f;
107 };
108 
109 struct Aborted_impl : public PackmlState
110 {
111 public:
112  std::string stateName()
113  {
114  return "Aborted";
115  }
116 
118  {
119  return StatesEnum::ABORTED;
120  }
121 };
122 
123 struct Clearing_impl : public PackmlState
124 {
125 public:
126  std::string stateName()
127  {
128  return "Clearing";
129  }
130 
132  {
133  return StatesEnum::CLEARING;
134  }
135 };
136 
137 struct Stopped_impl : public PackmlState
138 {
139 public:
140  std::string stateName()
141  {
142  return "Stopped";
143  }
144 
146  {
147  return StatesEnum::STOPPED;
148  }
149 };
150 
152 {
153 public:
154  std::string stateName()
155  {
156  return "Resetting";
157  }
158 
160  {
161  return StatesEnum::RESETTING;
162  }
163 };
164 
165 struct Idle_impl : public PackmlState
166 {
167 public:
168  std::string stateName()
169  {
170  return "Idle";
171  }
172 
174  {
175  return StatesEnum::IDLE;
176  }
177 };
178 
179 struct Starting_impl : public PackmlState
180 {
181 public:
182  std::string stateName()
183  {
184  return "Starting";
185  }
186 
188  {
189  return StatesEnum::STARTING;
190  }
191 };
192 
193 struct Execute_impl : public PackmlState
194 {
195 public:
196  std::string stateName()
197  {
198  return "Execute";
199  }
200 
202  {
203  return StatesEnum::EXECUTE;
204  }
205 };
206 
207 struct Holding_impl : public PackmlState
208 {
209 public:
210  std::string stateName()
211  {
212  return "Holding";
213  }
214 
216  {
217  return StatesEnum::HOLDING;
218  }
219 };
220 
221 struct Held_impl : public PackmlState
222 {
223 public:
224  std::string stateName()
225  {
226  return "Held";
227  }
228 
230  {
231  return StatesEnum::HELD;
232  }
233 };
234 
236 {
237 public:
238  std::string stateName()
239  {
240  return "UnHolding";
241  }
242 
244  {
245  return StatesEnum::UNHOLDING;
246  }
247 };
248 
250 {
251 public:
252  std::string stateName()
253  {
254  return "Suspending";
255  }
256 
258  {
259  return StatesEnum::SUSPENDING;
260  }
261 };
262 
264 {
265 public:
266  std::string stateName()
267  {
268  return "Suspended";
269  }
270 
272  {
273  return StatesEnum::SUSPENDED;
274  }
275 };
276 
278 {
279 public:
280  std::string stateName()
281  {
282  return "UnSuspending";
283  }
284 
286  {
288  }
289 };
290 
292 {
293 public:
294  std::string stateName()
295  {
296  return "Completing";
297  }
298 
300  {
301  return StatesEnum::COMPLETING;
302  }
303 };
304 
305 struct Complete_impl : public PackmlState
306 {
307 public:
308  std::string stateName()
309  {
310  return "Complete";
311  }
312 
314  {
315  return StatesEnum::COMPLETE;
316  }
317 };
318 
319 struct Aborting_impl : public PackmlState
320 {
321 public:
322  std::string stateName()
323  {
324  return "Aborting";
325  }
326 
328  {
329  return StatesEnum::ABORTING;
330  }
331 };
332 
333 struct Stopping_impl : public PackmlState
334 {
335 public:
336  std::string stateName()
337  {
338  return "Stopping";
339  }
340 
342  {
343  return StatesEnum::STOPPING;
344  }
345 };
346 }
std::function< int()> state_method_
void handleStateChangeNotify(const std::string &state_name, StatesEnum state_id)
virtual std::string stateName()=0
std::string stateName()
void on_exit(Event const &event, FSM &state_machine)
Definition: packml_states.h:87
static void LogInfo(const char *format,...)
Definition: dlog.cpp:27
std::atomic< bool > is_exiting_
void setStateMethod(std::function< int()> state_method)
Definition: packml_states.h:37
StatesEnum
Definition: common.h:35
virtual StatesEnum stateId()=0
std::atomic< bool > is_running_
void on_entry(Event const &event, FSM &state_machine)
Definition: packml_states.h:68
StatesEnum stateId()
void runStateMethod(FSM *state_machine_ptr)
Definition: packml_states.h:43
std::string stateName()
std::future< void > state_method_future_
StatesEnum stateId()
std::chrono::steady_clock::time_point start_time_


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