Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 import threading
00021 import time
00022 import OpenRTM_aist
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 class PeriodicTask(OpenRTM_aist.Task):
00047 """
00048 """
00049
00050
00051
00052
00053
00054
00055
00056
00057 def __init__(self):
00058 OpenRTM_aist.Task.__init__(self)
00059 self._period = OpenRTM_aist.TimeValue(0.0)
00060 self._nowait = False
00061 self._func = 0
00062 self._deleteInDtor = True
00063 self._alive = self.alive_t(False)
00064 self._suspend = self.suspend_t(False)
00065
00066
00067 self._execMeasure = False
00068 self._execCount = 0
00069 self._execCountMax = 10
00070 self._execStat = self.statistics_t()
00071 self._execTime = OpenRTM_aist.TimeMeasure()
00072
00073
00074 self._periodMeasure = False
00075 self._periodCount = 0
00076 self._periodCountMax = 10
00077 self._periodStat = self.statistics_t()
00078 self._periodTime = OpenRTM_aist.TimeMeasure()
00079
00080 return
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090 def __del__(self, Task=OpenRTM_aist.Task):
00091 self.finalize()
00092 self.wait()
00093 Task.__del__(self)
00094 return
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120 def activate(self):
00121 guard = OpenRTM_aist.ScopedLock(self._alive.mutex)
00122 if not self._func:
00123 return
00124
00125 if self._alive.value:
00126 return
00127
00128 self._alive.value = True
00129 OpenRTM_aist.Task.activate(self)
00130 return
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147 def finalize(self):
00148 guard = OpenRTM_aist.ScopedLock(self._alive.mutex)
00149 self._alive.value = False
00150
00151 self._suspend.cond.acquire()
00152 self._suspend.suspend = False
00153 self._suspend.cond.notify()
00154 self._suspend.cond.release()
00155 return
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172 def suspend(self):
00173 self._suspend.cond.acquire()
00174 self._suspend.suspend = True
00175 self._suspend.cond.release()
00176 return 0
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193 def resume(self):
00194 self._periodTime.reset()
00195 self._execTime.reset()
00196 self._suspend.cond.acquire()
00197 self._suspend.suspend = False
00198 self._suspend.cond.notify()
00199 self._suspend.cond.release()
00200 return 0
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217 def signal(self):
00218 self._suspend.cond.acquire()
00219 self._suspend.cond.notify()
00220 self._suspend.cond.release()
00221 return
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238 def setTask(self, func, delete_in_dtor = True):
00239 if not func:
00240 return False
00241
00242 self._deleteInDtor = delete_in_dtor
00243 self._func = func
00244 return True
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262 def setPeriod(self, period):
00263 if type(period) == float:
00264 self._period = OpenRTM_aist.TimeValue(period)
00265 else:
00266 self._period = period
00267
00268 if self._period.sec() == 0 and self._period.usec() == 0:
00269 self._nowait = True
00270 return
00271
00272 self._nowait = False
00273 return
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284 def executionMeasure(self, value):
00285 self._execMeasure = value
00286 return
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297 def executionMeasureCount(self, n):
00298 self._execCountMax = n
00299 return
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310 def periodicMeasure(self, value):
00311 self._periodMeasure = value
00312 return
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322 def periodicMeasureCount(self, n):
00323 self._periodCountMax = n
00324 return
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335 def getExecStat(self):
00336 guard = OpenRTM_aist.ScopedLock(self._execStat.mutex)
00337 return self._execStat.stat
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348 def getPeriodStat(self):
00349 guard = OpenRTM_aist.ScopedLock(self._periodStat.mutex)
00350 return self._periodStat.stat
00351
00352
00353
00354 def svc(self):
00355
00356 while self._alive.value:
00357 if self._periodMeasure:
00358 self._periodTime.tack()
00359
00360
00361 self._suspend.cond.acquire()
00362 if self._suspend.suspend:
00363 self._suspend.cond.wait()
00364
00365 if not self._alive.value:
00366 self._suspend.cond.release()
00367 return 0
00368 self._suspend.cond.release()
00369
00370 if self._periodMeasure:
00371 self._periodTime.tick()
00372
00373
00374 if self._execMeasure:
00375 self._execTime.tick()
00376
00377 self._func()
00378 if self._execMeasure:
00379 self._execTime.tack()
00380
00381
00382 self.updateExecStat()
00383 self.sleep()
00384 self.updatePeriodStat()
00385
00386
00387 return 0
00388
00389
00390
00391 def sleep(self):
00392 if self._nowait:
00393 return
00394
00395 sleep_sec = self._period - self._execTime.interval()
00396
00397 if sleep_sec.toDouble() < 0:
00398 return
00399
00400 time.sleep(sleep_sec.toDouble())
00401 return
00402
00403
00404
00405 def updateExecStat(self):
00406 if self._execCount > self._execCountMax:
00407 guard = OpenRTM_aist.ScopedLock(self._execStat.mutex)
00408 self._execStat.stat = self._execTime.getStatistics()
00409 self._execCount = 0
00410
00411 self._execCount += 1
00412 return
00413
00414
00415
00416 def updatePeriodStat(self):
00417 if self._periodCount > self._periodCountMax:
00418 guard = OpenRTM_aist.ScopedLock(self._periodStat.mutex)
00419 self._periodStat.stat = self._periodTime.getStatistics()
00420 self_periodCount = 0
00421
00422 self._periodCount += 1
00423 return
00424
00425
00426
00427 class alive_t:
00428 def __init__(self, val):
00429 self.value = val
00430 self.mutex = threading.RLock()
00431 return
00432
00433
00434 class suspend_t:
00435 def __init__(self, sus):
00436 self.suspend = sus
00437 self.mutex = threading.RLock()
00438 self.cond = threading.Condition(self.mutex)
00439 return
00440
00441
00442
00443 class statistics_t:
00444 def __init__(self):
00445 self.stat = OpenRTM_aist.TimeMeasure.Statistics()
00446 self.mutex = threading.RLock()
00447
00448