WPILibC++  2019.1.1-beta-4-16-gd817001
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
Watchdog.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2018 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 <wpi/SafeThread.h>
16 #include <wpi/StringMap.h>
17 #include <wpi/StringRef.h>
18 
19 namespace frc {
20 
30 class Watchdog {
31  public:
39  Watchdog(double timeout, std::function<void()> callback);
40 
41  template <typename Callable, typename Arg, typename... Args>
42  Watchdog(double timeout, Callable&& f, Arg&& arg, Args&&... args)
43  : Watchdog(timeout,
44  std::bind(std::forward<Callable>(f), std::forward<Arg>(arg),
45  std::forward<Args>(args)...)) {}
46 
47  ~Watchdog();
48 
49  Watchdog(Watchdog&&) = default;
50  Watchdog& operator=(Watchdog&&) = default;
51 
55  double GetTime() const;
56 
63  void SetTimeout(double timeout);
64 
68  double GetTimeout() const;
69 
73  bool IsExpired() const;
74 
83  void AddEpoch(wpi::StringRef epochName);
84 
88  void PrintEpochs();
89 
95  void Reset();
96 
100  void Enable();
101 
105  void Disable();
106 
115  void SuppressTimeoutMessage(bool suppress);
116 
117  private:
118  // Used for timeout print rate-limiting
119  static constexpr std::chrono::milliseconds kMinPrintPeriod{1000};
120 
121  hal::fpga_clock::time_point m_startTime;
122  std::chrono::microseconds m_timeout;
123  hal::fpga_clock::time_point m_expirationTime;
124  std::function<void()> m_callback;
125  hal::fpga_clock::time_point m_lastTimeoutPrintTime = hal::fpga_clock::epoch();
126  hal::fpga_clock::time_point m_lastEpochsPrintTime = hal::fpga_clock::epoch();
127 
129  bool m_isExpired = false;
130 
131  bool m_suppressTimeoutMessage = false;
132 
133  class Thread;
135 
136  bool operator>(const Watchdog& rhs);
137 
138  static wpi::SafeThreadOwner<Thread>& GetThreadOwner();
139 };
140 
141 } // namespace frc
WPILib FRC namespace.
Definition: SPIAccelerometerSim.h:18
void SuppressTimeoutMessage(bool suppress)
Enable or disable suppression of the generic timeout message.
void Enable()
Enables the watchdog timer.
double GetTimeout() const
Returns the watchdog's timeout in seconds.
bool IsExpired() const
Returns true if the watchdog timer has expired.
void Reset()
Resets the watchdog timer.
double GetTime() const
Returns the time in seconds since the watchdog was last fed.
void PrintEpochs()
Prints list of epochs added so far and their times.
void SetTimeout(double timeout)
Sets the watchdog's timeout.
void Disable()
Disables the watchdog timer.
A class that's a wrapper around a watchdog timer.
Definition: Watchdog.h:30
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
Watchdog(double timeout, std::function< void()> callback)
Watchdog constructor.
void AddEpoch(wpi::StringRef epochName)
Adds time since last epoch to the list printed by PrintEpochs().