Fang-Robotics-MCB
Fang Robotics Team Codebase
Loading...
Searching...
No Matches
wrapped_float.hpp
Go to the documentation of this file.
1/*****************************************************************************/
2/********** !!! WARNING: CODE GENERATED BY TAPROOT. DO NOT EDIT !!! **********/
3/*****************************************************************************/
4
5/*
6 * Copyright (c) 2020-2023 Advanced Robotics at the University of Washington <robomstr@uw.edu>
7 *
8 * This file is part of Taproot.
9 *
10 * Taproot is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * Taproot is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with Taproot. If not, see <https://www.gnu.org/licenses/>.
22 */
23
24#ifndef TAPROOT_WRAPPED_FLOAT_HPP_
25#define TAPROOT_WRAPPED_FLOAT_HPP_
26
27#include <assert.h>
28
29#include <cmath>
30
31#include <modm/architecture/interface/assert.hpp>
32#include <modm/math/utils.hpp>
33
34#include "math_user_utils.hpp"
35
36namespace tap
37{
38namespace algorithms
39{
51{
52public:
58 WrappedFloat(float value, float lowerBound, float upperBound);
59
60 inline WrappedFloat withSameBounds(const float value) const
61 {
62 return WrappedFloat(value, this->lowerBound, this->upperBound);
63 }
64
65 // Overloaded Operators ----------------
66
75 bool operator==(const WrappedFloat& other) const;
76
84 void operator+=(const WrappedFloat& other);
85
93 void operator-=(const WrappedFloat& other);
94
103 WrappedFloat operator+(const WrappedFloat& other) const;
104
113 WrappedFloat operator-(const WrappedFloat& other) const;
114
122 void operator+=(float other);
123
131 void operator-=(float other);
132
141 WrappedFloat operator+(float other) const;
142
151 WrappedFloat operator-(float other) const;
152
161 float minDifference(const WrappedFloat& other) const;
162
172 float minDifference(const float& unwrappedValue) const;
173
182 WrappedFloat minInterpolate(const WrappedFloat& other, float alpha) const;
183
189 void shiftBounds(float shiftMagnitude);
190
218 static float limitValue(
219 const WrappedFloat& valueToLimit,
220 const WrappedFloat& min,
221 const WrappedFloat& max,
222 int* status);
223
242 static float limitValue(
243 const WrappedFloat& valueToLimit,
244 const float min,
245 const float max,
246 int* status);
247
261 bool withinRange(const WrappedFloat& lowerBound, const WrappedFloat& upperBound) const;
262
275 static float rangeOverlap(
276 const WrappedFloat& lowerA,
277 const WrappedFloat& upperA,
278 const WrappedFloat& lowerB,
279 const WrappedFloat& upperB);
280
281 // Getters/Setters ----------------
282
286 inline float getUnwrappedValue() const
287 {
288 return wrapped + (upperBound - lowerBound) * revolutions;
289 };
290
294 inline float getWrappedValue() const { return wrapped; };
295
299 inline void setWrappedValue(float newWrappedValue)
300 {
301 this->wrapped = newWrappedValue;
302 wrapValue();
303 };
304
308 inline void setUnwrappedValue(float newUnwrappedValue)
309 {
310 this->wrapped = newUnwrappedValue;
311 this->revolutions = 0;
312 wrapValue();
313 };
314
316 {
317 WrappedFloat out(*this);
318 out.revolutions = 0;
319 return out;
320 }
321
325 inline int getRevolutions() const { return revolutions; };
326
330 inline float getUpperBound() const { return upperBound; };
331
335 inline float getLowerBound() const { return lowerBound; };
336
341 static constexpr float EPSILON = 1E-8;
342
343private:
347 float wrapped;
348
352 int revolutions{0};
353
357 float lowerBound;
358
362 float upperBound;
363
367 void wrapValue();
368
369 inline static void assertBoundsEqual(const WrappedFloat& a, const WrappedFloat& b)
370 {
371 modm_assert(
373 "WrappedFloat::assertBoundsEqual",
374 "Lower bounds do not match");
375 modm_assert(
377 "WrappedFloat::assertBoundsEqual",
378 "Upper bounds do not match");
379 }
380
381 inline void assertBoundsEqual(const WrappedFloat& other) const
382 {
383 assertBoundsEqual(*this, other);
384 }
385
386}; // class WrappedFloat
387
391class Angle : public WrappedFloat
392{
393public:
394 inline Angle(const float value) : WrappedFloat(value, 0, M_TWOPI){};
395
396 static inline WrappedFloat fromDegrees(const float degrees)
397 {
398 return Angle(modm::toRadian(degrees));
399 }
400};
401
402} // namespace algorithms
403
404} // namespace tap
405
406#endif // TAPROOT_WRAPPED_FLOAT_HPP_
Definition wrapped_float.hpp:392
static WrappedFloat fromDegrees(const float degrees)
Definition wrapped_float.hpp:396
Angle(const float value)
Definition wrapped_float.hpp:394
Definition wrapped_float.hpp:51
WrappedFloat getNormalized() const
Definition wrapped_float.hpp:315
float getLowerBound() const
Definition wrapped_float.hpp:335
float getWrappedValue() const
Definition wrapped_float.hpp:294
WrappedFloat operator+(const WrappedFloat &other) const
Definition wrapped_float.cpp:65
static constexpr float EPSILON
Definition wrapped_float.hpp:341
void setUnwrappedValue(float newUnwrappedValue)
Definition wrapped_float.hpp:308
static float limitValue(const WrappedFloat &valueToLimit, const WrappedFloat &min, const WrappedFloat &max, int *status)
Definition wrapped_float.cpp:153
float minDifference(const WrappedFloat &other) const
Definition wrapped_float.cpp:97
bool withinRange(const WrappedFloat &lowerBound, const WrappedFloat &upperBound) const
Definition wrapped_float.cpp:190
void shiftBounds(float shiftMagnitude)
Definition wrapped_float.cpp:121
float getUpperBound() const
Definition wrapped_float.hpp:330
bool operator==(const WrappedFloat &other) const
Definition wrapped_float.cpp:40
void operator+=(const WrappedFloat &other)
Definition wrapped_float.cpp:47
WrappedFloat withSameBounds(const float value) const
Definition wrapped_float.hpp:60
static float rangeOverlap(const WrappedFloat &lowerA, const WrappedFloat &upperA, const WrappedFloat &lowerB, const WrappedFloat &upperB)
Definition wrapped_float.cpp:200
WrappedFloat operator-(const WrappedFloat &other) const
Definition wrapped_float.cpp:74
void operator-=(const WrappedFloat &other)
Definition wrapped_float.cpp:56
void setWrappedValue(float newWrappedValue)
Definition wrapped_float.hpp:299
int getRevolutions() const
Definition wrapped_float.hpp:325
float getUnwrappedValue() const
Definition wrapped_float.hpp:286
WrappedFloat minInterpolate(const WrappedFloat &other, float alpha) const
Definition wrapped_float.cpp:114
bool compareFloatClose(float val1, float val2, float epsilon)
Definition math_user_utils.hpp:57
Definition ballistics.cpp:29