Package roslib ::
Module rostime
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 """
36 ROS Time representation, including Duration
37 """
38
39 import itertools
40 import time
51
52 import warnings
54 """This is a decorator which can be used to mark functions
55 as deprecated. It will result in a warning being emmitted
56 when the function is used."""
57 def newFunc(*args, **kwargs):
58 warnings.warn("Call to deprecated function %s." % func.__name__,
59 category=DeprecationWarning, stacklevel=2)
60 return func(*args, **kwargs)
61 newFunc.__name__ = func.__name__
62 newFunc.__doc__ = func.__doc__
63 newFunc.__dict__.update(func.__dict__)
64 return newFunc
65
67 """
68 Base class of L{Time} and L{Duration} representations. Representation
69 is secs+nanoseconds since epoch.
70 """
71
72 __slots__ = ['secs', 'nsecs']
74 """
75 @param secs: seconds. If secs is a float, then nsecs must not be set or 0
76 @type secs: int/float
77 @param nsecs: nanoseconds
78 @type nsecs: int
79 """
80 if type(secs) != int:
81
82 if nsecs != 0:
83 raise ValueError("if secs is a float, nsecs cannot be set")
84 float_secs = secs
85 secs = int(float_secs)
86 nsecs = int((float_secs - secs) * 1000000000)
87
88 self.secs, self.nsecs = _canon(secs, nsecs)
89
91 """
92 @return: True if time is zero (secs and nsecs are zero)
93 @rtype: bool
94 """
95 return self.secs == 0 and self.nsecs == 0
96
97 - def set(self, secs, nsecs):
98 """
99 Set time using separate secs and nsecs values
100
101 @param secs: seconds since epoch
102 @type secs: int
103 @param nsecs: nanoseconds since seconds
104 @type nsecs: int
105 """
106 self.secs = secs
107 self.nsecs = nsecs
108
110 """
111 Canonicalize the field representation in this instance. should
112 only be used when manually setting secs/nsecs slot values, as
113 in deserialization.
114 """
115 self.secs, self.nsecs = _canon(self.secs, self.nsecs)
116
118 """
119 @return: time as float seconds (same as time.time() representation)
120 @rtype: float
121 """
122 return float(self.secs) + float(self.nsecs) / 1e9
123
125 """
126 @return: time as nanoseconds
127 @rtype: long
128 """
129 return self.secs * long(1e9) + self.nsecs
130
132 """
133 Time values are hashable. Time values with identical fields have the same hash.
134 """
135 return ("%s.%s"%(self.secs, self.nsecs)) .__hash__()
136
139
141 return "rostime.TVal[%d]"%self.to_nsec()
142
144 """
145 Check if time value is zero
146 """
147 return self.secs or self.nsecs
148
150 """
151 < test for time values
152 """
153 try:
154 return self.__cmp__(other) < 0
155 except TypeError:
156 return NotImplemented
158 """
159 <= test for time values
160 """
161 try:
162 return self.__cmp__(other) <= 0
163 except TypeError:
164 return NotImplemented
166 """
167 > test for time values
168 """
169 try:
170 return self.__cmp__(other) > 0
171 except TypeError:
172 return NotImplemented
174 """
175 >= test for time values
176 """
177 try:
178 return self.__cmp__(other) >= 0
179 except TypeError:
180 return NotImplemented
182 return not self.__eq__(other)
184 if not isinstance(other, TVal):
185 raise TypeError("Cannot compare to non-TVal")
186 nanos = self.to_nsec() - other.to_nsec()
187 if nanos > 0:
188 return 1
189 if nanos == 0:
190 return 0
191 return -1
193 if not isinstance(other, TVal):
194 return False
195 return self.to_nsec() == other.to_nsec()
196
198 """
199 Time contains the ROS-wide 'time' primitive representation, which
200 consists of two integers: seconds since epoch and nanoseconds since
201 seconds. Time instances are mutable.
202 """
203 __slots__ = ['secs', 'nsecs']
205 """
206 Constructor: secs and nsecs are integers. You may prefer to use the static L{from_sec()} factory
207 method instead.
208
209 @param secs: seconds since epoch
210 @type secs: int
211 @param nsecs: nanoseconds since seconds (since epoch)
212 @type nsecs: int
213 """
214 super(Time, self).__init__(secs, nsecs)
215 if self.secs < 0:
216 raise TypeError("time values must be positive")
217
219 """
220 support for Python pickling
221 """
222 return [self.secs, self.nsecs]
223
225 """
226 support for Python pickling
227 """
228 self.secs, self.nsecs = state
229
230 @deprecated
232 """
233 Use from_sec() instead. This is retained for backwards compatibility.
234
235 @param float_secs: time value in time.time() format
236 @type float_secs: float
237 @return: Time instance for specified time
238 @rtype: L{Time}
239 """
240 return Time.from_sec(float_secs)
241
243 """
244 Create new Time instance using time.time() value (float
245 seconds)
246
247 @param float_secs: time value in time.time() format
248 @type float_secs: float
249 @return: Time instance for specified time
250 @rtype: L{Time}
251 """
252 secs = int(float_secs)
253 nsecs = int((float_secs - secs) * 1000000000)
254 return Time(secs, nsecs)
255
256 from_seconds = staticmethod(from_seconds)
257 from_sec = staticmethod(from_sec)
258
260 """
261 Get Time in time.time() format. alias of L{to_sec()}
262
263 @return: time in floating point secs (time.time() format)
264 @rtype: float
265 """
266 return self.to_sec()
267
269 return "rostime.Time[%d]"%self.to_nsec()
270
272 """
273 Add duration to this time
274
275 @param other: duration
276 @type other: L{Duration}
277 """
278 if not isinstance(other, Duration):
279 return NotImplemented
280 return Time(self.secs + other.secs, self.nsecs + other.nsecs)
281
283 """
284 Subtract time or duration from this time
285 @param other: duration
286 @type other: L{Duration}/L{Time}
287 @return: L{Duration} if other is a L{Time}, L{Time} if other is a L{Duration}
288 """
289 if isinstance(other, Time):
290 return Duration(self.secs - other.secs, self.nsecs - other.nsecs)
291 elif isinstance(other, Duration):
292 return Time(self.secs - other.secs, self.nsecs - other.nsecs)
293 else:
294 return NotImplemented
295
297 """
298 Compare to another time
299 @param other: Time
300 @type other: L{Time}
301 """
302 if not isinstance(other, Time):
303 raise TypeError("cannot compare to non-Time")
304 nanos = self.to_nsec() - other.to_nsec()
305 if nanos > 0:
306 return 1
307 if nanos == 0:
308 return 0
309 return -1
310
312 """
313 Equals test for Time. Comparison assumes that both time
314 instances are in canonical representation; only compares fields.
315
316 @param other: Time
317 @type other: L{Time}
318 """
319 if not isinstance(other, Time):
320 return False
321 return self.secs == other.secs and self.nsecs == other.nsecs
322
324 """
325 Duration represents the ROS 'duration' primitive, which consists
326 of two integers: seconds and nanoseconds. The Duration class
327 allows you to add and subtract Duration instances, including
328 adding and subtracting from L{Time} instances.
329 """
330 __slots__ = ['secs', 'nsecs']
332 """
333 Create new Duration instance. secs and nsecs are integers and correspond to the ROS 'duration' primitive.
334
335 @param secs: seconds
336 @type secs: int
337 @param nsecs: nanoseconds
338 @type nsecs: int
339 """
340 super(Duration, self).__init__(secs, nsecs)
341
343 """
344 support for Python pickling
345 """
346 return [self.secs, self.nsecs]
347
349 """
350 support for Python pickling
351 """
352 self.secs, self.nsecs = state
353
355 return "rostime.Duration[%d]"%self.to_nsec()
356
358 """
359 Create new Duration instance from float seconds format.
360
361 @param float_seconds: time value in specified as float seconds
362 @type float_seconds: float
363 @return: Duration instance for specified float_seconds
364 @rtype: Duration
365 """
366 secs = int(float_seconds)
367 nsecs = int((float_seconds - secs) * 1000000000)
368 return Duration(secs, nsecs)
369
370 from_sec = staticmethod(from_sec)
371
373 """
374 @return: Negative value of this duration
375 @rtype: L{Duration}
376 """
377 return Duration(-self.secs, -self.nsecs)
379 """
380 Absolute value of this duration
381 @return: positive duration
382 @rtype: L{Duration}
383 """
384 if self.secs > 0:
385 return self
386 return self.__neg__()
387
389 """
390 Add duration to this duration, or this duration to a time, creating a new time value as a result.
391 @param other: duration or time
392 @type other: L{Duration}/L{Time}
393 @return: L{Duration} if other is a L{Duration}, L{Time} if other is a L{Time}
394 """
395 if isinstance(other, Time):
396 return other.__add__(self)
397 elif isinstance(other, Duration):
398 return Duration(self.secs+other.secs, self.nsecs+other.nsecs)
399 else:
400 return NotImplemented
402 """
403 Subtract duration from this duration, returning a new duration
404 @param other: duration
405 @type other: L{Duration}
406 @return: L{Duration}
407 """
408 if not isinstance(other, Duration):
409 return NotImplemented
410 return Duration(self.secs-other.secs, self.nsecs-other.nsecs)
411
413 """
414 Multiply this duration by an integer or float
415 @param val: multiplication factor
416 @type val: int/float
417 @return: Duration multiplied by val
418 @rtype: L{Duration}
419 """
420 t = type(val)
421 if t in (int, long):
422 return Duration(self.secs * val, self.nsecs * val)
423 elif t == float:
424 return Duration.from_sec(self.to_sec() * val)
425 else:
426 return NotImplemented
427
429 """
430 Floor divide this duration by an integer or float
431 @param val: division factor
432 @type val: int/float
433 @return: Duration multiplied by val
434 @rtype: L{Duration}
435 """
436 t = type(val)
437 if t in (int, long):
438 return Duration(self.secs // val, self.nsecs // val)
439 elif t == float:
440 return Duration.from_sec(self.to_sec() // val)
441 else:
442 return NotImplemented
443
445 """
446 Divide this duration by an integer or float
447 @param val: division factor
448 @type val: int/float
449 @return: Duration multiplied by val
450 @rtype: L{Duration}
451 """
452
453 t = type(val)
454 if t in (int, long):
455 return Duration(self.secs // val, self.nsecs // val)
456 elif t == float:
457 return Duration.from_sec(self.to_sec() / val)
458 else:
459 return NotImplemented
460
462 """
463 Divide this duration by an integer or float
464 @param val: division factor
465 @type val: int/float
466 @return: Duration multiplied by val
467 @rtype: L{Duration}
468 """
469 t = type(val)
470 if t in (int, long):
471 return Duration(self.secs / val, self.nsecs / val)
472 elif t == float:
473 return Duration.from_sec(self.to_sec() / val)
474 else:
475 return NotImplemented
476
478 if not isinstance(other, Duration):
479 raise TypeError("Cannot compare to non-Duration")
480 nanos = self.to_nsec() - other.to_nsec()
481 if nanos > 0:
482 return 1
483 if nanos == 0:
484 return 0
485 return -1
486
491