File tree Expand file tree Collapse file tree 1 file changed +20
-0
lines changed Expand file tree Collapse file tree 1 file changed +20
-0
lines changed Original file line number Diff line number Diff line change 1
1
# rclpy: Time
2
2
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
+
3
23
To get the equivalent of rospy.Time.now(), you now need a ROS 2 node:
4
24
5
25
``` python
You can’t perform that action at this time.
0 commit comments