abstract_state_machine.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
22 #include "common.h"
23 
24 #include <map>
25 #include <mutex>
26 #include <chrono>
27 #include <functional>
28 
29 namespace packml_sm
30 {
36 {
37 public:
46 
52  {
53  }
54 
60  virtual bool activate() = 0;
61 
67  virtual bool deactivate() = 0;
68 
75  virtual bool setStarting(std::function<int()> state_method) = 0;
76 
83  virtual bool setExecute(std::function<int()> state_method) = 0;
84 
91  virtual bool setCompleting(std::function<int()> state_method) = 0;
92 
99  virtual bool setAborting(std::function<int()> state_method) = 0;
100 
107  virtual bool setClearing(std::function<int()> state_method) = 0;
108 
115  virtual bool setStopping(std::function<int()> state_method) = 0;
116 
123  virtual bool setResetting(std::function<int()> state_method) = 0;
124 
131  virtual bool setSuspending(std::function<int()> state_method) = 0;
132 
139  virtual bool setUnsuspending(std::function<int()> state_method) = 0;
140 
147  virtual bool setHolding(std::function<int()> state_method) = 0;
148 
155  virtual bool setUnholding(std::function<int()> state_method) = 0;
156 
162  virtual bool isActive() = 0;
163 
170  {
171  return current_state_;
172  }
173 
179  double getIdleTime();
180 
186  double getStartingTime();
187 
193  double getResettingTime();
194 
200  double getExecuteTime();
201 
207  double getHeldTime();
208 
214  double getHoldingTime();
215 
221  double getUnholdingTime();
222 
228  double getSuspendedTime();
229 
235  double getSuspendingTime();
236 
242  double getUnsuspendingTime();
243 
249  double getCompleteTime();
250 
256  double getStoppedTime();
257 
263  double getClearingTime();
264 
270  double getStoppingTime();
271 
277  double getAbortedTime();
278 
284  double getAbortingTime();
285 
290  void resetStats();
291 
296  void incrementErrorStatItem(int16_t id, int32_t count, double duration);
297 
302  void incrementQualityStatItem(int16_t id, int32_t count, double duration);
303 
308  void incrementSuccessCount();
309 
314  void incrementFailureCount();
315 
321  void setIdealCycleTime(float ideal_cycle_time);
322 
328  virtual bool start();
329 
335  virtual bool clear();
336 
342  virtual bool reset();
343 
349  virtual bool hold();
350 
356  virtual bool unhold();
357 
363  virtual bool suspend();
364 
370  virtual bool unsuspend();
371 
377  virtual bool stop();
378 
384  virtual bool abort();
385 
391  void getCurrentStatSnapshot(PackmlStatsSnapshot& snapshot_out);
392 
393 protected:
400  void invokeStateChangedEvent(const std::string& name, StatesEnum value);
401 
406  virtual void _start() = 0;
407 
412  virtual void _clear() = 0;
413 
418  virtual void _reset() = 0;
419 
424  virtual void _hold() = 0;
425 
430  virtual void _unhold() = 0;
431 
436  virtual void _suspend() = 0;
437 
442  virtual void _unsuspend() = 0;
443 
448  virtual void _stop() = 0;
449 
454  virtual void _abort() = 0;
455 
456 private:
457  std::map<int16_t, PackmlStatsItemized> itemized_error_map_;
458  std::map<int16_t, PackmlStatsItemized> itemized_quality_map_;
459  int success_count_ = 0;
460  int failure_count_ = 0;
461  float ideal_cycle_time_ = 0.0;
463  std::recursive_mutex stat_mutex_;
465  std::map<StatesEnum, double> duration_map_;
466  std::chrono::steady_clock::time_point start_time_;
476  void incrementMapStatItem(std::map<int16_t, PackmlStatsItemized>& itemized_map, int16_t id, int32_t count,
477  double duration);
478 
484  void updateClock(StatesEnum new_state);
485 
492  double getStateDuration(StatesEnum state);
493 
499  double calculateTotalTime();
500 };
501 }
virtual void _clear()=0
Override to call implementations version of clear command.
void resetStats()
Reset all of the tracked states.
virtual bool unsuspend()
Call to send the unsuspend command.
virtual bool setClearing(std::function< int()> state_method)=0
Override to handle setting the clearing state method.
virtual void _start()=0
Override to call implementations version of start command.
virtual bool setResetting(std::function< int()> state_method)=0
Override to handle setting the resetting state method.
void setIdealCycleTime(float ideal_cycle_time)
Sets the ideal cycle time in operations per second.
virtual bool activate()=0
Override to handle activate command.
void incrementMapStatItem(std::map< int16_t, PackmlStatsItemized > &itemized_map, int16_t id, int32_t count, double duration)
adds or updates the specific itemized map
double getUnsuspendingTime()
Accessor for the duration spent in unsuspending.
std::map< int16_t, PackmlStatsItemized > itemized_error_map_
virtual void _stop()=0
Override to call implementations version of the stop command.
The StateMachineInterface class defines a implementation independent interface to a PackML state mach...
double getHeldTime()
Accessor for the duration spent in held.
void invokeStateChangedEvent(const std::string &name, StatesEnum value)
Call to invoke a state changed event.
void getCurrentStatSnapshot(PackmlStatsSnapshot &snapshot_out)
Fills the reference variable with the current stats snapshot.
double getUnholdingTime()
Accessor for the duration spent in unholding.
Container for the current packml statistics snap shot.
virtual bool start()
Call to send the start command.
std::chrono::steady_clock::time_point start_time_
double getCompleteTime()
Accessor for the duration spent in complete.
std::map< StatesEnum, double > duration_map_
EventHandler< AbstractStateMachine, StateChangedEventArgs > stateChangedEvent
double getSuspendingTime()
Accessor for the duration spent in suspending.
virtual void _unhold()=0
Override to call implementations version of the unhold command.
double getExecuteTime()
Accessor for the duration spent in execute.
virtual bool setStarting(std::function< int()> state_method)=0
Override to handle setting the starting state method.
virtual void _abort()=0
Override to call implementations version of the abort command.
double calculateTotalTime()
Accessor for the total duration of the state machine.
virtual bool suspend()
Call to send the suspend command.
void incrementQualityStatItem(int16_t id, int32_t count, double duration)
Call to increment or add a specific Itemized quality stat.
double getAbortedTime()
Accessor for the duration spent in aborted.
double getAbortingTime()
Accessor for the duration spent in aborting.
virtual bool setSuspending(std::function< int()> state_method)=0
Override to handle setting the suspending state method.
double getResettingTime()
Accessor for the duration spent in resetting.
virtual bool hold()
Call to send the hold command.
double getSuspendedTime()
Accessor for the duration spent in suspended.
double getStoppingTime()
Accessor for the duration spent in stopping.
virtual bool isActive()=0
Override to provide an accessor to whether the state machine is currently active. ...
virtual bool setAborting(std::function< int()> state_method)=0
Override to handle setting the aborting state method.
double getStoppedTime()
Accessor for the duration spent in stopped.
double getStartingTime()
Accessor for the duration spent in starting.
virtual void _hold()=0
Override to call implementations version of the hold command.
virtual void _unsuspend()=0
Override to call implementations version of the unsuspend command.
virtual bool stop()
Call to send the stop command.
StatesEnum
Definition: common.h:35
void incrementErrorStatItem(int16_t id, int32_t count, double duration)
Call to increment or add a specific Itemized error stat.
virtual bool setUnholding(std::function< int()> state_method)=0
Override to handle setting unholding state method.
double getClearingTime()
Accessor for the duration spent in clearing.
virtual bool setStopping(std::function< int()> state_method)=0
Override to handle setting the stopping state method.
virtual bool abort()
Call to send the abort command.
virtual bool unhold()
Call to send the unhold command.
virtual ~AbstractStateMachine()
Destructor for AbstractStateMachine.
virtual bool setCompleting(std::function< int()> state_method)=0
Override to handle setting the completing state method.
std::map< int16_t, PackmlStatsItemized > itemized_quality_map_
virtual bool clear()
Call to send the clear command.
void incrementFailureCount()
Call to increment the failed operation count.
virtual bool setHolding(std::function< int()> state_method)=0
Override to handle setting the holding state method.
double getHoldingTime()
Accessor for the duration spent in holding.
void incrementSuccessCount()
Call to increment the successful operation count.
virtual bool setUnsuspending(std::function< int()> state_method)=0
Override to handle setting the un-suspending state method.
StatesEnum getCurrentState() const
Accessor for the current state.
virtual bool deactivate()=0
Override to handle deactivate command.
virtual bool setExecute(std::function< int()> state_method)=0
Override to handle setting the execute state method.
virtual void _reset()=0
Override to call implementations version of the reset command.
virtual void _suspend()=0
Override to call implementations version of the suspend command.
double getStateDuration(StatesEnum state)
Accessor for the given state duration.
AbstractStateMachine()
Constructor for AbstractStateMachine.
double getIdleTime()
Accessor for the duration spent in idle time.
virtual bool reset()
Call to send the reset command.
void updateClock(StatesEnum new_state)
Updates all of the durations for the states based on the new state.


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