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