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/Encoder.h" |
| 6 | |
| 7 | #include <utility> |
| 8 | |
| 9 | #include <hal/Encoder.h> |
| 10 | #include <hal/FRCUsageReporting.h> |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 11 | #include <wpi/NullDeleter.h> |
| 12 | #include <wpi/sendable/SendableBuilder.h> |
| 13 | #include <wpi/sendable/SendableRegistry.h> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 14 | |
| 15 | #include "frc/DigitalInput.h" |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 16 | #include "frc/Errors.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 17 | |
| 18 | using namespace frc; |
| 19 | |
| 20 | Encoder::Encoder(int aChannel, int bChannel, bool reverseDirection, |
| 21 | EncodingType encodingType) { |
| 22 | m_aSource = std::make_shared<DigitalInput>(aChannel); |
| 23 | m_bSource = std::make_shared<DigitalInput>(bChannel); |
| 24 | InitEncoder(reverseDirection, encodingType); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 25 | wpi::SendableRegistry::AddChild(this, m_aSource.get()); |
| 26 | wpi::SendableRegistry::AddChild(this, m_bSource.get()); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | Encoder::Encoder(DigitalSource* aSource, DigitalSource* bSource, |
| 30 | bool reverseDirection, EncodingType encodingType) |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 31 | : m_aSource(aSource, wpi::NullDeleter<DigitalSource>()), |
| 32 | m_bSource(bSource, wpi::NullDeleter<DigitalSource>()) { |
| 33 | if (!m_aSource) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 34 | throw FRC_MakeError(err::NullParameter, "aSource"); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 35 | } |
| 36 | if (!m_bSource) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 37 | throw FRC_MakeError(err::NullParameter, "bSource"); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 38 | } |
| 39 | InitEncoder(reverseDirection, encodingType); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | Encoder::Encoder(DigitalSource& aSource, DigitalSource& bSource, |
| 43 | bool reverseDirection, EncodingType encodingType) |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 44 | : m_aSource(&aSource, wpi::NullDeleter<DigitalSource>()), |
| 45 | m_bSource(&bSource, wpi::NullDeleter<DigitalSource>()) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 46 | InitEncoder(reverseDirection, encodingType); |
| 47 | } |
| 48 | |
| 49 | Encoder::Encoder(std::shared_ptr<DigitalSource> aSource, |
| 50 | std::shared_ptr<DigitalSource> bSource, bool reverseDirection, |
| 51 | EncodingType encodingType) |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 52 | : m_aSource(std::move(aSource)), m_bSource(std::move(bSource)) { |
| 53 | if (!m_aSource) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 54 | throw FRC_MakeError(err::NullParameter, "aSource"); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 55 | } |
| 56 | if (!m_bSource) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 57 | throw FRC_MakeError(err::NullParameter, "bSource"); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 58 | } |
| 59 | InitEncoder(reverseDirection, encodingType); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | Encoder::~Encoder() { |
| 63 | int32_t status = 0; |
| 64 | HAL_FreeEncoder(m_encoder, &status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 65 | FRC_ReportError(status, "FreeEncoder"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | int Encoder::Get() const { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 69 | int32_t status = 0; |
| 70 | int value = HAL_GetEncoder(m_encoder, &status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 71 | FRC_CheckErrorStatus(status, "Get"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 72 | return value; |
| 73 | } |
| 74 | |
| 75 | void Encoder::Reset() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 76 | int32_t status = 0; |
| 77 | HAL_ResetEncoder(m_encoder, &status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 78 | FRC_CheckErrorStatus(status, "Reset"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 81 | units::second_t Encoder::GetPeriod() const { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 82 | int32_t status = 0; |
| 83 | double value = HAL_GetEncoderPeriod(m_encoder, &status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 84 | FRC_CheckErrorStatus(status, "GetPeriod"); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 85 | return units::second_t{value}; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 88 | void Encoder::SetMaxPeriod(units::second_t maxPeriod) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 89 | int32_t status = 0; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 90 | HAL_SetEncoderMaxPeriod(m_encoder, maxPeriod.value(), &status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 91 | FRC_CheckErrorStatus(status, "SetMaxPeriod"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | bool Encoder::GetStopped() const { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 95 | int32_t status = 0; |
| 96 | bool value = HAL_GetEncoderStopped(m_encoder, &status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 97 | FRC_CheckErrorStatus(status, "GetStopped"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 98 | return value; |
| 99 | } |
| 100 | |
| 101 | bool Encoder::GetDirection() const { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 102 | int32_t status = 0; |
| 103 | bool value = HAL_GetEncoderDirection(m_encoder, &status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 104 | FRC_CheckErrorStatus(status, "GetDirection"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 105 | return value; |
| 106 | } |
| 107 | |
| 108 | int Encoder::GetRaw() const { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 109 | int32_t status = 0; |
| 110 | int value = HAL_GetEncoderRaw(m_encoder, &status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 111 | FRC_CheckErrorStatus(status, "GetRaw"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 112 | return value; |
| 113 | } |
| 114 | |
| 115 | int Encoder::GetEncodingScale() const { |
| 116 | int32_t status = 0; |
| 117 | int val = HAL_GetEncoderEncodingScale(m_encoder, &status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 118 | FRC_CheckErrorStatus(status, "GetEncodingScale"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 119 | return val; |
| 120 | } |
| 121 | |
| 122 | double Encoder::GetDistance() const { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 123 | int32_t status = 0; |
| 124 | double value = HAL_GetEncoderDistance(m_encoder, &status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 125 | FRC_CheckErrorStatus(status, "GetDistance"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 126 | return value; |
| 127 | } |
| 128 | |
| 129 | double Encoder::GetRate() const { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 130 | int32_t status = 0; |
| 131 | double value = HAL_GetEncoderRate(m_encoder, &status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 132 | FRC_CheckErrorStatus(status, "GetRate"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 133 | return value; |
| 134 | } |
| 135 | |
| 136 | void Encoder::SetMinRate(double minRate) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 137 | int32_t status = 0; |
| 138 | HAL_SetEncoderMinRate(m_encoder, minRate, &status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 139 | FRC_CheckErrorStatus(status, "SetMinRate"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | void Encoder::SetDistancePerPulse(double distancePerPulse) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 143 | int32_t status = 0; |
| 144 | HAL_SetEncoderDistancePerPulse(m_encoder, distancePerPulse, &status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 145 | FRC_CheckErrorStatus(status, "SetDistancePerPulse"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | double Encoder::GetDistancePerPulse() const { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 149 | int32_t status = 0; |
| 150 | double distancePerPulse = HAL_GetEncoderDistancePerPulse(m_encoder, &status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 151 | FRC_CheckErrorStatus(status, "GetDistancePerPulse"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 152 | return distancePerPulse; |
| 153 | } |
| 154 | |
| 155 | void Encoder::SetReverseDirection(bool reverseDirection) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 156 | int32_t status = 0; |
| 157 | HAL_SetEncoderReverseDirection(m_encoder, reverseDirection, &status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 158 | FRC_CheckErrorStatus(status, "SetReverseDirection"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | void Encoder::SetSamplesToAverage(int samplesToAverage) { |
| 162 | if (samplesToAverage < 1 || samplesToAverage > 127) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 163 | throw FRC_MakeError( |
| 164 | err::ParameterOutOfRange, |
| 165 | "Average counter values must be between 1 and 127, got {}", |
| 166 | samplesToAverage); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 167 | } |
| 168 | int32_t status = 0; |
| 169 | HAL_SetEncoderSamplesToAverage(m_encoder, samplesToAverage, &status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 170 | FRC_CheckErrorStatus(status, "SetSamplesToAverage"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | int Encoder::GetSamplesToAverage() const { |
| 174 | int32_t status = 0; |
| 175 | int result = HAL_GetEncoderSamplesToAverage(m_encoder, &status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 176 | FRC_CheckErrorStatus(status, "GetSamplesToAverage"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 177 | return result; |
| 178 | } |
| 179 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 180 | void Encoder::SetIndexSource(int channel, Encoder::IndexingType type) { |
| 181 | // Force digital input if just given an index |
| 182 | m_indexSource = std::make_shared<DigitalInput>(channel); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 183 | wpi::SendableRegistry::AddChild(this, m_indexSource.get()); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 184 | SetIndexSource(*m_indexSource.get(), type); |
| 185 | } |
| 186 | |
| 187 | void Encoder::SetIndexSource(const DigitalSource& source, |
| 188 | Encoder::IndexingType type) { |
| 189 | int32_t status = 0; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 190 | HAL_SetEncoderIndexSource(m_encoder, source.GetPortHandleForRouting(), |
| 191 | static_cast<HAL_AnalogTriggerType>( |
| 192 | source.GetAnalogTriggerTypeForRouting()), |
| 193 | static_cast<HAL_EncoderIndexingType>(type), |
| 194 | &status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 195 | FRC_CheckErrorStatus(status, "SetIndexSource"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | void Encoder::SetSimDevice(HAL_SimDeviceHandle device) { |
| 199 | HAL_SetEncoderSimDevice(m_encoder, device); |
| 200 | } |
| 201 | |
| 202 | int Encoder::GetFPGAIndex() const { |
| 203 | int32_t status = 0; |
| 204 | int val = HAL_GetEncoderFPGAIndex(m_encoder, &status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 205 | FRC_CheckErrorStatus(status, "GetFPGAIndex"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 206 | return val; |
| 207 | } |
| 208 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 209 | void Encoder::InitSendable(wpi::SendableBuilder& builder) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 210 | int32_t status = 0; |
| 211 | HAL_EncoderEncodingType type = HAL_GetEncoderEncodingType(m_encoder, &status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 212 | FRC_CheckErrorStatus(status, "GetEncodingType"); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 213 | if (type == HAL_EncoderEncodingType::HAL_Encoder_k4X) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 214 | builder.SetSmartDashboardType("Quadrature Encoder"); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 215 | } else { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 216 | builder.SetSmartDashboardType("Encoder"); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 217 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 218 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 219 | builder.AddDoubleProperty( |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 220 | "Speed", [=, this] { return GetRate(); }, nullptr); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 221 | builder.AddDoubleProperty( |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 222 | "Distance", [=, this] { return GetDistance(); }, nullptr); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 223 | builder.AddDoubleProperty( |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 224 | "Distance per Tick", [=, this] { return GetDistancePerPulse(); }, |
| 225 | nullptr); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | void Encoder::InitEncoder(bool reverseDirection, EncodingType encodingType) { |
| 229 | int32_t status = 0; |
| 230 | m_encoder = HAL_InitializeEncoder( |
| 231 | m_aSource->GetPortHandleForRouting(), |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 232 | static_cast<HAL_AnalogTriggerType>( |
| 233 | m_aSource->GetAnalogTriggerTypeForRouting()), |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 234 | m_bSource->GetPortHandleForRouting(), |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 235 | static_cast<HAL_AnalogTriggerType>( |
| 236 | m_bSource->GetAnalogTriggerTypeForRouting()), |
| 237 | reverseDirection, static_cast<HAL_EncoderEncodingType>(encodingType), |
| 238 | &status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 239 | FRC_CheckErrorStatus(status, "InitEncoder"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 240 | |
| 241 | HAL_Report(HALUsageReporting::kResourceType_Encoder, GetFPGAIndex() + 1, |
| 242 | encodingType); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 243 | wpi::SendableRegistry::AddLW(this, "Encoder", m_aSource->GetChannel()); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | double Encoder::DecodingScaleFactor() const { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 247 | int32_t status = 0; |
| 248 | double val = HAL_GetEncoderDecodingScaleFactor(m_encoder, &status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 249 | FRC_CheckErrorStatus(status, "DecodingScaleFactor"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 250 | return val; |
| 251 | } |