RaidZeroLib
Coroutine robot framework for VEX V5
Loading...
Searching...
No Matches
Settled.hpp
1#pragma once
2#include <optional>
3
4#include "RaidZeroLib/api/Units/Units.hpp"
5
6namespace rz {
7
13template <class Error>
14class Settled {
15 public:
16 struct Config {
17 Error tolerance;
20 };
21
22 explicit Settled(Config config) : config(config) {}
23
25 [[nodiscard]] bool update(Error error, Time now) {
26 if (au::abs(error) > config.tolerance) {
27 enteredAt.reset();
28 return false;
29 }
30 if (!enteredAt) {
31 enteredAt = now;
32 }
33 return now - *enteredAt >= config.time;
34 }
35
36 void reset() { enteredAt.reset(); }
37
38 private:
39 Config config;
40 std::optional<Time> enteredAt;
41};
42
43} // namespace rz
Settled(Config config)
Definition Settled.hpp:22
bool update(Error error, Time now)
Whether error (measured at now) has been within the tolerance for the configured time.
Definition Settled.hpp:25
void reset()
Definition Settled.hpp:36
Time now()
Time at the start of the current tick of the current runtime.
Definition Runtime.hpp:209
au::QuantityD< au::Seconds > Duration
Definition Units.hpp:16
au::QuantityPointD< au::Seconds > Time
Definition Units.hpp:17
Definition DriverControl.hpp:11
Definition Settled.hpp:16
Error tolerance
Definition Settled.hpp:17
Duration time
How long the error must stay within the tolerance. Zero means settled as soon as it is within it.
Definition Settled.hpp:19