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. |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 4 | |
| 5 | #include "frc/AnalogPotentiometer.h" |
| 6 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 7 | #include <utility> |
| 8 | |
| 9 | #include <wpi/NullDeleter.h> |
| 10 | #include <wpi/sendable/SendableBuilder.h> |
| 11 | #include <wpi/sendable/SendableRegistry.h> |
| 12 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 13 | #include "frc/RobotController.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 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) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 21 | wpi::SendableRegistry::AddChild(this, m_analog_input.get()); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | AnalogPotentiometer::AnalogPotentiometer(AnalogInput* input, double fullRange, |
| 25 | double offset) |
| 26 | : AnalogPotentiometer( |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 27 | std::shared_ptr<AnalogInput>(input, wpi::NullDeleter<AnalogInput>()), |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 28 | fullRange, offset) {} |
| 29 | |
| 30 | AnalogPotentiometer::AnalogPotentiometer(std::shared_ptr<AnalogInput> input, |
| 31 | double fullRange, double offset) |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 32 | : m_analog_input(std::move(input)), |
| 33 | m_fullRange(fullRange), |
| 34 | m_offset(offset) { |
| 35 | wpi::SendableRegistry::AddLW(this, "AnalogPotentiometer", |
| 36 | m_analog_input->GetChannel()); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | double AnalogPotentiometer::Get() const { |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 40 | return (m_analog_input->GetAverageVoltage() / |
| 41 | RobotController::GetVoltage5V()) * |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 42 | m_fullRange + |
| 43 | m_offset; |
| 44 | } |
| 45 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 46 | void AnalogPotentiometer::InitSendable(wpi::SendableBuilder& builder) { |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 47 | builder.SetSmartDashboardType("Analog Input"); |
| 48 | builder.AddDoubleProperty( |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 49 | "Value", [=] { return Get(); }, nullptr); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 50 | } |