ReduxLib C++ 2024.2.0
Loading...
Searching...
No Matches
CooldownWarning.h
1// Copyright (c) Redux Robotics and other contributors.
2// This is open source and can be modified and shared under the 3-clause BSD license.
3
4#pragma once
5#include <units/time.h>
6#include <vector>
7#include <string>
8#include <frc/Timer.h>
9namespace redux::canand {
10/**
11 * Class that yells at the user if feed is called too often in too short a succession.
12 *
13 * Typically used to help prevent obliterating flash.
14 */
16
17 public:
18 /**
19 * Alias for std::vector's size_type.
20 */
21 using size_type = std::vector<units::second_t>::size_type;
22 /**
23 * Constructor.
24 * @param threshold Maximum number of seconds that need to pass between the first and last calls
25 * @param cnt Number of calls that must pass within thresholdSeconds to trigger the warning
26 */
27 inline CooldownWarning(units::second_t threshold, size_type cnt) : threshold(threshold), sz(cnt) {
28 for (size_type i = 0; i < sz; i++) {
29 count.push_back(0_ms);
30 }
31 };
32
33 /**
34 * Feed the CooldownWarning.
35 * @return whether the error should trigger
36 */
37 inline bool feed() {
38 if (latch) return false;
39 units::second_t now = frc::Timer::GetFPGATimestamp();
40 count[idx] = now;
41 idx = (idx + 1) % sz;
42 units::second_t past = count[idx];
43 if ((now - past) < threshold) {
44 return true;
45 }
46 return false;
47 }
48 private:
49 units::second_t threshold;
50 std::vector<units::second_t> count;
51 size_type sz;
52 size_type idx{0};
53 bool latch{false};
54};
55}
Definition: CooldownWarning.h:15
bool feed()
Definition: CooldownWarning.h:37
CooldownWarning(units::second_t threshold, size_type cnt)
Definition: CooldownWarning.h:27
std::vector< units::second_t >::size_type size_type
Definition: CooldownWarning.h:21
Definition: CanandMessage.h:10