00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 import sys
00018 import copy
00019 import threading
00020 import time
00021 from omniORB import CORBA, PortableServer
00022
00023 import OpenRTM_aist
00024 import OpenRTM
00025 import RTC
00026
00027 DEFAULT_PERIOD = 0.000001
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 class PeriodicExecutionContext(OpenRTM_aist.ExecutionContextBase,
00043 OpenRTM_aist.Task):
00044 """
00045 """
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062 class DFP:
00063 """
00064 """
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079 def __init__(self, obj, id_):
00080 self._obj = obj
00081 self._active = True
00082 self.ec_id = id_
00083 self._sm = OpenRTM_aist.StateMachine(4)
00084 self._sm.setListener(self)
00085 self._sm.setEntryAction (RTC.ACTIVE_STATE,
00086 self.on_activated)
00087 self._sm.setDoAction (RTC.ACTIVE_STATE,
00088 self.on_execute)
00089 self._sm.setPostDoAction(RTC.ACTIVE_STATE,
00090 self.on_state_update)
00091 self._sm.setExitAction (RTC.ACTIVE_STATE,
00092 self.on_deactivated)
00093 self._sm.setEntryAction (RTC.ERROR_STATE,
00094 self.on_aborting)
00095 self._sm.setDoAction (RTC.ERROR_STATE,
00096 self.on_error)
00097 self._sm.setExitAction (RTC.ERROR_STATE,
00098 self.on_reset)
00099 st = OpenRTM_aist.StateHolder()
00100 st.prev = RTC.INACTIVE_STATE
00101 st.curr = RTC.INACTIVE_STATE
00102 st.next = RTC.INACTIVE_STATE
00103 self._sm.setStartState(st)
00104 self._sm.goTo(RTC.INACTIVE_STATE)
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121 def on_startup(self):
00122 return self._obj.on_startup(self.ec_id)
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137 def on_shutdown(self):
00138 return self._obj.on_shutdown(self.ec_id)
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156 def on_activated(self, st):
00157 if self._obj.on_activated(self.ec_id) != RTC.RTC_OK:
00158 self._sm.goTo(RTC.ERROR_STATE)
00159 return
00160 return
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177 def on_deactivated(self, st):
00178 self._obj.on_deactivated(self.ec_id)
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196 def on_aborting(self, st):
00197 self._obj.on_aborting(self.ec_id)
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215 def on_error(self, st):
00216 self._obj.on_error(self.ec_id)
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232 def on_reset(self, st):
00233 if self._obj.on_reset(self.ec_id) != RTC.RTC_OK:
00234 self._sm.goTo(RTC.ERROR_STATE)
00235 return
00236 return
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255 def on_execute(self, st):
00256 if self._obj.on_execute(self.ec_id) != RTC.RTC_OK:
00257 self._sm.goTo(RTC.ERROR_STATE)
00258 return
00259 return
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278 def on_state_update(self, st):
00279 if self._obj.on_state_update(self.ec_id) != RTC.RTC_OK:
00280 self._sm.goTo(RTC.ERROR_STATE)
00281 return
00282 return
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297 def on_rate_changed(self):
00298 self._obj.on_rate_changed(self.ec_id)
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316 def worker(self):
00317 return self._sm.worker()
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335 def get_state(self):
00336 return self._sm.getState()
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353 def __init__(self, owner=None, rate=None):
00354 self._rtcout = OpenRTM_aist.Manager.instance().getLogbuf("rtobject.periodic_ec")
00355 self._rtcout.RTC_TRACE("PeriodicExecutionContext()")
00356
00357 OpenRTM_aist.Task.__init__(self)
00358
00359 self._nowait = False
00360 self._running = False
00361
00362 self._worker = self.Worker()
00363
00364 global DEFAULT_PERIOD
00365
00366 if rate is None:
00367 self._period = OpenRTM_aist.TimeValue(DEFAULT_PERIOD)
00368 else:
00369 if rate == 0:
00370 rate = 1.0 / DEFAULT_PERIOD
00371 self._period = OpenRTM_aist.TimeValue(1.0 / rate)
00372
00373 if self._period.sec() == 0 and self._period.usec() < 0.000001:
00374 self._nowait = True
00375
00376 self._rtcout.RTC_DEBUG("Actual rate: %d [sec], %d [usec]",
00377 (self._period.sec(), self._period.usec()))
00378
00379 self._comps = []
00380 self._profile = RTC.ExecutionContextProfile(RTC.PERIODIC, (1.0/self._period.toDouble()), None, [], [])
00381 self._ref = self._this()
00382 self._mutex_del = threading.RLock()
00383 return
00384
00385
00386 def __del__(self, Task=OpenRTM_aist.Task):
00387 self._rtcout.RTC_TRACE("~PeriodicExecutionContext()")
00388 self._worker._cond.acquire()
00389 self._worker._running = True
00390 self._worker._cond.notify()
00391 self._worker._cond.release()
00392 self._running = False
00393
00394
00395 self._profile.owner = None
00396 self._profile.paarticipants = []
00397 self._profile.properties = []
00398 guard = OpenRTM_aist.ScopedLock(self._mutex_del)
00399 Task.__del__(self)
00400 del guard
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416 def getObjRef(self):
00417 return self._ref
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435 def svc(self):
00436 self._rtcout.RTC_TRACE("svc()")
00437 flag = True
00438 count_ = 0
00439
00440 guard = OpenRTM_aist.ScopedLock(self._mutex_del)
00441 while flag:
00442 self._worker._cond.acquire()
00443 while not self._worker._running:
00444 self._worker._cond.wait()
00445
00446 t0_ = OpenRTM_aist.Time()
00447
00448 if self._worker._running:
00449 for comp in self._comps:
00450 comp._sm.worker()
00451
00452 self._worker._cond.release()
00453
00454 t1_ = OpenRTM_aist.Time()
00455
00456 if count_ > 1000:
00457 exctm_ = (t1_ - t0_).getTime().toDouble()
00458 slptm_ = self._period.toDouble() - exctm_
00459 self._rtcout.RTC_PARANOID("Period: %f [s]", self._period.toDouble())
00460 self._rtcout.RTC_PARANOID("Execution: %f [s]", exctm_)
00461 self._rtcout.RTC_PARANOID("Sleep: %f [s]", slptm_)
00462
00463 t2_ = OpenRTM_aist.Time()
00464
00465 if not self._nowait and self._period.toDouble() > ((t1_ - t0_).getTime().toDouble()):
00466 if count_ > 1000:
00467 self._rtcout.RTC_PARANOID("sleeping...")
00468 slptm_ = self._period.toDouble() - (t1_ - t0_).getTime().toDouble()
00469 time.sleep(slptm_)
00470
00471 if count_ > 1000:
00472 t3_ = OpenRTM_aist.Time()
00473 self._rtcout.RTC_PARANOID("Slept: %f [s]", (t3_ - t2_).getTime().toDouble())
00474 count_ = 0
00475 count_ += 1
00476 flag = self._running
00477 del guard
00478 return 0
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503 def close(self, flags):
00504 self._rtcout.RTC_TRACE("close()")
00505 return 0
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531 def is_running(self):
00532 self._rtcout.RTC_TRACE("is_running()")
00533 return self._running
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563 def start(self):
00564 self._rtcout.RTC_TRACE("start()")
00565 if self._running:
00566 return RTC.PRECONDITION_NOT_MET
00567
00568 for comp in self._comps:
00569 comp._sm.on_startup()
00570
00571 self._running = True
00572
00573 self._worker._cond.acquire()
00574 self._worker._running = True
00575 self._worker._cond.notify()
00576 self._worker._cond.release()
00577
00578 try:
00579 self.activate()
00580 except:
00581 self._running = False
00582
00583 self._worker._cond.acquire()
00584 self._worker._running = False
00585 self._worker._cond.notify()
00586 self._worker._cond.release()
00587 self._rtcout.RTC_ERROR(OpenRTM_aist.Logger.print_exception())
00588
00589 return RTC.RTC_OK
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618 def stop(self):
00619 self._rtcout.RTC_TRACE("stop()")
00620 if not self._running:
00621 return RTC.PRECONDITION_NOT_MET
00622
00623 self._running = False
00624 self._worker._cond.acquire()
00625 self._worker._running = False
00626 self._worker._cond.release()
00627
00628 for comp in self._comps:
00629 comp._sm.on_shutdown()
00630
00631
00632 return RTC.RTC_OK
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653 def get_rate(self):
00654 self._rtcout.RTC_TRACE("get_rate()")
00655 return self._profile.rate
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683 def set_rate(self, rate):
00684 self._rtcout.RTC_TRACE("set_rate(%f)", rate)
00685 if rate > 0.0:
00686 self._profile.rate = rate
00687 self._period.set_time(1.0/rate)
00688 if self._period.toDouble() == 0.0:
00689 self._nowait = True
00690
00691 for comp in self._comps:
00692 comp._sm.on_rate_changed()
00693 return RTC.RTC_OK
00694 return RTC.BAD_PARAMETER
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726 def activate_component(self, comp):
00727 self._rtcout.RTC_TRACE("activate_component()")
00728 for compIn in self._comps:
00729 if compIn._ref._is_equivalent(comp):
00730 if not compIn._sm._sm.isIn(RTC.INACTIVE_STATE):
00731 return RTC.PRECONDITION_NOT_MET
00732 compIn._sm._sm.goTo(RTC.ACTIVE_STATE)
00733 return RTC.RTC_OK
00734
00735 return RTC.BAD_PARAMETER
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767 def deactivate_component(self, comp):
00768 self._rtcout.RTC_TRACE("deactivate_component()")
00769 for compIn in self._comps:
00770 if compIn._ref._is_equivalent(comp):
00771 if not compIn._sm._sm.isIn(RTC.ACTIVE_STATE):
00772 return RTC.PRECONDITION_NOT_MET
00773 compIn._sm._sm.goTo(RTC.INACTIVE_STATE)
00774 count_ = 0
00775 usec_per_sec_ = 1.0e6
00776 sleeptime_ = 10.0 * usec_per_sec_ / float(self.get_rate())
00777 self._rtcout.RTC_PARANOID("Sleep time is %f [us]", sleeptime_)
00778 while compIn._sm._sm.isIn(RTC.ACTIVE_STATE):
00779 self._rtcout.RTC_TRACE("Waiting to be the INACTIVE state %d %f", (count_, float(time.time())))
00780 time.sleep(sleeptime_/usec_per_sec_)
00781 if count_ > 1000:
00782 self._rtcout.RTC_ERROR("The component is not responding.")
00783 break
00784 count_ += 1
00785 if compIn._sm._sm.isIn(RTC.INACTIVE_STATE):
00786 self._rtcout.RTC_TRACE("The component has been properly deactivated.")
00787 return RTC.RTC_OK
00788 self._rtcout.RTC_ERROR("The component could not be deactivated.")
00789 return RTC.RTC_ERROR
00790
00791 return RTC.BAD_PARAMETER
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822 def reset_component(self, comp):
00823 self._rtcout.RTC_TRACE("reset_component()")
00824 for compIn in self._comps:
00825 if compIn._ref._is_equivalent(comp):
00826 if not compIn._sm._sm.isIn(RTC.ERROR_STATE):
00827 return RTC.PRECONDITION_NOT_MET
00828 compIn._sm._sm.goTo(RTC.INACTIVE_STATE)
00829 return RTC.RTC_OK
00830
00831 return RTC.BAD_PARAMETER
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855 def get_component_state(self, comp):
00856 self._rtcout.RTC_TRACE("get_component_state()")
00857 for compIn in self._comps:
00858 if compIn._ref._is_equivalent(comp):
00859 return compIn._sm._sm.getState()
00860
00861 return RTC.CREATED_STATE
00862
00863
00864
00865
00866
00867
00868
00869
00870
00871
00872
00873
00874
00875
00876
00877
00878
00879
00880
00881 def get_kind(self):
00882 self._rtcout.RTC_TRACE("get_kind()")
00883 return self._profile.kind
00884
00885
00886
00887
00888
00889
00890
00891
00892
00893
00894
00895
00896
00897
00898
00899
00900
00901
00902
00903
00904
00905
00906
00907
00908
00909
00910
00911
00912 def add_component(self, comp):
00913 self._rtcout.RTC_TRACE("add_component()")
00914 if CORBA.is_nil(comp):
00915 return RTC.BAD_PARAMETER
00916 try:
00917 dfp_ = comp._narrow(OpenRTM.DataFlowComponent)
00918 rtc_ = comp._narrow(RTC.RTObject)
00919 if CORBA.is_nil(dfp_) or CORBA.is_nil(rtc_):
00920 return RTC.BAD_PARAMETER
00921
00922 id_ = dfp_.attach_context(self._ref)
00923 comp_ = self.Comp(ref=comp, dfp=dfp_, id=id_)
00924 self._comps.append(comp_)
00925 self._profile.participants.append(rtc_)
00926 return RTC.RTC_OK
00927 except CORBA.Exception:
00928 self._rtcout.RTC_ERROR(OpenRTM_aist.Logger.print_exception())
00929 return RTC.BAD_PARAMETER
00930
00931 return RTC.RTC_OK
00932
00933
00934 def bindComponent(self, rtc):
00935 self._rtcout.RTC_TRACE("bindComponent()")
00936 if rtc is None:
00937 return RTC.BAD_PARAMETER
00938
00939 comp_ = rtc.getObjRef()
00940 dfp_ = comp_._narrow(OpenRTM.DataFlowComponent)
00941 id_ = rtc.bindContext(self._ref)
00942 if id_ < 0 or id_ > OpenRTM_aist.ECOTHER_OFFSET:
00943 self._rtcout.RTC_ERROR("bindContext returns invalid id: %d", id_)
00944 return RTC.RTC_ERROR
00945
00946 self._rtcout.RTC_DEBUG("bindContext returns id = %d", id_)
00947
00948 self._comps.append(self.Comp(ref=comp_, dfp=dfp_, id=id_))
00949 self._profile.owner = dfp_
00950 return RTC.RTC_OK
00951
00952
00953
00954
00955
00956
00957
00958
00959
00960
00961
00962
00963
00964
00965
00966
00967
00968
00969
00970
00971
00972
00973
00974
00975
00976
00977 def remove_component(self, comp):
00978 self._rtcout.RTC_TRACE("remove_component()")
00979 len_ = len(self._comps)
00980 for i in range(len_):
00981 idx = (len_ - 1) - i
00982 if self._comps[idx]._ref._is_equivalent(comp):
00983 self._comps[idx]._ref.detach_context(self._comps[idx]._sm.ec_id)
00984 del self._comps[idx]
00985 rtcomp = comp._narrow(RTC.RTObject)
00986 if CORBA.is_nil(rtcomp):
00987 self._rtcout.RTC_ERROR("Invalid object reference.")
00988 return RTC.RTC_ERROR
00989 OpenRTM_aist.CORBA_SeqUtil.erase_if(self._profile.participants,
00990 self.find_participant(rtcomp))
00991 return RTC.RTC_OK
00992
00993 return RTC.BAD_PARAMETER
00994
00995
00996
00997
00998
00999
01000
01001
01002
01003
01004
01005
01006
01007
01008
01009
01010
01011
01012
01013
01014 def get_profile(self):
01015 self._rtcout.RTC_TRACE("get_profile()")
01016 return self._profile
01017
01018
01019 class find_participant:
01020 def __init__(self, comp):
01021 self._comp = comp
01022 return
01023
01024 def __call__(self, comp):
01025 return self._comp._is_equivalent(comp)
01026
01027
01028
01029
01030
01031
01032
01033
01034 class Comp:
01035 def __init__(self, ref=None, dfp=None, id=None, comp=None):
01036 if comp is None:
01037 self._ref = ref
01038 self._sm = PeriodicExecutionContext.DFP(dfp,id)
01039 else:
01040 self._ref = comp._ref
01041 self._sm = PeriodicExecutionContext.DFP(comp._sm._obj,comp._sm.ec_id)
01042
01043
01044
01045
01046
01047
01048
01049
01050
01051
01052
01053
01054
01055 class Worker:
01056
01057
01058
01059
01060
01061
01062
01063
01064
01065
01066
01067
01068 def __init__(self):
01069 self._mutex = threading.RLock()
01070 self._cond = threading.Condition(self._mutex)
01071 self._running = False
01072
01073
01074
01075
01076
01077
01078
01079
01080
01081
01082
01083
01084 def PeriodicExecutionContextInit(manager):
01085 manager.registerECFactory("PeriodicExecutionContext",
01086 OpenRTM_aist.PeriodicExecutionContext,
01087 OpenRTM_aist.ECDelete)