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 "TestBench.h" |
| 6 | #include "frc/AnalogInput.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 7 | #include "frc/DigitalInput.h" |
| 8 | #include "frc/DigitalOutput.h" |
| 9 | #include "frc/DoubleSolenoid.h" |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 10 | #include "frc/PneumaticsControlModule.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 11 | #include "frc/Solenoid.h" |
| 12 | #include "frc/Timer.h" |
| 13 | #include "gtest/gtest.h" |
| 14 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 15 | /* The PCM switches the compressor up to a couple seconds after the pressure |
| 16 | switch changes. */ |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 17 | static constexpr auto kCompressorDelayTime = 3_s; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 18 | |
| 19 | /* Solenoids should change much more quickly */ |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 20 | static constexpr auto kSolenoidDelayTime = 0.5_s; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 21 | |
| 22 | /* The voltage divider on the test bench should bring the compressor output |
| 23 | to around these values. */ |
| 24 | static const double kCompressorOnVoltage = 5.00; |
| 25 | static const double kCompressorOffVoltage = 1.68; |
| 26 | |
| 27 | class PCMTest : public testing::Test { |
| 28 | protected: |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 29 | frc::PneumaticsControlModule m_pneumaticsModule; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 30 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 31 | frc::DigitalOutput m_fakePressureSwitch{ |
| 32 | TestBench::kFakePressureSwitchChannel}; |
| 33 | frc::AnalogInput m_fakeCompressor{TestBench::kFakeCompressorChannel}; |
| 34 | frc::DigitalInput m_fakeSolenoid1{TestBench::kFakeSolenoid1Channel}; |
| 35 | frc::DigitalInput m_fakeSolenoid2{TestBench::kFakeSolenoid2Channel}; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 36 | |
| 37 | void Reset() { |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 38 | m_pneumaticsModule.DisableCompressor(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 39 | m_fakePressureSwitch.Set(false); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 40 | } |
| 41 | }; |
| 42 | |
| 43 | /** |
| 44 | * Test if the compressor turns on and off when the pressure switch is toggled |
| 45 | */ |
| 46 | TEST_F(PCMTest, PressureSwitch) { |
| 47 | Reset(); |
| 48 | |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 49 | m_pneumaticsModule.EnableCompressorDigital(); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 50 | |
| 51 | // Turn on the compressor |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 52 | m_fakePressureSwitch.Set(true); |
| 53 | frc::Wait(kCompressorDelayTime); |
| 54 | EXPECT_NEAR(kCompressorOnVoltage, m_fakeCompressor.GetVoltage(), 0.5) |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 55 | << "Compressor did not turn on when the pressure switch turned on."; |
| 56 | |
| 57 | // Turn off the compressor |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 58 | m_fakePressureSwitch.Set(false); |
| 59 | frc::Wait(kCompressorDelayTime); |
| 60 | EXPECT_NEAR(kCompressorOffVoltage, m_fakeCompressor.GetVoltage(), 0.5) |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 61 | << "Compressor did not turn off when the pressure switch turned off."; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Test if the correct solenoids turn on and off when they should |
| 66 | */ |
| 67 | TEST_F(PCMTest, Solenoid) { |
| 68 | Reset(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 69 | frc::Solenoid solenoid1{frc::PneumaticsModuleType::CTREPCM, |
| 70 | TestBench::kSolenoidChannel1}; |
| 71 | frc::Solenoid solenoid2{frc::PneumaticsModuleType::CTREPCM, |
| 72 | TestBench::kSolenoidChannel2}; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 73 | |
| 74 | // Turn both solenoids off |
| 75 | solenoid1.Set(false); |
| 76 | solenoid2.Set(false); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 77 | frc::Wait(kSolenoidDelayTime); |
| 78 | EXPECT_TRUE(m_fakeSolenoid1.Get()) << "Solenoid #1 did not turn off"; |
| 79 | EXPECT_TRUE(m_fakeSolenoid2.Get()) << "Solenoid #2 did not turn off"; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 80 | EXPECT_FALSE(solenoid1.Get()) << "Solenoid #1 did not read off"; |
| 81 | EXPECT_FALSE(solenoid2.Get()) << "Solenoid #2 did not read off"; |
| 82 | |
| 83 | // Turn one solenoid on and one off |
| 84 | solenoid1.Set(true); |
| 85 | solenoid2.Set(false); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 86 | frc::Wait(kSolenoidDelayTime); |
| 87 | EXPECT_FALSE(m_fakeSolenoid1.Get()) << "Solenoid #1 did not turn on"; |
| 88 | EXPECT_TRUE(m_fakeSolenoid2.Get()) << "Solenoid #2 did not turn off"; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 89 | EXPECT_TRUE(solenoid1.Get()) << "Solenoid #1 did not read on"; |
| 90 | EXPECT_FALSE(solenoid2.Get()) << "Solenoid #2 did not read off"; |
| 91 | |
| 92 | // Turn one solenoid on and one off |
| 93 | solenoid1.Set(false); |
| 94 | solenoid2.Set(true); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 95 | frc::Wait(kSolenoidDelayTime); |
| 96 | EXPECT_TRUE(m_fakeSolenoid1.Get()) << "Solenoid #1 did not turn off"; |
| 97 | EXPECT_FALSE(m_fakeSolenoid2.Get()) << "Solenoid #2 did not turn on"; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 98 | EXPECT_FALSE(solenoid1.Get()) << "Solenoid #1 did not read off"; |
| 99 | EXPECT_TRUE(solenoid2.Get()) << "Solenoid #2 did not read on"; |
| 100 | |
| 101 | // Turn both on |
| 102 | solenoid1.Set(true); |
| 103 | solenoid2.Set(true); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 104 | frc::Wait(kSolenoidDelayTime); |
| 105 | EXPECT_FALSE(m_fakeSolenoid1.Get()) << "Solenoid #1 did not turn on"; |
| 106 | EXPECT_FALSE(m_fakeSolenoid2.Get()) << "Solenoid #2 did not turn on"; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 107 | EXPECT_TRUE(solenoid1.Get()) << "Solenoid #1 did not read on"; |
| 108 | EXPECT_TRUE(solenoid2.Get()) << "Solenoid #2 did not read on"; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Test if the correct solenoids turn on and off when they should when used |
| 113 | * with the DoubleSolenoid class. |
| 114 | */ |
| 115 | TEST_F(PCMTest, DoubleSolenoid) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 116 | frc::DoubleSolenoid solenoid{frc::PneumaticsModuleType::CTREPCM, |
| 117 | TestBench::kSolenoidChannel1, |
| 118 | TestBench::kSolenoidChannel2}; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 119 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 120 | solenoid.Set(frc::DoubleSolenoid::kOff); |
| 121 | frc::Wait(kSolenoidDelayTime); |
| 122 | EXPECT_TRUE(m_fakeSolenoid1.Get()) << "Solenoid #1 did not turn off"; |
| 123 | EXPECT_TRUE(m_fakeSolenoid2.Get()) << "Solenoid #2 did not turn off"; |
| 124 | EXPECT_TRUE(solenoid.Get() == frc::DoubleSolenoid::kOff) |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 125 | << "Solenoid does not read off"; |
| 126 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 127 | solenoid.Set(frc::DoubleSolenoid::kForward); |
| 128 | frc::Wait(kSolenoidDelayTime); |
| 129 | EXPECT_FALSE(m_fakeSolenoid1.Get()) << "Solenoid #1 did not turn on"; |
| 130 | EXPECT_TRUE(m_fakeSolenoid2.Get()) << "Solenoid #2 did not turn off"; |
| 131 | EXPECT_TRUE(solenoid.Get() == frc::DoubleSolenoid::kForward) |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 132 | << "Solenoid does not read forward"; |
| 133 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 134 | solenoid.Set(frc::DoubleSolenoid::kReverse); |
| 135 | frc::Wait(kSolenoidDelayTime); |
| 136 | EXPECT_TRUE(m_fakeSolenoid1.Get()) << "Solenoid #1 did not turn off"; |
| 137 | EXPECT_FALSE(m_fakeSolenoid2.Get()) << "Solenoid #2 did not turn on"; |
| 138 | EXPECT_TRUE(solenoid.Get() == frc::DoubleSolenoid::kReverse) |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 139 | << "Solenoid does not read reverse"; |
| 140 | } |
| 141 | |
| 142 | TEST_F(PCMTest, OneShot) { |
| 143 | Reset(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 144 | frc::Solenoid solenoid1{frc::PneumaticsModuleType::CTREPCM, |
| 145 | TestBench::kSolenoidChannel1}; |
| 146 | frc::Solenoid solenoid2{frc::PneumaticsModuleType::CTREPCM, |
| 147 | TestBench::kSolenoidChannel2}; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 148 | |
| 149 | // Turn both solenoids off |
| 150 | solenoid1.Set(false); |
| 151 | solenoid2.Set(false); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 152 | frc::Wait(kSolenoidDelayTime); |
| 153 | EXPECT_TRUE(m_fakeSolenoid1.Get()) << "Solenoid #1 did not turn off"; |
| 154 | EXPECT_TRUE(m_fakeSolenoid2.Get()) << "Solenoid #2 did not turn off"; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 155 | EXPECT_FALSE(solenoid1.Get()) << "Solenoid #1 did not read off"; |
| 156 | EXPECT_FALSE(solenoid2.Get()) << "Solenoid #2 did not read off"; |
| 157 | |
| 158 | // Pulse Solenoid #1 on, and turn Solenoid #2 off |
| 159 | solenoid1.SetPulseDuration(2 * kSolenoidDelayTime); |
| 160 | solenoid2.Set(false); |
| 161 | solenoid1.StartPulse(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 162 | frc::Wait(kSolenoidDelayTime); |
| 163 | EXPECT_FALSE(m_fakeSolenoid1.Get()) << "Solenoid #1 did not turn on"; |
| 164 | EXPECT_TRUE(m_fakeSolenoid2.Get()) << "Solenoid #2 did not turn off"; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 165 | EXPECT_TRUE(solenoid1.Get()) << "Solenoid #1 did not read on"; |
| 166 | EXPECT_FALSE(solenoid2.Get()) << "Solenoid #2 did not read off"; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 167 | frc::Wait(2 * kSolenoidDelayTime); |
| 168 | EXPECT_TRUE(m_fakeSolenoid1.Get()) << "Solenoid #1 did not turn off"; |
| 169 | EXPECT_TRUE(m_fakeSolenoid2.Get()) << "Solenoid #2 did not turn off"; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 170 | EXPECT_FALSE(solenoid1.Get()) << "Solenoid #1 did not read off"; |
| 171 | EXPECT_FALSE(solenoid2.Get()) << "Solenoid #2 did not read off"; |
| 172 | |
| 173 | // Turn Solenoid #1 off, and pulse Solenoid #2 on |
| 174 | solenoid1.Set(false); |
| 175 | solenoid2.SetPulseDuration(2 * kSolenoidDelayTime); |
| 176 | solenoid2.StartPulse(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 177 | frc::Wait(kSolenoidDelayTime); |
| 178 | EXPECT_TRUE(m_fakeSolenoid1.Get()) << "Solenoid #1 did not turn off"; |
| 179 | EXPECT_FALSE(m_fakeSolenoid2.Get()) << "Solenoid #2 did not turn on"; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 180 | EXPECT_FALSE(solenoid1.Get()) << "Solenoid #1 did not read off"; |
| 181 | EXPECT_TRUE(solenoid2.Get()) << "Solenoid #2 did not read on"; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 182 | frc::Wait(2 * kSolenoidDelayTime); |
| 183 | EXPECT_TRUE(m_fakeSolenoid1.Get()) << "Solenoid #1 did not turn off"; |
| 184 | EXPECT_TRUE(m_fakeSolenoid2.Get()) << "Solenoid #2 did not turn off"; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 185 | EXPECT_FALSE(solenoid1.Get()) << "Solenoid #1 did not read off"; |
| 186 | EXPECT_FALSE(solenoid2.Get()) << "Solenoid #2 did not read off"; |
| 187 | |
| 188 | // Pulse both Solenoids on |
| 189 | solenoid1.SetPulseDuration(2 * kSolenoidDelayTime); |
| 190 | solenoid2.SetPulseDuration(2 * kSolenoidDelayTime); |
| 191 | solenoid1.StartPulse(); |
| 192 | solenoid2.StartPulse(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 193 | frc::Wait(kSolenoidDelayTime); |
| 194 | EXPECT_FALSE(m_fakeSolenoid1.Get()) << "Solenoid #1 did not turn on"; |
| 195 | EXPECT_FALSE(m_fakeSolenoid2.Get()) << "Solenoid #2 did not turn on"; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 196 | EXPECT_TRUE(solenoid1.Get()) << "Solenoid #1 did not read on"; |
| 197 | EXPECT_TRUE(solenoid2.Get()) << "Solenoid #2 did not read on"; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 198 | frc::Wait(2 * kSolenoidDelayTime); |
| 199 | EXPECT_TRUE(m_fakeSolenoid1.Get()) << "Solenoid #1 did not turn off"; |
| 200 | EXPECT_TRUE(m_fakeSolenoid2.Get()) << "Solenoid #2 did not turn off"; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 201 | EXPECT_FALSE(solenoid1.Get()) << "Solenoid #1 did not read off"; |
| 202 | EXPECT_FALSE(solenoid2.Get()) << "Solenoid #2 did not read off"; |
| 203 | |
| 204 | // Pulse both Solenoids on with different durations |
| 205 | solenoid1.SetPulseDuration(1.5 * kSolenoidDelayTime); |
| 206 | solenoid2.SetPulseDuration(2.5 * kSolenoidDelayTime); |
| 207 | solenoid1.StartPulse(); |
| 208 | solenoid2.StartPulse(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 209 | frc::Wait(kSolenoidDelayTime); |
| 210 | EXPECT_FALSE(m_fakeSolenoid1.Get()) << "Solenoid #1 did not turn on"; |
| 211 | EXPECT_FALSE(m_fakeSolenoid2.Get()) << "Solenoid #2 did not turn on"; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 212 | EXPECT_TRUE(solenoid1.Get()) << "Solenoid #1 did not read on"; |
| 213 | EXPECT_TRUE(solenoid2.Get()) << "Solenoid #2 did not read on"; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 214 | frc::Wait(kSolenoidDelayTime); |
| 215 | EXPECT_TRUE(m_fakeSolenoid1.Get()) << "Solenoid #1 did not turn off"; |
| 216 | EXPECT_FALSE(m_fakeSolenoid2.Get()) << "Solenoid #2 did not turn on"; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 217 | EXPECT_FALSE(solenoid1.Get()) << "Solenoid #1 did not read off"; |
| 218 | EXPECT_TRUE(solenoid2.Get()) << "Solenoid #2 did not read on"; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 219 | frc::Wait(2 * kSolenoidDelayTime); |
| 220 | EXPECT_TRUE(m_fakeSolenoid1.Get()) << "Solenoid #1 did not turn off"; |
| 221 | EXPECT_TRUE(m_fakeSolenoid2.Get()) << "Solenoid #2 did not turn off"; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 222 | EXPECT_FALSE(solenoid1.Get()) << "Solenoid #1 did not read off"; |
| 223 | EXPECT_FALSE(solenoid2.Get()) << "Solenoid #2 did not read off"; |
| 224 | } |