Brian Silverman | f7f267a | 2017-02-04 16:16:08 -0800 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) FIRST 2014-2017. All Rights Reserved. */ |
| 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #include "Encoder.h" |
| 9 | #include "Jaguar.h" |
| 10 | #include "Talon.h" |
| 11 | #include "TestBench.h" |
| 12 | #include "Timer.h" |
| 13 | #include "Victor.h" |
| 14 | #include "gtest/gtest.h" |
| 15 | |
| 16 | using namespace frc; |
| 17 | |
| 18 | enum MotorInvertingTestType { TEST_VICTOR, TEST_JAGUAR, TEST_TALON }; |
| 19 | static const double motorSpeed = 0.15; |
| 20 | static const double delayTime = 0.5; |
| 21 | std::ostream& operator<<(std::ostream& os, MotorInvertingTestType const& type) { |
| 22 | switch (type) { |
| 23 | case TEST_VICTOR: |
| 24 | os << "Victor"; |
| 25 | break; |
| 26 | case TEST_JAGUAR: |
| 27 | os << "Jaguar"; |
| 28 | break; |
| 29 | case TEST_TALON: |
| 30 | os << "Talon"; |
| 31 | break; |
| 32 | } |
| 33 | |
| 34 | return os; |
| 35 | } |
| 36 | class MotorInvertingTest |
| 37 | : public testing::TestWithParam<MotorInvertingTestType> { |
| 38 | protected: |
| 39 | SpeedController* m_speedController; |
| 40 | Encoder* m_encoder; |
| 41 | void SetUp() override { |
| 42 | switch (GetParam()) { |
| 43 | case TEST_VICTOR: |
| 44 | m_speedController = new Victor(TestBench::kVictorChannel); |
| 45 | m_encoder = new Encoder(TestBench::kVictorEncoderChannelA, |
| 46 | TestBench::kVictorEncoderChannelB); |
| 47 | break; |
| 48 | |
| 49 | case TEST_JAGUAR: |
| 50 | m_speedController = new Jaguar(TestBench::kJaguarChannel); |
| 51 | m_encoder = new Encoder(TestBench::kJaguarEncoderChannelA, |
| 52 | TestBench::kJaguarEncoderChannelB); |
| 53 | break; |
| 54 | |
| 55 | case TEST_TALON: |
| 56 | m_speedController = new Talon(TestBench::kTalonChannel); |
| 57 | m_encoder = new Encoder(TestBench::kTalonEncoderChannelA, |
| 58 | TestBench::kTalonEncoderChannelB); |
| 59 | break; |
| 60 | } |
| 61 | } |
| 62 | void TearDown() override { |
| 63 | delete m_speedController; |
| 64 | delete m_encoder; |
| 65 | } |
| 66 | |
| 67 | void Reset() { |
| 68 | m_speedController->SetInverted(false); |
| 69 | m_speedController->Set(0.0); |
| 70 | m_encoder->Reset(); |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | TEST_P(MotorInvertingTest, InvertingPositive) { |
| 75 | Reset(); |
| 76 | m_speedController->Set(motorSpeed); |
| 77 | Wait(delayTime); |
| 78 | bool initDirection = m_encoder->GetDirection(); |
| 79 | m_speedController->SetInverted(true); |
| 80 | m_speedController->Set(motorSpeed); |
| 81 | Wait(delayTime); |
| 82 | EXPECT_TRUE(m_encoder->GetDirection() != initDirection) |
| 83 | << "Inverting with Positive value does not change direction"; |
| 84 | Reset(); |
| 85 | } |
| 86 | TEST_P(MotorInvertingTest, InvertingNegative) { |
| 87 | Reset(); |
| 88 | m_speedController->SetInverted(false); |
| 89 | m_speedController->Set(-motorSpeed); |
| 90 | Wait(delayTime); |
| 91 | bool initDirection = m_encoder->GetDirection(); |
| 92 | m_speedController->SetInverted(true); |
| 93 | m_speedController->Set(-motorSpeed); |
| 94 | Wait(delayTime); |
| 95 | EXPECT_TRUE(m_encoder->GetDirection() != initDirection) |
| 96 | << "Inverting with Negative value does not change direction"; |
| 97 | Reset(); |
| 98 | } |
| 99 | TEST_P(MotorInvertingTest, InvertingSwitchingPosToNeg) { |
| 100 | Reset(); |
| 101 | m_speedController->SetInverted(false); |
| 102 | m_speedController->Set(motorSpeed); |
| 103 | Wait(delayTime); |
| 104 | bool initDirection = m_encoder->GetDirection(); |
| 105 | m_speedController->SetInverted(true); |
| 106 | m_speedController->Set(-motorSpeed); |
| 107 | Wait(delayTime); |
| 108 | EXPECT_TRUE(m_encoder->GetDirection() == initDirection) |
| 109 | << "Inverting with Switching value does change direction"; |
| 110 | Reset(); |
| 111 | } |
| 112 | TEST_P(MotorInvertingTest, InvertingSwitchingNegToPos) { |
| 113 | Reset(); |
| 114 | m_speedController->SetInverted(false); |
| 115 | m_speedController->Set(-motorSpeed); |
| 116 | Wait(delayTime); |
| 117 | bool initDirection = m_encoder->GetDirection(); |
| 118 | m_speedController->SetInverted(true); |
| 119 | m_speedController->Set(motorSpeed); |
| 120 | Wait(delayTime); |
| 121 | EXPECT_TRUE(m_encoder->GetDirection() == initDirection) |
| 122 | << "Inverting with Switching value does change direction"; |
| 123 | Reset(); |
| 124 | } |
| 125 | INSTANTIATE_TEST_CASE_P(Test, MotorInvertingTest, |
| 126 | testing::Values(TEST_VICTOR, TEST_JAGUAR, TEST_TALON)); |