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