Rate-limit Watchdog timeout prints to 1Hz (#1459)

This commit is contained in:
Tyler Veness
2018-12-07 19:39:02 -08:00
committed by Peter Johnson
parent 97ba195b88
commit 6d4326a560
5 changed files with 51 additions and 19 deletions

View File

@@ -9,6 +9,7 @@
#include <chrono>
#include <functional>
#include <utility>
#include <hal/cpp/fpga_clock.h>
#include <wpi/SafeThread.h>
@@ -37,6 +38,12 @@ class Watchdog {
*/
Watchdog(double timeout, std::function<void()> callback);
template <typename Callable, typename Arg, typename... Args>
Watchdog(double timeout, Callable&& f, Arg&& arg, Args&&... args)
: Watchdog(timeout,
std::bind(std::forward<Callable>(f), std::forward<Arg>(arg),
std::forward<Args>(args)...)) {}
~Watchdog();
Watchdog(Watchdog&&) = default;
@@ -98,10 +105,15 @@ class Watchdog {
void Disable();
private:
// Used for timeout print rate-limiting
static constexpr std::chrono::milliseconds kMinPrintPeriod{1000};
hal::fpga_clock::time_point m_startTime;
std::chrono::microseconds m_timeout;
hal::fpga_clock::time_point m_expirationTime;
std::function<void()> m_callback;
hal::fpga_clock::time_point m_lastTimeoutPrintTime = hal::fpga_clock::epoch();
hal::fpga_clock::time_point m_lastEpochsPrintTime = hal::fpga_clock::epoch();
wpi::StringMap<std::chrono::microseconds> m_epochs;
bool m_isExpired = false;