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/Encoder.h" // NOLINT(build/include_order) |
| 6 | |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 7 | #include <gtest/gtest.h> |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 8 | #include <units/time.h> |
| 9 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 10 | #include "TestBench.h" |
| 11 | #include "frc/AnalogOutput.h" |
| 12 | #include "frc/AnalogTrigger.h" |
| 13 | #include "frc/DigitalOutput.h" |
| 14 | #include "frc/Timer.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 15 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 16 | static constexpr auto kDelayTime = 1_ms; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 17 | |
| 18 | class FakeEncoderTest : public testing::Test { |
| 19 | protected: |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 20 | frc::DigitalOutput m_outputA{TestBench::kLoop2OutputChannel}; |
| 21 | frc::DigitalOutput m_outputB{TestBench::kLoop1OutputChannel}; |
| 22 | frc::AnalogOutput m_indexOutput{TestBench::kAnalogOutputChannel}; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 23 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 24 | frc::Encoder m_encoder{TestBench::kLoop1InputChannel, |
| 25 | TestBench::kLoop2InputChannel}; |
| 26 | frc::AnalogTrigger m_indexAnalogTrigger{TestBench::kFakeAnalogOutputChannel}; |
| 27 | std::shared_ptr<frc::AnalogTriggerOutput> m_indexAnalogTriggerOutput = |
| 28 | m_indexAnalogTrigger.CreateOutput(frc::AnalogTriggerType::kState); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 29 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 30 | FakeEncoderTest() { |
| 31 | m_outputA.Set(false); |
| 32 | m_outputB.Set(false); |
| 33 | m_indexAnalogTrigger.SetLimitsVoltage(2.0, 3.0); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Output pulses to the encoder's input channels to simulate a change of 100 |
| 38 | * ticks |
| 39 | */ |
| 40 | void Simulate100QuadratureTicks() { |
| 41 | for (int32_t i = 0; i < 100; i++) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 42 | m_outputA.Set(true); |
| 43 | frc::Wait(kDelayTime); |
| 44 | m_outputB.Set(true); |
| 45 | frc::Wait(kDelayTime); |
| 46 | m_outputA.Set(false); |
| 47 | frc::Wait(kDelayTime); |
| 48 | m_outputB.Set(false); |
| 49 | frc::Wait(kDelayTime); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
| 53 | void SetIndexHigh() { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 54 | m_indexOutput.SetVoltage(5.0); |
| 55 | frc::Wait(kDelayTime); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | void SetIndexLow() { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 59 | m_indexOutput.SetVoltage(0.0); |
| 60 | frc::Wait(kDelayTime); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 61 | } |
| 62 | }; |
| 63 | |
| 64 | /** |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 65 | * Test the encoder by resetting it to 0 and reading the value. |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 66 | */ |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 67 | TEST_F(FakeEncoderTest, DefaultState) { |
| 68 | EXPECT_DOUBLE_EQ(0.0, m_encoder.Get()) << "The encoder did not start at 0."; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Test the encoder by setting the digital outputs and reading the value. |
| 73 | */ |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 74 | TEST_F(FakeEncoderTest, CountUp) { |
| 75 | m_encoder.Reset(); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 76 | Simulate100QuadratureTicks(); |
| 77 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 78 | EXPECT_DOUBLE_EQ(100.0, m_encoder.Get()) << "Encoder did not count to 100."; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Test that the encoder can stay reset while the index source is high |
| 83 | */ |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 84 | TEST_F(FakeEncoderTest, ResetWhileHigh) { |
| 85 | m_encoder.SetIndexSource(*m_indexAnalogTriggerOutput, |
| 86 | frc::Encoder::IndexingType::kResetWhileHigh); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 87 | |
| 88 | SetIndexLow(); |
| 89 | Simulate100QuadratureTicks(); |
| 90 | SetIndexHigh(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 91 | EXPECT_EQ(0, m_encoder.Get()); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 92 | |
| 93 | Simulate100QuadratureTicks(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 94 | EXPECT_EQ(0, m_encoder.Get()); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Test that the encoder can reset when the index source goes from low to high |
| 99 | */ |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 100 | TEST_F(FakeEncoderTest, ResetOnRisingEdge) { |
| 101 | m_encoder.SetIndexSource(*m_indexAnalogTriggerOutput, |
| 102 | frc::Encoder::IndexingType::kResetOnRisingEdge); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 103 | |
| 104 | SetIndexLow(); |
| 105 | Simulate100QuadratureTicks(); |
| 106 | SetIndexHigh(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 107 | EXPECT_EQ(0, m_encoder.Get()); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 108 | |
| 109 | Simulate100QuadratureTicks(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 110 | EXPECT_EQ(100, m_encoder.Get()); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Test that the encoder can stay reset while the index source is low |
| 115 | */ |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 116 | TEST_F(FakeEncoderTest, ResetWhileLow) { |
| 117 | m_encoder.SetIndexSource(*m_indexAnalogTriggerOutput, |
| 118 | frc::Encoder::IndexingType::kResetWhileLow); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 119 | |
| 120 | SetIndexHigh(); |
| 121 | Simulate100QuadratureTicks(); |
| 122 | SetIndexLow(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 123 | EXPECT_EQ(0, m_encoder.Get()); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 124 | |
| 125 | Simulate100QuadratureTicks(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 126 | EXPECT_EQ(0, m_encoder.Get()); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Test that the encoder can reset when the index source goes from high to low |
| 131 | */ |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 132 | TEST_F(FakeEncoderTest, ResetOnFallingEdge) { |
| 133 | m_encoder.SetIndexSource(*m_indexAnalogTriggerOutput, |
| 134 | frc::Encoder::IndexingType::kResetOnFallingEdge); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 135 | |
| 136 | SetIndexHigh(); |
| 137 | Simulate100QuadratureTicks(); |
| 138 | SetIndexLow(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 139 | EXPECT_EQ(0, m_encoder.Get()); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 140 | |
| 141 | Simulate100QuadratureTicks(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 142 | EXPECT_EQ(100, m_encoder.Get()); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 143 | } |