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