wpilibc: Add overloads for units (#1815)

Add unit-taking overloads to the following classes:
- IterativeRobotBase
- LinearFilter
- Notifier
- TimedRobot
- Timer (HasPeriodPassed only)
- frc2::PIDController

The corresponding non-units-taking functions have been deprecated.

The return value of TimedRobot::GetPeriod() was updated.
This is a breaking change, users should use to<double> to get the value in seconds.

Other return values, e.g. Timer::Get(), have NOT been updated due to much wider use.
This commit is contained in:
Prateek Machiraju
2019-08-17 00:56:48 -04:00
committed by Peter Johnson
parent f1d71da8a9
commit c07ac23532
15 changed files with 136 additions and 35 deletions

View File

@@ -11,13 +11,14 @@
#include <memory>
#include <random>
#include <units/units.h>
#include <wpi/math>
#include "gtest/gtest.h"
// Filter constants
static constexpr double kFilterStep = 0.005;
static constexpr double kFilterTime = 2.0;
static constexpr units::second_t kFilterStep = 0.005_s;
static constexpr units::second_t kFilterTime = 2.0_s;
static constexpr double kSinglePoleIIRTimeConstant = 0.015915;
static constexpr int32_t kMovAvgTaps = 6;
@@ -75,8 +76,8 @@ TEST_P(LinearFilterNoiseTest, NoiseReduce) {
std::mt19937 gen{rd()};
std::normal_distribution<double> distr{0.0, 10.0};
for (double t = 0; t < kFilterTime; t += kFilterStep) {
double theory = GetData(t);
for (auto t = 0_s; t < kFilterTime; t += kFilterStep) {
double theory = GetData(t.to<double>());
double noise = distr(gen);
filterError += std::abs(m_filter->Calculate(theory + noise) - theory);
noiseGenError += std::abs(noise - theory);

View File

@@ -12,13 +12,14 @@
#include <memory>
#include <random>
#include <units/units.h>
#include <wpi/math>
#include "gtest/gtest.h"
// Filter constants
static constexpr double kFilterStep = 0.005;
static constexpr double kFilterTime = 2.0;
static constexpr units::second_t kFilterStep = 0.005_s;
static constexpr units::second_t kFilterTime = 2.0_s;
static constexpr double kSinglePoleIIRTimeConstant = 0.015915;
static constexpr double kSinglePoleIIRExpectedOutput = -3.2172003;
static constexpr double kHighPassTimeConstant = 0.006631;
@@ -119,8 +120,8 @@ class LinearFilterOutputTest
*/
TEST_P(LinearFilterOutputTest, Output) {
double filterOutput = 0.0;
for (double t = 0.0; t < kFilterTime; t += kFilterStep) {
filterOutput = m_filter->Calculate(m_data(t));
for (auto t = 0_s; t < kFilterTime; t += kFilterStep) {
filterOutput = m_filter->Calculate(m_data(t.to<double>()));
}
RecordProperty("LinearFilterOutput", filterOutput);

View File

@@ -57,7 +57,7 @@ TEST_F(PIDInputOutputTest, IntegralGainOutputTest) {
out = controller->Calculate(.025, 0);
}
EXPECT_DOUBLE_EQ(-.5 * controller->GetPeriod(), out);
EXPECT_DOUBLE_EQ(-.5 * controller->GetPeriod().to<double>(), out);
}
TEST_F(PIDInputOutputTest, DerivativeGainOutputTest) {
@@ -65,6 +65,6 @@ TEST_F(PIDInputOutputTest, DerivativeGainOutputTest) {
controller->Calculate(0, 0);
EXPECT_DOUBLE_EQ(-.01 / controller->GetPeriod(),
EXPECT_DOUBLE_EQ(-.01 / controller->GetPeriod().to<double>(),
controller->Calculate(.0025, 0));
}