21 from omniORB
import CORBA, PortableServer
27 DEFAULT_PERIOD = 0.000001
83 self._sm.setListener(self)
84 self._sm.setEntryAction (RTC.ACTIVE_STATE,
86 self._sm.setDoAction (RTC.ACTIVE_STATE,
88 self._sm.setPostDoAction(RTC.ACTIVE_STATE,
90 self._sm.setExitAction (RTC.ACTIVE_STATE,
92 self._sm.setEntryAction (RTC.ERROR_STATE,
94 self._sm.setDoAction (RTC.ERROR_STATE,
96 self._sm.setExitAction (RTC.ERROR_STATE,
99 st.prev = RTC.INACTIVE_STATE
100 st.curr = RTC.INACTIVE_STATE
101 st.next = RTC.INACTIVE_STATE
102 self._sm.setStartState(st)
103 self._sm.goTo(RTC.INACTIVE_STATE)
121 return self._obj.on_startup(self.
ec_id)
137 return self._obj.on_shutdown(self.
ec_id)
156 if self._obj.on_activated(self.
ec_id) != RTC.RTC_OK:
157 self._sm.goTo(RTC.ERROR_STATE)
177 self._obj.on_deactivated(self.
ec_id)
232 if self._obj.on_reset(self.
ec_id) != RTC.RTC_OK:
233 self._sm.goTo(RTC.ERROR_STATE)
255 if self._obj.on_execute(self.
ec_id) != RTC.RTC_OK:
256 self._sm.goTo(RTC.ERROR_STATE)
278 if self._obj.on_state_update(self.
ec_id) != RTC.RTC_OK:
279 self._sm.goTo(RTC.ERROR_STATE)
297 self._obj.on_rate_changed(self.
ec_id)
316 return self._sm.worker()
335 return self._sm.getState()
352 def __init__(self, owner=None, rate=None):
353 self.
_rtcout = OpenRTM_aist.Manager.instance().getLogbuf(
"rtobject.periodic_ec")
354 self._rtcout.RTC_TRACE(
"PeriodicExecutionContext()")
356 OpenRTM_aist.Task.__init__(self)
363 global DEFAULT_PERIOD
369 rate = 1.0 / DEFAULT_PERIOD
372 if self._period.sec() == 0
and self._period.usec() < 0.000001:
375 self._rtcout.RTC_DEBUG(
"Actual rate: %d [sec], %d [usec]",
376 (self._period.sec(), self._period.usec()))
379 self.
_profile = RTC.ExecutionContextProfile(RTC.PERIODIC, (1.0/self._period.toDouble()),
None, [], [])
385 def __del__(self, Task=OpenRTM_aist.Task):
386 self._rtcout.RTC_TRACE(
"~PeriodicExecutionContext()")
387 self._worker._cond.acquire()
388 self._worker._running =
True 389 self._worker._cond.notify()
390 self._worker._cond.release()
394 self._profile.owner =
None 395 self._profile.paarticipants = []
396 self._profile.properties = []
435 self._rtcout.RTC_TRACE(
"svc()")
441 self._worker._cond.acquire()
442 while not self._worker._running:
443 self._worker._cond.wait()
447 if self._worker._running:
451 self._worker._cond.release()
456 exctm_ = (t1_ - t0_).getTime().toDouble()
457 slptm_ = self._period.toDouble() - exctm_
458 self._rtcout.RTC_PARANOID(
"Period: %f [s]", self._period.toDouble())
459 self._rtcout.RTC_PARANOID(
"Execution: %f [s]", exctm_)
460 self._rtcout.RTC_PARANOID(
"Sleep: %f [s]", slptm_)
464 if not self.
_nowait and self._period.toDouble() > ((t1_ - t0_).getTime().toDouble()):
466 self._rtcout.RTC_PARANOID(
"sleeping...")
467 slptm_ = self._period.toDouble() - (t1_ - t0_).getTime().toDouble()
472 self._rtcout.RTC_PARANOID(
"Slept: %f [s]", (t3_ - t2_).getTime().toDouble())
502 def close(self, flags):
503 self._rtcout.RTC_TRACE(
"close()")
531 self._rtcout.RTC_TRACE(
"is_running()")
563 self._rtcout.RTC_TRACE(
"start()")
565 return RTC.PRECONDITION_NOT_MET
568 comp._sm.on_startup()
572 self._worker._cond.acquire()
573 self._worker._running =
True 574 self._worker._cond.notify()
575 self._worker._cond.release()
582 self._worker._cond.acquire()
583 self._worker._running =
False 584 self._worker._cond.notify()
585 self._worker._cond.release()
586 self._rtcout.RTC_ERROR(OpenRTM_aist.Logger.print_exception())
618 self._rtcout.RTC_TRACE(
"stop()")
620 return RTC.PRECONDITION_NOT_MET
623 self._worker._cond.acquire()
624 self._worker._running =
False 625 self._worker._cond.release()
628 comp._sm.on_shutdown()
653 self._rtcout.RTC_TRACE(
"get_rate()")
654 return self._profile.rate
683 self._rtcout.RTC_TRACE(
"set_rate(%f)", rate)
685 self._profile.rate = rate
686 self._period.set_time(1.0/rate)
687 if self._period.toDouble() == 0.0:
691 comp._sm.on_rate_changed()
693 return RTC.BAD_PARAMETER
726 self._rtcout.RTC_TRACE(
"activate_component()")
727 for compIn
in self.
_comps:
728 if compIn._ref._is_equivalent(comp):
729 if not compIn._sm._sm.isIn(RTC.INACTIVE_STATE):
730 return RTC.PRECONDITION_NOT_MET
731 compIn._sm._sm.goTo(RTC.ACTIVE_STATE)
734 return RTC.BAD_PARAMETER
767 self._rtcout.RTC_TRACE(
"deactivate_component()")
768 for compIn
in self.
_comps:
769 if compIn._ref._is_equivalent(comp):
770 if not compIn._sm._sm.isIn(RTC.ACTIVE_STATE):
771 return RTC.PRECONDITION_NOT_MET
772 compIn._sm._sm.goTo(RTC.INACTIVE_STATE)
774 usec_per_sec_ = 1.0e6
775 sleeptime_ = 10.0 * usec_per_sec_ / float(self.
get_rate())
776 self._rtcout.RTC_PARANOID(
"Sleep time is %f [us]", sleeptime_)
777 while compIn._sm._sm.isIn(RTC.ACTIVE_STATE):
778 self._rtcout.RTC_TRACE(
"Waiting to be the INACTIVE state %d %f", (count_, float(time.time())))
779 time.sleep(sleeptime_/usec_per_sec_)
781 self._rtcout.RTC_ERROR(
"The component is not responding.")
784 if compIn._sm._sm.isIn(RTC.INACTIVE_STATE):
785 self._rtcout.RTC_TRACE(
"The component has been properly deactivated.")
787 self._rtcout.RTC_ERROR(
"The component could not be deactivated.")
790 return RTC.BAD_PARAMETER
822 self._rtcout.RTC_TRACE(
"reset_component()")
823 for compIn
in self.
_comps:
824 if compIn._ref._is_equivalent(comp):
825 if not compIn._sm._sm.isIn(RTC.ERROR_STATE):
826 return RTC.PRECONDITION_NOT_MET
827 compIn._sm._sm.goTo(RTC.INACTIVE_STATE)
830 return RTC.BAD_PARAMETER
855 self._rtcout.RTC_TRACE(
"get_component_state()")
856 for compIn
in self.
_comps:
857 if compIn._ref._is_equivalent(comp):
858 return compIn._sm._sm.getState()
860 return RTC.CREATED_STATE
881 self._rtcout.RTC_TRACE(
"get_kind()")
882 return self._profile.kind
912 self._rtcout.RTC_TRACE(
"add_component()")
913 if CORBA.is_nil(comp):
914 return RTC.BAD_PARAMETER
916 dfp_ = comp._narrow(OpenRTM.DataFlowComponent)
917 rtc_ = comp._narrow(RTC.RTObject)
918 if CORBA.is_nil(dfp_)
or CORBA.is_nil(rtc_):
919 return RTC.BAD_PARAMETER
921 id_ = dfp_.attach_context(self.
_ref)
922 comp_ = self.
Comp(ref=comp, dfp=dfp_, id=id_)
923 self._comps.append(comp_)
924 self._profile.participants.append(rtc_)
926 except CORBA.Exception:
927 self._rtcout.RTC_ERROR(OpenRTM_aist.Logger.print_exception())
928 return RTC.BAD_PARAMETER
934 self._rtcout.RTC_TRACE(
"bindComponent()")
936 return RTC.BAD_PARAMETER
938 comp_ = rtc.getObjRef()
939 dfp_ = comp_._narrow(OpenRTM.DataFlowComponent)
940 id_ = rtc.bindContext(self.
_ref)
941 if id_ < 0
or id_ > OpenRTM_aist.ECOTHER_OFFSET:
942 self._rtcout.RTC_ERROR(
"bindContext returns invalid id: %d", id_)
945 self._rtcout.RTC_DEBUG(
"bindContext returns id = %d", id_)
947 self._comps.append(self.
Comp(ref=comp_, dfp=dfp_, id=id_))
948 self._profile.owner = dfp_
977 self._rtcout.RTC_TRACE(
"remove_component()")
979 for i
in range(len_):
981 if self.
_comps[idx]._ref._is_equivalent(comp):
982 self.
_comps[idx]._ref.detach_context(self.
_comps[idx]._sm.ec_id)
984 rtcomp = comp._narrow(RTC.RTObject)
985 if CORBA.is_nil(rtcomp):
986 self._rtcout.RTC_ERROR(
"Invalid object reference.")
992 return RTC.BAD_PARAMETER
1014 self._rtcout.RTC_TRACE(
"get_profile()")
1024 return self._comp._is_equivalent(comp)
1034 def __init__(self, ref=None, dfp=None, id=None, comp=None):
1039 self.
_ref = comp._ref
1084 manager.registerECFactory(
"PeriodicExecutionContext",
1086 OpenRTM_aist.ECDelete)
1087 def deactivate_component(self, comp)
Deactivate a RT-component.
def get_profile(self)
Get the ExecutionContextProfile.
def start(self)
Start ExecutionContext.
def set_rate(self, rate)
Set ExecutionRate.
def get_rate(self)
Get ExecutionRate.
def on_activated(self, st)
def add_component(self, comp)
Add a RT-component.
def PeriodicExecutionContextInit(manager)
PeriodicExecutionContext class.
def on_state_update(self, st)
def __init__(self, ref=None, dfp=None, id=None, comp=None)
def svc(self)
Create internal activity thread.
def remove_component(self, comp)
Remove the RT-component from participant list.
def close(self, flags)
Close activity thread.
def __init__(self)
Constructor.
def get_component_state(self, comp)
Get RT-component's state.
def on_deactivated(self, st)
def is_running(self)
Check for ExecutionContext running state.
def on_aborting(self, st)
def __del__(self, Task=OpenRTM_aist.Task)
def activate_component(self, comp)
Activate a RT-component.
def stop(self)
Stop ExecutionContext.
def get_kind(self)
Get the ExecutionKind.
def __init__(self, obj, id_)
Constructor.
def bindComponent(self, rtc)
def on_rate_changed(self)
def reset_component(self, comp)
Reset a RT-component.