WPILibC++  2020.3.2
Watchdog.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2018-2019 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #pragma once
9 
10 #include <chrono>
11 #include <functional>
12 #include <utility>
13 
14 #include <hal/cpp/fpga_clock.h>
15 #include <units/units.h>
16 #include <wpi/SafeThread.h>
17 #include <wpi/StringMap.h>
18 #include <wpi/StringRef.h>
19 #include <wpi/deprecated.h>
20 
21 namespace frc {
22 
32 class Watchdog {
33  public:
41  WPI_DEPRECATED("Use unit-safe version instead")
42  Watchdog(double timeout, std::function<void()> callback);
43 
51  Watchdog(units::second_t timeout, std::function<void()> callback);
52 
53  template <typename Callable, typename Arg, typename... Args>
54  WPI_DEPRECATED("Use unit-safe version instead")
55  Watchdog(double timeout, Callable&& f, Arg&& arg, Args&&... args)
56  : Watchdog(units::second_t{timeout}, arg, args...) {}
57 
58  template <typename Callable, typename Arg, typename... Args>
59  Watchdog(units::second_t timeout, Callable&& f, Arg&& arg, Args&&... args)
60  : Watchdog(timeout,
61  std::bind(std::forward<Callable>(f), std::forward<Arg>(arg),
62  std::forward<Args>(args)...)) {}
63 
64  ~Watchdog();
65 
66  Watchdog(Watchdog&&) = default;
67  Watchdog& operator=(Watchdog&&) = default;
68 
72  double GetTime() const;
73 
80  WPI_DEPRECATED("Use unit-safe version instead")
81  void SetTimeout(double timeout);
82 
89  void SetTimeout(units::second_t timeout);
90 
94  double GetTimeout() const;
95 
99  bool IsExpired() const;
100 
109  void AddEpoch(wpi::StringRef epochName);
110 
114  void PrintEpochs();
115 
121  void Reset();
122 
126  void Enable();
127 
131  void Disable();
132 
141  void SuppressTimeoutMessage(bool suppress);
142 
143  private:
144  // Used for timeout print rate-limiting
145  static constexpr std::chrono::milliseconds kMinPrintPeriod{1000};
146 
147  hal::fpga_clock::time_point m_startTime;
148  std::chrono::nanoseconds m_timeout;
149  hal::fpga_clock::time_point m_expirationTime;
150  std::function<void()> m_callback;
151  hal::fpga_clock::time_point m_lastTimeoutPrintTime = hal::fpga_clock::epoch();
152  hal::fpga_clock::time_point m_lastEpochsPrintTime = hal::fpga_clock::epoch();
153 
155  bool m_isExpired = false;
156 
157  bool m_suppressTimeoutMessage = false;
158 
159  class Thread;
161 
162  bool operator>(const Watchdog& rhs);
163 
164  static wpi::SafeThreadOwner<Thread>& GetThreadOwner();
165 };
166 
167 } // namespace frc
wpi::SafeThreadOwner< Thread >
frc::Watchdog::Disable
void Disable()
Disables the watchdog timer.
frc::Watchdog::IsExpired
bool IsExpired() const
Returns true if the watchdog timer has expired.
frc::Watchdog::Reset
void Reset()
Resets the watchdog timer.
frc::Watchdog::Watchdog
Watchdog(double timeout, std::function< void()> callback)
Watchdog constructor.
frc::Watchdog
A class that's a wrapper around a watchdog timer.
Definition: Watchdog.h:32
frc::Watchdog::SuppressTimeoutMessage
void SuppressTimeoutMessage(bool suppress)
Enable or disable suppression of the generic timeout message.
wpi::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
frc::Watchdog::GetTime
double GetTime() const
Returns the time in seconds since the watchdog was last fed.
wpi::StringMap< std::chrono::nanoseconds >
frc::Watchdog::AddEpoch
void AddEpoch(wpi::StringRef epochName)
Adds time since last epoch to the list printed by PrintEpochs().
frc
A class that enforces constraints on the differential drive kinematics.
Definition: SPIAccelerometerSim.h:16
frc::Watchdog::Enable
void Enable()
Enables the watchdog timer.
frc::Watchdog::SetTimeout
void SetTimeout(double timeout)
Sets the watchdog's timeout.
frc::Watchdog::GetTimeout
double GetTimeout() const
Returns the watchdog's timeout in seconds.
frc::Watchdog::PrintEpochs
void PrintEpochs()
Prints list of epochs added so far and their times.