Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) 2014-2018 FIRST. All Rights Reserved. */ |
| 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #include "TestBench.h" |
| 9 | #include "frc/PIDController.h" |
| 10 | #include "frc/PIDOutput.h" |
| 11 | #include "frc/PIDSource.h" |
| 12 | #include "frc/Timer.h" |
| 13 | #include "gtest/gtest.h" |
| 14 | |
| 15 | using namespace frc; |
| 16 | |
| 17 | class PIDToleranceTest : public testing::Test { |
| 18 | protected: |
| 19 | const double setpoint = 50.0; |
| 20 | const double range = 200; |
| 21 | const double tolerance = 10.0; |
| 22 | |
| 23 | class FakeInput : public PIDSource { |
| 24 | public: |
| 25 | double val = 0; |
| 26 | |
| 27 | void SetPIDSourceType(PIDSourceType pidSource) {} |
| 28 | |
| 29 | PIDSourceType GetPIDSourceType() { return PIDSourceType::kDisplacement; } |
| 30 | |
| 31 | double PIDGet() { return val; } |
| 32 | }; |
| 33 | |
| 34 | class FakeOutput : public PIDOutput { |
| 35 | void PIDWrite(double output) {} |
| 36 | }; |
| 37 | |
| 38 | FakeInput inp; |
| 39 | FakeOutput out; |
| 40 | PIDController* pid; |
| 41 | |
| 42 | void SetUp() override { |
| 43 | pid = new PIDController(0.5, 0.0, 0.0, &inp, &out); |
| 44 | pid->SetInputRange(-range / 2, range / 2); |
| 45 | } |
| 46 | |
| 47 | void TearDown() override { delete pid; } |
| 48 | |
| 49 | void Reset() { inp.val = 0; } |
| 50 | }; |
| 51 | |
| 52 | TEST_F(PIDToleranceTest, Absolute) { |
| 53 | Reset(); |
| 54 | |
| 55 | pid->SetAbsoluteTolerance(tolerance); |
| 56 | pid->SetSetpoint(setpoint); |
| 57 | pid->Enable(); |
| 58 | |
| 59 | EXPECT_FALSE(pid->OnTarget()) |
| 60 | << "Error was in tolerance when it should not have been. Error was " |
| 61 | << pid->GetError(); |
| 62 | |
| 63 | inp.val = setpoint + tolerance / 2; |
| 64 | Wait(1.0); |
| 65 | |
| 66 | EXPECT_TRUE(pid->OnTarget()) |
| 67 | << "Error was not in tolerance when it should have been. Error was " |
| 68 | << pid->GetError(); |
| 69 | |
| 70 | inp.val = setpoint + 10 * tolerance; |
| 71 | Wait(1.0); |
| 72 | |
| 73 | EXPECT_FALSE(pid->OnTarget()) |
| 74 | << "Error was in tolerance when it should not have been. Error was " |
| 75 | << pid->GetError(); |
| 76 | } |
| 77 | |
| 78 | TEST_F(PIDToleranceTest, Percent) { |
| 79 | Reset(); |
| 80 | |
| 81 | pid->SetPercentTolerance(tolerance); |
| 82 | pid->SetSetpoint(setpoint); |
| 83 | pid->Enable(); |
| 84 | |
| 85 | EXPECT_FALSE(pid->OnTarget()) |
| 86 | << "Error was in tolerance when it should not have been. Error was " |
| 87 | << pid->GetError(); |
| 88 | |
| 89 | inp.val = |
| 90 | setpoint + (tolerance) / 200 * |
| 91 | range; // half of percent tolerance away from setpoint |
| 92 | Wait(1.0); |
| 93 | |
| 94 | EXPECT_TRUE(pid->OnTarget()) |
| 95 | << "Error was not in tolerance when it should have been. Error was " |
| 96 | << pid->GetError(); |
| 97 | |
| 98 | inp.val = |
| 99 | setpoint + |
| 100 | (tolerance) / 50 * range; // double percent tolerance away from setPoint |
| 101 | |
| 102 | Wait(1.0); |
| 103 | |
| 104 | EXPECT_FALSE(pid->OnTarget()) |
| 105 | << "Error was in tolerance when it should not have been. Error was " |
| 106 | << pid->GetError(); |
| 107 | } |