WPILibC++ 2023.4.3-108-ge5452e3
Watchdog.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <functional>
8#include <string_view>
9#include <utility>
10
11#include <units/time.h>
12
13#include "frc/Tracer.h"
14
15namespace frc {
16
17/**
18 * A class that's a wrapper around a watchdog timer.
19 *
20 * When the timer expires, a message is printed to the console and an optional
21 * user-provided callback is invoked.
22 *
23 * The watchdog is initialized disabled, so the user needs to call Enable()
24 * before use.
25 */
26class Watchdog {
27 public:
28 /**
29 * Watchdog constructor.
30 *
31 * @param timeout The watchdog's timeout in seconds with microsecond
32 * resolution.
33 * @param callback This function is called when the timeout expires.
34 */
35 Watchdog(units::second_t timeout, std::function<void()> callback);
36
37 template <typename Callable, typename Arg, typename... Args>
38 Watchdog(units::second_t timeout, Callable&& f, Arg&& arg, Args&&... args)
39 : Watchdog(timeout,
40 std::bind(std::forward<Callable>(f), std::forward<Arg>(arg),
41 std::forward<Args>(args)...)) {}
42
44
47
48 /**
49 * Returns the time since the watchdog was last fed.
50 */
51 units::second_t GetTime() const;
52
53 /**
54 * Sets the watchdog's timeout.
55 *
56 * @param timeout The watchdog's timeout in seconds with microsecond
57 * resolution.
58 */
59 void SetTimeout(units::second_t timeout);
60
61 /**
62 * Returns the watchdog's timeout.
63 */
64 units::second_t GetTimeout() const;
65
66 /**
67 * Returns true if the watchdog timer has expired.
68 */
69 bool IsExpired() const;
70
71 /**
72 * Adds time since last epoch to the list printed by PrintEpochs().
73 *
74 * Epochs are a way to partition the time elapsed so that when overruns occur,
75 * one can determine which parts of an operation consumed the most time.
76 *
77 * @param epochName The name to associate with the epoch.
78 */
79 void AddEpoch(std::string_view epochName);
80
81 /**
82 * Prints list of epochs added so far and their times.
83 */
85
86 /**
87 * Resets the watchdog timer.
88 *
89 * This also enables the timer if it was previously disabled.
90 */
91 void Reset();
92
93 /**
94 * Enables the watchdog timer.
95 */
96 void Enable();
97
98 /**
99 * Disables the watchdog timer.
100 */
101 void Disable();
102
103 /**
104 * Enable or disable suppression of the generic timeout message.
105 *
106 * This may be desirable if the user-provided callback already prints a more
107 * specific message.
108 *
109 * @param suppress Whether to suppress generic timeout message.
110 */
111 void SuppressTimeoutMessage(bool suppress);
112
113 private:
114 // Used for timeout print rate-limiting
115 static constexpr auto kMinPrintPeriod = 1_s;
116
117 units::second_t m_startTime = 0_s;
118 units::second_t m_timeout;
119 units::second_t m_expirationTime = 0_s;
120 std::function<void()> m_callback;
121 units::second_t m_lastTimeoutPrintTime = 0_s;
122
123 Tracer m_tracer;
124 bool m_isExpired = false;
125
126 bool m_suppressTimeoutMessage = false;
127
128 class Impl;
129 Impl* m_impl;
130
131 bool operator>(const Watchdog& rhs) const;
132
133 static Impl* GetImpl();
134};
135
136} // namespace frc
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const ArgReturnType arg() const
Definition: ArrayCwiseUnaryOps.h:66
A class for keeping track of how much time it takes for different parts of code to execute.
Definition: Tracer.h:26
A class that's a wrapper around a watchdog timer.
Definition: Watchdog.h:26
Watchdog & operator=(Watchdog &&rhs)
Watchdog(Watchdog &&rhs)
bool IsExpired() const
Returns true if the watchdog timer has expired.
units::second_t GetTime() const
Returns the time since the watchdog was last fed.
void Disable()
Disables the watchdog timer.
void AddEpoch(std::string_view epochName)
Adds time since last epoch to the list printed by PrintEpochs().
void SuppressTimeoutMessage(bool suppress)
Enable or disable suppression of the generic timeout message.
Watchdog(units::second_t timeout, Callable &&f, Arg &&arg, Args &&... args)
Definition: Watchdog.h:38
void Reset()
Resets the watchdog timer.
units::second_t GetTimeout() const
Returns the watchdog's timeout.
void Enable()
Enables the watchdog timer.
void SetTimeout(units::second_t timeout)
Sets the watchdog's timeout.
Watchdog(units::second_t timeout, std::function< void()> callback)
Watchdog constructor.
void PrintEpochs()
Prints list of epochs added so far and their times.
basic_string_view< char > string_view
Definition: core.h:520
Definition: AprilTagPoseEstimator.h:15
Definition: BFloat16.h:88
constexpr bool operator>(const UNIT_LIB_DEFAULT_TYPE lhs, const Units &rhs) noexcept
Definition: base.h:2751