diff --git a/wpilibc/wpilibC++/lib/Relay.cpp b/wpilibc/wpilibC++/lib/Relay.cpp index 608495374a..e2dfab8434 100644 --- a/wpilibc/wpilibC++/lib/Relay.cpp +++ b/wpilibc/wpilibC++/lib/Relay.cpp @@ -16,7 +16,7 @@ static Resource *relayChannels = NULL; /** - * Common relay intitialization methode. + * Common relay initialization method. * This code is common to all Relay constructors and initializes the relay and reserves * all resources that need to be locked. Initially the relay is set to both lines at 0v. */ diff --git a/wpilibc/wpilibC++IntegrationTests/include/TestBench.h b/wpilibc/wpilibC++IntegrationTests/include/TestBench.h index 66ad4208e4..8eb1a8223e 100644 --- a/wpilibc/wpilibC++IntegrationTests/include/TestBench.h +++ b/wpilibc/wpilibC++IntegrationTests/include/TestBench.h @@ -42,6 +42,11 @@ public: static const uint32_t kFakePressureSwitchChannel = 11; static const uint32_t kFakeSolenoid1Channel = 12; static const uint32_t kFakeSolenoid2Channel = 13; + static const uint32_t kFakeRelayForward = 14; + static const uint32_t kFakeRelayReverse = 15; + + /* Relay channels */ + static const uint32_t kRelayChannel = 0; /* CAN IDs */ static const uint32_t kCANJaguarID = 1; diff --git a/wpilibc/wpilibC++IntegrationTests/src/CounterTest.cpp b/wpilibc/wpilibC++IntegrationTests/src/CounterTest.cpp new file mode 100644 index 0000000000..368c42f7b7 --- /dev/null +++ b/wpilibc/wpilibC++IntegrationTests/src/CounterTest.cpp @@ -0,0 +1,97 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) FIRST 2014. All Rights Reserved. */ +/* Open Source Software - may be modified and shared by FRC teams. The code */ +/* must be accompanied by the FIRST BSD license file in the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +#include "WPILib.h" +#include "gtest/gtest.h" +#include "TestBench.h" + +enum CounterTestType { TEST_VICTOR, TEST_JAGUAR, TEST_TALON }; + +std::ostream &operator<<(std::ostream &os, CounterTestType const &type) { + switch(type) { + case TEST_VICTOR: os << "Victor"; break; + case TEST_JAGUAR: os << "Jaguar"; break; + case TEST_TALON: os << "Talon"; break; + } + + return os; +} + +static constexpr double kMotorTime = 0.5; + +/** + * Tests the counter by moving the motor and determining if the + * counter is counting. + */ + +class CounterTest : public testing::TestWithParam { +protected: + SpeedController *m_speedController; + Counter *m_counter; + + virtual void SetUp() { + switch(GetParam()) { + case TEST_VICTOR: + m_counter = new Counter(TestBench::kVictorEncoderChannelA); + m_speedController = new Victor(TestBench::kVictorChannel); + break; + + case TEST_JAGUAR: + m_counter = new Counter(TestBench::kJaguarEncoderChannelA); + m_speedController = new Jaguar(TestBench::kJaguarChannel); + break; + + case TEST_TALON: + m_counter = new Counter(TestBench::kTalonEncoderChannelA); + m_speedController = new Talon(TestBench::kTalonChannel); + break; + } + } + + virtual void TearDown() { + delete m_counter; + delete m_speedController; + } + + void Reset() { + m_counter->Reset(); + m_speedController->Set(0.0f); + } + +}; + + + /** + * Test resetting a counter + */ + TEST_P(CounterTest, Reset) { + Reset(); + EXPECT_FLOAT_EQ(0.0f, m_counter->Get()) << "Counter did not restart to 0."; + } + + /** + * Test the counter by running the motor forward and determining if + * the counter value has changed. + */ + TEST_P(CounterTest, Count) { + Reset(); + m_counter->Start(); + + /*Run the motor forward and determine if the counter is counting. */ + m_speedController->Set(1.0f); + Wait(kMotorTime); + m_speedController->Set(0.0f); + EXPECT_NE(0.0f, m_counter->Get()) << "Counter did not count."; + } + + + INSTANTIATE_TEST_CASE_P(Test, CounterTest, + testing::Values(TEST_VICTOR, TEST_JAGUAR, TEST_TALON)); + + + + diff --git a/wpilibc/wpilibC++IntegrationTests/src/PCMTest.cpp b/wpilibc/wpilibC++IntegrationTests/src/PCMTest.cpp index e61450735a..5ca8d8da05 100644 --- a/wpilibc/wpilibC++IntegrationTests/src/PCMTest.cpp +++ b/wpilibc/wpilibC++IntegrationTests/src/PCMTest.cpp @@ -11,7 +11,7 @@ /* The PCM switches the compressor up to 2 seconds after the pressure switch changes. */ -static const double kCompressoDelayTime = 2.0; +static const double kCompressorDelayTime = 2.0; /* Solenoids should change much more quickly */ static const double kSolenoidDelayTime = 0.1; @@ -72,13 +72,13 @@ TEST_F(PCMTest, PressureSwitch) { // Turn on the compressor m_fakePressureSwitch->Set(true); - Wait(kCompressoDelayTime); + Wait(kCompressorDelayTime); EXPECT_NEAR(kCompressorOnVoltage, m_fakeCompressor->GetVoltage(), 0.1) << "Compressor did not turn on when the pressure switch turned on."; // Turn off the compressor m_fakePressureSwitch->Set(false); - Wait(kCompressoDelayTime); + Wait(kCompressorDelayTime); EXPECT_NEAR(kCompressorOffVoltage, m_fakeCompressor->GetVoltage(), 0.1) << "Compressor did not turn off when the pressure switch turned off."; } @@ -104,11 +104,11 @@ TEST_F(PCMTest, Solenoid) { EXPECT_TRUE(m_fakeSolenoid2->Get()) << "Solenoid #2 did not turn off"; // Turn one solenoid on and one off - m_solenoid1->Set(true); - m_solenoid2->Set(false); + m_solenoid1->Set(false); + m_solenoid2->Set(true); Wait(kSolenoidDelayTime); - EXPECT_FALSE(m_fakeSolenoid1->Get()) << "Solenoid #1 did not turn on"; - EXPECT_TRUE(m_fakeSolenoid2->Get()) << "Solenoid #2 did not turn off"; + EXPECT_TRUE(m_fakeSolenoid1->Get()) << "Solenoid #1 did not turn off"; + EXPECT_FALSE(m_fakeSolenoid2->Get()) << "Solenoid #2 did not turn on"; // Turn both on m_solenoid1->Set(true); diff --git a/wpilibc/wpilibC++IntegrationTests/src/RelayTest.cpp b/wpilibc/wpilibC++IntegrationTests/src/RelayTest.cpp new file mode 100644 index 0000000000..86b2a74fa8 --- /dev/null +++ b/wpilibc/wpilibC++IntegrationTests/src/RelayTest.cpp @@ -0,0 +1,71 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) FIRST 2014. All Rights Reserved. */ +/* Open Source Software - may be modified and shared by FRC teams. The code */ +/* must be accompanied by the FIRST BSD license file in the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +#include "WPILib.h" +#include "gtest/gtest.h" +#include "TestBench.h" +#include "Relay.h" + +class RelayTest : public testing::Test { +protected: + Relay *m_relay; + DigitalInput *m_forward; + DigitalInput *m_reverse; + + + virtual void SetUp() { + m_relay = new Relay(TestBench::kRelayChannel); + m_forward = new DigitalInput(TestBench::kFakeRelayForward); + m_reverse = new DigitalInput(TestBench::kFakeRelayReverse); + } + + virtual void TearDown() { + delete m_relay; + delete m_forward; + delete m_reverse; + } + + virtual void Reset() { + m_relay->Set(Relay::kOff); + } +}; + /** + * Test the relay by setting it forward, reverse, off, and on. + */ + TEST_F(RelayTest, Relay) { + Reset(); + + //set the relay to forward + m_relay->Set(Relay::kForward); + EXPECT_TRUE(m_forward->Get()) + <<"Relay did not set forward"; + EXPECT_FALSE(m_reverse->Get()) + <<"Relay did not set forward"; + + + //set the relay to reverse + m_relay->Set(Relay::kReverse); + EXPECT_TRUE(m_reverse->Get()) + <<"Relay did not set reverse"; + EXPECT_FALSE(m_forward->Get()) + <<"Relay did not set reverse"; + + //set the relay to off + m_relay->Set(Relay::kOff); + EXPECT_FALSE(m_forward->Get()) + <<"Relay did not set off"; + EXPECT_FALSE(m_reverse->Get()) + <<"Relay did not set off"; + + + //set the relay to on + m_relay->Set(Relay::kOn); + EXPECT_TRUE(m_forward->Get()) + <<"Relay did not set on"; + EXPECT_TRUE(m_reverse->Get()) + <<"Relay did not set on"; + } diff --git a/wpilibj/wpilibJavaIntegrationTests/src/main/java/edu/wpi/first/wpilibj/PCMTest.java b/wpilibj/wpilibJavaIntegrationTests/src/main/java/edu/wpi/first/wpilibj/PCMTest.java new file mode 100644 index 0000000000..ded166d98b --- /dev/null +++ b/wpilibj/wpilibJavaIntegrationTests/src/main/java/edu/wpi/first/wpilibj/PCMTest.java @@ -0,0 +1,150 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) FIRST 2014. All Rights Reserved. */ +/* Open Source Software - may be modified and shared by FRC teams. The code */ +/* must be accompanied by the FIRST BSD license file in the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +package edu.wpi.first.wpilibj; + +import static org.junit.Assert.*; + +import java.util.concurrent.Delayed; +import java.util.logging.Logger; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import edu.wpi.first.wpilibj.test.AbstractComsSetup; + +/** + * @author Kacper Puczydlowski + * + */ + +public class PCMTest extends AbstractComsSetup { + private static final Logger logger = Logger.getLogger(PCMTest.class.getName()); + /* The PCM switches the compressor up to 2 seconds after the pressure switch + changes. */ + protected static final double kCompressorDelayTime = 2.0; + + /* Solenoids should change much more quickly */ + protected static final double kSolenoidDelayTime = 0.1; + + /* The voltage divider on the test bench should bring the compressor output + to around these values. */ + protected static final double kCompressorOnVoltage = 5.00; + protected static final double kCompressorOffVoltage = 1.68; + + private static Compressor compressor; + + private static DigitalOutput fakePressureSwitch; + private static AnalogInput fakeCompressor; + + private static Solenoid solenoid1, solenoid2; + private static DigitalInput fakeSolenoid1, fakeSolenoid2; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + compressor = new Compressor(); + + fakePressureSwitch = new DigitalOutput(11); + fakeCompressor = new AnalogInput(1); + + solenoid1 = new Solenoid(1); + solenoid2 = new Solenoid(2); + + fakeSolenoid1 = new DigitalInput(12); + fakeSolenoid2 = new DigitalInput(13); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + compressor.free(); + + fakePressureSwitch.free(); + fakeCompressor.free(); + + solenoid1.free(); + solenoid2.free(); + + fakeSolenoid1.free(); + fakeSolenoid2.free(); + } + + @Before + public void setUp() throws Exception { + } + + @Before + public void reset() throws Exception { + compressor.stop(); + fakePressureSwitch.set(false); + solenoid1.set(false); + solenoid2.set(false); + } + + @After + public void tearDown() throws Exception { + } + + /** + * Test if the compressor turns on and off when the pressure switch is toggled + */ + @Test + public void testPressureSwitch() throws Exception { + double range = 0.1; + reset(); + compressor.setClosedLoopControl(true); + + // Turn on the compressor + fakePressureSwitch.set(true); + Timer.delay(kCompressorDelayTime); + assertEquals("Compressor did not turn on when the pressure switch turned on.", + kCompressorOnVoltage, fakeCompressor.getVoltage(), range); + } + + /** + * Test if the correct solenoids turn on and off when they should + */ + @Test + public void testSolenoid() throws Exception { + reset(); + + solenoid1.set(false); + solenoid2.set(false); + + Timer.delay(kSolenoidDelayTime); + + assertTrue("Solenoid did not turn off.",solenoid1.get()); + assertTrue("Solenoid did not turn off.",solenoid2.get()); + + // Turn Solenoid #1 on, and turn Solenoid #2 off + solenoid1.set(true); + solenoid2.set(false); + Timer.delay(kSolenoidDelayTime); + assertFalse("Solenoid #1 did not turn on",fakeSolenoid1.get()); + assertTrue("Solenoid #2 did not turn off",fakeSolenoid2.get()); + + // Turn Solenoid #1 off, and turn Solenoid #2 on + solenoid1.set(false); + solenoid2.set(true); + Timer.delay(kSolenoidDelayTime); + assertTrue("Solenoid #1 did not turn off",fakeSolenoid1.get()); + assertFalse("Solenoid #2 did not turn on",fakeSolenoid2.get()); + + // Turn both Solenoids on + solenoid1.set(true); + solenoid2.set(true); + Timer.delay(kSolenoidDelayTime); + assertFalse("Solenoid #1 did not turn on",fakeSolenoid1.get()); + assertFalse("Solenoid #2 did not turn on",fakeSolenoid2.get()); + } + + protected Logger getClassLogger(){ + return logger; + } +} diff --git a/wpilibj/wpilibJavaIntegrationTests/src/main/java/edu/wpi/first/wpilibj/WpiLibJTestSuite.java b/wpilibj/wpilibJavaIntegrationTests/src/main/java/edu/wpi/first/wpilibj/WpiLibJTestSuite.java index cd8754f1d5..a9105bf390 100644 --- a/wpilibj/wpilibJavaIntegrationTests/src/main/java/edu/wpi/first/wpilibj/WpiLibJTestSuite.java +++ b/wpilibj/wpilibJavaIntegrationTests/src/main/java/edu/wpi/first/wpilibj/WpiLibJTestSuite.java @@ -22,6 +22,7 @@ import org.junit.runners.Suite.SuiteClasses; EncoderTest.class, MotorEncoderTest.class, PIDTest.class, + PCMTest.class, PrefrencesTest.class, RelayCrossConnectTest.class, SampleTest.class, diff --git a/wpilibj/wpilibJavaIntegrationTests/src/main/java/edu/wpi/first/wpilibj/test/TestSuite.java b/wpilibj/wpilibJavaIntegrationTests/src/main/java/edu/wpi/first/wpilibj/test/TestSuite.java index 64872d612a..896d177789 100644 --- a/wpilibj/wpilibJavaIntegrationTests/src/main/java/edu/wpi/first/wpilibj/test/TestSuite.java +++ b/wpilibj/wpilibJavaIntegrationTests/src/main/java/edu/wpi/first/wpilibj/test/TestSuite.java @@ -21,6 +21,7 @@ import edu.wpi.first.wpilibj.CounterTest; import edu.wpi.first.wpilibj.DIOCrossConnectTest; import edu.wpi.first.wpilibj.EncoderTest; import edu.wpi.first.wpilibj.MotorEncoderTest; +import edu.wpi.first.wpilibj.PCMTest; import edu.wpi.first.wpilibj.PIDTest; import edu.wpi.first.wpilibj.PrefrencesTest; import edu.wpi.first.wpilibj.RelayCrossConnectTest; @@ -46,7 +47,7 @@ import edu.wpi.first.wpilibj.smartdashboard.SmartDashboardTestSuite; * suite classes annotation. */ @RunWith(Suite.class) -@SuiteClasses({ +@SuiteClasses({ WpiLibJTestSuite.class, CommandTestSuite.class, SmartDashboardTestSuite.class @@ -67,7 +68,7 @@ public class TestSuite { Logger.getAnonymousLogger().severe("Could not load default logging.properties file"); Logger.getAnonymousLogger().severe(e.getMessage()); } - + System.out.println("Starting Tests"); } private static final Logger WPILIBJ_ROOT_LOGGER = Logger.getLogger("edu.wpi.first.wpilibj");