OpenRTM-aist  2.0.1
RTObjectStateMachine.h
Go to the documentation of this file.
1 // -*- C++ -*-
19 #ifndef RTC_IMPL_RTOBJECTSTATEMACHINE_H
20 #define RTC_IMPL_RTOBJECTSTATEMACHINE_H
21 
22 #include <cstdlib>
23 #include <rtm/SystemLogger.h>
24 #include <coil/TimeMeasure.h>
25 #include <rtm/idl/RTCSkel.h>
26 #include <rtm/StateMachine.h>
27 #include <cassert>
28 #include <iostream>
29 #include <atomic>
30 
31 #define NUM_OF_LIFECYCLESTATE 4
32 namespace RTC
33 {
34  class RTObject_impl;
35 } // namespace RTC
36 namespace RTC_impl
37 {
38  using Ecid = RTC::ExecutionContextHandle_t;
39  using ExecContextState = RTC::LifeCycleState;
41 
43  {
44  public:
45  RTObjectStateMachine(RTC::ExecutionContextHandle_t id,
48  virtual ~RTObjectStateMachine();
49  RTObjectStateMachine& operator=(RTObjectStateMachine const&) = delete;
50 
51  // functions for stored RTObject reference
52  RTC::LightweightRTObject_ptr getRTObject();
53  bool isEquivalent(RTC::LightweightRTObject_ptr comp);
54 
55  RTC::ExecutionContextHandle_t getExecutionContextHandle();
56 
57  // RTC::ComponentAction operations
58  void onStartup();
59  void onShutdown();
60  void onActivated(const ExecContextStates& st);
61  void onDeactivated(const ExecContextStates& st);
62  void onAborting(const ExecContextStates& st);
63  void onError(const ExecContextStates& st);
64  void onReset(const ExecContextStates& st);
65 
66  // RTC::DataflowComponentAction
67  void onExecute(const ExecContextStates& st);
68  void onStateUpdate(const ExecContextStates& st);
69  RTC::ReturnCode_t onRateChanged();
70 
71  // FsmParticipantAction
72  void onAction(const ExecContextStates& st);
73 
74  // MultiModeComponentAction
75  void onModeChanged(const ExecContextStates& st);
76 
77  // Getting state of the context
78  ExecContextState getState();
79  ExecContextStates getStates();
80  bool isCurrentState(ExecContextState state);
81  bool isNextState(ExecContextState state);
82  void goTo(ExecContextState state);
83 
84  // Workers
85  void workerPreDo();
86  void workerDo();
87  void workerPostDo();
88 
89  bool activate();
90  bool deactivate();
91  bool reset();
92 
93  protected:
94  void setComponentAction(RTC::LightweightRTObject_ptr comp);
95  void setDataFlowComponentAction(RTC::LightweightRTObject_ptr comp);
96  void setFsmParticipantAction(RTC::LightweightRTObject_ptr comp);
97  void setMultiModeComponentAction(RTC::LightweightRTObject_ptr comp);
98  void updateState();
99 
100  private: // member variables
101  RTC::Logger rtclog;
102  // ExecutionContext id
103  RTC::ExecutionContextHandle_t m_id;
104  // Associated RTObject reference
106  // State machine
108  RTObjectStateMachine> m_sm;
109  bool m_ca;
110  bool m_dfc;
111  bool m_fsm;
112  bool m_mode;
113  RTC::ComponentAction_var m_caVar;
114  RTC::DataFlowComponentAction_var m_dfcVar;
115  RTC::FsmParticipantAction_var m_fsmVar;
116  RTC::MultiModeComponentAction_var m_modeVar;
117  RTC::RTObject_impl* m_rtobjPtr;
118  bool m_measure;
119  // Component action invoker
120  coil::TimeMeasure m_svtMeasure;
121  coil::TimeMeasure m_refMeasure;
122  std::atomic<bool> m_activation;
123  std::atomic<bool> m_deactivation;
124  std::atomic<bool> m_reset;
125  };
126 } // namespace RTC_impl
127 
128 #endif // RTC_RTOBJECTSTATEMACHINE_H
RTC::ReturnCode_t reset(RTC::RTObject_ptr rtc, RTC::UniqueId ec_id=0)
RT-Component.
RTC::ExecutionContextHandle_t Ecid
Definition: RTObjectStateMachine.h:38
RTC::LifeCycleState ExecContextState
Definition: RTObjectStateMachine.h:39
RTC::LightweightRTObject_var LightweightRTObject_var
Definition: ExecutionContextWorker.h:38
RT component logger class.
Definition: ExecutionContextProfile.h:28
RTC::ReturnCode_t activate(RTC::RTObject_ptr rtc, RTC::UniqueId ec_id=0)
RT-Component class.
Definition: RTObject.h:90
RTC::LightweightRTObject_ptr LightweightRTObject_ptr
Definition: ExecutionContextWorker.h:37
State machine class.
Definition: StateMachine.h:266
Logger class.
Definition: SystemLogger.h:99
Definition: RTObjectStateMachine.h:42
RTC::ReturnCode_t deactivate(RTC::RTObject_ptr rtc, RTC::UniqueId ec_id=0)
State machine template class.