Skip to content

Commit

Permalink
Set the system timer resolution to the minimum value for higher preci…
Browse files Browse the repository at this point in the history
…sion on Windows (#907)
  • Loading branch information
GiulioRomualdi authored Oct 30, 2024
1 parent a60b73d commit 78bae85
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ All notable changes to this project are documented in this file.

### Changed
- Change device jtcvc to use motor velocity and joint velocity in input to PINN models (https://github.com/ami-iit/bipedal-locomotion-framework/pull/903)
- Set the system timer resolution to the minimum value for higher precision on `Windows` (https://github.com/ami-iit/bipedal-locomotion-framework/pull/907)

### Fixed
- Bug fix of `JointTorqueControlDevice` device (https://github.com/ami-iit/bipedal-locomotion-framework/pull/890)
Expand Down
6 changes: 6 additions & 0 deletions cmake/AddBipedalLocomotionLibrary.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ function(add_bipedal_locomotion_library)
PRIVATE_HEADERS
PUBLIC_LINK_LIBRARIES
PRIVATE_LINK_LIBRARIES
PRIVATE_WINDOWS_LINK_LIBRARIES
SUBDIRECTORIES)

set(prefix "bipedal_component")
Expand All @@ -32,6 +33,7 @@ function(add_bipedal_locomotion_library)
set(private_headers ${${prefix}_PRIVATE_HEADERS})
set(public_link_libraries ${${prefix}_PUBLIC_LINK_LIBRARIES})
set(private_link_libraries ${${prefix}_PRIVATE_LINK_LIBRARIES})
set(private_windows_link_libraries ${${prefix}_PRIVATE_WINDOWS_LINK_LIBRARIES})
set(subdirectories ${${prefix}_SUBDIRECTORIES})

if(NOT installation_folder)
Expand Down Expand Up @@ -76,6 +78,10 @@ function(add_bipedal_locomotion_library)
target_link_libraries(${name} PUBLIC ${public_link_libraries})
target_link_libraries(${name} PRIVATE ${private_link_libraries})

if(WIN32)
target_link_libraries(${name} PRIVATE ${private_windows_link_libraries})
endif()

set_target_properties(${name} PROPERTIES
OUTPUT_NAME "${PROJECT_NAME}${name}"
VERSION ${BipedalLocomotionFramework_VERSION}
Expand Down
31 changes: 16 additions & 15 deletions src/System/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,22 @@ if(FRAMEWORK_COMPILE_System)

# set target name
add_bipedal_locomotion_library(
NAME System
PUBLIC_HEADERS ${H_PREFIX}/InputPort.h ${H_PREFIX}/OutputPort.h
${H_PREFIX}/Advanceable.h ${H_PREFIX}/Source.h ${H_PREFIX}/Sink.h
${H_PREFIX}/Factory.h
${H_PREFIX}/VariablesHandler.h ${H_PREFIX}/LinearTask.h ${H_PREFIX}/ILinearTaskSolver.h ${H_PREFIX}/ILinearTaskFactory.h ${H_PREFIX}/ITaskControllerManager.h
${H_PREFIX}/IClock.h ${H_PREFIX}/StdClock.h ${H_PREFIX}/Clock.h
${H_PREFIX}/SharedResource.h ${H_PREFIX}/AdvanceableRunner.h
${H_PREFIX}/QuitHandler.h
${H_PREFIX}/Barrier.h ${H_PREFIX}/TimeProfiler.h
${H_PREFIX}/WeightProvider.h ${H_PREFIX}/ConstantWeightProvider.h
SOURCES src/VariablesHandler.cpp src/LinearTask.cpp
src/StdClock.cpp src/Clock.cpp src/QuitHandler.cpp src/Barrier.cpp
src/ConstantWeightProvider.cpp src/TimeProfiler.cpp
PUBLIC_LINK_LIBRARIES BipedalLocomotion::ParametersHandler Eigen3::Eigen
SUBDIRECTORIES tests YarpImplementation RosImplementation
NAME System
PUBLIC_HEADERS ${H_PREFIX}/InputPort.h ${H_PREFIX}/OutputPort.h
${H_PREFIX}/Advanceable.h ${H_PREFIX}/Source.h ${H_PREFIX}/Sink.h
${H_PREFIX}/Factory.h
${H_PREFIX}/VariablesHandler.h ${H_PREFIX}/LinearTask.h ${H_PREFIX}/ILinearTaskSolver.h ${H_PREFIX}/ILinearTaskFactory.h ${H_PREFIX}/ITaskControllerManager.h
${H_PREFIX}/IClock.h ${H_PREFIX}/StdClock.h ${H_PREFIX}/Clock.h
${H_PREFIX}/SharedResource.h ${H_PREFIX}/AdvanceableRunner.h
${H_PREFIX}/QuitHandler.h
${H_PREFIX}/Barrier.h ${H_PREFIX}/TimeProfiler.h
${H_PREFIX}/WeightProvider.h ${H_PREFIX}/ConstantWeightProvider.h
SOURCES src/VariablesHandler.cpp src/LinearTask.cpp
src/StdClock.cpp src/Clock.cpp src/QuitHandler.cpp src/Barrier.cpp
src/ConstantWeightProvider.cpp src/TimeProfiler.cpp
PUBLIC_LINK_LIBRARIES BipedalLocomotion::ParametersHandler Eigen3::Eigen
PRIVATE_WINDOWS_LINK_LIBRARIES Winmm
SUBDIRECTORIES tests YarpImplementation RosImplementation
)

endif()
27 changes: 27 additions & 0 deletions src/System/include/BipedalLocomotion/System/StdClock.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,33 @@ class StdClock final : public IClock
* threads to run.
*/
void yield() final;

private:

/**
* PrecisionScheduler is a class that allows to set the system timer resolution to the minimum
* value for higher precision. This class is used only on Windows systems.
*/
struct PrecisionScheduler
{
/**
* Constructor.
* It sets the system timer resolution to the minimum value for higher precision.
* @note Only affects Windows systems.
*/
PrecisionScheduler();

/**
* Destructor.
* It restores the system timer resolution to the default value.
* @note Only affects Windows systems.
*/
~PrecisionScheduler();
};

PrecisionScheduler m_precisionScheduler; /**< PrecisionScheduler object.
It is used only on Windows systems. */

};

class StdClockFactory final : public ClockFactory
Expand Down
28 changes: 28 additions & 0 deletions src/System/src/StdClock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
#include <chrono>
#include <thread>

#if defined(_WIN32)
#include <Windows.h>
#endif

#include <BipedalLocomotion/System/StdClock.h>

using namespace BipedalLocomotion::System;
Expand All @@ -32,6 +36,30 @@ void StdClock::yield()
std::this_thread::yield();
}

StdClock::PrecisionScheduler::PrecisionScheduler()
{
#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
}

StdClock::PrecisionScheduler::~PrecisionScheduler()
{
#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
}

IClock& StdClockFactory::createClock()
{
// Create the singleton. Meyers' implementation. It is automatically threadsafe
Expand Down

0 comments on commit 78bae85

Please sign in to comment.