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
41 import sys
42
43 if sys.version > '3':
44 long = int
45
55
57 """
58 Base class of L{Time} and L{Duration} representations. Representation
59 is secs+nanoseconds since epoch.
60 """
61
62 __slots__ = ['secs', 'nsecs']
64 """
65 @param secs: seconds. If secs is a float, then nsecs must not be set or 0
66 @type secs: int/float
67 @param nsecs: nanoseconds
68 @type nsecs: int
69 """
70 if type(secs) != int:
71
72 if nsecs != 0:
73 raise ValueError("if secs is a float, nsecs cannot be set")
74 float_secs = secs
75 secs = int(float_secs)
76 nsecs = int((float_secs - secs) * 1000000000)
77
78 self.secs, self.nsecs = _canon(secs, nsecs)
79
81 """
82 @return: True if time is zero (secs and nsecs are zero)
83 @rtype: bool
84 """
85 return self.secs == 0 and self.nsecs == 0
86
87 - def set(self, secs, nsecs):
88 """
89 Set time using separate secs and nsecs values
90
91 @param secs: seconds since epoch
92 @type secs: int
93 @param nsecs: nanoseconds since seconds
94 @type nsecs: int
95 """
96 self.secs = secs
97 self.nsecs = nsecs
98
100 """
101 Canonicalize the field representation in this instance. should
102 only be used when manually setting secs/nsecs slot values, as
103 in deserialization.
104 """
105 self.secs, self.nsecs = _canon(self.secs, self.nsecs)
106
108 """
109 @return: time as float seconds (same as time.time() representation)
110 @rtype: float
111 """
112 return float(self.secs) + float(self.nsecs) / 1e9
113
115 """
116 @return: time as nanoseconds
117 @rtype: long
118 """
119 return self.secs * long(1e9) + self.nsecs
120
122 """
123 Time values are hashable. Time values with identical fields have the same hash.
124 """
125 return ("%s.%s"%(self.secs, self.nsecs)) .__hash__()
126
129
131 return "rostime.TVal[%d]"%self.to_nsec()
132
134 """
135 Check if time value is zero
136 """
137 return self.secs or self.nsecs
138
140 """
141 < test for time values
142 """
143 try:
144 return self.__cmp__(other) < 0
145 except TypeError:
146 return NotImplemented
148 """
149 <= test for time values
150 """
151 try:
152 return self.__cmp__(other) <= 0
153 except TypeError:
154 return NotImplemented
156 """
157 > test for time values
158 """
159 try:
160 return self.__cmp__(other) > 0
161 except TypeError:
162 return NotImplemented
164 """
165 >= test for time values
166 """
167 try:
168 return self.__cmp__(other) >= 0
169 except TypeError:
170 return NotImplemented
172 return not self.__eq__(other)
174 if not isinstance(other, TVal):
175 raise TypeError("Cannot compare to non-TVal")
176 nanos = self.to_nsec() - other.to_nsec()
177 if nanos > 0:
178 return 1
179 if nanos == 0:
180 return 0
181 return -1
183 if not isinstance(other, TVal):
184 return False
185 return self.to_nsec() == other.to_nsec()
186
188 """
189 Time contains the ROS-wide 'time' primitive representation, which
190 consists of two integers: seconds since epoch and nanoseconds since
191 seconds. Time instances are mutable.
192 """
193 __slots__ = ['secs', 'nsecs']
195 """
196 Constructor: secs and nsecs are integers. You may prefer to use the static L{from_sec()} factory
197 method instead.
198
199 @param secs: seconds since epoch
200 @type secs: int
201 @param nsecs: nanoseconds since seconds (since epoch)
202 @type nsecs: int
203 """
204 super(Time, self).__init__(secs, nsecs)
205 if self.secs < 0:
206 raise TypeError("time values must be positive")
207
209 """
210 support for Python pickling
211 """
212 return [self.secs, self.nsecs]
213
215 """
216 support for Python pickling
217 """
218 self.secs, self.nsecs = state
219
221 """
222 Create new Time instance using time.time() value (float
223 seconds)
224
225 @param float_secs: time value in time.time() format
226 @type float_secs: float
227 @return: Time instance for specified time
228 @rtype: L{Time}
229 """
230 secs = int(float_secs)
231 nsecs = int((float_secs - secs) * 1000000000)
232 return Time(secs, nsecs)
233
234 from_sec = staticmethod(from_sec)
235
237 """
238 Get Time in time.time() format. alias of L{to_sec()}
239
240 @return: time in floating point secs (time.time() format)
241 @rtype: float
242 """
243 return self.to_sec()
244
246 return "rostime.Time[%d]"%self.to_nsec()
247
249 """
250 Add duration to this time
251
252 @param other: duration
253 @type other: L{Duration}
254 """
255 if not isinstance(other, Duration):
256 return NotImplemented
257 return Time(self.secs + other.secs, self.nsecs + other.nsecs)
258
260 """
261 Subtract time or duration from this time
262 @param other: duration
263 @type other: L{Duration}/L{Time}
264 @return: L{Duration} if other is a L{Time}, L{Time} if other is a L{Duration}
265 """
266 if isinstance(other, Time):
267 return Duration(self.secs - other.secs, self.nsecs - other.nsecs)
268 elif isinstance(other, Duration):
269 return Time(self.secs - other.secs, self.nsecs - other.nsecs)
270 else:
271 return NotImplemented
272
274 """
275 Compare to another time
276 @param other: Time
277 @type other: L{Time}
278 """
279 if not isinstance(other, Time):
280 raise TypeError("cannot compare to non-Time")
281 nanos = self.to_nsec() - other.to_nsec()
282 if nanos > 0:
283 return 1
284 if nanos == 0:
285 return 0
286 return -1
287
289 """
290 Equals test for Time. Comparison assumes that both time
291 instances are in canonical representation; only compares fields.
292
293 @param other: Time
294 @type other: L{Time}
295 """
296 if not isinstance(other, Time):
297 return False
298 return self.secs == other.secs and self.nsecs == other.nsecs
299
301 """
302 Duration represents the ROS 'duration' primitive, which consists
303 of two integers: seconds and nanoseconds. The Duration class
304 allows you to add and subtract Duration instances, including
305 adding and subtracting from L{Time} instances.
306 """
307 __slots__ = ['secs', 'nsecs']
309 """
310 Create new Duration instance. secs and nsecs are integers and correspond to the ROS 'duration' primitive.
311
312 @param secs: seconds
313 @type secs: int
314 @param nsecs: nanoseconds
315 @type nsecs: int
316 """
317 super(Duration, self).__init__(secs, nsecs)
318
320 """
321 support for Python pickling
322 """
323 return [self.secs, self.nsecs]
324
326 """
327 support for Python pickling
328 """
329 self.secs, self.nsecs = state
330
332 return "rostime.Duration[%d]"%self.to_nsec()
333
335 """
336 Create new Duration instance from float seconds format.
337
338 @param float_seconds: time value in specified as float seconds
339 @type float_seconds: float
340 @return: Duration instance for specified float_seconds
341 @rtype: Duration
342 """
343 secs = int(float_seconds)
344 nsecs = int((float_seconds - secs) * 1000000000)
345 return Duration(secs, nsecs)
346
347 from_sec = staticmethod(from_sec)
348
350 """
351 @return: Negative value of this duration
352 @rtype: L{Duration}
353 """
354 return Duration(-self.secs, -self.nsecs)
356 """
357 Absolute value of this duration
358 @return: positive duration
359 @rtype: L{Duration}
360 """
361 if self.secs > 0:
362 return self
363 return self.__neg__()
364
366 """
367 Add duration to this duration, or this duration to a time, creating a new time value as a result.
368 @param other: duration or time
369 @type other: L{Duration}/L{Time}
370 @return: L{Duration} if other is a L{Duration}, L{Time} if other is a L{Time}
371 """
372 if isinstance(other, Time):
373 return other.__add__(self)
374 elif isinstance(other, Duration):
375 return Duration(self.secs+other.secs, self.nsecs+other.nsecs)
376 else:
377 return NotImplemented
379 """
380 Subtract duration from this duration, returning a new duration
381 @param other: duration
382 @type other: L{Duration}
383 @return: L{Duration}
384 """
385 if not isinstance(other, Duration):
386 return NotImplemented
387 return Duration(self.secs-other.secs, self.nsecs-other.nsecs)
388
390 """
391 Multiply this duration by an integer or float
392 @param val: multiplication factor
393 @type val: int/float
394 @return: Duration multiplied by val
395 @rtype: L{Duration}
396 """
397 t = type(val)
398 if t in (int, long):
399 return Duration(self.secs * val, self.nsecs * val)
400 elif t == float:
401 return Duration.from_sec(self.to_sec() * val)
402 else:
403 return NotImplemented
404
406 """
407 Floor divide this duration by an integer or float
408 @param val: division factor
409 @type val: int/float
410 @return: Duration multiplied by val
411 @rtype: L{Duration}
412 """
413 t = type(val)
414 if t in (int, long):
415 return Duration(self.secs // val, self.nsecs // val)
416 elif t == float:
417 return Duration.from_sec(self.to_sec() // val)
418 else:
419 return NotImplemented
420
422 """
423 Divide this duration by an integer or float
424 @param val: division factor
425 @type val: int/float
426 @return: Duration multiplied by val
427 @rtype: L{Duration}
428 """
429
430
431 t = type(val)
432 if t in (int, long):
433 return Duration(self.secs // val, self.nsecs // val)
434 elif t == float:
435 return Duration.from_sec(self.to_sec() / val)
436 else:
437 return NotImplemented
438
440 """
441 Divide this duration by an integer or float
442 @param val: division factor
443 @type val: int/float
444 @return: Duration multiplied by val
445 @rtype: L{Duration}
446 """
447 if type(val) in (int, long, float):
448 return Duration.from_sec(self.to_sec() / val)
449 else:
450 return NotImplemented
451
453 if not isinstance(other, Duration):
454 raise TypeError("Cannot compare to non-Duration")
455 nanos = self.to_nsec() - other.to_nsec()
456 if nanos > 0:
457 return 1
458 if nanos == 0:
459 return 0
460 return -1
461
466