diff --git a/src/shared/units/Acceleration.h b/src/shared/units/Acceleration.h new file mode 100644 index 0000000000000000000000000000000000000000..eac700e5a2d1fd112470338a01b9276b7646e7dc --- /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 index dbc26b1fcd6a7adcb1bab318ad91543fb413f897..6a1c8eef1ea6843dcc72f7d75a730b6211d62ef9 100644 --- a/src/shared/units/Angle.h +++ b/src/shared/units/Angle.h @@ -36,20 +36,35 @@ 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)>>; - -auto operator""_rad(long double n) { return Radian(static_cast<float>(n)); }; -auto operator""_deg(long double n) { return Degree(static_cast<float>(n)); }; - -template <class ToAngle, class FromAngle> -ToAngle angle_cast(FromAngle const &from) +// Floats +constexpr auto operator""_rad(long double n) { - return ToAngle(from); -} + 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 diff --git a/src/shared/units/Length.h b/src/shared/units/Length.h index 6afec19cc16310bb09bbfe2437437a9d85fdbcdc..cb6f024c5199900b5674d061f14f660100d87c85 100644 --- a/src/shared/units/Length.h +++ b/src/shared/units/Length.h @@ -48,11 +48,48 @@ using Decimeter = Length<std::deci>; // Length in decimeters using Meter = Length<>; // Length in meters using Kilometer = Length<std::kilo>; // Length in kilometers -auto operator""_mm(long double n) { return Millimeter(static_cast<float>(n)); }; -auto operator""_cm(long double n) { return Centimeter(static_cast<float>(n)); }; -auto operator""_dm(long double n) { return Decimeter(static_cast<float>(n)); }; -auto operator""_m(long double n) { return Meter(static_cast<float>(n)); }; -auto operator""_km(long double n) { return Kilometer(static_cast<float>(n)); }; +// 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 diff --git a/src/shared/units/Pressure.h b/src/shared/units/Pressure.h index ac65bf249227a4b5f29927fc8fc238377a036313..30dc96bb0868948bfbfeffa223101646c752c149 100644 --- a/src/shared/units/Pressure.h +++ b/src/shared/units/Pressure.h @@ -46,9 +46,32 @@ using Pascal = Pressure<>; // Pressure in Pascals using Bar = Pressure<std::ratio<100000>>; // Pressure in Bars using Atm = Pressure<std::ratio<101325>>; // Pressure in Atmospheres -auto operator""_pa(long double n) { return Pascal(static_cast<float>(n)); }; -auto operator""_bar(long double n) { return Bar(static_cast<float>(n)); }; -auto operator""_atm(long double n) { return Atm(static_cast<float>(n)); }; +// 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 diff --git a/src/shared/units/Speed.h b/src/shared/units/Speed.h new file mode 100644 index 0000000000000000000000000000000000000000..051d90e074b4b5d80f7ba42408c85c442256d536 --- /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 index 41c7bb44b5ef4436305530b044a7784b8ca100cf..155fef4d969b0d13135eada8b46105c92fdb46ee 100644 --- a/src/shared/units/Time.h +++ b/src/shared/units/Time.h @@ -35,16 +35,7 @@ namespace Time { template <class Ratio = std::ratio<1>> -class Time : public Unit<UnitKind::Time, Ratio> -{ - using Unit<UnitKind::Time, Ratio>::Unit; - -public: - std::chrono::duration<float> chrono() const - { - return std::chrono::duration<float, Ratio>(this->value()); - } -}; +using Time = Unit<UnitKind::Time, Ratio>; template <class ToTime, class FromTime> ToTime time_cast(FromTime const &from) @@ -52,33 +43,68 @@ 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 -using Day = Time<std::ratio<86400>>; // Time in days -using Week = Time<std::ratio<604800>>; // Time in weeks -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)); }; -auto operator""_us(long double n) +// 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)); }; -auto operator""_ms(long double n) +constexpr auto operator""_ms(unsigned long long n) { return Millisecond(static_cast<float>(n)); }; -auto operator""_s(long double n) { return Second(static_cast<float>(n)); }; -auto operator""_min(long double n) { return Minute(static_cast<float>(n)); }; -auto operator""_h(long double n) { return Hour(static_cast<float>(n)); }; -auto operator""_d(long double n) { return Day(static_cast<float>(n)); }; -auto operator""_w(long double n) { return Week(static_cast<float>(n)); }; -auto operator""_mo(long double n) { return Month(static_cast<float>(n)); }; -auto operator""_y(long double n) { return Year(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 diff --git a/src/shared/units/Units.h b/src/shared/units/Units.h index 64e37e9e8ef3b4f68d0f539190bece5f77351b17..6e59cbcd052c0cf7835dafe233871f686542b8b7 100644 --- a/src/shared/units/Units.h +++ b/src/shared/units/Units.h @@ -37,7 +37,9 @@ enum class UnitKind Angle, Length, Pressure, - Time + Time, + Speed, + Acceleration, }; // Base class to implement custom measurement units logic. @@ -45,7 +47,8 @@ template <UnitKind Kind, class Ratio = std::ratio<1>> class Unit { public: - Unit(float val) : _value(val){}; + 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>()) @@ -54,7 +57,7 @@ public: // Get the value of the unit in the specified ratio. template <class TargetRatio = Ratio> - float value() const + constexpr float value() const { constexpr auto currentRatio = static_cast<float>(Ratio::num) / static_cast<float>(Ratio::den); diff --git a/src/tests/catch/test-units.cpp b/src/tests/catch/test-units.cpp index 66e1048abf23af72b1d449368d5a250653a22319..4bc4025bdcf51d43408a829af9530638e7dd318f 100644 --- a/src/tests/catch/test-units.cpp +++ b/src/tests/catch/test-units.cpp @@ -20,9 +20,11 @@ * 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> @@ -45,6 +47,10 @@ TEST_CASE("Units Test") 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))); @@ -62,10 +68,12 @@ TEST_CASE("Units Test") 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))); + + 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)));