Files
allwpilib/wpilibc/src/test/native/cpp/PIDInputOutputTest.cpp

71 lines
2.0 KiB
C++
Raw Normal View History

Add replacement PIDController class (#1300) Originally, PIDController used PIDSource with its "PIDSourceType" to determine whether a class should return position or velocity to the controller. However, the supported languages have changed a lot over 10 years and now support lambdas. Instead of using PIDSource and PIDOutput, users can pass in doubles to the Calculate() function synchronously. This makes the controller much more flexible for team's needs as they no longer have to make a separate PIDSource-inheriting class just to provide a custom input. The built-in feedforward was removed. Since PIDController is synchronous now, they can add their own feedforward on top of what Calculate() returns. To facilitate running the controller asynchronously, there is a PIDControllerRunner class that handles that. By separating the loop from the control law, PIDController can now be composed with others and be used to control a drivetrain (a multiple input, multiple output system that requires summing the results from two controllers) much easier. Also, motion profiling can be used to set the reference over time. All the classes related to the old PIDController are now deprecated. The new classes are in an experimental namespace to avoid name conflicts. While this is a large change, I think it is a necessary one for growth. The old PIDController design was created in a time when languages only supported OOP, and we have more tools at our disposal now to solve problems. This more versatile implementation can be used in more places like as a replacement for Pathfinder's "EncoderFollower" class. There has been hesitation to add lambda support to WPILib for a while now out of concerns for requiring teams to learn more features of C++ or Java. In my opinion, this change makes PIDController easier to use, not harder. The concept of a function is a building block of OOP and should be learned before classes. The ability to store functions as first-class objects and invoke them just like variables is rather natural. Note that PID constants for the new controller will be different from the old one. The original controller didn't take the discretization period into account. To fix this, teams should just have to divide their Ki gain by 0.05 and multiply their Kd gain by 0.05 where 0.05 is the original default period.
2019-07-07 15:37:13 -07:00
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2014-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/PIDController.h"
#include "gtest/gtest.h"
class PIDInputOutputTest : public testing::Test {
protected:
frc2::PIDController* controller;
void SetUp() override { controller = new frc2::PIDController(0, 0, 0); }
void TearDown() override { delete controller; }
};
TEST_F(PIDInputOutputTest, OutputRangeTest) {
controller->SetP(1);
controller->SetOutputRange(-50, 50);
EXPECT_DOUBLE_EQ(-50, controller->Calculate(100, 0));
EXPECT_DOUBLE_EQ(50, controller->Calculate(0, 100));
}
TEST_F(PIDInputOutputTest, InputRangeTest) {
controller->SetP(1);
controller->SetOutputRange(-1000, 1000);
controller->SetInputRange(-50, 50);
EXPECT_DOUBLE_EQ(-100, controller->Calculate(100, 0));
EXPECT_DOUBLE_EQ(50, controller->Calculate(0, 100));
}
TEST_F(PIDInputOutputTest, ContinuousInputTest) {
controller->SetP(1);
controller->SetInputRange(-180, 180);
controller->SetContinuous(true);
EXPECT_TRUE(controller->Calculate(-179, 179) < 0);
}
TEST_F(PIDInputOutputTest, ProportionalGainOutputTest) {
controller->SetP(4);
EXPECT_DOUBLE_EQ(-.1, controller->Calculate(.025, 0));
}
TEST_F(PIDInputOutputTest, IntegralGainOutputTest) {
controller->SetI(4);
double out = 0;
for (int i = 0; i < 5; i++) {
out = controller->Calculate(.025, 0);
}
EXPECT_DOUBLE_EQ(-.5 * controller->GetPeriod(), out);
}
TEST_F(PIDInputOutputTest, DerivativeGainOutputTest) {
controller->SetD(4);
controller->Calculate(0, 0);
EXPECT_DOUBLE_EQ(-.01 / controller->GetPeriod(),
controller->Calculate(.0025, 0));
}