PeriodicExecutionContext.h
Go to the documentation of this file.
1 // -*- C++ -*-
20 #ifndef RTC_PERIODICEXECUTIONCONTEXT_H
21 #define RTC_PERIODICEXECUTIONCONTEXT_H
22 
23 #include <coil/Task.h>
24 #include <coil/Mutex.h>
25 #include <coil/Condition.h>
26 #include <vector>
27 #include <iostream>
28 
29 #include <rtm/RTC.h>
30 #include <rtm/idl/RTCSkel.h>
31 #include <rtm/idl/OpenRTMSkel.h>
32 #include <rtm/Manager.h>
33 #include <rtm/StateMachine.h>
35 
36 #define NUM_OF_LIFECYCLESTATE 4
37 
38 #ifdef WIN32
39 #pragma warning( disable : 4290 )
40 #endif
41 
42 namespace RTC
43 {
65  : public virtual ExecutionContextBase,
66  public coil::Task
67  {
69  public:
90 
112  PeriodicExecutionContext(OpenRTM::DataFlowComponent_ptr owner,
113  double rate = 1000.0);
114 
128  virtual ~PeriodicExecutionContext(void);
129 
149  virtual ExecutionContextService_ptr getObjRef(void) {return m_ref;}
150 
175  virtual int open(void *args);
176 
196  virtual int svc(void);
197 
225  virtual int close(unsigned long flags);
226 
227  //============================================================
228  // ExecutionContext
229  //============================================================
254  virtual CORBA::Boolean is_running(void)
255  throw (CORBA::SystemException);
256 
284  virtual ReturnCode_t start(void)
285  throw (CORBA::SystemException);
286 
313  virtual ReturnCode_t stop(void)
314  throw (CORBA::SystemException);
315 
336  virtual CORBA::Double get_rate(void)
337  throw (CORBA::SystemException);
338 
368  virtual ReturnCode_t set_rate(CORBA::Double rate)
369  throw (CORBA::SystemException);
370 
404  virtual ReturnCode_t activate_component(LightweightRTObject_ptr comp)
405  throw (CORBA::SystemException);
406 
439  virtual ReturnCode_t deactivate_component(LightweightRTObject_ptr comp)
440  throw (CORBA::SystemException);
441 
473  virtual ReturnCode_t reset_component(LightweightRTObject_ptr comp)
474  throw (CORBA::SystemException);
475 
502  virtual LifeCycleState get_component_state(LightweightRTObject_ptr comp)
503  throw (CORBA::SystemException);
504 
524  virtual ExecutionKind get_kind(void)
525  throw (CORBA::SystemException);
526 
558  virtual ReturnCode_t add_component(LightweightRTObject_ptr comp)
559  throw (CORBA::SystemException);
560 
579 
610  virtual ReturnCode_t remove_component(LightweightRTObject_ptr comp)
611  throw (CORBA::SystemException);
612 
632  virtual ExecutionContextProfile* get_profile(void)
633  throw (CORBA::SystemException);
634 
635  protected:
636  //============================================================
637  // DFPBase
638  //============================================================
640  /*
641  enum ExecContextState
642  {
643  INACTIVE_STATE,
644  ACTIVE_STATE,
645  ERROR_STATE,
646  };
647  */
649 
670  class DFPBase
671  {
672  public:
673 
693  {
694  m_sm.setListener(this);
695  m_sm.setEntryAction (ACTIVE_STATE, &DFPBase::on_activated);
696  m_sm.setDoAction (ACTIVE_STATE, &DFPBase::on_execute);
697  m_sm.setPostDoAction(ACTIVE_STATE, &DFPBase::on_state_update);
698  m_sm.setExitAction (ACTIVE_STATE, &DFPBase::on_deactivated);
699  m_sm.setEntryAction (ERROR_STATE, &DFPBase::on_aborting);
700  m_sm.setDoAction (ERROR_STATE, &DFPBase::on_error);
701  m_sm.setExitAction (ERROR_STATE, &DFPBase::on_reset);
702 
703  ECStates st;
704  st.prev = INACTIVE_STATE;
705  st.curr = INACTIVE_STATE;
706  st.next = INACTIVE_STATE;
707  m_sm.setStartState(st);
708  m_sm.goTo(INACTIVE_STATE);
709  }
710 
724  virtual ~DFPBase(void){}
725 
742  virtual void on_startup(void) = 0;
743 
760  virtual void on_shutdown(void) = 0;
761 
783  virtual void on_activated(const ECStates& st) = 0;
784 
806  virtual void on_deactivated(const ECStates& st) = 0;
807 
829  virtual void on_aborting(const ECStates& st) = 0;
830 
856  virtual void on_error(const ECStates& st) = 0;
857 
883  virtual void on_reset(const ECStates& st) = 0;
884 
911  virtual void on_execute(const ECStates& st) = 0;
912 
939  virtual void on_state_update(const ECStates& st) = 0;
940 
958  virtual void on_rate_changed(void) = 0;
959 
978  virtual void worker(void) {return m_sm.worker();}
979 
997  virtual ExecContextState get_state(void){ return m_sm.getState();}
998 
1007 
1016  };
1017 
1018  //============================================================
1019  // DFP
1020  //============================================================
1046  template <class Object>
1047  class DFP
1048  : public DFPBase
1049  {
1050  public:
1071  : DFPBase(id), m_obj(obj), m_active(true)
1072  {
1073  }
1074 
1090  void on_startup(void)
1091  {
1092  m_obj->on_startup(ec_id);
1093  }
1094 
1110  void on_shutdown(void)
1111  {
1112  m_obj->on_shutdown(ec_id);
1113  }
1114 
1137  void on_activated(const ECStates& st)
1138  {
1139  if (m_obj->on_activated(ec_id) != RTC::RTC_OK)
1140  {
1141  m_sm.goTo(ERROR_STATE);
1142  return;
1143  }
1144  return;
1145  }
1146 
1167  void on_deactivated(const ECStates& st)
1168  {
1169  m_obj->on_deactivated(ec_id);
1170  }
1171 
1191  void on_aborting(const ECStates& st)
1192  {
1193  m_obj->on_aborting(ec_id);
1194  }
1195 
1215  void on_error(const ECStates& st)
1216  {
1217  m_obj->on_error(ec_id);
1218  }
1219 
1239  void on_reset(const ECStates& st)
1240  {
1241  if (m_obj->on_reset(ec_id) != RTC::RTC_OK)
1242  {
1243  m_sm.goTo(ERROR_STATE);
1244  return;
1245  }
1246  return;
1247  }
1248 
1274  void on_execute(const ECStates& st)
1275  {
1276  if (m_obj->on_execute(ec_id) != RTC::RTC_OK)
1277  {
1278  m_sm.goTo(ERROR_STATE);
1279  return;
1280  }
1281  return;
1282  }
1283 
1309  void on_state_update(const ECStates& st)
1310  {
1311  if (m_obj->on_state_update(ec_id) != RTC::RTC_OK)
1312  {
1313  m_sm.goTo(ERROR_STATE);
1314  return;
1315  }
1316  return;
1317  }
1318 
1336  void on_rate_changed(void)
1337  {
1338  m_obj->on_rate_changed(ec_id);
1339  }
1340 
1348  Object m_obj;
1349 
1357  bool m_active;
1358  };
1359 
1367  struct Comp
1368  {
1369  Comp(LightweightRTObject_ptr ref, OpenRTM::DataFlowComponent_ptr dfp,
1371  : _ref(LightweightRTObject::_duplicate(ref)),
1372  _sm(OpenRTM::DataFlowComponent::_duplicate(dfp), id)
1373  {
1374  }
1375  ~Comp(void)
1376  {
1377  }
1378  Comp(const Comp& comp)
1379  : _ref(comp._ref), _sm(comp._sm.m_obj, comp._sm.ec_id)
1380  {
1381  }
1382  Comp& operator=(const Comp& comp)
1383  {
1384  _ref = comp._ref;
1385  _sm.m_obj = comp._sm.m_obj;
1386  _sm.ec_id = comp._sm.ec_id;
1387  return *this;
1388  }
1389  LightweightRTObject_var _ref;
1391  };
1392 
1400  struct find_comp
1401  {
1402  LightweightRTObject_var m_comp;
1403  find_comp(LightweightRTObject_ptr comp)
1404  : m_comp(LightweightRTObject::_duplicate(comp)) {}
1405  bool operator()(Comp& comp)
1406  {
1407  return comp._ref->_is_equivalent(m_comp);
1408  }
1409  };
1410 
1419  {
1420  void operator()(Comp& comp)
1421  {
1422  comp._sm.on_startup();
1423  }
1424  };
1425 
1434  {
1435  void operator()(Comp& comp)
1436  {
1437  comp._sm.on_shutdown();
1438  }
1439  };
1440 
1449  {
1450  void operator()(Comp& comp)
1451  {
1452  comp._sm.on_rate_changed();
1453  }
1454  };
1455 
1464  {
1465  void operator()(Comp& comp)
1466  {
1467  comp._sm.worker();
1468  }
1469  };
1470 
1478  std::vector<Comp> m_comps;
1479  typedef std::vector<Comp>::iterator CompItr;
1480 
1489 
1500 
1508  bool m_svc;
1509 
1517  struct Worker
1518  {
1519  Worker() : cond_(mutex_), running_(false) {};
1520  coil::Mutex mutex_;
1522  bool running_;
1523  };
1524 
1533 
1543 
1552 
1560  ExecutionContextService_var m_ref;
1561 
1570  bool m_nowait;
1571 
1573  {
1574  RTObject_var m_comp;
1575  public:
1576  find_participant(RTObject_ptr comp)
1577  : m_comp(RTObject::_duplicate(comp)) {}
1578  bool operator()(RTObject_ptr comp)
1579  {
1580  return m_comp->_is_equivalent(comp);
1581  }
1582  };
1583  }; // class PeriodicExecutionContext
1584 }; // namespace RTC
1585 
1586 #ifdef WIN32
1587 #pragma warning( default : 4290 )
1588 #endif
1589 
1590 
1591 extern "C"
1592 {
1601 };
1602 
1603 #endif // RTC_PERIODICEXECUTIONCONTEXT_H
virtual void on_shutdown(void)=0
Pure virtual function to be invoked when ExecutionContext stops.
bool m_active
State flag of the target component to manage.
Condition variable class for worker.
EXECUTION_HANDLE_TYPE_NATIVE ExecutionContextHandle_t
Definition: IRTC.h:63
RT-Component.
virtual ReturnCode_t remove_component(LightweightRTObject_ptr comp)
Remove the RT-Component from participant list.
virtual ExecutionContextService_ptr getObjRef(void)
Get the reference to the CORBA object.
std::vector< Comp > m_comps
List of the participating component.
virtual void on_reset(const ECStates &st)=0
Pure virtual function to be invoked when RT-Component resets.
Mutex class.
ReturnCode_t
Definition: doil.h:53
virtual void on_activated(const ECStates &st)=0
Pure virtual function to be invoked when RT-Component is activated.
virtual RTC::ReturnCode_t bindComponent(RTObject_impl *rtc)
Bind the component.
A base class for ExecutionContext.
The structure for the component management.
virtual void on_state_update(const ECStates &st)=0
Pure virtual function to be periodically invoked while RT-Component is running.
bool m_svc
The thread running flag of ExecutionContext.
RT-Component class.
Definition: RTObject.h:89
DFP< OpenRTM::DataFlowComponent_var > _sm
Comp(LightweightRTObject_ptr ref, OpenRTM::DataFlowComponent_ptr dfp, ExecutionContextHandle_t id)
void on_error(const ECStates &st)
Function to be invoked while RT-Component is in the error state.
Manager class.
Definition: Manager.h:80
void on_rate_changed(void)
Function to be invoked when the execution cycles of ExecutionContext is changed.
#define NUM_OF_LIFECYCLESTATE
ExecutionContext base class.
virtual ReturnCode_t add_component(LightweightRTObject_ptr comp)
Add an RT-component.
virtual CORBA::Boolean is_running(void)
Check for ExecutionContext running state.
void on_deactivated(const ECStates &st)
Function to be invoked when RT-Component was deactivated.
virtual int open(void *args)
Generate internal activity thread for ExecutionContext.
virtual int close(unsigned long flags)
Thread execution function for ExecutionContext.
virtual void on_aborting(const ECStates &st)=0
Pure virtual function to be invoked when RT-Component occurs error.
void on_aborting(const ECStates &st)
Function to be invoked when RT-Component occured error.
PeriodicExecutionContext()
Default Constructor.
void on_activated(const ECStates &st)
Function to be invoked when RT-Component was activated.
RTC_Utils::StateHolder< ExecContextState > ECStates
Object m_obj
The target component to manage.
TimeValue class.
Definition: TimeValue.h:40
RTComponent manager class.
Worker m_worker
A condition variable for external triggered worker.
virtual void on_execute(const ECStates &st)=0
Pure virtual function to be periodically invoked while RT-Component is running.
virtual void on_startup(void)=0
Pure virtual function to be invoked when ExecutionContext starts.
DFP(Object obj, ExecutionContextHandle_t id)
Default constructor.
State machine class.
Definition: StateMachine.h:263
bool m_nowait
Flag of ExecutionContext to run immediately (to run without waiting)
void on_state_update(const ECStates &st)
Function to be invoked periodically while RT-Component executes.
ExecutionKind
Definition: IRTC.h:48
virtual void worker(void)
Get the worker to execute the state transition.
virtual ReturnCode_t start(void)
Start the ExecutionContext.
coil::Guard< coil::Mutex > Guard
virtual ReturnCode_t activate_component(LightweightRTObject_ptr comp)
Activate an RT-component.
virtual ExecContextState get_state(void)
Get the current state of the target component.
virtual ReturnCode_t reset_component(LightweightRTObject_ptr comp)
Reset the RT-component.
void PeriodicExecutionContextInit(RTC::Manager *manager)
Initialization function to register to ECFactory.
virtual ReturnCode_t stop(void)
Stop the ExecutionContext.
virtual ReturnCode_t deactivate_component(LightweightRTObject_ptr comp)
Deactivate an RT-component.
virtual ~PeriodicExecutionContext(void)
Destructor.
virtual ReturnCode_t set_rate(CORBA::Double rate)
Set execution rate(Hz) of ExecutionContext.
virtual ExecutionKind get_kind(void)
Get the ExecutionKind.
virtual void on_rate_changed(void)=0
Pure virtual function to be invoked when when the execution cycles of ExecutionContext is changed...
ExecutionContextProfile m_profile
ExecutionContextProfile.
void on_startup(void)
Function to be invoked when ExecutionContext starts.
Task class.
LifeCycleState
Definition: IRTC.h:40
std::vector< Comp >::iterator CompItr
virtual int svc(void)
Thread execution function for ExecutionContext.
RTComponent header.
DFPBase(RTC::ExecutionContextHandle_t id)
Constructor.
ExecutionContextService_var m_ref
Reference to ExecutionContextService object.
void on_shutdown(void)
Function to be invoked when ExecutionContext stops.
State machine template class.
virtual void on_error(const ECStates &st)=0
Pure virtual function to be invoked while RT-Component is in the error state.
ExecutionContextHandle_t ec_id
ID of participating ExecutionContext.
bool m_running
The running state of ExecutionContext true: running, false: stopped.
virtual LifeCycleState get_component_state(LightweightRTObject_ptr comp)
Get RT-component&#39;s state.
virtual CORBA::Double get_rate(void)
Get execution rate(Hz) of ExecutionContext.
RTC_Utils::StateMachine< ExecContextState, DFPBase > m_sm
The state machine of the target RT-Component to manage.
virtual void on_deactivated(const ECStates &st)=0
Pure virtual function to be invoked when RT-Component is deactivated.
void on_reset(const ECStates &st)
Function to be invoked when RT-Component is reset.
coil::TimeValue m_period
Execution cycle of ExecutionContext.
void on_execute(const ECStates &st)
Periodic exection function while running RT-Component.
virtual ExecutionContextProfile * get_profile(void)
Get the ExecutionContextProfile.
RTC::Local::ExecutionContextProfile ExecutionContextProfile


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Thu Jun 6 2019 19:25:59