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
00021
00022 import time
00023 import math
00024
00025 import OpenRTM_aist
00026
00027 ULLONG_MAX = 0xffffffffffffffff
00028
00029
00030
00031
00032
00033
00034 usec_per_sec = 1000000
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 class Time:
00049 """
00050 """
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 def __init__(self):
00069 global usec_per_sec
00070 tm = time.time()
00071 tm_f = float(tm - long(tm))
00072 self.sec = long(tm - tm_f)
00073 self.usec = long(float(tm_f) * float(usec_per_sec))
00074 self._timevalue = OpenRTM_aist.TimeValue(self.sec,self.usec)
00075 return
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092 def __sub__(self, tm):
00093 global usec_per_sec
00094
00095 res = Time()
00096
00097 if self.sec >= tm.sec:
00098 if self.usec >= tm.usec:
00099 res.sec = self.sec - tm.sec
00100 res.usec = self.usec - tm.usec
00101 else:
00102 res.sec = self.sec - tm.sec - 1
00103 res.usec = (self.usec + usec_per_sec) - tm.usec
00104 else:
00105 if tm.usec >= self.usec:
00106 res.sec = -(tm.sec - self.sec)
00107 res.usec = -(tm.usec - self.usec)
00108 else:
00109 res.sec = -(tm.sec - self.sec - 1)
00110 res.usec = -(tm.usec + usec_per_sec) + self.usec
00111 return res
00112
00113
00114 def gettimeofday(self):
00115 global usec_per_sec
00116 tm = time.time()
00117 tm_f = float(tm - long(tm))
00118 self.sec = long(float(tm) - float(tm_f))
00119 self.usec = long(float(tm_f) * float(usec_per_sec))
00120 return OpenRTM_aist.TimeValue(self.sec, self.usec)
00121
00122
00123 def getTime(self):
00124 return OpenRTM_aist.TimeValue(self.sec, self.usec)
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134 class TimeMeasure:
00135 """
00136 """
00137
00138
00139
00140
00141
00142
00143 def __init__(self, buflen=100):
00144 self._countMax = buflen + 1
00145 self._record = [OpenRTM_aist.TimeValue(0, 0) for i in range(self._countMax)]
00146 self._begin = Time().gettimeofday()
00147 self._end = Time().gettimeofday()
00148 self._count = 0
00149 self._recurred = False
00150 self._interval = OpenRTM_aist.TimeValue(0.0)
00151 return
00152
00153
00154
00155
00156
00157
00158 def tick(self):
00159 self._begin = Time().gettimeofday()
00160 return
00161
00162
00163
00164
00165
00166
00167 def tack(self):
00168 if self._begin.sec() == 0:
00169 return
00170
00171 self._interval = Time().gettimeofday() - self._begin
00172 self._end = Time().gettimeofday()
00173 self._record[self._count] = self._interval
00174 self._count += 1
00175 if self._count == self._countMax:
00176 self._count = 0
00177 self._recurred = True
00178 return
00179
00180 def interval(self):
00181 return self._interval
00182
00183
00184 def reset(self):
00185 self._count = 0
00186 self._recurred = False
00187 self._begin = OpenRTM_aist.TimeValue(0.0)
00188 return
00189
00190
00191
00192
00193
00194
00195
00196 def count(self):
00197 if self._recurred:
00198 return len(self._record)
00199 else:
00200 return self._count
00201 return
00202
00203
00204
00205
00206
00207
00208 def getStatistics(self, max_interval=None, min_interval=None,
00209 mean_interval=None, stddev=None):
00210 global ULLONG_MAX
00211
00212 if not max_interval and not min_interval and not mean_interval and not stddev:
00213 max_i = [0.0]
00214 min_i = [0.0]
00215 mean_i = [0.0]
00216 stdd = [0.0]
00217 return self.getStatistics(max_i, min_i, mean_i, stdd)
00218
00219 max_interval[0] = 0.0
00220 min_interval[0] = ULLONG_MAX
00221
00222 sum_ = 0.0
00223 sq_sum_ = 0.0
00224 len_ = self.count()
00225
00226 if len_ == 0:
00227 return False
00228
00229 for i in range(len_):
00230 trecord_ = self._record[i].toDouble()
00231 sum_ += trecord_
00232 sq_sum_ += (trecord_ * trecord_)
00233
00234 if trecord_ > max_interval[0]:
00235 max_interval[0] = trecord_
00236
00237 if trecord_ < min_interval[0]:
00238 min_interval[0] = trecord_
00239
00240
00241 mean_interval[0] = sum_ / len_
00242 stddev[0] = math.sqrt(sq_sum_ / len_ - (mean_interval[0] * mean_interval[0]))
00243
00244 return True
00245
00246
00247 class Statistics:
00248 def __init__(self, max=None, min=None, mean=None, stdd=None):
00249 if not max and not min and not mean and not stdd:
00250 self._max_interval = 0.0
00251 self._min_interval = 0.0
00252 self._mean_interval = 0.0
00253 self._std_deviation = 0.0
00254 return
00255
00256 self._max_interval = max
00257 self._min_interval = min
00258 self._mean_interval = mean
00259 self._std_deviation = stdd
00260 return