mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
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.
This commit is contained in:
committed by
Peter Johnson
parent
9b798d228f
commit
ea9512977c
70
wpilibc/src/test/native/cpp/PIDInputOutputTest.cpp
Normal file
70
wpilibc/src/test/native/cpp/PIDInputOutputTest.cpp
Normal file
@@ -0,0 +1,70 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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));
|
||||
}
|
||||
111
wpilibc/src/test/native/cpp/PIDToleranceTest.cpp
Normal file
111
wpilibc/src/test/native/cpp/PIDToleranceTest.cpp
Normal file
@@ -0,0 +1,111 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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/Timer.h"
|
||||
#include "frc/controller/PIDController.h"
|
||||
#include "frc/controller/PIDControllerRunner.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
using namespace frc;
|
||||
|
||||
class PIDToleranceTest : public testing::Test {
|
||||
protected:
|
||||
const double setpoint = 50.0;
|
||||
const double range = 200;
|
||||
const double tolerance = 10.0;
|
||||
|
||||
class FakeInput {
|
||||
public:
|
||||
double val = 0;
|
||||
|
||||
double Get() { return val; }
|
||||
};
|
||||
|
||||
class FakeOutput {
|
||||
public:
|
||||
void Set(double output) {}
|
||||
};
|
||||
|
||||
FakeInput inp;
|
||||
FakeOutput out;
|
||||
frc2::PIDController* pidController;
|
||||
frc::PIDControllerRunner* pidRunner;
|
||||
|
||||
void SetUp() override {
|
||||
pidController = new frc2::PIDController(0.5, 0.0, 0.0);
|
||||
pidController->SetInputRange(-range / 2, range / 2);
|
||||
pidRunner =
|
||||
new frc::PIDControllerRunner(*pidController, [&] { return inp.Get(); },
|
||||
[&](double output) { out.Set(output); });
|
||||
}
|
||||
|
||||
void TearDown() override {
|
||||
delete pidRunner;
|
||||
delete pidController;
|
||||
}
|
||||
|
||||
void Reset() { inp.val = 0; }
|
||||
};
|
||||
|
||||
TEST_F(PIDToleranceTest, Initial) { EXPECT_FALSE(pidController->AtSetpoint()); }
|
||||
|
||||
TEST_F(PIDToleranceTest, Absolute) {
|
||||
Reset();
|
||||
|
||||
pidController->SetAbsoluteTolerance(tolerance);
|
||||
pidController->SetSetpoint(setpoint);
|
||||
pidRunner->Enable();
|
||||
|
||||
EXPECT_FALSE(pidController->AtSetpoint())
|
||||
<< "Error was in tolerance when it should not have been. Error was "
|
||||
<< pidController->GetError();
|
||||
|
||||
inp.val = setpoint + tolerance / 2;
|
||||
Wait(1.0);
|
||||
|
||||
EXPECT_TRUE(pidController->AtSetpoint())
|
||||
<< "Error was not in tolerance when it should have been. Error was "
|
||||
<< pidController->GetError();
|
||||
|
||||
inp.val = setpoint + 10 * tolerance;
|
||||
Wait(1.0);
|
||||
|
||||
EXPECT_FALSE(pidController->AtSetpoint())
|
||||
<< "Error was in tolerance when it should not have been. Error was "
|
||||
<< pidController->GetError();
|
||||
}
|
||||
|
||||
TEST_F(PIDToleranceTest, Percent) {
|
||||
Reset();
|
||||
|
||||
pidController->SetPercentTolerance(tolerance);
|
||||
pidController->SetSetpoint(setpoint);
|
||||
pidRunner->Enable();
|
||||
|
||||
EXPECT_FALSE(pidController->AtSetpoint())
|
||||
<< "Error was in tolerance when it should not have been. Error was "
|
||||
<< pidController->GetError();
|
||||
|
||||
inp.val =
|
||||
setpoint + (tolerance) / 200 *
|
||||
range; // half of percent tolerance away from setpoint
|
||||
Wait(1.0);
|
||||
|
||||
EXPECT_TRUE(pidController->AtSetpoint())
|
||||
<< "Error was not in tolerance when it should have been. Error was "
|
||||
<< pidController->GetError();
|
||||
|
||||
inp.val =
|
||||
setpoint +
|
||||
(tolerance) / 50 * range; // double percent tolerance away from setPoint
|
||||
|
||||
Wait(1.0);
|
||||
|
||||
EXPECT_FALSE(pidController->AtSetpoint())
|
||||
<< "Error was in tolerance when it should not have been. Error was "
|
||||
<< pidController->GetError();
|
||||
}
|
||||
Reference in New Issue
Block a user