Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/ |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame^] | 2 | /* Copyright (c) 2016-2020 FIRST. All Rights Reserved. */ |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [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 "frc/AnalogPotentiometer.h" |
| 9 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame^] | 10 | #include "frc/Base.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 11 | #include "frc/RobotController.h" |
| 12 | #include "frc/smartdashboard/SendableBuilder.h" |
| 13 | #include "frc/smartdashboard/SendableRegistry.h" |
| 14 | |
| 15 | using namespace frc; |
| 16 | |
| 17 | AnalogPotentiometer::AnalogPotentiometer(int channel, double fullRange, |
| 18 | double offset) |
| 19 | : AnalogPotentiometer(std::make_shared<AnalogInput>(channel), fullRange, |
| 20 | offset) { |
| 21 | SendableRegistry::GetInstance().AddChild(this, m_analog_input.get()); |
| 22 | } |
| 23 | |
| 24 | AnalogPotentiometer::AnalogPotentiometer(AnalogInput* input, double fullRange, |
| 25 | double offset) |
| 26 | : AnalogPotentiometer( |
| 27 | std::shared_ptr<AnalogInput>(input, NullDeleter<AnalogInput>()), |
| 28 | fullRange, offset) {} |
| 29 | |
| 30 | AnalogPotentiometer::AnalogPotentiometer(std::shared_ptr<AnalogInput> input, |
| 31 | double fullRange, double offset) |
| 32 | : m_analog_input(input), m_fullRange(fullRange), m_offset(offset) { |
| 33 | SendableRegistry::GetInstance().AddLW(this, "AnalogPotentiometer", |
| 34 | m_analog_input->GetChannel()); |
| 35 | } |
| 36 | |
| 37 | double AnalogPotentiometer::Get() const { |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame^] | 38 | return (m_analog_input->GetAverageVoltage() / |
| 39 | RobotController::GetVoltage5V()) * |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 40 | m_fullRange + |
| 41 | m_offset; |
| 42 | } |
| 43 | |
| 44 | double AnalogPotentiometer::PIDGet() { return Get(); } |
| 45 | |
| 46 | void AnalogPotentiometer::InitSendable(SendableBuilder& builder) { |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame^] | 47 | builder.SetSmartDashboardType("Analog Input"); |
| 48 | builder.AddDoubleProperty( |
| 49 | "Value", [=]() { return Get(); }, nullptr); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 50 | } |