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
@@ -8,10 +8,11 @@
|
||||
#include "TestBench.h"
|
||||
#include "frc/Encoder.h"
|
||||
#include "frc/Jaguar.h"
|
||||
#include "frc/PIDController.h"
|
||||
#include "frc/Talon.h"
|
||||
#include "frc/Timer.h"
|
||||
#include "frc/Victor.h"
|
||||
#include "frc/controller/PIDController.h"
|
||||
#include "frc/controller/PIDControllerRunner.h"
|
||||
#include "frc/filters/LinearDigitalFilter.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
@@ -139,22 +140,24 @@ TEST_P(MotorEncoderTest, ClampSpeed) {
|
||||
TEST_P(MotorEncoderTest, PositionPIDController) {
|
||||
Reset();
|
||||
double goal = 1000;
|
||||
m_encoder->SetPIDSourceType(PIDSourceType::kDisplacement);
|
||||
PIDController pid(0.001, 0.0005, 0.0, m_encoder, m_speedController);
|
||||
pid.SetAbsoluteTolerance(50.0);
|
||||
pid.SetOutputRange(-0.2, 0.2);
|
||||
pid.SetSetpoint(goal);
|
||||
frc2::PIDController pidController(0.001, 0.01, 0.0);
|
||||
pidController.SetAbsoluteTolerance(50.0);
|
||||
pidController.SetOutputRange(-0.2, 0.2);
|
||||
pidController.SetSetpoint(goal);
|
||||
|
||||
/* 10 seconds should be plenty time to get to the setpoint */
|
||||
pid.Enable();
|
||||
/* 10 seconds should be plenty time to get to the reference */
|
||||
frc::PIDControllerRunner pidRunner(
|
||||
pidController, [&] { return m_encoder->GetDistance(); },
|
||||
[&](double output) { m_speedController->Set(output); });
|
||||
pidRunner.Enable();
|
||||
Wait(10.0);
|
||||
pid.Disable();
|
||||
pidRunner.Disable();
|
||||
|
||||
RecordProperty("PIDError", pid.GetError());
|
||||
RecordProperty("PIDError", pidController.GetError());
|
||||
|
||||
EXPECT_TRUE(pid.OnTarget())
|
||||
EXPECT_TRUE(pidController.AtSetpoint())
|
||||
<< "PID loop did not converge within 10 seconds. Goal was: " << goal
|
||||
<< " Error was: " << pid.GetError();
|
||||
<< " Error was: " << pidController.GetError();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -164,20 +167,23 @@ TEST_P(MotorEncoderTest, VelocityPIDController) {
|
||||
Reset();
|
||||
|
||||
m_encoder->SetPIDSourceType(PIDSourceType::kRate);
|
||||
PIDController pid(1e-5, 0.0, 3e-5, 8e-5, m_filter, m_speedController);
|
||||
pid.SetAbsoluteTolerance(200.0);
|
||||
pid.SetOutputRange(-0.3, 0.3);
|
||||
pid.SetSetpoint(600);
|
||||
frc2::PIDController pidController(1e-5, 0.0, 0.0006);
|
||||
pidController.SetAbsoluteTolerance(200.0);
|
||||
pidController.SetOutputRange(-0.3, 0.3);
|
||||
pidController.SetSetpoint(600);
|
||||
|
||||
/* 10 seconds should be plenty time to get to the setpoint */
|
||||
pid.Enable();
|
||||
/* 10 seconds should be plenty time to get to the reference */
|
||||
frc::PIDControllerRunner pidRunner(
|
||||
pidController, [&] { return m_filter->PIDGet(); },
|
||||
[&](double output) { m_speedController->Set(output + 8e-5); });
|
||||
pidRunner.Enable();
|
||||
Wait(10.0);
|
||||
pid.Disable();
|
||||
RecordProperty("PIDError", pid.GetError());
|
||||
pidRunner.Disable();
|
||||
RecordProperty("PIDError", pidController.GetError());
|
||||
|
||||
EXPECT_TRUE(pid.OnTarget())
|
||||
EXPECT_TRUE(pidController.AtSetpoint())
|
||||
<< "PID loop did not converge within 10 seconds. Goal was: " << 600
|
||||
<< " Error was: " << pid.GetError();
|
||||
<< " Error was: " << pidController.GetError();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,107 +0,0 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2014-2018 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 "TestBench.h"
|
||||
#include "frc/PIDController.h"
|
||||
#include "frc/PIDOutput.h"
|
||||
#include "frc/PIDSource.h"
|
||||
#include "frc/Timer.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 PIDSource {
|
||||
public:
|
||||
double val = 0;
|
||||
|
||||
void SetPIDSourceType(PIDSourceType pidSource) {}
|
||||
|
||||
PIDSourceType GetPIDSourceType() { return PIDSourceType::kDisplacement; }
|
||||
|
||||
double PIDGet() { return val; }
|
||||
};
|
||||
|
||||
class FakeOutput : public PIDOutput {
|
||||
void PIDWrite(double output) {}
|
||||
};
|
||||
|
||||
FakeInput inp;
|
||||
FakeOutput out;
|
||||
PIDController* pid;
|
||||
|
||||
void SetUp() override {
|
||||
pid = new PIDController(0.5, 0.0, 0.0, &inp, &out);
|
||||
pid->SetInputRange(-range / 2, range / 2);
|
||||
}
|
||||
|
||||
void TearDown() override { delete pid; }
|
||||
|
||||
void Reset() { inp.val = 0; }
|
||||
};
|
||||
|
||||
TEST_F(PIDToleranceTest, Absolute) {
|
||||
Reset();
|
||||
|
||||
pid->SetAbsoluteTolerance(tolerance);
|
||||
pid->SetSetpoint(setpoint);
|
||||
pid->Enable();
|
||||
|
||||
EXPECT_FALSE(pid->OnTarget())
|
||||
<< "Error was in tolerance when it should not have been. Error was "
|
||||
<< pid->GetError();
|
||||
|
||||
inp.val = setpoint + tolerance / 2;
|
||||
Wait(1.0);
|
||||
|
||||
EXPECT_TRUE(pid->OnTarget())
|
||||
<< "Error was not in tolerance when it should have been. Error was "
|
||||
<< pid->GetError();
|
||||
|
||||
inp.val = setpoint + 10 * tolerance;
|
||||
Wait(1.0);
|
||||
|
||||
EXPECT_FALSE(pid->OnTarget())
|
||||
<< "Error was in tolerance when it should not have been. Error was "
|
||||
<< pid->GetError();
|
||||
}
|
||||
|
||||
TEST_F(PIDToleranceTest, Percent) {
|
||||
Reset();
|
||||
|
||||
pid->SetPercentTolerance(tolerance);
|
||||
pid->SetSetpoint(setpoint);
|
||||
pid->Enable();
|
||||
|
||||
EXPECT_FALSE(pid->OnTarget())
|
||||
<< "Error was in tolerance when it should not have been. Error was "
|
||||
<< pid->GetError();
|
||||
|
||||
inp.val =
|
||||
setpoint + (tolerance) / 200 *
|
||||
range; // half of percent tolerance away from setpoint
|
||||
Wait(1.0);
|
||||
|
||||
EXPECT_TRUE(pid->OnTarget())
|
||||
<< "Error was not in tolerance when it should have been. Error was "
|
||||
<< pid->GetError();
|
||||
|
||||
inp.val =
|
||||
setpoint +
|
||||
(tolerance) / 50 * range; // double percent tolerance away from setPoint
|
||||
|
||||
Wait(1.0);
|
||||
|
||||
EXPECT_FALSE(pid->OnTarget())
|
||||
<< "Error was in tolerance when it should not have been. Error was "
|
||||
<< pid->GetError();
|
||||
}
|
||||
Reference in New Issue
Block a user