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 | #pragma once |
| 9 | |
| 10 | #include <memory> |
| 11 | #include <string> |
| 12 | |
| 13 | #include "AnalogInput.h" |
| 14 | #include "LiveWindow/LiveWindowSendable.h" |
| 15 | #include "interfaces/Potentiometer.h" |
| 16 | |
| 17 | namespace frc { |
| 18 | |
| 19 | /** |
| 20 | * Class for reading analog potentiometers. Analog potentiometers read |
| 21 | * in an analog voltage that corresponds to a position. Usually the |
| 22 | * position is either degrees or meters. However, if no conversion is |
| 23 | * given it remains volts. |
| 24 | */ |
| 25 | class AnalogPotentiometer : public Potentiometer, public LiveWindowSendable { |
| 26 | public: |
| 27 | /** |
| 28 | * AnalogPotentiometer constructor. |
| 29 | * |
| 30 | * Use the scaling and offset values so that the output produces meaningful |
| 31 | * values. I.E: you have a 270 degree potentiometer and you want the output |
| 32 | * to be degrees with the halfway point as 0 degrees. The scale value is |
| 33 | * 270.0(degrees)/5.0(volts) and the offset is -135.0 since the halfway point |
| 34 | * after scaling is 135 degrees. |
| 35 | * |
| 36 | * @param channel The analog channel this potentiometer is plugged into. |
| 37 | * @param scale The scaling to multiply the voltage by to get a meaningful |
| 38 | * unit. |
| 39 | * @param offset The offset to add to the scaled value for controlling the |
| 40 | * zero value |
| 41 | */ |
| 42 | explicit AnalogPotentiometer(int channel, double scale = 1.0, |
| 43 | double offset = 0.0); |
| 44 | |
| 45 | AnalogPotentiometer(AnalogInput* input, double scale = 1.0, |
| 46 | double offset = 0.0); |
| 47 | |
| 48 | AnalogPotentiometer(AnalogInput& input, double scale = 1.0, |
| 49 | double offset = 0.0); |
| 50 | |
| 51 | virtual ~AnalogPotentiometer(); |
| 52 | |
| 53 | /** |
| 54 | * Get the current reading of the potentiomere. |
| 55 | * |
| 56 | * @return The current position of the potentiometer. |
| 57 | */ |
| 58 | virtual double Get() const; |
| 59 | |
| 60 | /** |
| 61 | * Implement the PIDSource interface. |
| 62 | * |
| 63 | * @return The current reading. |
| 64 | */ |
| 65 | double PIDGet() override; |
| 66 | |
| 67 | /* |
| 68 | * Live Window code, only does anything if live window is activated. |
| 69 | */ |
| 70 | std::string GetSmartDashboardType() const override; |
| 71 | void InitTable(std::shared_ptr<ITable> subtable) override; |
| 72 | void UpdateTable() override; |
| 73 | std::shared_ptr<ITable> GetTable() const override; |
| 74 | |
| 75 | /** |
| 76 | * AnalogPotentiometers don't have to do anything special when entering the |
| 77 | * LiveWindow. |
| 78 | */ |
| 79 | void StartLiveWindowMode() override {} |
| 80 | |
| 81 | /** |
| 82 | * AnalogPotentiometers don't have to do anything special when exiting the |
| 83 | * LiveWindow. |
| 84 | */ |
| 85 | void StopLiveWindowMode() override {} |
| 86 | |
| 87 | private: |
| 88 | double m_scale, m_offset; |
| 89 | AnalogInput* m_analog_input; |
| 90 | std::shared_ptr<ITable> m_table; |
| 91 | bool m_init_analog_input; |
| 92 | |
| 93 | /** |
| 94 | * Common initialization code called by all constructors. |
| 95 | */ |
| 96 | void initPot(AnalogInput* input, double scale, double offset); |
| 97 | }; |
| 98 | |
| 99 | } // namespace frc |