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