diff --git a/CMakeLists.txt b/CMakeLists.txt index aaba63944cb7e1096e7e70e16796acbfec10e304..f50a6b1ba33c7a5edfa0ba33fee2218f89bb7b22 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/Angle.h b/src/shared/units/Angle.h index 4693f12d38a30906dd98e95986f498bc1615b559..411296dfc0b60d66f1ad737d1ffad3bc0445d4e9 100644 --- a/src/shared/units/Angle.h +++ b/src/shared/units/Angle.h @@ -41,8 +41,9 @@ class Angle : public Unit<Ratio> using Degree = Angle<>; // Angle in degrees using Radian = // Angle in radians - Angle<std::ratio<static_cast<std::intmax_t>(3.14159265358979323846 * 1e10), - static_cast<std::intmax_t>(180 * 1e10)>>; + Angle< + std::ratio<static_cast<std::intmax_t>(180 * 1e10), + static_cast<std::intmax_t>(3.14159265358979323846 * 1e10)>>; auto operator""_rad(long double n) { return Radian(static_cast<float>(n)); }; auto operator""_deg(long double n) { return Degree(static_cast<float>(n)); }; diff --git a/src/shared/units/Pressure.h b/src/shared/units/Pressure.h index 452b3454c1d6e942f037955c334b3e8bee2bdcb8..8f5e76b81ca028b58d1db87a07b898cd8693e0d3 100644 --- a/src/shared/units/Pressure.h +++ b/src/shared/units/Pressure.h @@ -40,7 +40,7 @@ class Pressure : public Unit<Ratio> }; template <class ToPressure, class FromPressure> -ToPressure pressure_cast(FromPressure &from) +ToPressure pressure_cast(FromPressure const &from) { return ToPressure(from); } diff --git a/src/shared/units/Time.h b/src/shared/units/Time.h index 7cd9f5000f9d5af6d0ebf481714128b9c3e3174d..a56760b9b7981f0a7fa7e0ade47d91c98c2ee0c2 100644 --- a/src/shared/units/Time.h +++ b/src/shared/units/Time.h @@ -47,7 +47,7 @@ public: }; template <class ToTime, class FromTime> -ToTime time_cast(FromTime &from) +ToTime time_cast(FromTime const &from) { return ToTime(from); } @@ -60,7 +60,7 @@ using Minute = Time<std::ratio<60>>; // Time in minutes using Hour = Time<std::ratio<3600>>; // Time in hours using Day = Time<std::ratio<86400>>; // Time in days using Week = Time<std::ratio<604800>>; // Time in weeks -using Month = Time<std::ratio<262800>>; // Time in months +using Month = Time<std::ratio<2628000>>; // Time in months using Year = Time<std::ratio<31536000>>; // Time in years auto operator""_ns(long double n) { return Nanosecond(static_cast<float>(n)); }; diff --git a/src/shared/units/Units.h b/src/shared/units/Units.h index 46aca63c8ec9c82e1d4ea59e1817228a09468408..dd080649b8b0d8f79069a817e293c7aeed45406a 100644 --- a/src/shared/units/Units.h +++ b/src/shared/units/Units.h @@ -53,7 +53,7 @@ public: const auto targetRatio = static_cast<float>(TargetRatio::num) / static_cast<float>(TargetRatio::den); - return _value / currentRatio * targetRatio; + return _value * currentRatio / targetRatio; } template <class TargetRatio = Ratio> @@ -80,15 +80,27 @@ constexpr auto operator-(const DerivedUnit &lhs, const DerivedUnit &rhs) } template <class DerivedUnit> -constexpr auto operator*(const DerivedUnit &lhs, const DerivedUnit &rhs) +constexpr auto operator*(const DerivedUnit &lhs, float rhs) { - return DerivedUnit(lhs.template value() * rhs.template value()); + return DerivedUnit(lhs.template value() * rhs); } template <class DerivedUnit> -constexpr auto operator/(const DerivedUnit &lhs, const DerivedUnit &rhs) +constexpr auto operator*(float lhs, const DerivedUnit &rhs) { - return DerivedUnit(lhs.template value() / rhs.template value()); + return DerivedUnit(lhs * rhs.template value()); +} + +template <class DerivedUnit> +constexpr auto operator/(const DerivedUnit &lhs, float rhs) +{ + return DerivedUnit(lhs.template value() / rhs); +} + +template <class DerivedUnit> +constexpr auto operator/(float lhs, const DerivedUnit &rhs) +{ + return DerivedUnit(lhs / rhs.template value()); } // Comparison operators @@ -144,14 +156,14 @@ constexpr DerivedUnit &operator-=(DerivedUnit &lhs, const DerivedUnit &rhs) } template <class DerivedUnit> -constexpr DerivedUnit &operator*=(DerivedUnit &lhs, const DerivedUnit &rhs) +constexpr DerivedUnit &operator*=(DerivedUnit &lhs, float rhs) { lhs = lhs * rhs; return lhs; } template <class DerivedUnit> -constexpr DerivedUnit &operator/=(DerivedUnit &lhs, const DerivedUnit &rhs) +constexpr DerivedUnit &operator/=(DerivedUnit &lhs, float rhs) { lhs = lhs / rhs; return lhs; diff --git a/src/tests/catch/test-units.cpp b/src/tests/catch/test-units.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c2f473a54f550a58842dd50d165f52500a5c4333 --- /dev/null +++ b/src/tests/catch/test-units.cpp @@ -0,0 +1,98 @@ +/* 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/Angle.h> +#include <units/Length.h> +#include <units/Pressure.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; + + // 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(Day(1) == time_cast<Day>(Second(86400))); + REQUIRE(Week(1) == time_cast<Week>(Second(604800))); + REQUIRE(Month(1) == time_cast<Month>(Second(2628000))); + REQUIRE(Year(1) == time_cast<Year>(Second(31536000))); + + // 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