Brian Silverman | f7f267a | 2017-02-04 16:16:08 -0800 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) FIRST 2016-2017. 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 "AnalogPotentiometer.h" |
| 9 | |
| 10 | #include "ControllerPower.h" |
| 11 | |
| 12 | using namespace frc; |
| 13 | |
| 14 | /** |
| 15 | * Construct an Analog Potentiometer object from a channel number. |
| 16 | * |
| 17 | * @param channel The channel number on the roboRIO to represent. 0-3 are |
| 18 | * on-board 4-7 are on the MXP port. |
| 19 | * @param fullRange The angular value (in desired units) representing the full |
| 20 | * 0-5V range of the input. |
| 21 | * @param offset The angular value (in desired units) representing the |
| 22 | * angular output at 0V. |
| 23 | */ |
| 24 | AnalogPotentiometer::AnalogPotentiometer(int channel, double fullRange, |
| 25 | double offset) |
| 26 | : m_analog_input(std::make_unique<AnalogInput>(channel)), |
| 27 | m_fullRange(fullRange), |
| 28 | m_offset(offset) {} |
| 29 | |
| 30 | /** |
| 31 | * Construct an Analog Potentiometer object from an existing Analog Input |
| 32 | * pointer. |
| 33 | * |
| 34 | * @param channel The existing Analog Input pointer |
| 35 | * @param fullRange The angular value (in desired units) representing the full |
| 36 | * 0-5V range of the input. |
| 37 | * @param offset The angular value (in desired units) representing the |
| 38 | * angular output at 0V. |
| 39 | */ |
| 40 | AnalogPotentiometer::AnalogPotentiometer(AnalogInput* input, double fullRange, |
| 41 | double offset) |
| 42 | : m_analog_input(input, NullDeleter<AnalogInput>()), |
| 43 | m_fullRange(fullRange), |
| 44 | m_offset(offset) {} |
| 45 | |
| 46 | /** |
| 47 | * Construct an Analog Potentiometer object from an existing Analog Input |
| 48 | * pointer. |
| 49 | * |
| 50 | * @param channel The existing Analog Input pointer |
| 51 | * @param fullRange The angular value (in desired units) representing the full |
| 52 | * 0-5V range of the input. |
| 53 | * @param offset The angular value (in desired units) representing the |
| 54 | * angular output at 0V. |
| 55 | */ |
| 56 | AnalogPotentiometer::AnalogPotentiometer(std::shared_ptr<AnalogInput> input, |
| 57 | double fullRange, double offset) |
| 58 | : m_analog_input(input), m_fullRange(fullRange), m_offset(offset) {} |
| 59 | |
| 60 | /** |
| 61 | * Get the current reading of the potentiometer. |
| 62 | * |
| 63 | * @return The current position of the potentiometer (in the units used for |
| 64 | * fullRange and offset). |
| 65 | */ |
| 66 | double AnalogPotentiometer::Get() const { |
| 67 | return (m_analog_input->GetVoltage() / ControllerPower::GetVoltage5V()) * |
| 68 | m_fullRange + |
| 69 | m_offset; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Implement the PIDSource interface. |
| 74 | * |
| 75 | * @return The current reading. |
| 76 | */ |
| 77 | double AnalogPotentiometer::PIDGet() { return Get(); } |
| 78 | |
| 79 | /** |
| 80 | * @return the Smart Dashboard Type |
| 81 | */ |
| 82 | std::string AnalogPotentiometer::GetSmartDashboardType() const { |
| 83 | return "Analog Input"; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Live Window code, only does anything if live window is activated. |
| 88 | */ |
| 89 | void AnalogPotentiometer::InitTable(std::shared_ptr<ITable> subtable) { |
| 90 | m_table = subtable; |
| 91 | UpdateTable(); |
| 92 | } |
| 93 | |
| 94 | void AnalogPotentiometer::UpdateTable() { |
| 95 | if (m_table != nullptr) { |
| 96 | m_table->PutNumber("Value", Get()); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | std::shared_ptr<ITable> AnalogPotentiometer::GetTable() const { |
| 101 | return m_table; |
| 102 | } |