blob: 4d1460312b923761f6995f36cca20a0ab465b4c1 [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
James Kuszmaulb13e13f2023-11-22 20:44:04 -08005#include <gtest/gtest.h>
6
Brian Silverman8fce7482020-01-05 13:18:21 -08007#include "TestBench.h"
8#include "frc/AnalogInput.h"
Brian Silverman8fce7482020-01-05 13:18:21 -08009#include "frc/DigitalInput.h"
10#include "frc/DigitalOutput.h"
11#include "frc/DoubleSolenoid.h"
Austin Schuh812d0d12021-11-04 20:16:48 -070012#include "frc/PneumaticsControlModule.h"
Brian Silverman8fce7482020-01-05 13:18:21 -080013#include "frc/Solenoid.h"
14#include "frc/Timer.h"
Brian Silverman8fce7482020-01-05 13:18:21 -080015
Brian Silverman8fce7482020-01-05 13:18:21 -080016/* The PCM switches the compressor up to a couple seconds after the pressure
17 switch changes. */
Austin Schuh812d0d12021-11-04 20:16:48 -070018static constexpr auto kCompressorDelayTime = 3_s;
Brian Silverman8fce7482020-01-05 13:18:21 -080019
20/* Solenoids should change much more quickly */
Austin Schuh812d0d12021-11-04 20:16:48 -070021static constexpr auto kSolenoidDelayTime = 0.5_s;
Brian Silverman8fce7482020-01-05 13:18:21 -080022
23/* The voltage divider on the test bench should bring the compressor output
24 to around these values. */
25static const double kCompressorOnVoltage = 5.00;
26static const double kCompressorOffVoltage = 1.68;
27
28class PCMTest : public testing::Test {
29 protected:
Austin Schuh812d0d12021-11-04 20:16:48 -070030 frc::PneumaticsControlModule m_pneumaticsModule;
Brian Silverman8fce7482020-01-05 13:18:21 -080031
Austin Schuh812d0d12021-11-04 20:16:48 -070032 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 Silverman8fce7482020-01-05 13:18:21 -080037
38 void Reset() {
Austin Schuh75263e32022-02-22 18:05:32 -080039 m_pneumaticsModule.DisableCompressor();
Austin Schuh812d0d12021-11-04 20:16:48 -070040 m_fakePressureSwitch.Set(false);
Brian Silverman8fce7482020-01-05 13:18:21 -080041 }
42};
43
44/**
45 * Test if the compressor turns on and off when the pressure switch is toggled
46 */
47TEST_F(PCMTest, PressureSwitch) {
48 Reset();
49
Austin Schuh75263e32022-02-22 18:05:32 -080050 m_pneumaticsModule.EnableCompressorDigital();
Brian Silverman8fce7482020-01-05 13:18:21 -080051
52 // Turn on the compressor
Austin Schuh812d0d12021-11-04 20:16:48 -070053 m_fakePressureSwitch.Set(true);
54 frc::Wait(kCompressorDelayTime);
55 EXPECT_NEAR(kCompressorOnVoltage, m_fakeCompressor.GetVoltage(), 0.5)
Brian Silverman8fce7482020-01-05 13:18:21 -080056 << "Compressor did not turn on when the pressure switch turned on.";
57
58 // Turn off the compressor
Austin Schuh812d0d12021-11-04 20:16:48 -070059 m_fakePressureSwitch.Set(false);
60 frc::Wait(kCompressorDelayTime);
61 EXPECT_NEAR(kCompressorOffVoltage, m_fakeCompressor.GetVoltage(), 0.5)
Brian Silverman8fce7482020-01-05 13:18:21 -080062 << "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 */
68TEST_F(PCMTest, Solenoid) {
69 Reset();
Austin Schuh812d0d12021-11-04 20:16:48 -070070 frc::Solenoid solenoid1{frc::PneumaticsModuleType::CTREPCM,
71 TestBench::kSolenoidChannel1};
72 frc::Solenoid solenoid2{frc::PneumaticsModuleType::CTREPCM,
73 TestBench::kSolenoidChannel2};
Brian Silverman8fce7482020-01-05 13:18:21 -080074
75 // Turn both solenoids off
76 solenoid1.Set(false);
77 solenoid2.Set(false);
Austin Schuh812d0d12021-11-04 20:16:48 -070078 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 Silverman8fce7482020-01-05 13:18:21 -080081 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 Schuh812d0d12021-11-04 20:16:48 -070087 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 Silverman8fce7482020-01-05 13:18:21 -080090 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 Schuh812d0d12021-11-04 20:16:48 -070096 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 Silverman8fce7482020-01-05 13:18:21 -080099 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 Schuh812d0d12021-11-04 20:16:48 -0700105 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 Silverman8fce7482020-01-05 13:18:21 -0800108 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 */
116TEST_F(PCMTest, DoubleSolenoid) {
Austin Schuh812d0d12021-11-04 20:16:48 -0700117 frc::DoubleSolenoid solenoid{frc::PneumaticsModuleType::CTREPCM,
118 TestBench::kSolenoidChannel1,
119 TestBench::kSolenoidChannel2};
Brian Silverman8fce7482020-01-05 13:18:21 -0800120
Austin Schuh812d0d12021-11-04 20:16:48 -0700121 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 Silverman8fce7482020-01-05 13:18:21 -0800126 << "Solenoid does not read off";
127
Austin Schuh812d0d12021-11-04 20:16:48 -0700128 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 Silverman8fce7482020-01-05 13:18:21 -0800133 << "Solenoid does not read forward";
134
Austin Schuh812d0d12021-11-04 20:16:48 -0700135 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 Silverman8fce7482020-01-05 13:18:21 -0800140 << "Solenoid does not read reverse";
141}
142
143TEST_F(PCMTest, OneShot) {
144 Reset();
Austin Schuh812d0d12021-11-04 20:16:48 -0700145 frc::Solenoid solenoid1{frc::PneumaticsModuleType::CTREPCM,
146 TestBench::kSolenoidChannel1};
147 frc::Solenoid solenoid2{frc::PneumaticsModuleType::CTREPCM,
148 TestBench::kSolenoidChannel2};
Brian Silverman8fce7482020-01-05 13:18:21 -0800149
150 // Turn both solenoids off
151 solenoid1.Set(false);
152 solenoid2.Set(false);
Austin Schuh812d0d12021-11-04 20:16:48 -0700153 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 Silverman8fce7482020-01-05 13:18:21 -0800156 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 Schuh812d0d12021-11-04 20:16:48 -0700163 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 Silverman8fce7482020-01-05 13:18:21 -0800166 EXPECT_TRUE(solenoid1.Get()) << "Solenoid #1 did not read on";
167 EXPECT_FALSE(solenoid2.Get()) << "Solenoid #2 did not read off";
Austin Schuh812d0d12021-11-04 20:16:48 -0700168 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 Silverman8fce7482020-01-05 13:18:21 -0800171 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 Schuh812d0d12021-11-04 20:16:48 -0700178 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 Silverman8fce7482020-01-05 13:18:21 -0800181 EXPECT_FALSE(solenoid1.Get()) << "Solenoid #1 did not read off";
182 EXPECT_TRUE(solenoid2.Get()) << "Solenoid #2 did not read on";
Austin Schuh812d0d12021-11-04 20:16:48 -0700183 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 Silverman8fce7482020-01-05 13:18:21 -0800186 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 Schuh812d0d12021-11-04 20:16:48 -0700194 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 Silverman8fce7482020-01-05 13:18:21 -0800197 EXPECT_TRUE(solenoid1.Get()) << "Solenoid #1 did not read on";
198 EXPECT_TRUE(solenoid2.Get()) << "Solenoid #2 did not read on";
Austin Schuh812d0d12021-11-04 20:16:48 -0700199 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 Silverman8fce7482020-01-05 13:18:21 -0800202 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 Schuh812d0d12021-11-04 20:16:48 -0700210 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 Silverman8fce7482020-01-05 13:18:21 -0800213 EXPECT_TRUE(solenoid1.Get()) << "Solenoid #1 did not read on";
214 EXPECT_TRUE(solenoid2.Get()) << "Solenoid #2 did not read on";
Austin Schuh812d0d12021-11-04 20:16:48 -0700215 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 Silverman8fce7482020-01-05 13:18:21 -0800218 EXPECT_FALSE(solenoid1.Get()) << "Solenoid #1 did not read off";
219 EXPECT_TRUE(solenoid2.Get()) << "Solenoid #2 did not read on";
Austin Schuh812d0d12021-11-04 20:16:48 -0700220 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 Silverman8fce7482020-01-05 13:18:21 -0800223 EXPECT_FALSE(solenoid1.Get()) << "Solenoid #1 did not read off";
224 EXPECT_FALSE(solenoid2.Get()) << "Solenoid #2 did not read off";
225}