Skip to content

Increase internal Windows timer rates #1565

@Flova

Description

@Flova

By default windows timing is only accurate to about ~15ms. Things like time.monotonic() tick with only that rate even if the datatype would allow for higher prescision. This is a common issue for things like Games as well, so newer windows versions offer ways to boost the scheduler rate. Fyi this does not only concern ROS timers, even if they might be affected by it.

In the past this has lead to a number of issues, that have been fixed mostly in a symptomatic manner. Some of them include:

The following PR includes a bit of off-topic discussion, including some simple experiments, regarding this isuue: #1563

As originally suggested by (@traversaro) possible fix could include something like this during the start/stop of rclpy:

WindowsSchedulerBooster()
{
#if defined(_WIN32)
    // Only affects Windows systems.
    TIMECAPS tm; // Stores system timer capabilities.
    // Get the minimum timer resolution supported by the system.
    timeGetDevCaps(&tm, sizeof(TIMECAPS));
    // Set the system timer resolution to the minimum value for higher precision.
    timeBeginPeriod(tm.wPeriodMin);
#endif
}

~WindowsSchedulerBooster()
{
#if defined(_WIN32)
    // Only affects Windows systems.
    TIMECAPS tm; // Stores system timer capabilities.
    // Get the minimum timer resolution supported by the system.
    timeGetDevCaps(&tm, sizeof(TIMECAPS));
    // Restore the system timer resolution to the default value.
    timeEndPeriod(tm.wPeriodMin);
#endif
}

Here is a blog post that talks about timer accuracy in windows: https://www.siliceum.com/en/blog/post/windows-high-resolution-timers/

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions