Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 1 | // Copyright (c) FIRST and other WPILib contributors. |
| 2 | // Open Source Software; you can modify and/or share it under the terms of |
| 3 | // the WPILib BSD license file in the root directory of this project. |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 4 | |
| 5 | #include "frc/Counter.h" // NOLINT(build/include_order) |
| 6 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 7 | #include <units/time.h> |
| 8 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 9 | #include "TestBench.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 10 | #include "frc/Timer.h" |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 11 | #include "frc/motorcontrol/Jaguar.h" |
| 12 | #include "frc/motorcontrol/Talon.h" |
| 13 | #include "frc/motorcontrol/Victor.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 14 | #include "gtest/gtest.h" |
| 15 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 16 | static constexpr auto kMotorDelay = 2.5_s; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 17 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 18 | static constexpr auto kMaxPeriod = 2_s; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 19 | |
| 20 | class CounterTest : public testing::Test { |
| 21 | protected: |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 22 | frc::Counter m_talonCounter{TestBench::kTalonEncoderChannelA}; |
| 23 | frc::Counter m_victorCounter{TestBench::kVictorEncoderChannelA}; |
| 24 | frc::Counter m_jaguarCounter{TestBench::kJaguarEncoderChannelA}; |
| 25 | frc::Talon m_talon{TestBench::kVictorChannel}; |
| 26 | frc::Victor m_victor{TestBench::kTalonChannel}; |
| 27 | frc::Jaguar m_jaguar{TestBench::kJaguarChannel}; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 28 | |
| 29 | void Reset() { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 30 | m_talonCounter.Reset(); |
| 31 | m_victorCounter.Reset(); |
| 32 | m_jaguarCounter.Reset(); |
| 33 | m_talon.Set(0.0); |
| 34 | m_victor.Set(0.0); |
| 35 | m_jaguar.Set(0.0); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 36 | } |
| 37 | }; |
| 38 | |
| 39 | /** |
| 40 | * Tests the counter by moving the motor and determining if the |
| 41 | * counter is counting. |
| 42 | */ |
| 43 | TEST_F(CounterTest, CountTalon) { |
| 44 | Reset(); |
| 45 | |
| 46 | /* Run the motor forward and determine if the counter is counting. */ |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 47 | m_talon.Set(1.0); |
| 48 | frc::Wait(0.5_s); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 49 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 50 | EXPECT_NE(0.0, m_talonCounter.Get()) << "The counter did not count (talon)"; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 51 | |
| 52 | /* Set the motor to 0 and determine if the counter resets to 0. */ |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 53 | m_talon.Set(0.0); |
| 54 | frc::Wait(0.5_s); |
| 55 | m_talonCounter.Reset(); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 56 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 57 | EXPECT_FLOAT_EQ(0.0, m_talonCounter.Get()) |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 58 | << "The counter did not restart to 0 (talon)"; |
| 59 | } |
| 60 | |
| 61 | TEST_F(CounterTest, CountVictor) { |
| 62 | Reset(); |
| 63 | |
| 64 | /* Run the motor forward and determine if the counter is counting. */ |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 65 | m_victor.Set(1.0); |
| 66 | frc::Wait(0.5_s); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 67 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 68 | EXPECT_NE(0.0, m_victorCounter.Get()) << "The counter did not count (victor)"; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 69 | |
| 70 | /* Set the motor to 0 and determine if the counter resets to 0. */ |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 71 | m_victor.Set(0.0); |
| 72 | frc::Wait(0.5_s); |
| 73 | m_victorCounter.Reset(); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 74 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 75 | EXPECT_FLOAT_EQ(0.0, m_victorCounter.Get()) |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 76 | << "The counter did not restart to 0 (jaguar)"; |
| 77 | } |
| 78 | |
| 79 | TEST_F(CounterTest, CountJaguar) { |
| 80 | Reset(); |
| 81 | |
| 82 | /* Run the motor forward and determine if the counter is counting. */ |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 83 | m_jaguar.Set(1.0); |
| 84 | frc::Wait(0.5_s); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 85 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 86 | EXPECT_NE(0.0, m_jaguarCounter.Get()) << "The counter did not count (jaguar)"; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 87 | |
| 88 | /* Set the motor to 0 and determine if the counter resets to 0. */ |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 89 | m_jaguar.Set(0.0); |
| 90 | frc::Wait(0.5_s); |
| 91 | m_jaguarCounter.Reset(); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 92 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 93 | EXPECT_FLOAT_EQ(0.0, m_jaguarCounter.Get()) |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 94 | << "The counter did not restart to 0 (jaguar)"; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Tests the GetStopped and SetMaxPeriod methods by setting the Max Period and |
| 99 | * getting the value after a period of time. |
| 100 | */ |
| 101 | TEST_F(CounterTest, TalonGetStopped) { |
| 102 | Reset(); |
| 103 | |
| 104 | /* Set the Max Period of the counter and run the motor */ |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 105 | m_talonCounter.SetMaxPeriod(kMaxPeriod); |
| 106 | m_talon.Set(1.0); |
| 107 | frc::Wait(0.5_s); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 108 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 109 | EXPECT_FALSE(m_talonCounter.GetStopped()) << "The counter did not count."; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 110 | |
| 111 | /* Stop the motor and wait until the Max Period is exceeded */ |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 112 | m_talon.Set(0.0); |
| 113 | frc::Wait(kMotorDelay); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 114 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 115 | EXPECT_TRUE(m_talonCounter.GetStopped()) |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 116 | << "The counter did not stop counting."; |
| 117 | } |
| 118 | |
| 119 | TEST_F(CounterTest, VictorGetStopped) { |
| 120 | Reset(); |
| 121 | |
| 122 | /* Set the Max Period of the counter and run the motor */ |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 123 | m_victorCounter.SetMaxPeriod(kMaxPeriod); |
| 124 | m_victor.Set(1.0); |
| 125 | frc::Wait(0.5_s); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 126 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 127 | EXPECT_FALSE(m_victorCounter.GetStopped()) << "The counter did not count."; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 128 | |
| 129 | /* Stop the motor and wait until the Max Period is exceeded */ |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 130 | m_victor.Set(0.0); |
| 131 | frc::Wait(kMotorDelay); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 132 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 133 | EXPECT_TRUE(m_victorCounter.GetStopped()) |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 134 | << "The counter did not stop counting."; |
| 135 | } |
| 136 | |
| 137 | TEST_F(CounterTest, JaguarGetStopped) { |
| 138 | Reset(); |
| 139 | |
| 140 | /* Set the Max Period of the counter and run the motor */ |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 141 | m_jaguarCounter.SetMaxPeriod(kMaxPeriod); |
| 142 | m_jaguar.Set(1.0); |
| 143 | frc::Wait(0.5_s); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 144 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 145 | EXPECT_FALSE(m_jaguarCounter.GetStopped()) << "The counter did not count."; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 146 | |
| 147 | /* Stop the motor and wait until the Max Period is exceeded */ |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 148 | m_jaguar.Set(0.0); |
| 149 | frc::Wait(kMotorDelay); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 150 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 151 | EXPECT_TRUE(m_jaguarCounter.GetStopped()) |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 152 | << "The counter did not stop counting."; |
| 153 | } |