blob: ea271a5dc80d6b9051987db69882e01866165137 [file] [log] [blame]
Austin Schuh812d0d12021-11-04 20:16:48 -07001// 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 Silverman8fce7482020-01-05 13:18:21 -08004
5#include "TestBench.h"
6#include "frc/AnalogInput.h"
Brian Silverman8fce7482020-01-05 13:18:21 -08007#include "frc/DigitalInput.h"
8#include "frc/DigitalOutput.h"
9#include "frc/DoubleSolenoid.h"
Austin Schuh812d0d12021-11-04 20:16:48 -070010#include "frc/PneumaticsControlModule.h"
Brian Silverman8fce7482020-01-05 13:18:21 -080011#include "frc/Solenoid.h"
12#include "frc/Timer.h"
13#include "gtest/gtest.h"
14
Brian Silverman8fce7482020-01-05 13:18:21 -080015/* The PCM switches the compressor up to a couple seconds after the pressure
16 switch changes. */
Austin Schuh812d0d12021-11-04 20:16:48 -070017static constexpr auto kCompressorDelayTime = 3_s;
Brian Silverman8fce7482020-01-05 13:18:21 -080018
19/* Solenoids should change much more quickly */
Austin Schuh812d0d12021-11-04 20:16:48 -070020static constexpr auto kSolenoidDelayTime = 0.5_s;
Brian Silverman8fce7482020-01-05 13:18:21 -080021
22/* The voltage divider on the test bench should bring the compressor output
23 to around these values. */
24static const double kCompressorOnVoltage = 5.00;
25static const double kCompressorOffVoltage = 1.68;
26
27class PCMTest : public testing::Test {
28 protected:
Austin Schuh812d0d12021-11-04 20:16:48 -070029 frc::PneumaticsControlModule m_pneumaticsModule;
Brian Silverman8fce7482020-01-05 13:18:21 -080030
Austin Schuh812d0d12021-11-04 20:16:48 -070031 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 Silverman8fce7482020-01-05 13:18:21 -080036
37 void Reset() {
Austin Schuh75263e32022-02-22 18:05:32 -080038 m_pneumaticsModule.DisableCompressor();
Austin Schuh812d0d12021-11-04 20:16:48 -070039 m_fakePressureSwitch.Set(false);
Brian Silverman8fce7482020-01-05 13:18:21 -080040 }
41};
42
43/**
44 * Test if the compressor turns on and off when the pressure switch is toggled
45 */
46TEST_F(PCMTest, PressureSwitch) {
47 Reset();
48
Austin Schuh75263e32022-02-22 18:05:32 -080049 m_pneumaticsModule.EnableCompressorDigital();
Brian Silverman8fce7482020-01-05 13:18:21 -080050
51 // Turn on the compressor
Austin Schuh812d0d12021-11-04 20:16:48 -070052 m_fakePressureSwitch.Set(true);
53 frc::Wait(kCompressorDelayTime);
54 EXPECT_NEAR(kCompressorOnVoltage, m_fakeCompressor.GetVoltage(), 0.5)
Brian Silverman8fce7482020-01-05 13:18:21 -080055 << "Compressor did not turn on when the pressure switch turned on.";
56
57 // Turn off the compressor
Austin Schuh812d0d12021-11-04 20:16:48 -070058 m_fakePressureSwitch.Set(false);
59 frc::Wait(kCompressorDelayTime);
60 EXPECT_NEAR(kCompressorOffVoltage, m_fakeCompressor.GetVoltage(), 0.5)
Brian Silverman8fce7482020-01-05 13:18:21 -080061 << "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 */
67TEST_F(PCMTest, Solenoid) {
68 Reset();
Austin Schuh812d0d12021-11-04 20:16:48 -070069 frc::Solenoid solenoid1{frc::PneumaticsModuleType::CTREPCM,
70 TestBench::kSolenoidChannel1};
71 frc::Solenoid solenoid2{frc::PneumaticsModuleType::CTREPCM,
72 TestBench::kSolenoidChannel2};
Brian Silverman8fce7482020-01-05 13:18:21 -080073
74 // Turn both solenoids off
75 solenoid1.Set(false);
76 solenoid2.Set(false);
Austin Schuh812d0d12021-11-04 20:16:48 -070077 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 Silverman8fce7482020-01-05 13:18:21 -080080 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 Schuh812d0d12021-11-04 20:16:48 -070086 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 Silverman8fce7482020-01-05 13:18:21 -080089 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 Schuh812d0d12021-11-04 20:16:48 -070095 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 Silverman8fce7482020-01-05 13:18:21 -080098 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 Schuh812d0d12021-11-04 20:16:48 -0700104 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 Silverman8fce7482020-01-05 13:18:21 -0800107 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 */
115TEST_F(PCMTest, DoubleSolenoid) {
Austin Schuh812d0d12021-11-04 20:16:48 -0700116 frc::DoubleSolenoid solenoid{frc::PneumaticsModuleType::CTREPCM,
117 TestBench::kSolenoidChannel1,
118 TestBench::kSolenoidChannel2};
Brian Silverman8fce7482020-01-05 13:18:21 -0800119
Austin Schuh812d0d12021-11-04 20:16:48 -0700120 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 Silverman8fce7482020-01-05 13:18:21 -0800125 << "Solenoid does not read off";
126
Austin Schuh812d0d12021-11-04 20:16:48 -0700127 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 Silverman8fce7482020-01-05 13:18:21 -0800132 << "Solenoid does not read forward";
133
Austin Schuh812d0d12021-11-04 20:16:48 -0700134 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 Silverman8fce7482020-01-05 13:18:21 -0800139 << "Solenoid does not read reverse";
140}
141
142TEST_F(PCMTest, OneShot) {
143 Reset();
Austin Schuh812d0d12021-11-04 20:16:48 -0700144 frc::Solenoid solenoid1{frc::PneumaticsModuleType::CTREPCM,
145 TestBench::kSolenoidChannel1};
146 frc::Solenoid solenoid2{frc::PneumaticsModuleType::CTREPCM,
147 TestBench::kSolenoidChannel2};
Brian Silverman8fce7482020-01-05 13:18:21 -0800148
149 // Turn both solenoids off
150 solenoid1.Set(false);
151 solenoid2.Set(false);
Austin Schuh812d0d12021-11-04 20:16:48 -0700152 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 Silverman8fce7482020-01-05 13:18:21 -0800155 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 Schuh812d0d12021-11-04 20:16:48 -0700162 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 Silverman8fce7482020-01-05 13:18:21 -0800165 EXPECT_TRUE(solenoid1.Get()) << "Solenoid #1 did not read on";
166 EXPECT_FALSE(solenoid2.Get()) << "Solenoid #2 did not read off";
Austin Schuh812d0d12021-11-04 20:16:48 -0700167 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 Silverman8fce7482020-01-05 13:18:21 -0800170 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 Schuh812d0d12021-11-04 20:16:48 -0700177 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 Silverman8fce7482020-01-05 13:18:21 -0800180 EXPECT_FALSE(solenoid1.Get()) << "Solenoid #1 did not read off";
181 EXPECT_TRUE(solenoid2.Get()) << "Solenoid #2 did not read on";
Austin Schuh812d0d12021-11-04 20:16:48 -0700182 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 Silverman8fce7482020-01-05 13:18:21 -0800185 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 Schuh812d0d12021-11-04 20:16:48 -0700193 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 Silverman8fce7482020-01-05 13:18:21 -0800196 EXPECT_TRUE(solenoid1.Get()) << "Solenoid #1 did not read on";
197 EXPECT_TRUE(solenoid2.Get()) << "Solenoid #2 did not read on";
Austin Schuh812d0d12021-11-04 20:16:48 -0700198 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 Silverman8fce7482020-01-05 13:18:21 -0800201 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 Schuh812d0d12021-11-04 20:16:48 -0700209 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 Silverman8fce7482020-01-05 13:18:21 -0800212 EXPECT_TRUE(solenoid1.Get()) << "Solenoid #1 did not read on";
213 EXPECT_TRUE(solenoid2.Get()) << "Solenoid #2 did not read on";
Austin Schuh812d0d12021-11-04 20:16:48 -0700214 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 Silverman8fce7482020-01-05 13:18:21 -0800217 EXPECT_FALSE(solenoid1.Get()) << "Solenoid #1 did not read off";
218 EXPECT_TRUE(solenoid2.Get()) << "Solenoid #2 did not read on";
Austin Schuh812d0d12021-11-04 20:16:48 -0700219 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 Silverman8fce7482020-01-05 13:18:21 -0800222 EXPECT_FALSE(solenoid1.Get()) << "Solenoid #1 did not read off";
223 EXPECT_FALSE(solenoid2.Get()) << "Solenoid #2 did not read off";
224}