Haptic Controller
Loading...
Searching...
No Matches
helpers.hpp
Go to the documentation of this file.
1#pragma once
2#include <math.h>
3#include <stdexcept>
4#include <wiring.h>
5#include <iostream>
6#include <optional>
7
13bool float_close_compare(float a, float b, float tol = 1e-6)
14{
15 return std::abs(a - b) < tol;
16}
17
24template <typename T>
25T limit(T input, const T max_value, const std::optional<T>(min_value) = std::nullopt)
26{
27 return std::min(std::max(input, min_value.value_or(-1.0 * max_value)), max_value);
28}
29
32template <typename T>
33struct Limits
34{
37
38 T over_limits(T compare) const
39 {
40 if (compare > upper)
41 {
42 return compare - upper;
43 }
44 if (compare < lower)
45 {
46 return compare - lower;
47 }
48 return 0.0;
49 }
50};
51
57template <typename T>
58T limit(T input, Limits<T> limits)
59{
60 return limit<T>(input, limits.upper, limits.lower);
61}
bool float_close_compare(float a, float b, float tol=1e-6)
Compare two floats for equality under a tolerance.
Definition helpers.hpp:13
T limit(T input, const T max_value, const std::optional< T >(min_value)=std::nullopt)
Clamps an input value to a.
Definition helpers.hpp:25
Limit struct to store upper and lower limits.
Definition helpers.hpp:34
T over_limits(T compare) const
Definition helpers.hpp:38
T upper
Definition helpers.hpp:36
T lower
Definition helpers.hpp:35