[wpilib] Add timestamp getters with configurable time base (#7378)

This commit is contained in:
Jonah Bonner
2024-11-16 10:43:38 -05:00
committed by GitHub
parent 91142ba5fe
commit ca51197486
27 changed files with 180 additions and 53 deletions

View File

@@ -6,6 +6,7 @@
#include <stdint.h>
#include <functional>
#include <string>
#include <units/temperature.h>
@@ -78,6 +79,24 @@ class RobotController {
*/
static int32_t GetTeamNumber();
/**
* Sets a new source to provide the clock time in microseconds. Changing this
* affects the return value of {@code GetTime}.
*
* @param supplier Function to return the time in microseconds.
*/
static void SetTimeSource(std::function<uint64_t()> supplier);
/**
* Read the microsecond timestamp. By default, the time is based on the FPGA
* hardware clock in microseconds since the FPGA started. However, the return
* value of this method may be modified to use any time base, including
* non-monotonic and non-continuous time bases.
*
* @return The current time in microseconds.
*/
static uint64_t GetTime();
/**
* Read the microsecond-resolution timer on the FPGA.
*
@@ -320,6 +339,9 @@ class RobotController {
* @return The status of the CAN bus
*/
static CANStatus GetCANStatus();
private:
static std::function<uint64_t()> m_timeSource;
};
} // namespace frc

View File

@@ -56,6 +56,17 @@ class TimedRobot : public IterativeRobotBase {
~TimedRobot() override;
/**
* Return the system clock time in micrseconds for the start of the current
* periodic loop. This is in the same time base as Timer.GetFPGATimestamp(),
* but is stable through a loop. It is updated at the beginning of every
* periodic callback (including the normal periodic loop).
*
* @return Robot running time in microseconds, as of the start of the current
* periodic function.
*/
uint64_t GetLoopStartTime();
/**
* Add a callback to run at a specific period with a starting time offset.
*
@@ -103,6 +114,7 @@ class TimedRobot : public IterativeRobotBase {
hal::Handle<HAL_NotifierHandle, HAL_CleanNotifier> m_notifier;
std::chrono::microseconds m_startTime;
uint64_t m_loopStartTimeUs = 0;
wpi::priority_queue<Callback, std::vector<Callback>, std::greater<Callback>>
m_callbacks;

View File

@@ -118,6 +118,16 @@ class Timer {
*/
bool IsRunning() const;
/**
* Return the clock time in seconds. By default, the time is based on the FPGA
* hardware clock in seconds since the FPGA started. However, the return value
* of this method may be modified to use any time base, including
* non-monotonic time bases.
*
* @returns Robot running time in seconds.
*/
static units::second_t GetTimestamp();
/**
* Return the FPGA system clock time in seconds.
*