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 "frc/Counter.h" // NOLINT(build/include_order) |
| 9 | |
| 10 | #include "TestBench.h" |
| 11 | #include "frc/Jaguar.h" |
| 12 | #include "frc/Talon.h" |
| 13 | #include "frc/Timer.h" |
| 14 | #include "frc/Victor.h" |
| 15 | #include "gtest/gtest.h" |
| 16 | |
| 17 | using namespace frc; |
| 18 | |
| 19 | static const double kMotorDelay = 2.5; |
| 20 | |
| 21 | static const double kMaxPeriod = 2.0; |
| 22 | |
| 23 | class CounterTest : public testing::Test { |
| 24 | protected: |
| 25 | Counter* m_talonCounter; |
| 26 | Counter* m_victorCounter; |
| 27 | Counter* m_jaguarCounter; |
| 28 | Talon* m_talon; |
| 29 | Victor* m_victor; |
| 30 | Jaguar* m_jaguar; |
| 31 | |
| 32 | void SetUp() override { |
| 33 | m_talonCounter = new Counter(TestBench::kTalonEncoderChannelA); |
| 34 | m_victorCounter = new Counter(TestBench::kVictorEncoderChannelA); |
| 35 | m_jaguarCounter = new Counter(TestBench::kJaguarEncoderChannelA); |
| 36 | m_victor = new Victor(TestBench::kVictorChannel); |
| 37 | m_talon = new Talon(TestBench::kTalonChannel); |
| 38 | m_jaguar = new Jaguar(TestBench::kJaguarChannel); |
| 39 | } |
| 40 | |
| 41 | void TearDown() override { |
| 42 | delete m_talonCounter; |
| 43 | delete m_victorCounter; |
| 44 | delete m_jaguarCounter; |
| 45 | delete m_victor; |
| 46 | delete m_talon; |
| 47 | delete m_jaguar; |
| 48 | } |
| 49 | |
| 50 | void Reset() { |
| 51 | m_talonCounter->Reset(); |
| 52 | m_victorCounter->Reset(); |
| 53 | m_jaguarCounter->Reset(); |
| 54 | m_talon->Set(0.0); |
| 55 | m_victor->Set(0.0); |
| 56 | m_jaguar->Set(0.0); |
| 57 | } |
| 58 | }; |
| 59 | |
| 60 | /** |
| 61 | * Tests the counter by moving the motor and determining if the |
| 62 | * counter is counting. |
| 63 | */ |
| 64 | TEST_F(CounterTest, CountTalon) { |
| 65 | Reset(); |
| 66 | |
| 67 | /* Run the motor forward and determine if the counter is counting. */ |
| 68 | m_talon->Set(1.0); |
| 69 | Wait(0.5); |
| 70 | |
| 71 | EXPECT_NE(0.0, m_talonCounter->Get()) << "The counter did not count (talon)"; |
| 72 | |
| 73 | /* Set the motor to 0 and determine if the counter resets to 0. */ |
| 74 | m_talon->Set(0.0); |
| 75 | Wait(0.5); |
| 76 | m_talonCounter->Reset(); |
| 77 | |
| 78 | EXPECT_FLOAT_EQ(0.0, m_talonCounter->Get()) |
| 79 | << "The counter did not restart to 0 (talon)"; |
| 80 | } |
| 81 | |
| 82 | TEST_F(CounterTest, CountVictor) { |
| 83 | Reset(); |
| 84 | |
| 85 | /* Run the motor forward and determine if the counter is counting. */ |
| 86 | m_victor->Set(1.0); |
| 87 | Wait(0.5); |
| 88 | |
| 89 | EXPECT_NE(0.0, m_victorCounter->Get()) |
| 90 | << "The counter did not count (victor)"; |
| 91 | |
| 92 | /* Set the motor to 0 and determine if the counter resets to 0. */ |
| 93 | m_victor->Set(0.0); |
| 94 | Wait(0.5); |
| 95 | m_victorCounter->Reset(); |
| 96 | |
| 97 | EXPECT_FLOAT_EQ(0.0, m_victorCounter->Get()) |
| 98 | << "The counter did not restart to 0 (jaguar)"; |
| 99 | } |
| 100 | |
| 101 | TEST_F(CounterTest, CountJaguar) { |
| 102 | Reset(); |
| 103 | |
| 104 | /* Run the motor forward and determine if the counter is counting. */ |
| 105 | m_jaguar->Set(1.0); |
| 106 | Wait(0.5); |
| 107 | |
| 108 | EXPECT_NE(0.0, m_jaguarCounter->Get()) |
| 109 | << "The counter did not count (jaguar)"; |
| 110 | |
| 111 | /* Set the motor to 0 and determine if the counter resets to 0. */ |
| 112 | m_jaguar->Set(0.0); |
| 113 | Wait(0.5); |
| 114 | m_jaguarCounter->Reset(); |
| 115 | |
| 116 | EXPECT_FLOAT_EQ(0.0, m_jaguarCounter->Get()) |
| 117 | << "The counter did not restart to 0 (jaguar)"; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Tests the GetStopped and SetMaxPeriod methods by setting the Max Period and |
| 122 | * getting the value after a period of time. |
| 123 | */ |
| 124 | TEST_F(CounterTest, TalonGetStopped) { |
| 125 | Reset(); |
| 126 | |
| 127 | /* Set the Max Period of the counter and run the motor */ |
| 128 | m_talonCounter->SetMaxPeriod(kMaxPeriod); |
| 129 | m_talon->Set(1.0); |
| 130 | Wait(0.5); |
| 131 | |
| 132 | EXPECT_FALSE(m_talonCounter->GetStopped()) << "The counter did not count."; |
| 133 | |
| 134 | /* Stop the motor and wait until the Max Period is exceeded */ |
| 135 | m_talon->Set(0.0); |
| 136 | Wait(kMotorDelay); |
| 137 | |
| 138 | EXPECT_TRUE(m_talonCounter->GetStopped()) |
| 139 | << "The counter did not stop counting."; |
| 140 | } |
| 141 | |
| 142 | TEST_F(CounterTest, VictorGetStopped) { |
| 143 | Reset(); |
| 144 | |
| 145 | /* Set the Max Period of the counter and run the motor */ |
| 146 | m_victorCounter->SetMaxPeriod(kMaxPeriod); |
| 147 | m_victor->Set(1.0); |
| 148 | Wait(0.5); |
| 149 | |
| 150 | EXPECT_FALSE(m_victorCounter->GetStopped()) << "The counter did not count."; |
| 151 | |
| 152 | /* Stop the motor and wait until the Max Period is exceeded */ |
| 153 | m_victor->Set(0.0); |
| 154 | Wait(kMotorDelay); |
| 155 | |
| 156 | EXPECT_TRUE(m_victorCounter->GetStopped()) |
| 157 | << "The counter did not stop counting."; |
| 158 | } |
| 159 | |
| 160 | TEST_F(CounterTest, JaguarGetStopped) { |
| 161 | Reset(); |
| 162 | |
| 163 | /* Set the Max Period of the counter and run the motor */ |
| 164 | m_jaguarCounter->SetMaxPeriod(kMaxPeriod); |
| 165 | m_jaguar->Set(1.0); |
| 166 | Wait(0.5); |
| 167 | |
| 168 | EXPECT_FALSE(m_jaguarCounter->GetStopped()) << "The counter did not count."; |
| 169 | |
| 170 | /* Stop the motor and wait until the Max Period is exceeded */ |
| 171 | m_jaguar->Set(0.0); |
| 172 | Wait(kMotorDelay); |
| 173 | |
| 174 | EXPECT_TRUE(m_jaguarCounter->GetStopped()) |
| 175 | << "The counter did not stop counting."; |
| 176 | } |