Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 1 | // 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. |
| 4 | |
| 5 | #include <hal/HAL.h> |
| 6 | |
| 7 | #include "frc/AnalogPotentiometer.h" |
| 8 | #include "frc/simulation/AnalogInputSim.h" |
| 9 | #include "frc/simulation/RoboRioSim.h" |
| 10 | #include "gtest/gtest.h" |
| 11 | |
| 12 | namespace frc { |
| 13 | using namespace frc::sim; |
| 14 | TEST(AnalogPotentiometerTest, InitializeWithAnalogInput) { |
| 15 | HAL_Initialize(500, 0); |
| 16 | |
| 17 | AnalogInput ai{0}; |
| 18 | AnalogPotentiometer pot{&ai}; |
| 19 | AnalogInputSim sim{ai}; |
| 20 | |
| 21 | RoboRioSim::ResetData(); |
| 22 | sim.SetVoltage(4.0); |
| 23 | EXPECT_EQ(0.8, pot.Get()); |
| 24 | } |
| 25 | |
| 26 | TEST(AnalogPotentiometerTest, InitializeWithAnalogInputAndScale) { |
| 27 | HAL_Initialize(500, 0); |
| 28 | |
| 29 | AnalogInput ai{0}; |
| 30 | AnalogPotentiometer pot{&ai, 270.0}; |
| 31 | RoboRioSim::ResetData(); |
| 32 | AnalogInputSim sim{ai}; |
| 33 | |
| 34 | sim.SetVoltage(5.0); |
| 35 | EXPECT_EQ(270.0, pot.Get()); |
| 36 | |
| 37 | sim.SetVoltage(2.5); |
| 38 | EXPECT_EQ(135, pot.Get()); |
| 39 | |
| 40 | sim.SetVoltage(0.0); |
| 41 | EXPECT_EQ(0.0, pot.Get()); |
| 42 | } |
| 43 | |
| 44 | TEST(AnalogPotentiometerTest, InitializeWithChannel) { |
| 45 | HAL_Initialize(500, 0); |
| 46 | |
| 47 | AnalogPotentiometer pot{1}; |
| 48 | AnalogInputSim sim{1}; |
| 49 | |
| 50 | sim.SetVoltage(5.0); |
| 51 | EXPECT_EQ(1.0, pot.Get()); |
| 52 | } |
| 53 | |
| 54 | TEST(AnalogPotentiometerTest, InitializeWithChannelAndScale) { |
| 55 | HAL_Initialize(500, 0); |
| 56 | |
| 57 | AnalogPotentiometer pot{1, 180.0}; |
| 58 | RoboRioSim::ResetData(); |
| 59 | AnalogInputSim sim{1}; |
| 60 | |
| 61 | sim.SetVoltage(5.0); |
| 62 | EXPECT_EQ(180.0, pot.Get()); |
| 63 | |
| 64 | sim.SetVoltage(0.0); |
| 65 | EXPECT_EQ(0.0, pot.Get()); |
| 66 | } |
| 67 | |
| 68 | TEST(AnalogPotentiometerTest, WithModifiedBatteryVoltage) { |
| 69 | AnalogPotentiometer pot{1, 180.0, 90.0}; |
| 70 | RoboRioSim::ResetData(); |
| 71 | AnalogInputSim sim{1}; |
| 72 | |
| 73 | // Test at 5v |
| 74 | sim.SetVoltage(5.0); |
| 75 | EXPECT_EQ(270, pot.Get()); |
| 76 | |
| 77 | sim.SetVoltage(0.0); |
| 78 | EXPECT_EQ(90, pot.Get()); |
| 79 | |
| 80 | // Simulate a lower battery voltage |
| 81 | RoboRioSim::SetUserVoltage5V(units::volt_t{2.5}); |
| 82 | |
| 83 | sim.SetVoltage(2.5); |
| 84 | EXPECT_EQ(270, pot.Get()); |
| 85 | |
| 86 | sim.SetVoltage(2.0); |
| 87 | EXPECT_EQ(234, pot.Get()); |
| 88 | |
| 89 | sim.SetVoltage(0.0); |
| 90 | EXPECT_EQ(90, pot.Get()); |
| 91 | } |
| 92 | } // namespace frc |