1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#pragma once
#include <functional>
#include <string_view>
#include <utility>
#include <units/time.h>
#include "frc/Tracer.h"
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.
*
* @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);
template <typename Callable, typename Arg, typename... Args>
Watchdog(units::second_t 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&& rhs);
Watchdog& operator=(Watchdog&& rhs);
/**
* Returns the time since the watchdog was last fed.
*/
units::second_t GetTime() const;
/**
* Sets the watchdog's timeout.
*
* @param timeout The watchdog's timeout in seconds with microsecond
* resolution.
*/
void SetTimeout(units::second_t timeout);
/**
* Returns the watchdog's timeout.
*/
units::second_t GetTimeout() const;
/**
* Returns true if the watchdog timer has expired.
*/
bool IsExpired() const;
/**
* Adds time since last epoch to the list printed by PrintEpochs().
*
* 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.
*
* @param epochName The name to associate with the epoch.
*/
void AddEpoch(std::string_view 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();
/**
* Disables the watchdog timer.
*/
void Disable();
/**
* 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);
private:
// Used for timeout print rate-limiting
static constexpr auto kMinPrintPeriod = 1_s;
units::second_t m_startTime = 0_s;
units::second_t m_timeout;
units::second_t m_expirationTime = 0_s;
std::function<void()> m_callback;
units::second_t m_lastTimeoutPrintTime = 0_s;
Tracer m_tracer;
bool m_isExpired = false;
bool m_suppressTimeoutMessage = false;
class Impl;
Impl* m_impl;
bool operator>(const Watchdog& rhs) const;
static Impl* GetImpl();
};
} // namespace frc