一、问题描述

因项目需要,需要"ros::Time::now()" 改成获取机器开机时间,此处针对rospy的机器时间修改。
ros time 时间戳改为机器开机时间-LMLPHP

二、修改方法

修改ros源码的文件 /opt/ros/noetic/lib/python3/dist-packages/rospy/rostime.py
修改如下:
定位到 get_rostime() ,并将 float_secs = time.time() 改为 float_secs = time.monotonic()

def get_rostime():
    """
    Get the current time as a L{Time} object    
    @return: current time as a L{rospy.Time} object
    @rtype: L{Time}
    """
    if not _rostime_initialized:
        raise rospy.exceptions.ROSInitException("time is not initialized. Have you called init_node()?")
    if _rostime_current is not None:
        # initialize with sim time
        return _rostime_current
    else:
        # initialize with wallclock
        float_secs = time.monotonic()  # float_secs = time.time()
        secs = int(float_secs)
        nsecs = int((float_secs - secs) * 1000000000)
        return Time(secs, nsecs)

修改后重启机器,时间戳打印如下:
ros time 时间戳改为机器开机时间-LMLPHP

03-22 21:11