특징 | rclpy.time.Time | builtin_interfaces.msg._time.Time | time.time() |
---|---|---|---|
소속 | ROS 2 (rclpy 라이브러리) | ROS 2 메시지 정의 | Python 표준 라이브러리 |
주 용도 | ROS 2 내부의 시간 관리 및 연산 | ROS 2 메시지의 타임스탬프 | 시스템의 현재 시간 반환 |
시간 단위 | 나노초 | 초와 나노초 | 초 |
시간 소스 | ROS 시뮬레이션 시간 또는 실제 시간 | ROS 노드 간 메시지 교환에 사용 | 시스템 시간 (Unix epoch 기준) |
정밀도 | 나노초 | 나노초 | 마이크로초 |
기능 | 시간 연산, 비교, 변환 | 메시지 타입으로 시간 정보를 포함 | 현재 시간 측정, 타이밍 |
rclpy.time.Time
import rclpy
from rclpy.time import Time
from rclpy.node import Node
def create_ros_time() -> Time:
"""
ROS 2 Time 객체를 생성하여 반환합니다.
Returns:
Time: 현재 시간을 나타내는 ROS 2 Time 객체
"""
# ROS 2 초기화
rclpy.init()
# 임시 노드 생성
node = Node("example_node")
# 현재 ROS 시간 가져오기
current_time: Time = node.get_clock().now()
# ROS 2 종료
rclpy.shutdown()
return current_time
# 사용 예시
ros_time = create_ros_time()
print(f"현재 ROS 시간: {ros_time}")
builtin_interfaces.msg._time.Time
builtin_interfaces.msg._time.Time
은 ROS 2 메시지 정의 중 하나from builtin_interfaces.msg import Time as ROS2Time
def create_builtin_time(seconds: int, nanoseconds: int) -> ROS2Time:
"""
주어진 초와 나노초를 사용하여 ROS 2 Time 메시지를 생성합니다.
Args:
seconds (int): 초 단위 시간
nanoseconds (int): 나노초 단위 시간
Returns:
ROS2Time: 주어진 시간을 나타내는 ROS 2 Time 메시지
"""
ros2_time = ROS2Time()
ros2_time.sec = seconds
ros2_time.nanosec = nanoseconds
return ros2_time
# 사용 예시
ros2_time_message = create_builtin_time(1625077225, 123456789)
print(f"ROS 2 Time 메시지: {ros2_time_message}")
from time import time의 time.time()
각각의 시간 관련 개념은 그 용도와 기능에 따라 사용됩니다. rclpy.time.Time
과 builtin_interfaces.msg._time.Time
은 ROS 2 환경에서 주로 사용되며, 시스템의 동기화와 메시지 타임스탬프를 관리하는 데 중점을 둡니다. 반면, time.time()
은 일반적인 Python 프로그램에서 현재 시간을 측정하거나 경과 시간을 계산하는 데 사용됩니다.