ros下Time类能获取系统从1970-1-1至今累计的时间,通过秒及纳秒表示。win下使用ros的Time类以获取时间戳时,发现时间戳计算错误,计时源代码在time.cpp中的ros_walltime函数中:
void ros_walltime(uint32_t& sec, uint32_t& nsec)
{
#if !defined(_WIN32)
#if HAS_CLOCK_GETTIME
timespec start;
clock_gettime(CLOCK_REALTIME, &start);
if (start.tv_sec < 0 || start.tv_sec > std::numeric_limits<uint32_t>::max())
throw std::runtime_error("Timespec is out of dual 32-bit range");
sec = start.tv_sec;
nsec = start.tv_nsec;
#else
struct timeval timeofday;
gettimeofday(&timeofday,NULL);
if (timeofday.tv_sec < 0 || timeofday.tv_sec > std::numeric_limits<uint32_t>::max())
throw std::runtime_error("Timeofday is out of dual signed 32-bit range");
sec = timeofday.tv_sec;
nsec = timeofday.tv_usec * 1000;
#endif
#else
// Win32 implementation
// unless I've missed something obvious, the only way to get high-precision
// time on Windows is via the QueryPerformanceCounter() call. However,
// this is somewhat problematic in Windows XP on some processors, especially
// AMD, because the Windows implementation can freak out when the CPU clocks
// down to save power. Time can jump or even go backwards. Microsoft has
// fixed this bug for most systems now, but it can still show up if you have
// not installed the latest CPU drivers (an oxymoron). They fixed all these
// problems in Windows Vista, and this API is by far the most accurate that
// I know of in Windows, so I'll use it here despite all these caveats
static LARGE_INTEGER cpu_freq, init_cpu_time;
static uint32_t start_sec = 0;
static uint32_t start_nsec = 0;
if ( ( start_sec == 0 ) && ( start_nsec == 0 ) )
{
QueryPerformanceFrequency(&cpu_freq);
if (cpu_freq.QuadPart == 0) {
throw NoHighPerformanceTimersException();
}
QueryPerformanceCounter(&init_cpu_time);
// compute an offset from the Epoch using the lower-performance timer API
FILETIME ft;
GetSystemTimeAsFileTime(&ft);
LARGE_INTEGER start_li;
start_li.LowPart = ft.dwLowDateTime;
start_li.HighPart = ft.dwHighDateTime;
// why did they choose 1601 as the time zero, instead of 1970?
// there were no outstanding hard rock bands in 1601.
#ifdef _MSC_VER
start_li.QuadPart -= 116444736000000000Ui64;
#else
start_li.QuadPart -= 116444736000000000ULL;
#endif
int64_t start_sec64 = start_li.QuadPart / 10000000; // 100-ns units
if (start_sec64 < 0 || start_sec64 > std::numeric_limits<uint32_t>::max())
throw std::runtime_error("SystemTime is out of dual 32-bit range");
start_sec = (uint32_t)start_sec64;
start_nsec = (start_li.LowPart % 10000000) * 100;
}
LARGE_INTEGER cur_time;
QueryPerformanceCounter(&cur_time);
LARGE_INTEGER delta_cpu_time;
delta_cpu_time.QuadPart = cur_time.QuadPart - init_cpu_time.QuadPart;
// todo: how to handle cpu clock drift. not sure it's a big deal for us.
// also, think about clock wraparound. seems extremely unlikey, but possible
double d_delta_cpu_time = delta_cpu_time.QuadPart / (double) cpu_freq.QuadPart;
uint32_t delta_sec = (uint32_t) floor(d_delta_cpu_time);
uint32_t delta_nsec = (uint32_t) boost::math::round((d_delta_cpu_time-delta_sec) * 1e9);
int64_t sec_sum = (int64_t)start_sec + (int64_t)delta_sec;
int64_t nsec_sum = (int64_t)start_nsec + (int64_t)delta_nsec;
// Throws an exception if we go out of 32-bit range
normalizeSecNSecUnsigned(sec_sum, nsec_sum);
sec = sec_sum;
nsec = nsec_sum;
#endif
}
错误是由代码第57行start_nsec = (start_li.LowPart % 10000000) * 100;引起的,计算纳秒时间时采用了低位数值取余计算导致了错误,改为start_nsec = (start_li.QuadPart% 10000000) * 100;就正常了。
8886

被折叠的 条评论
为什么被折叠?



