From 297ae353c2cdde859b8917684e8c637b13a05cd2 Mon Sep 17 00:00:00 2001 From: Davide Basso <davide.basso@skywarder.eu> Date: Tue, 27 Feb 2024 18:44:19 +0000 Subject: [PATCH] [Units] Add measurements units API --- CMakeLists.txt | 1 + src/shared/units/Acceleration.h | 66 +++++++++++ src/shared/units/Angle.h | 71 ++++++++++++ src/shared/units/Length.h | 96 ++++++++++++++++ src/shared/units/Pressure.h | 78 +++++++++++++ src/shared/units/Speed.h | 70 ++++++++++++ src/shared/units/Time.h | 111 ++++++++++++++++++ src/shared/units/Units.h | 194 ++++++++++++++++++++++++++++++++ src/tests/catch/test-units.cpp | 106 +++++++++++++++++ 9 files changed, 793 insertions(+) create mode 100644 src/shared/units/Acceleration.h create mode 100644 src/shared/units/Angle.h create mode 100644 src/shared/units/Length.h create mode 100644 src/shared/units/Pressure.h create mode 100644 src/shared/units/Speed.h create mode 100644 src/shared/units/Time.h create mode 100644 src/shared/units/Units.h create mode 100644 src/tests/catch/test-units.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index aaba63944..f50a6b1ba 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -134,6 +134,7 @@ add_executable(catch-tests-boardcore src/tests/catch/test-MEA.cpp src/tests/catch/test-airbrakesInterp.cpp src/tests/catch/test-pitot.cpp + src/tests/catch/test-units.cpp ) target_compile_definitions(catch-tests-boardcore PRIVATE USE_MOCK_PERIPHERALS) sbs_target(catch-tests-boardcore stm32f429zi_stm32f4discovery) diff --git a/src/shared/units/Acceleration.h b/src/shared/units/Acceleration.h new file mode 100644 index 000000000..eac700e5a --- /dev/null +++ b/src/shared/units/Acceleration.h @@ -0,0 +1,66 @@ +/* Copyright (c) 2023 Skyward Experimental Rocketry + * Authors: Davide Basso + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#pragma once + +#include <ratio> + +#include "Units.h" + +namespace Boardcore +{ +namespace Units +{ +namespace Acceleration +{ + +template <class Ratio = std::ratio<1>> +using Acceleration = Unit<UnitKind::Acceleration, Ratio>; + +template <class ToAcceleration, class FromAcceleration> +ToAcceleration acceleration_cast(FromAcceleration const &from) +{ + return ToAcceleration(from); +} + +using MeterPerSecondSquared = Acceleration<>; // Acceleration in m/s^2 +using G = Acceleration<std::ratio<981, 100>>; // Acceleration in Gs + +// Floats +constexpr auto operator""_mps2(long double n) +{ + return MeterPerSecondSquared(static_cast<float>(n)); +}; +constexpr auto operator""_g(long double n) { return G(static_cast<float>(n)); }; +// Integers +constexpr auto operator""_mps2(unsigned long long n) +{ + return MeterPerSecondSquared(static_cast<float>(n)); +}; +constexpr auto operator""_g(unsigned long long n) +{ + return G(static_cast<float>(n)); +}; + +} // namespace Acceleration +} // namespace Units +} // namespace Boardcore \ No newline at end of file diff --git a/src/shared/units/Angle.h b/src/shared/units/Angle.h new file mode 100644 index 000000000..6a1c8eef1 --- /dev/null +++ b/src/shared/units/Angle.h @@ -0,0 +1,71 @@ +/* Copyright (c) 2023 Skyward Experimental Rocketry + * Authors: Davide Basso + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#pragma once + +#include <ratio> + +#include "Units.h" + +namespace Boardcore +{ +namespace Units +{ +namespace Angle +{ + +template <class Ratio = std::ratio<1>> +using Angle = Unit<UnitKind::Angle, Ratio>; + +template <class ToAngle, class FromAngle> +ToAngle angle_cast(FromAngle const &from) +{ + return ToAngle(from); +} + +using Degree = Angle<>; // Angle in degrees +using Radian = // Angle in radians + Angle< + std::ratio<static_cast<std::intmax_t>(180 * 1e10), + static_cast<std::intmax_t>(3.14159265358979323846 * 1e10)>>; +// Floats +constexpr auto operator""_rad(long double n) +{ + return Radian(static_cast<float>(n)); +}; +constexpr auto operator""_deg(long double n) +{ + return Degree(static_cast<float>(n)); +}; +// Integers +constexpr auto operator""_rad(unsigned long long n) +{ + return Radian(static_cast<float>(n)); +}; +constexpr auto operator""_deg(unsigned long long n) +{ + return Degree(static_cast<float>(n)); +}; + +} // namespace Angle +} // namespace Units +} // namespace Boardcore \ No newline at end of file diff --git a/src/shared/units/Length.h b/src/shared/units/Length.h new file mode 100644 index 000000000..cb6f024c5 --- /dev/null +++ b/src/shared/units/Length.h @@ -0,0 +1,96 @@ +/* Copyright (c) 2023 Skyward Experimental Rocketry + * Authors: Davide Basso + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#pragma once + +#include <ratio> + +#include "Units.h" + +namespace Boardcore +{ +namespace Units +{ +namespace Length +{ + +template <class Ratio = std::ratio<1>> +using Length = Unit<UnitKind::Length, Ratio>; + +template <class ToLength, class FromLength> +ToLength length_cast(FromLength const &from) +{ + return ToLength(from); +} + +using Millimeter = Length<std::milli>; // Length in millimeters +using Centimeter = Length<std::centi>; // Length in centimeters +using Decimeter = Length<std::deci>; // Length in decimeters +using Meter = Length<>; // Length in meters +using Kilometer = Length<std::kilo>; // Length in kilometers + +// Floats +constexpr auto operator""_mm(long double n) +{ + return Millimeter(static_cast<float>(n)); +}; +constexpr auto operator""_cm(long double n) +{ + return Centimeter(static_cast<float>(n)); +}; +constexpr auto operator""_dm(long double n) +{ + return Decimeter(static_cast<float>(n)); +}; +constexpr auto operator""_m(long double n) +{ + return Meter(static_cast<float>(n)); +}; +constexpr auto operator""_km(long double n) +{ + return Kilometer(static_cast<float>(n)); +}; +// Integers +constexpr auto operator""_mm(unsigned long long n) +{ + return Millimeter(static_cast<float>(n)); +}; +constexpr auto operator""_cm(unsigned long long n) +{ + return Centimeter(static_cast<float>(n)); +}; +constexpr auto operator""_dm(unsigned long long n) +{ + return Decimeter(static_cast<float>(n)); +}; +constexpr auto operator""_m(unsigned long long n) +{ + return Meter(static_cast<float>(n)); +}; +constexpr auto operator""_km(unsigned long long n) +{ + return Kilometer(static_cast<float>(n)); +}; + +} // namespace Length +} // namespace Units +} // namespace Boardcore \ No newline at end of file diff --git a/src/shared/units/Pressure.h b/src/shared/units/Pressure.h new file mode 100644 index 000000000..30dc96bb0 --- /dev/null +++ b/src/shared/units/Pressure.h @@ -0,0 +1,78 @@ +/* Copyright (c) 2023 Skyward Experimental Rocketry + * Authors: Davide Basso + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#pragma once + +#include <ratio> + +#include "Units.h" + +namespace Boardcore +{ +namespace Units +{ +namespace Pressure +{ + +template <class Ratio = std::ratio<1>> +using Pressure = Unit<UnitKind::Pressure, Ratio>; + +template <class ToPressure, class FromPressure> +ToPressure pressure_cast(FromPressure const &from) +{ + return ToPressure(from); +} + +using Pascal = Pressure<>; // Pressure in Pascals +using Bar = Pressure<std::ratio<100000>>; // Pressure in Bars +using Atm = Pressure<std::ratio<101325>>; // Pressure in Atmospheres + +// Floats +constexpr auto operator""_pa(long double n) +{ + return Pascal(static_cast<float>(n)); +}; +constexpr auto operator""_bar(long double n) +{ + return Bar(static_cast<float>(n)); +}; +constexpr auto operator""_atm(long double n) +{ + return Atm(static_cast<float>(n)); +}; +// Integers +constexpr auto operator""_pa(unsigned long long n) +{ + return Pascal(static_cast<float>(n)); +}; +constexpr auto operator""_bar(unsigned long long n) +{ + return Bar(static_cast<float>(n)); +}; +constexpr auto operator""_atm(unsigned long long n) +{ + return Atm(static_cast<float>(n)); +}; + +} // namespace Pressure +} // namespace Units +} // namespace Boardcore \ No newline at end of file diff --git a/src/shared/units/Speed.h b/src/shared/units/Speed.h new file mode 100644 index 000000000..051d90e07 --- /dev/null +++ b/src/shared/units/Speed.h @@ -0,0 +1,70 @@ +/* Copyright (c) 2023 Skyward Experimental Rocketry + * Authors: Davide Basso + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#pragma once + +#include <ratio> + +#include "Units.h" + +namespace Boardcore +{ +namespace Units +{ +namespace Speed +{ + +template <class Ratio = std::ratio<1>> +using Speed = Unit<UnitKind::Speed, Ratio>; + +template <class ToSpeed, class FromSpeed> +ToSpeed speed_cast(FromSpeed const &from) +{ + return ToSpeed(from); +} + +using MeterPerSecond = Speed<>; // Speed in meters per second +using KilometerPerHour = + Speed<std::ratio<1000, 3600>>; // Speed in kilometers per hour + +// Floats +constexpr auto operator""_mps(long double n) +{ + return MeterPerSecond(static_cast<float>(n)); +}; +constexpr auto operator""_kmh(long double n) +{ + return KilometerPerHour(static_cast<float>(n)); +}; +// Integers +constexpr auto operator""_mps(unsigned long long n) +{ + return MeterPerSecond(static_cast<float>(n)); +}; +constexpr auto operator""_kmh(unsigned long long n) +{ + return KilometerPerHour(static_cast<float>(n)); +}; + +} // namespace Speed +} // namespace Units +} // namespace Boardcore \ No newline at end of file diff --git a/src/shared/units/Time.h b/src/shared/units/Time.h new file mode 100644 index 000000000..155fef4d9 --- /dev/null +++ b/src/shared/units/Time.h @@ -0,0 +1,111 @@ +/* Copyright (c) 2023 Skyward Experimental Rocketry + * Authors: Davide Basso + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#pragma once + +#include <chrono> +#include <ratio> + +#include "Units.h" + +namespace Boardcore +{ +namespace Units +{ +namespace Time +{ + +template <class Ratio = std::ratio<1>> +using Time = Unit<UnitKind::Time, Ratio>; + +template <class ToTime, class FromTime> +ToTime time_cast(FromTime const &from) +{ + return ToTime(from); +} + +std::chrono::duration<float> to_chrono(Time<> const &from) +{ + return std::chrono::duration<float>(from.value()); +} + +using Nanosecond = Time<std::ratio<1, 1000000000>>; // Time in nanoseconds +using Microsecond = Time<std::ratio<1, 1000000>>; // Time in microseconds +using Millisecond = Time<std::ratio<1, 1000>>; // Time in milliseconds +using Second = Time<>; // Time in seconds +using Minute = Time<std::ratio<60>>; // Time in minutes +using Hour = Time<std::ratio<3600>>; // Time in hours + +// Floats +constexpr auto operator""_ns(long double n) +{ + return Nanosecond(static_cast<float>(n)); +}; +constexpr auto operator""_us(long double n) +{ + return Microsecond(static_cast<float>(n)); +}; +constexpr auto operator""_ms(long double n) +{ + return Millisecond(static_cast<float>(n)); +}; +constexpr auto operator""_s(long double n) +{ + return Second(static_cast<float>(n)); +}; +constexpr auto operator""_min(long double n) +{ + return Minute(static_cast<float>(n)); +}; +constexpr auto operator""_h(long double n) +{ + return Hour(static_cast<float>(n)); +}; +// Integers +constexpr auto operator""_ns(unsigned long long n) +{ + return Nanosecond(static_cast<float>(n)); +}; +constexpr auto operator""_us(unsigned long long n) +{ + return Microsecond(static_cast<float>(n)); +}; +constexpr auto operator""_ms(unsigned long long n) +{ + return Millisecond(static_cast<float>(n)); +}; +constexpr auto operator""_s(unsigned long long n) +{ + return Second(static_cast<float>(n)); +}; +constexpr auto operator""_min(unsigned long long n) +{ + return Minute(static_cast<float>(n)); +}; +constexpr auto operator""_h(unsigned long long n) +{ + return Hour(static_cast<float>(n)); +}; + +} // namespace Time +} // namespace Units +} // namespace Boardcore \ No newline at end of file diff --git a/src/shared/units/Units.h b/src/shared/units/Units.h new file mode 100644 index 000000000..6e59cbcd0 --- /dev/null +++ b/src/shared/units/Units.h @@ -0,0 +1,194 @@ +/* Copyright (c) 2023 Skyward Experimental Rocketry + * Authors: Davide Basso + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#pragma once + +#include <utils/Debug.h> + +#include <ratio> +#include <typeinfo> + +namespace Boardcore +{ +namespace Units +{ + +enum class UnitKind +{ + Angle, + Length, + Pressure, + Time, + Speed, + Acceleration, +}; + +// Base class to implement custom measurement units logic. +template <UnitKind Kind, class Ratio = std::ratio<1>> +class Unit +{ +public: + constexpr explicit Unit(float val) : _value(val) {} + + template <UnitKind FromKind, class FromRatio> + constexpr explicit Unit(Unit<FromKind, FromRatio> const &from) + : _value(from.template value<Ratio>()) + { + } + + // Get the value of the unit in the specified ratio. + template <class TargetRatio = Ratio> + constexpr float value() const + { + constexpr auto currentRatio = + static_cast<float>(Ratio::num) / static_cast<float>(Ratio::den); + constexpr auto targetRatio = static_cast<float>(TargetRatio::num) / + static_cast<float>(TargetRatio::den); + + return _value * currentRatio / targetRatio; + } + + template <UnitKind TargetKind, class TargetRatio = Ratio> + constexpr explicit operator Unit<TargetKind, TargetRatio>() const + { + return Unit<TargetKind, TargetRatio>(value<TargetRatio>()); + } + +private: + float _value; +}; + +// Sum, Subtraction, Multiplication, Division +template <UnitKind Kind, class Ratio> +constexpr auto operator+(const Unit<Kind, Ratio> &lhs, + const Unit<Kind, Ratio> &rhs) +{ + return Unit<Kind, Ratio>(lhs.template value() + rhs.template value()); +} + +template <UnitKind Kind, class Ratio> +constexpr auto operator-(const Unit<Kind, Ratio> &lhs, + const Unit<Kind, Ratio> &rhs) +{ + return Unit<Kind, Ratio>(lhs.template value() - rhs.template value()); +} + +template <UnitKind Kind, class Ratio> +constexpr auto operator*(const Unit<Kind, Ratio> &lhs, float rhs) +{ + return Unit<Kind, Ratio>(lhs.template value() * rhs); +} + +template <UnitKind Kind, class Ratio> +constexpr auto operator*(float lhs, const Unit<Kind, Ratio> &rhs) +{ + return Unit<Kind, Ratio>(lhs * rhs.template value()); +} + +template <UnitKind Kind, class Ratio> +constexpr auto operator/(const Unit<Kind, Ratio> &lhs, float rhs) +{ + return Unit<Kind, Ratio>(lhs.template value() / rhs); +} + +template <UnitKind Kind, class Ratio> +constexpr auto operator/(float lhs, const Unit<Kind, Ratio> &rhs) +{ + return Unit<Kind, Ratio>(lhs / rhs.template value()); +} + +// Comparison operators +template <UnitKind Kind, class Ratio> +constexpr bool operator==(const Unit<Kind, Ratio> &lhs, + const Unit<Kind, Ratio> &rhs) +{ + return lhs.template value() == rhs.template value(); +} + +template <UnitKind Kind, class Ratio> +constexpr bool operator!=(const Unit<Kind, Ratio> &lhs, + const Unit<Kind, Ratio> &rhs) +{ + return lhs.template value() != rhs.template value(); +} + +template <UnitKind Kind, class Ratio> +constexpr bool operator<(const Unit<Kind, Ratio> &lhs, + const Unit<Kind, Ratio> &rhs) +{ + return lhs.template value() < rhs.template value(); +} + +template <UnitKind Kind, class Ratio> +constexpr bool operator>(const Unit<Kind, Ratio> &lhs, + const Unit<Kind, Ratio> &rhs) +{ + return lhs.template value() > rhs.template value(); +} + +template <UnitKind Kind, class Ratio> +constexpr bool operator<=(const Unit<Kind, Ratio> &lhs, + const Unit<Kind, Ratio> &rhs) +{ + return lhs.template value() <= rhs.template value(); +} + +template <UnitKind Kind, class Ratio> +constexpr bool operator>=(const Unit<Kind, Ratio> &lhs, + const Unit<Kind, Ratio> &rhs) +{ + return lhs.template value() >= rhs.template value(); +} + +// Direct assignment operators +template <UnitKind Kind, class Ratio> +constexpr Unit<Kind, Ratio> &operator+=(Unit<Kind, Ratio> &lhs, + const Unit<Kind, Ratio> &rhs) +{ + lhs = lhs + rhs; + return lhs; +} + +template <UnitKind Kind, class Ratio> +constexpr Unit<Kind, Ratio> &operator-=(Unit<Kind, Ratio> &lhs, + const Unit<Kind, Ratio> &rhs) +{ + lhs = lhs - rhs; + return lhs; +} + +template <UnitKind Kind, class Ratio> +constexpr Unit<Kind, Ratio> &operator*=(Unit<Kind, Ratio> &lhs, float rhs) +{ + lhs = lhs * rhs; + return lhs; +} + +template <UnitKind Kind, class Ratio> +constexpr Unit<Kind, Ratio> &operator/=(Unit<Kind, Ratio> &lhs, float rhs) +{ + lhs = lhs / rhs; + return lhs; +} + +} // namespace Units +} // namespace Boardcore diff --git a/src/tests/catch/test-units.cpp b/src/tests/catch/test-units.cpp new file mode 100644 index 000000000..4bc4025bd --- /dev/null +++ b/src/tests/catch/test-units.cpp @@ -0,0 +1,106 @@ +/* Copyright (c) 2023 Skyward Experimental Rocketry + * Authors: Davide Basso + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include <units/Acceleration.h> +#include <units/Angle.h> +#include <units/Length.h> +#include <units/Pressure.h> +#include <units/Speed.h> +#include <units/Time.h> +#include <utils/Debug.h> + +#include <catch2/catch.hpp> +#include <cmath> + +using namespace Boardcore; + +template <class T> +bool eq(T const &a, T const &b) +{ + return std::fabs(a.value() - b.value()) < 1e-6; +} + +TEST_CASE("Units Test") +{ + constexpr float PI = 3.14159265358979323846; + + using namespace Units::Angle; + using namespace Units::Length; + using namespace Units::Pressure; + using namespace Units::Time; + using namespace Units::Speed; + using namespace Units::Acceleration; + + Meter len = 1_m; + + // Verify ratios + REQUIRE(Radian(PI) == angle_cast<Radian>(Degree(180))); + + REQUIRE(Millimeter(1000) == length_cast<Millimeter>(Meter(1))); + REQUIRE(Centimeter(100) == length_cast<Centimeter>(Meter(1))); + REQUIRE(Decimeter(10) == length_cast<Decimeter>(Meter(1))); + REQUIRE(Kilometer(0.001) == length_cast<Kilometer>(Meter(1))); + + REQUIRE(Pascal(100000) == pressure_cast<Pascal, Bar>(Bar(1))); + REQUIRE(Atm(1) == pressure_cast<Atm, Pascal>(Pascal(101325))); + + REQUIRE(Nanosecond(1000000000) == time_cast<Nanosecond>(Second(1))); + REQUIRE(Microsecond(1000000) == time_cast<Microsecond>(Second(1))); + REQUIRE(Millisecond(1000) == time_cast<Millisecond>(Second(1))); + REQUIRE(Minute(1) == time_cast<Minute>(Second(60))); + REQUIRE(Hour(1) == time_cast<Hour>(Second(3600))); + + REQUIRE(MeterPerSecond(1) == + speed_cast<MeterPerSecond>(KilometerPerHour(3.6))); + + REQUIRE(MeterPerSecondSquared(9.81) == + acceleration_cast<MeterPerSecondSquared>(G(1))); + + // Test operators + REQUIRE(eq(Radian(2 * PI), Radian(PI) + Radian(PI))); + REQUIRE(eq(Radian(0), Radian(PI) - Radian(PI))); + REQUIRE(eq(Radian(PI * 2), Radian(PI) * 2)); + REQUIRE(eq(Radian(2 * PI), 2 * Radian(PI))); + REQUIRE(eq(Radian(PI / 2), Radian(PI) / 2)); + REQUIRE(eq(Radian(2 / PI), 2 / Radian(PI))); + + // Test comparisons + REQUIRE(Radian(PI) == Radian(PI)); + REQUIRE(Radian(PI) != Radian(2 * PI)); + REQUIRE(Radian(PI) < Radian(2 * PI)); + REQUIRE(Radian(PI) <= Radian(2 * PI)); + REQUIRE(Radian(PI) <= Radian(PI)); + REQUIRE(Radian(2 * PI) > Radian(PI)); + REQUIRE(Radian(2 * PI) >= Radian(PI)); + REQUIRE(Radian(PI) >= Radian(PI)); + + // Test assignment operators + auto a = Radian(PI); + a += Radian(PI); + REQUIRE(a == Radian(2 * PI)); + a -= Radian(PI); + REQUIRE(a == Radian(PI)); + a *= 2; + REQUIRE(a == Radian(2 * PI)); + a /= 2; + REQUIRE(a == Radian(PI)); +} \ No newline at end of file -- GitLab