PeriodicTask.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: euc-jp -*-
3 
4 
5 
19 
20 import threading
21 import time
22 import OpenRTM_aist
23 
24 
47  """
48  """
49 
50 
57  def __init__(self):
58  OpenRTM_aist.Task.__init__(self)
60  self._nowait = False
61  self._func = 0
62  self._deleteInDtor = True
63  self._alive = self.alive_t(False)
64  self._suspend = self.suspend_t(False)
65 
66  # variables for execution time measurement
67  self._execMeasure = False
68  self._execCount = 0
69  self._execCountMax = 10
70  self._execStat = self.statistics_t()
72 
73  # variables for period time measurement
74  self._periodMeasure = False
75  self._periodCount = 0
76  self._periodCountMax = 10
77  self._periodStat = self.statistics_t()
79 
80  return
81 
82 
83 
90  def __del__(self, Task=OpenRTM_aist.Task):
91  self.finalize()
92  self.wait()
93  Task.__del__(self)
94  return
95 
96 
97 
120  def activate(self):
121  guard = OpenRTM_aist.ScopedLock(self._alive.mutex)
122  if not self._func:
123  return
124 
125  if self._alive.value:
126  return
127 
128  self._alive.value = True
129  OpenRTM_aist.Task.activate(self)
130  return
131 
132 
133 
147  def finalize(self):
148  guard = OpenRTM_aist.ScopedLock(self._alive.mutex)
149  self._alive.value = False
150 
151  self._suspend.cond.acquire()
152  self._suspend.suspend = False
153  self._suspend.cond.notify()
154  self._suspend.cond.release()
155  return
156 
157 
158 
172  def suspend(self):
173  self._suspend.cond.acquire()
174  self._suspend.suspend = True
175  self._suspend.cond.release()
176  return 0
177 
178 
179 
193  def resume(self):
194  self._periodTime.reset()
195  self._execTime.reset()
196  self._suspend.cond.acquire()
197  self._suspend.suspend = False
198  self._suspend.cond.notify()
199  self._suspend.cond.release()
200  return 0
201 
202 
203 
217  def signal(self):
218  self._suspend.cond.acquire()
219  self._suspend.cond.notify()
220  self._suspend.cond.release()
221  return
222 
223 
224 
238  def setTask(self, func, delete_in_dtor = True):
239  if not func:
240  return False
241 
242  self._deleteInDtor = delete_in_dtor
243  self._func = func
244  return True
245 
246 
247 
262  def setPeriod(self, period):
263  if type(period) == float:
264  self._period = OpenRTM_aist.TimeValue(period)
265  else:
266  self._period = period
267 
268  if self._period.sec() == 0 and self._period.usec() == 0:
269  self._nowait = True
270  return
271 
272  self._nowait = False
273  return
274 
275 
276 
284  def executionMeasure(self, value):
285  self._execMeasure = value
286  return
287 
288 
289 
297  def executionMeasureCount(self, n):
298  self._execCountMax = n
299  return
300 
301 
302 
310  def periodicMeasure(self, value):
311  self._periodMeasure = value
312  return
313 
314 
322  def periodicMeasureCount(self, n):
323  self._periodCountMax = n
324  return
325 
326 
327 
335  def getExecStat(self):
336  guard = OpenRTM_aist.ScopedLock(self._execStat.mutex)
337  return self._execStat.stat
338 
339 
340 
348  def getPeriodStat(self):
349  guard = OpenRTM_aist.ScopedLock(self._periodStat.mutex)
350  return self._periodStat.stat
351 
352 
353 
354  def svc(self):
355 
356  while self._alive.value: # needs lock?
357  if self._periodMeasure:
358  self._periodTime.tack()
359 
360  # wait if suspended
361  self._suspend.cond.acquire()
362  if self._suspend.suspend:
363  self._suspend.cond.wait()
364  # break if finalized
365  if not self._alive.value:
366  self._suspend.cond.release()
367  return 0
368  self._suspend.cond.release()
369 
370  if self._periodMeasure:
371  self._periodTime.tick()
372 
373  # task execution
374  if self._execMeasure:
375  self._execTime.tick()
376 
377  self._func()
378  if self._execMeasure:
379  self._execTime.tack()
380 
381  # wait for next period
382  self.updateExecStat()
383  self.sleep()
384  self.updatePeriodStat()
385 
386 
387  return 0
388 
389 
390 
391  def sleep(self):
392  if self._nowait:
393  return
394 
395  sleep_sec = self._period - self._execTime.interval()
396 
397  if sleep_sec.toDouble() < 0:
398  return
399 
400  time.sleep(sleep_sec.toDouble())
401  return
402 
403 
404 
405  def updateExecStat(self):
406  if self._execCount > self._execCountMax:
407  guard = OpenRTM_aist.ScopedLock(self._execStat.mutex)
408  self._execStat.stat = self._execTime.getStatistics()
409  self._execCount = 0
410 
411  self._execCount += 1
412  return
413 
414 
415 
416  def updatePeriodStat(self):
417  if self._periodCount > self._periodCountMax:
418  guard = OpenRTM_aist.ScopedLock(self._periodStat.mutex)
419  self._periodStat.stat = self._periodTime.getStatistics()
420  self_periodCount = 0
421 
422  self._periodCount += 1
423  return
424 
425 
426  # alive flag
427  class alive_t:
428  def __init__(self, val):
429  self.value = val
430  self.mutex = threading.RLock()
431  return
432 
433  # suspend flag
434  class suspend_t:
435  def __init__(self, sus):
436  self.suspend = sus
437  self.mutex = threading.RLock()
438  self.cond = threading.Condition(self.mutex)
439  return
440 
441 
442  # time measurement statistics struct
444  def __init__(self):
445  self.stat = OpenRTM_aist.TimeMeasure.Statistics()
446  self.mutex = threading.RLock()
447 
448 
def suspend(self)
Suspending the task.
def updatePeriodStat(self)
virtual void updatePeriodStat();
def __del__(self, Task=OpenRTM_aist.Task)
dtor
Definition: PeriodicTask.py:90
def signal(self)
Executing the suspended task one tick.
def getExecStat(self)
virtual TimeMeasure::Statistics getExecStat();
def getPeriodStat(self)
virtual TimeMeasure::Statistics getPeriodStat();
def updateExecStat(self)
virtual void updateExecStat();
def setPeriod(self, period)
Setting task execution period.
def setTask(self, func, delete_in_dtor=True)
Setting task execution function.
def wait(self)
Definition: Task.py:56
def activate(self)
Starting the task.
def resume(self)
Resuming the suspended task.
def periodicMeasure(self, value)
virtual void periodicMeasure(bool value);
def executionMeasure(self, value)
virtual void executionMeasure(bool value);
def sleep(self)
virtual void sleep();
def svc(self)
virtual int svc();
def finalize(self)
Finalizing the task.
def reset(self)
Definition: Task.py:70
def executionMeasureCount(self, n)
virtual void executionMeasureCount(int n);
def periodicMeasureCount(self, n)
virtual void periodicMeasureCount(int n);


openrtm_aist_python
Author(s): Shinji Kurihara
autogenerated on Mon Feb 28 2022 23:01:06