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/AnalogEncoder.h" |
| 6 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 7 | #include <wpi/NullDeleter.h> |
| 8 | #include <wpi/sendable/SendableBuilder.h> |
| 9 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 10 | #include "frc/AnalogInput.h" |
| 11 | #include "frc/Counter.h" |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 12 | #include "frc/Errors.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 13 | |
| 14 | using namespace frc; |
| 15 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 16 | AnalogEncoder::AnalogEncoder(int channel) |
| 17 | : AnalogEncoder(std::make_shared<AnalogInput>(channel)) {} |
| 18 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 19 | AnalogEncoder::AnalogEncoder(AnalogInput& analogInput) |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 20 | : m_analogInput{&analogInput, wpi::NullDeleter<AnalogInput>{}}, |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 21 | m_analogTrigger{m_analogInput.get()}, |
| 22 | m_counter{} { |
| 23 | Init(); |
| 24 | } |
| 25 | |
| 26 | AnalogEncoder::AnalogEncoder(AnalogInput* analogInput) |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 27 | : m_analogInput{analogInput, wpi::NullDeleter<AnalogInput>{}}, |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 28 | m_analogTrigger{m_analogInput.get()}, |
| 29 | m_counter{} { |
| 30 | Init(); |
| 31 | } |
| 32 | |
| 33 | AnalogEncoder::AnalogEncoder(std::shared_ptr<AnalogInput> analogInput) |
| 34 | : m_analogInput{std::move(analogInput)}, |
| 35 | m_analogTrigger{m_analogInput.get()}, |
| 36 | m_counter{} { |
| 37 | Init(); |
| 38 | } |
| 39 | |
| 40 | void AnalogEncoder::Init() { |
| 41 | m_simDevice = hal::SimDevice{"AnalogEncoder", m_analogInput->GetChannel()}; |
| 42 | |
| 43 | if (m_simDevice) { |
| 44 | m_simPosition = m_simDevice.CreateDouble("Position", false, 0.0); |
| 45 | } |
| 46 | |
| 47 | m_analogTrigger.SetLimitsVoltage(1.25, 3.75); |
| 48 | m_counter.SetUpSource( |
| 49 | m_analogTrigger.CreateOutput(AnalogTriggerType::kRisingPulse)); |
| 50 | m_counter.SetDownSource( |
| 51 | m_analogTrigger.CreateOutput(AnalogTriggerType::kFallingPulse)); |
| 52 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 53 | wpi::SendableRegistry::AddLW(this, "DutyCycle Encoder", |
| 54 | m_analogInput->GetChannel()); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | units::turn_t AnalogEncoder::Get() const { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 58 | if (m_simPosition) { |
| 59 | return units::turn_t{m_simPosition.Get()}; |
| 60 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 61 | |
| 62 | // As the values are not atomic, keep trying until we get 2 reads of the same |
| 63 | // value If we don't within 10 attempts, error |
| 64 | for (int i = 0; i < 10; i++) { |
| 65 | auto counter = m_counter.Get(); |
| 66 | auto pos = m_analogInput->GetVoltage(); |
| 67 | auto counter2 = m_counter.Get(); |
| 68 | auto pos2 = m_analogInput->GetVoltage(); |
| 69 | if (counter == counter2 && pos == pos2) { |
| 70 | units::turn_t turns{counter + pos - m_positionOffset}; |
| 71 | m_lastPosition = turns; |
| 72 | return turns; |
| 73 | } |
| 74 | } |
| 75 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 76 | FRC_ReportError( |
| 77 | warn::Warning, "{}", |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 78 | "Failed to read Analog Encoder. Potential Speed Overrun. Returning last " |
| 79 | "value"); |
| 80 | return m_lastPosition; |
| 81 | } |
| 82 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 83 | double AnalogEncoder::GetPositionOffset() const { |
| 84 | return m_positionOffset; |
| 85 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 86 | |
| 87 | void AnalogEncoder::SetDistancePerRotation(double distancePerRotation) { |
| 88 | m_distancePerRotation = distancePerRotation; |
| 89 | } |
| 90 | |
| 91 | double AnalogEncoder::GetDistancePerRotation() const { |
| 92 | return m_distancePerRotation; |
| 93 | } |
| 94 | |
| 95 | double AnalogEncoder::GetDistance() const { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 96 | return Get().value() * GetDistancePerRotation(); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | void AnalogEncoder::Reset() { |
| 100 | m_counter.Reset(); |
| 101 | m_positionOffset = m_analogInput->GetVoltage(); |
| 102 | } |
| 103 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 104 | int AnalogEncoder::GetChannel() const { |
| 105 | return m_analogInput->GetChannel(); |
| 106 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 107 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 108 | void AnalogEncoder::InitSendable(wpi::SendableBuilder& builder) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 109 | builder.SetSmartDashboardType("AbsoluteEncoder"); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 110 | builder.AddDoubleProperty( |
| 111 | "Distance", [this] { return this->GetDistance(); }, nullptr); |
| 112 | builder.AddDoubleProperty( |
| 113 | "Distance Per Rotation", |
| 114 | [this] { return this->GetDistancePerRotation(); }, nullptr); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 115 | } |