2018-06-24 02:29:21 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2020-03-31 20:43:04 -07:00
|
|
|
/* Copyright (c) 2018-2020 FIRST. All Rights Reserved. */
|
2018-06-24 02:29:21 -05:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <functional>
|
2018-12-07 19:39:02 -08:00
|
|
|
#include <utility>
|
2018-06-24 02:29:21 -05:00
|
|
|
|
2020-06-29 22:25:09 -07:00
|
|
|
#include <units/time.h>
|
2018-06-24 02:29:21 -05:00
|
|
|
#include <wpi/StringRef.h>
|
2019-09-03 15:58:31 -07:00
|
|
|
#include <wpi/deprecated.h>
|
2018-06-24 02:29:21 -05:00
|
|
|
|
2020-04-01 23:10:28 -04:00
|
|
|
#include "frc/Tracer.h"
|
|
|
|
|
|
2018-06-24 02:29:21 -05:00
|
|
|
namespace frc {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A class that's a wrapper around a watchdog timer.
|
|
|
|
|
*
|
|
|
|
|
* When the timer expires, a message is printed to the console and an optional
|
|
|
|
|
* user-provided callback is invoked.
|
|
|
|
|
*
|
|
|
|
|
* The watchdog is initialized disabled, so the user needs to call Enable()
|
|
|
|
|
* before use.
|
|
|
|
|
*/
|
|
|
|
|
class Watchdog {
|
|
|
|
|
public:
|
|
|
|
|
/**
|
|
|
|
|
* Watchdog constructor.
|
|
|
|
|
*
|
2020-03-31 20:43:04 -07:00
|
|
|
* @deprecated use unit-safe version instead.
|
|
|
|
|
* Watchdog(units::second_t timeout, std::function<void()> callback)
|
|
|
|
|
*
|
2018-12-01 00:05:33 -08:00
|
|
|
* @param timeout The watchdog's timeout in seconds with microsecond
|
|
|
|
|
* resolution.
|
2018-06-24 02:29:21 -05:00
|
|
|
* @param callback This function is called when the timeout expires.
|
|
|
|
|
*/
|
2019-09-03 15:58:31 -07:00
|
|
|
WPI_DEPRECATED("Use unit-safe version instead")
|
2018-12-01 00:05:33 -08:00
|
|
|
Watchdog(double timeout, std::function<void()> callback);
|
|
|
|
|
|
2019-09-03 15:58:31 -07:00
|
|
|
/**
|
|
|
|
|
* Watchdog constructor.
|
|
|
|
|
*
|
|
|
|
|
* @param timeout The watchdog's timeout in seconds with microsecond
|
|
|
|
|
* resolution.
|
|
|
|
|
* @param callback This function is called when the timeout expires.
|
|
|
|
|
*/
|
|
|
|
|
Watchdog(units::second_t timeout, std::function<void()> callback);
|
|
|
|
|
|
2018-12-07 19:39:02 -08:00
|
|
|
template <typename Callable, typename Arg, typename... Args>
|
2019-09-03 15:58:31 -07:00
|
|
|
WPI_DEPRECATED("Use unit-safe version instead")
|
2018-12-07 19:39:02 -08:00
|
|
|
Watchdog(double timeout, Callable&& f, Arg&& arg, Args&&... args)
|
2019-09-03 15:58:31 -07:00
|
|
|
: Watchdog(units::second_t{timeout}, arg, args...) {}
|
|
|
|
|
|
|
|
|
|
template <typename Callable, typename Arg, typename... Args>
|
|
|
|
|
Watchdog(units::second_t timeout, Callable&& f, Arg&& arg, Args&&... args)
|
2018-12-07 19:39:02 -08:00
|
|
|
: Watchdog(timeout,
|
|
|
|
|
std::bind(std::forward<Callable>(f), std::forward<Arg>(arg),
|
|
|
|
|
std::forward<Args>(args)...)) {}
|
|
|
|
|
|
2018-12-01 00:05:33 -08:00
|
|
|
~Watchdog();
|
2018-06-24 02:29:21 -05:00
|
|
|
|
2020-07-21 22:58:16 -07:00
|
|
|
Watchdog(Watchdog&& rhs);
|
|
|
|
|
Watchdog& operator=(Watchdog&& rhs);
|
2018-06-24 02:29:21 -05:00
|
|
|
|
|
|
|
|
/**
|
2018-12-01 00:05:33 -08:00
|
|
|
* Returns the time in seconds since the watchdog was last fed.
|
2018-06-24 02:29:21 -05:00
|
|
|
*/
|
|
|
|
|
double GetTime() const;
|
|
|
|
|
|
2018-12-01 00:05:33 -08:00
|
|
|
/**
|
|
|
|
|
* Sets the watchdog's timeout.
|
|
|
|
|
*
|
2020-03-31 20:43:04 -07:00
|
|
|
* @deprecated use the unit safe version instead.
|
|
|
|
|
* SetTimeout(units::second_t timeout)
|
|
|
|
|
*
|
2018-12-01 00:05:33 -08:00
|
|
|
* @param timeout The watchdog's timeout in seconds with microsecond
|
|
|
|
|
* resolution.
|
|
|
|
|
*/
|
2019-09-03 15:58:31 -07:00
|
|
|
WPI_DEPRECATED("Use unit-safe version instead")
|
2018-12-01 00:05:33 -08:00
|
|
|
void SetTimeout(double timeout);
|
|
|
|
|
|
2019-09-03 15:58:31 -07:00
|
|
|
/**
|
|
|
|
|
* Sets the watchdog's timeout.
|
|
|
|
|
*
|
|
|
|
|
* @param timeout The watchdog's timeout in seconds with microsecond
|
|
|
|
|
* resolution.
|
|
|
|
|
*/
|
|
|
|
|
void SetTimeout(units::second_t timeout);
|
|
|
|
|
|
2018-12-01 00:05:33 -08:00
|
|
|
/**
|
|
|
|
|
* Returns the watchdog's timeout in seconds.
|
|
|
|
|
*/
|
|
|
|
|
double GetTimeout() const;
|
|
|
|
|
|
2018-06-24 02:29:21 -05:00
|
|
|
/**
|
|
|
|
|
* Returns true if the watchdog timer has expired.
|
|
|
|
|
*/
|
|
|
|
|
bool IsExpired() const;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adds time since last epoch to the list printed by PrintEpochs().
|
|
|
|
|
*
|
2018-09-26 22:53:34 -07:00
|
|
|
* Epochs are a way to partition the time elapsed so that when overruns occur,
|
|
|
|
|
* one can determine which parts of an operation consumed the most time.
|
|
|
|
|
*
|
2018-06-24 02:29:21 -05:00
|
|
|
* @param epochName The name to associate with the epoch.
|
|
|
|
|
*/
|
|
|
|
|
void AddEpoch(wpi::StringRef epochName);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Prints list of epochs added so far and their times.
|
|
|
|
|
*/
|
|
|
|
|
void PrintEpochs();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Resets the watchdog timer.
|
|
|
|
|
*
|
|
|
|
|
* This also enables the timer if it was previously disabled.
|
|
|
|
|
*/
|
|
|
|
|
void Reset();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Enables the watchdog timer.
|
|
|
|
|
*/
|
|
|
|
|
void Enable();
|
|
|
|
|
|
|
|
|
|
/**
|
2018-12-01 00:05:33 -08:00
|
|
|
* Disables the watchdog timer.
|
2018-06-24 02:29:21 -05:00
|
|
|
*/
|
|
|
|
|
void Disable();
|
|
|
|
|
|
2018-12-29 16:22:54 -08:00
|
|
|
/**
|
|
|
|
|
* Enable or disable suppression of the generic timeout message.
|
|
|
|
|
*
|
|
|
|
|
* This may be desirable if the user-provided callback already prints a more
|
|
|
|
|
* specific message.
|
|
|
|
|
*
|
|
|
|
|
* @param suppress Whether to suppress generic timeout message.
|
|
|
|
|
*/
|
|
|
|
|
void SuppressTimeoutMessage(bool suppress);
|
|
|
|
|
|
2018-06-24 02:29:21 -05:00
|
|
|
private:
|
2018-12-07 19:39:02 -08:00
|
|
|
// Used for timeout print rate-limiting
|
2020-07-21 22:58:16 -07:00
|
|
|
static constexpr units::second_t kMinPrintPeriod = 1_s;
|
2018-12-07 19:39:02 -08:00
|
|
|
|
2020-07-21 22:58:16 -07:00
|
|
|
units::second_t m_startTime = 0_s;
|
|
|
|
|
units::second_t m_timeout;
|
|
|
|
|
units::second_t m_expirationTime = 0_s;
|
2018-06-24 02:29:21 -05:00
|
|
|
std::function<void()> m_callback;
|
2020-07-21 22:58:16 -07:00
|
|
|
units::second_t m_lastTimeoutPrintTime = 0_s;
|
2018-06-24 02:29:21 -05:00
|
|
|
|
2020-04-01 23:10:28 -04:00
|
|
|
Tracer m_tracer;
|
2018-06-24 02:29:21 -05:00
|
|
|
bool m_isExpired = false;
|
|
|
|
|
|
2018-12-29 16:22:54 -08:00
|
|
|
bool m_suppressTimeoutMessage = false;
|
|
|
|
|
|
2020-07-21 22:58:16 -07:00
|
|
|
class Impl;
|
|
|
|
|
Impl* m_impl;
|
2018-12-01 00:05:33 -08:00
|
|
|
|
|
|
|
|
bool operator>(const Watchdog& rhs);
|
|
|
|
|
|
2020-07-21 22:58:16 -07:00
|
|
|
static Impl* GetImpl();
|
2018-06-24 02:29:21 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace frc
|