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