Make feedforward classes constexpr (#2103)

ArmFeedforward::Calculate() can't be made constexpr because std::cos()
and thus units::math::cos() is not constexpr.

Fixes #2101.
This commit is contained in:
Tyler Veness
2019-11-19 06:47:59 -08:00
committed by Peter Johnson
parent 500c43fb84
commit 845aba33fe
5 changed files with 27 additions and 53 deletions

View File

@@ -8,6 +8,7 @@
#pragma once
#include <units/units.h>
#include <wpi/MathExtras.h>
namespace frc {
/**
@@ -26,7 +27,7 @@ class ArmFeedforward {
units::compound_unit<units::volts, units::inverse<Acceleration>>;
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_unit> kV,
units::unit_t<ka_unit> kA = units::unit_t<ka_unit>(0));
constexpr ArmFeedforward(
units::volt_t kS, units::volt_t kCos, units::unit_t<kv_unit> kV,
units::unit_t<ka_unit> kA = units::unit_t<ka_unit>(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> angle,
units::unit_t<Velocity> velocity,
units::unit_t<Acceleration> acceleration =
units::unit_t<Acceleration>(0));
units::unit_t<Acceleration>(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};

View File

@@ -26,7 +26,7 @@ class ElevatorFeedforward {
units::compound_unit<units::volts, units::inverse<Acceleration>>;
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_unit> kV,
units::unit_t<ka_unit> kA = units::unit_t<ka_unit>(0))
constexpr ElevatorFeedforward(
units::volt_t kS, units::volt_t kG, units::unit_t<kv_unit> kV,
units::unit_t<ka_unit> kA = units::unit_t<ka_unit>(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> velocity,
units::unit_t<Acceleration> acceleration =
units::unit_t<Acceleration>(0)) {
constexpr units::volt_t Calculate(units::unit_t<Velocity> velocity,
units::unit_t<Acceleration> acceleration =
units::unit_t<Acceleration>(0)) const {
return m_kS * wpi::sgn(velocity) + m_kG + m_kV * velocity +
m_kA * acceleration;
}

View File

@@ -26,7 +26,7 @@ class SimpleMotorFeedforward {
units::compound_unit<units::volts, units::inverse<Acceleration>>;
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_unit> kV,
units::unit_t<ka_unit> kA = units::unit_t<ka_unit>(0))
constexpr SimpleMotorFeedforward(
units::volt_t kS, units::unit_t<kv_unit> kV,
units::unit_t<ka_unit> kA = units::unit_t<ka_unit>(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> velocity,
units::unit_t<Acceleration> acceleration =
units::unit_t<Acceleration>(0)) {
constexpr units::volt_t Calculate(units::unit_t<Velocity> velocity,
units::unit_t<Acceleration> acceleration =
units::unit_t<Acceleration>(0)) const {
return m_kS * wpi::sgn(velocity) + m_kV * velocity + m_kA * acceleration;
}