diff --git a/wpilibc/src/main/native/cpp/controller/ArmFeedforward.cpp b/wpilibc/src/main/native/cpp/controller/ArmFeedforward.cpp deleted file mode 100644 index 25e72b37d5..0000000000 --- a/wpilibc/src/main/native/cpp/controller/ArmFeedforward.cpp +++ /dev/null @@ -1,33 +0,0 @@ -/*----------------------------------------------------------------------------*/ -/* Copyright (c) 2019 FIRST. All Rights Reserved. */ -/* Open Source Software - may be modified and shared by FRC teams. The code */ -/* must be accompanied by the FIRST BSD license file in the root directory of */ -/* the project. */ -/*----------------------------------------------------------------------------*/ - -#include "frc/controller/ArmFeedforward.h" - -#include - -using namespace frc; - -using Angle = units::radians; -using Velocity = units::radians_per_second; -using Acceleration = units::compound_unit>; -using kv_unit = units::compound_unit>; -using ka_unit = - units::compound_unit>; - -ArmFeedforward::ArmFeedforward(units::volt_t kS, units::volt_t kCos, - units::unit_t kV, - units::unit_t kA) - : m_kS(kS), m_kCos(kCos), m_kV(kV), m_kA(kA) {} - -units::volt_t ArmFeedforward::Calculate( - units::unit_t angle, units::unit_t velocity, - units::unit_t acceleration) { - return m_kS * wpi::sgn(velocity) + m_kCos * units::math::cos(angle) + - m_kV * velocity + m_kA * acceleration; -} diff --git a/wpilibc/src/main/native/include/frc/controller/ArmFeedforward.h b/wpilibc/src/main/native/include/frc/controller/ArmFeedforward.h index 75bcd216b2..d1a8ceac9b 100644 --- a/wpilibc/src/main/native/include/frc/controller/ArmFeedforward.h +++ b/wpilibc/src/main/native/include/frc/controller/ArmFeedforward.h @@ -8,6 +8,7 @@ #pragma once #include +#include namespace frc { /** @@ -26,7 +27,7 @@ class ArmFeedforward { units::compound_unit>; public: - ArmFeedforward() = default; + constexpr ArmFeedforward() = default; /** * Creates a new ArmFeedforward with the specified gains. @@ -36,9 +37,10 @@ class ArmFeedforward { * @param kV The velocity gain, in volt seconds per radian. * @param kA The acceleration gain, in volt seconds^2 per radian. */ - ArmFeedforward(units::volt_t kS, units::volt_t kCos, - units::unit_t kV, - units::unit_t kA = units::unit_t(0)); + constexpr ArmFeedforward( + units::volt_t kS, units::volt_t kCos, units::unit_t kV, + units::unit_t kA = units::unit_t(0)) + : m_kS(kS), m_kCos(kCos), m_kV(kV), m_kA(kA) {} /** * Calculates the feedforward from the gains and setpoints. @@ -51,7 +53,10 @@ class ArmFeedforward { units::volt_t Calculate(units::unit_t angle, units::unit_t velocity, units::unit_t acceleration = - units::unit_t(0)); + units::unit_t(0)) const { + return m_kS * wpi::sgn(velocity) + m_kCos * units::math::cos(angle) + + m_kV * velocity + m_kA * acceleration; + } private: units::volt_t m_kS{0}; diff --git a/wpilibc/src/main/native/include/frc/controller/ElevatorFeedforward.h b/wpilibc/src/main/native/include/frc/controller/ElevatorFeedforward.h index 2c1e9e12c0..9938b7b46c 100644 --- a/wpilibc/src/main/native/include/frc/controller/ElevatorFeedforward.h +++ b/wpilibc/src/main/native/include/frc/controller/ElevatorFeedforward.h @@ -26,7 +26,7 @@ class ElevatorFeedforward { units::compound_unit>; public: - ElevatorFeedforward() = default; + constexpr ElevatorFeedforward() = default; /** * Creates a new ElevatorFeedforward with the specified gains. @@ -36,9 +36,9 @@ class ElevatorFeedforward { * @param kV The velocity gain, in volt seconds per distance. * @param kA The acceleration gain, in volt seconds^2 per distance. */ - ElevatorFeedforward(units::volt_t kS, units::volt_t kG, - units::unit_t kV, - units::unit_t kA = units::unit_t(0)) + constexpr ElevatorFeedforward( + units::volt_t kS, units::volt_t kG, units::unit_t kV, + units::unit_t kA = units::unit_t(0)) : m_kS(kS), m_kG(kG), m_kV(kV), m_kA(kA) {} /** @@ -48,9 +48,9 @@ class ElevatorFeedforward { * @param acceleration The acceleration setpoint, in distance per second^2. * @return The computed feedforward, in volts. */ - units::volt_t Calculate(units::unit_t velocity, - units::unit_t acceleration = - units::unit_t(0)) { + constexpr units::volt_t Calculate(units::unit_t velocity, + units::unit_t acceleration = + units::unit_t(0)) const { return m_kS * wpi::sgn(velocity) + m_kG + m_kV * velocity + m_kA * acceleration; } diff --git a/wpilibc/src/main/native/include/frc/controller/SimpleMotorFeedforward.h b/wpilibc/src/main/native/include/frc/controller/SimpleMotorFeedforward.h index 8293fc9e91..03aebf2c07 100644 --- a/wpilibc/src/main/native/include/frc/controller/SimpleMotorFeedforward.h +++ b/wpilibc/src/main/native/include/frc/controller/SimpleMotorFeedforward.h @@ -26,7 +26,7 @@ class SimpleMotorFeedforward { units::compound_unit>; public: - SimpleMotorFeedforward() = default; + constexpr SimpleMotorFeedforward() = default; /** * Creates a new SimpleMotorFeedforward with the specified gains. @@ -35,8 +35,9 @@ class SimpleMotorFeedforward { * @param kV The velocity gain, in volt seconds per distance. * @param kA The acceleration gain, in volt seconds^2 per distance. */ - SimpleMotorFeedforward(units::volt_t kS, units::unit_t kV, - units::unit_t kA = units::unit_t(0)) + constexpr SimpleMotorFeedforward( + units::volt_t kS, units::unit_t kV, + units::unit_t kA = units::unit_t(0)) : m_kS(kS), m_kV(kV), m_kA(kA) {} /** @@ -46,9 +47,9 @@ class SimpleMotorFeedforward { * @param acceleration The acceleration setpoint, in distance per second^2. * @return The computed feedforward, in volts. */ - units::volt_t Calculate(units::unit_t velocity, - units::unit_t acceleration = - units::unit_t(0)) { + constexpr units::volt_t Calculate(units::unit_t velocity, + units::unit_t acceleration = + units::unit_t(0)) const { return m_kS * wpi::sgn(velocity) + m_kV * velocity + m_kA * acceleration; } diff --git a/wpiutil/src/main/native/include/wpi/MathExtras.h b/wpiutil/src/main/native/include/wpi/MathExtras.h index 8d5fec490c..52a56a1ae5 100644 --- a/wpiutil/src/main/native/include/wpi/MathExtras.h +++ b/wpiutil/src/main/native/include/wpi/MathExtras.h @@ -838,9 +838,10 @@ SaturatingMultiplyAdd(T X, T Y, T A, bool *ResultOverflowed = nullptr) { return SaturatingAdd(A, Product, &Overflowed); } -// Typesafe implementation of the signum function. Returns -1 if negative, 1 if positive, 0 if 0. +// Typesafe implementation of the signum function. +// Returns -1 if negative, 1 if positive, 0 if 0. template -int sgn(T val) { +constexpr int sgn(T val) { return (T(0) < val) - (val < T(0)); }