Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) 2014-2018 FIRST. 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 "frc/AnalogPotentiometer.h" // NOLINT(build/include_order) |
| 9 | |
| 10 | #include "TestBench.h" |
| 11 | #include "frc/AnalogOutput.h" |
| 12 | #include "frc/RobotController.h" |
| 13 | #include "frc/Timer.h" |
| 14 | #include "gtest/gtest.h" |
| 15 | |
| 16 | using namespace frc; |
| 17 | |
| 18 | static const double kScale = 270.0; |
| 19 | static const double kAngle = 180.0; |
| 20 | |
| 21 | class AnalogPotentiometerTest : public testing::Test { |
| 22 | protected: |
| 23 | AnalogOutput* m_fakePot; |
| 24 | AnalogPotentiometer* m_pot; |
| 25 | |
| 26 | void SetUp() override { |
| 27 | m_fakePot = new AnalogOutput(TestBench::kAnalogOutputChannel); |
| 28 | m_pot = |
| 29 | new AnalogPotentiometer(TestBench::kFakeAnalogOutputChannel, kScale); |
| 30 | } |
| 31 | |
| 32 | void TearDown() override { |
| 33 | delete m_fakePot; |
| 34 | delete m_pot; |
| 35 | } |
| 36 | }; |
| 37 | |
| 38 | TEST_F(AnalogPotentiometerTest, TestInitialSettings) { |
| 39 | m_fakePot->SetVoltage(0.0); |
| 40 | Wait(0.1); |
| 41 | EXPECT_NEAR(0.0, m_pot->Get(), 5.0) |
| 42 | << "The potentiometer did not initialize to 0."; |
| 43 | } |
| 44 | |
| 45 | TEST_F(AnalogPotentiometerTest, TestRangeValues) { |
| 46 | m_fakePot->SetVoltage(kAngle / kScale * RobotController::GetVoltage5V()); |
| 47 | Wait(0.1); |
| 48 | EXPECT_NEAR(kAngle, m_pot->Get(), 2.0) |
| 49 | << "The potentiometer did not measure the correct angle."; |
| 50 | } |