Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 508bce5

Browse files
authoredNov 21, 2024··
Times table (#24)
1 parent c6d6551 commit 508bce5

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
 

‎rclpy/time.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
# rclpy: Time
22

3+
There are four separate ways that you may see time represented in ROS 2:
4+
* Plain ol' `int`: representing a number of nanoseconds.
5+
* `float`: representing the fractional number of seconds
6+
* [`rclpy.time.Time`](https://github.com/ros2/rclpy/blob/rolling/rclpy/rclpy/time.py) - the preferred Pythonic interface
7+
* [`builtin_interfaces/msg/Time.msg`](https://github.com/ros2/rcl_interfaces/blob/master/builtin_interfaces/msg/Time.msg): the message representation.
8+
9+
Here's how you can convert them.
10+
| Converting this →<br>to this ↓ | int | float | rclpy | msg |
11+
|--------------------------------|--------------------------------|----------------------------------------------------------------------------------------------------|-----------------------|---------------------------|
12+
| int | - | `int(t * 1e9)` | `t.nanoseconds` | `t.sec * 1e9 + t.nanosec` |
13+
| float | `t / 1e9` | - | `t.nanoseconds / 1e9` | `t.sec + t.nanosec / 1e9` |
14+
| rclpy | `Time(nanoseconds=t)` | `nano, sec = math.modf(t)`<br>`Time(int(sec), int(nano * 1e9))` | - | `Time.from_msg(t)` |
15+
| msg | `Time(nanoseconds=t).to_msg()` | `nano, sec = math.modf(t)`<br>`builtin_interfaces.msg.Time(sec=int(sec), nanosec=int(nano * 1e9))` | `t.to_msg()` | - |
16+
17+
Important notes:
18+
* You cannot do comparisons/math between mixed types or messages
19+
* It is only mildly infuriating that `rclpy.time.Time` has the full word `nanoseconds` accessor and the message has `nanosec`.
20+
21+
## Now
22+
323
To get the equivalent of rospy.Time.now(), you now need a ROS 2 node:
424

525
```python

0 commit comments

Comments
 (0)
Please sign in to comment.