Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/ |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 2 | /* Copyright (c) FIRST 2008-2016. All Rights Reserved. */ |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #include "Encoder.h" |
| 9 | #include "Resource.h" |
| 10 | #include "WPIErrors.h" |
| 11 | #include "LiveWindow/LiveWindow.h" |
| 12 | |
| 13 | /** |
| 14 | * Common initialization code for Encoders. |
| 15 | * This code allocates resources for Encoders and is common to all constructors. |
| 16 | * |
| 17 | * The counter will start counting immediately. |
| 18 | * |
| 19 | * @param reverseDirection If true, counts down instead of up (this is all relative) |
| 20 | * @param encodingType either k1X, k2X, or k4X to indicate 1X, 2X or 4X decoding. If 4X is |
| 21 | * selected, then an encoder FPGA object is used and the returned counts will be 4x the encoder |
| 22 | * spec'd value since all rising and falling edges are counted. If 1X or 2X are selected then |
| 23 | * a counter object will be used and the returned value will either exactly match the spec'd count |
| 24 | * or be double (2x) the spec'd count. |
| 25 | */ |
| 26 | void Encoder::InitEncoder(int channelA, int channelB, bool reverseDirection, EncodingType encodingType) |
| 27 | { |
| 28 | m_table = nullptr; |
| 29 | this->channelA = channelA; |
| 30 | this->channelB = channelB; |
| 31 | m_encodingType = encodingType; |
| 32 | m_encodingScale = encodingType == k4X ? 4 |
| 33 | : encodingType == k2X ? 2 |
| 34 | : 1; |
| 35 | |
| 36 | int32_t index = 0; |
| 37 | m_distancePerPulse = 1.0; |
| 38 | |
| 39 | LiveWindow::GetInstance()->AddSensor("Encoder", channelA, this); |
| 40 | |
| 41 | if (channelB < channelA) { // Swap ports |
| 42 | int channel = channelB; |
| 43 | channelB = channelA; |
| 44 | channelA = channel; |
| 45 | m_reverseDirection = !reverseDirection; |
| 46 | } else { |
| 47 | m_reverseDirection = reverseDirection; |
| 48 | } |
| 49 | char buffer[50]; |
| 50 | int n = sprintf(buffer, "dio/%d/%d", channelA, channelB); |
| 51 | impl = new SimEncoder(buffer); |
| 52 | impl->Start(); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Encoder constructor. |
| 57 | * Construct a Encoder given a and b channels. |
| 58 | * |
| 59 | * The counter will start counting immediately. |
| 60 | * |
| 61 | * @param aChannel The a channel digital input channel. |
| 62 | * @param bChannel The b channel digital input channel. |
| 63 | * @param reverseDirection represents the orientation of the encoder and inverts the output values |
| 64 | * if necessary so forward represents positive values. |
| 65 | * @param encodingType either k1X, k2X, or k4X to indicate 1X, 2X or 4X decoding. If 4X is |
| 66 | * selected, then an encoder FPGA object is used and the returned counts will be 4x the encoder |
| 67 | * spec'd value since all rising and falling edges are counted. If 1X or 2X are selected then |
| 68 | * a counter object will be used and the returned value will either exactly match the spec'd count |
| 69 | * or be double (2x) the spec'd count. |
| 70 | */ |
| 71 | Encoder::Encoder(uint32_t aChannel, uint32_t bChannel, bool reverseDirection, EncodingType encodingType) |
| 72 | { |
| 73 | InitEncoder(aChannel, bChannel, reverseDirection, encodingType); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Encoder constructor. |
| 78 | * Construct a Encoder given a and b channels as digital inputs. This is used in the case |
| 79 | * where the digital inputs are shared. The Encoder class will not allocate the digital inputs |
| 80 | * and assume that they already are counted. |
| 81 | * |
| 82 | * The counter will start counting immediately. |
| 83 | * |
| 84 | * @param aSource The source that should be used for the a channel. |
| 85 | * @param bSource the source that should be used for the b channel. |
| 86 | * @param reverseDirection represents the orientation of the encoder and inverts the output values |
| 87 | * if necessary so forward represents positive values. |
| 88 | * @param encodingType either k1X, k2X, or k4X to indicate 1X, 2X or 4X decoding. If 4X is |
| 89 | * selected, then an encoder FPGA object is used and the returned counts will be 4x the encoder |
| 90 | * spec'd value since all rising and falling edges are counted. If 1X or 2X are selected then |
| 91 | * a counter object will be used and the returned value will either exactly match the spec'd count |
| 92 | * or be double (2x) the spec'd count. |
| 93 | */ |
| 94 | /* TODO: [Not Supported] Encoder::Encoder(DigitalSource *aSource, DigitalSource *bSource, bool reverseDirection, EncodingType encodingType) : |
| 95 | m_encoder(nullptr), |
| 96 | m_counter(nullptr) |
| 97 | { |
| 98 | m_aSource = aSource; |
| 99 | m_bSource = bSource; |
| 100 | m_allocatedASource = false; |
| 101 | m_allocatedBSource = false; |
| 102 | if (m_aSource == nullptr || m_bSource == nullptr) |
| 103 | wpi_setWPIError(NullParameter); |
| 104 | else |
| 105 | InitEncoder(reverseDirection, encodingType); |
| 106 | }*/ |
| 107 | |
| 108 | /** |
| 109 | * Encoder constructor. |
| 110 | * Construct a Encoder given a and b channels as digital inputs. This is used in the case |
| 111 | * where the digital inputs are shared. The Encoder class will not allocate the digital inputs |
| 112 | * and assume that they already are counted. |
| 113 | * |
| 114 | * The counter will start counting immediately. |
| 115 | * |
| 116 | * @param aSource The source that should be used for the a channel. |
| 117 | * @param bSource the source that should be used for the b channel. |
| 118 | * @param reverseDirection represents the orientation of the encoder and inverts the output values |
| 119 | * if necessary so forward represents positive values. |
| 120 | * @param encodingType either k1X, k2X, or k4X to indicate 1X, 2X or 4X decoding. If 4X is |
| 121 | * selected, then an encoder FPGA object is used and the returned counts will be 4x the encoder |
| 122 | * spec'd value since all rising and falling edges are counted. If 1X or 2X are selected then |
| 123 | * a counter object will be used and the returned value will either exactly match the spec'd count |
| 124 | * or be double (2x) the spec'd count. |
| 125 | */ |
| 126 | /*// TODO: [Not Supported] Encoder::Encoder(DigitalSource &aSource, DigitalSource &bSource, bool reverseDirection, EncodingType encodingType) : |
| 127 | m_encoder(nullptr), |
| 128 | m_counter(nullptr) |
| 129 | { |
| 130 | m_aSource = &aSource; |
| 131 | m_bSource = &bSource; |
| 132 | m_allocatedASource = false; |
| 133 | m_allocatedBSource = false; |
| 134 | InitEncoder(reverseDirection, encodingType); |
| 135 | }*/ |
| 136 | |
| 137 | /** |
| 138 | * Reset the Encoder distance to zero. |
| 139 | * Resets the current count to zero on the encoder. |
| 140 | */ |
| 141 | void Encoder::Reset() |
| 142 | { |
| 143 | impl->Reset(); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Determine if the encoder is stopped. |
| 148 | * Using the MaxPeriod value, a boolean is returned that is true if the encoder is considered |
| 149 | * stopped and false if it is still moving. A stopped encoder is one where the most recent pulse |
| 150 | * width exceeds the MaxPeriod. |
| 151 | * @return True if the encoder is considered stopped. |
| 152 | */ |
| 153 | bool Encoder::GetStopped() const |
| 154 | { |
| 155 | throw "Simulation doesn't currently support this method."; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * The last direction the encoder value changed. |
| 160 | * @return The last direction the encoder value changed. |
| 161 | */ |
| 162 | bool Encoder::GetDirection() const |
| 163 | { |
| 164 | throw "Simulation doesn't currently support this method."; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * The scale needed to convert a raw counter value into a number of encoder pulses. |
| 169 | */ |
| 170 | double Encoder::DecodingScaleFactor() const |
| 171 | { |
| 172 | switch (m_encodingType) |
| 173 | { |
| 174 | case k1X: |
| 175 | return 1.0; |
| 176 | case k2X: |
| 177 | return 0.5; |
| 178 | case k4X: |
| 179 | return 0.25; |
| 180 | default: |
| 181 | return 0.0; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * The encoding scale factor 1x, 2x, or 4x, per the requested encodingType. |
| 187 | * Used to divide raw edge counts down to spec'd counts. |
| 188 | */ |
| 189 | int32_t Encoder::GetEncodingScale() const { return m_encodingScale; } |
| 190 | |
| 191 | /** |
| 192 | * Gets the raw value from the encoder. |
| 193 | * The raw value is the actual count unscaled by the 1x, 2x, or 4x scale |
| 194 | * factor. |
| 195 | * @return Current raw count from the encoder |
| 196 | */ |
| 197 | int32_t Encoder::GetRaw() const |
| 198 | { |
| 199 | throw "Simulation doesn't currently support this method."; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Gets the current count. |
| 204 | * Returns the current count on the Encoder. |
| 205 | * This method compensates for the decoding type. |
| 206 | * |
| 207 | * @return Current count from the Encoder adjusted for the 1x, 2x, or 4x scale factor. |
| 208 | */ |
| 209 | int32_t Encoder::Get() const |
| 210 | { |
| 211 | throw "Simulation doesn't currently support this method."; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Returns the period of the most recent pulse. |
| 216 | * Returns the period of the most recent Encoder pulse in seconds. |
| 217 | * This method compenstates for the decoding type. |
| 218 | * |
| 219 | * @deprecated Use GetRate() in favor of this method. This returns unscaled periods and GetRate() scales using value from SetDistancePerPulse(). |
| 220 | * |
| 221 | * @return Period in seconds of the most recent pulse. |
| 222 | */ |
| 223 | double Encoder::GetPeriod() const |
| 224 | { |
| 225 | throw "Simulation doesn't currently support this method."; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Sets the maximum period for stopped detection. |
| 230 | * Sets the value that represents the maximum period of the Encoder before it will assume |
| 231 | * that the attached device is stopped. This timeout allows users to determine if the wheels or |
| 232 | * other shaft has stopped rotating. |
| 233 | * This method compensates for the decoding type. |
| 234 | * |
| 235 | * @deprecated Use SetMinRate() in favor of this method. This takes unscaled periods and SetMinRate() scales using value from SetDistancePerPulse(). |
| 236 | * |
| 237 | * @param maxPeriod The maximum time between rising and falling edges before the FPGA will |
| 238 | * report the device stopped. This is expressed in seconds. |
| 239 | */ |
| 240 | void Encoder::SetMaxPeriod(double maxPeriod) |
| 241 | { |
| 242 | throw "Simulation doesn't currently support this method."; |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Get the distance the robot has driven since the last reset. |
| 247 | * |
| 248 | * @return The distance driven since the last reset as scaled by the value from SetDistancePerPulse(). |
| 249 | */ |
| 250 | double Encoder::GetDistance() const |
| 251 | { |
| 252 | return m_distancePerPulse * impl->GetPosition(); |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Get the current rate of the encoder. |
| 257 | * Units are distance per second as scaled by the value from SetDistancePerPulse(). |
| 258 | * |
| 259 | * @return The current rate of the encoder. |
| 260 | */ |
| 261 | double Encoder::GetRate() const |
| 262 | { |
| 263 | return m_distancePerPulse * impl->GetVelocity(); |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Set the minimum rate of the device before the hardware reports it stopped. |
| 268 | * |
| 269 | * @param minRate The minimum rate. The units are in distance per second as scaled by the value from SetDistancePerPulse(). |
| 270 | */ |
| 271 | void Encoder::SetMinRate(double minRate) |
| 272 | { |
| 273 | throw "Simulation doesn't currently support this method."; |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Set the distance per pulse for this encoder. |
| 278 | * This sets the multiplier used to determine the distance driven based on the count value |
| 279 | * from the encoder. |
| 280 | * Do not include the decoding type in this scale. The library already compensates for the decoding type. |
| 281 | * Set this value based on the encoder's rated Pulses per Revolution and |
| 282 | * factor in gearing reductions following the encoder shaft. |
| 283 | * This distance can be in any units you like, linear or angular. |
| 284 | * |
| 285 | * @param distancePerPulse The scale factor that will be used to convert pulses to useful units. |
| 286 | */ |
| 287 | void Encoder::SetDistancePerPulse(double distancePerPulse) |
| 288 | { |
| 289 | if (m_reverseDirection) { |
| 290 | m_distancePerPulse = -distancePerPulse; |
| 291 | } else { |
| 292 | m_distancePerPulse = distancePerPulse; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Set the direction sensing for this encoder. |
| 298 | * This sets the direction sensing on the encoder so that it could count in the correct |
| 299 | * software direction regardless of the mounting. |
| 300 | * @param reverseDirection true if the encoder direction should be reversed |
| 301 | */ |
| 302 | void Encoder::SetReverseDirection(bool reverseDirection) |
| 303 | { |
| 304 | throw "Simulation doesn't currently support this method."; |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Set which parameter of the encoder you are using as a process control variable. |
| 309 | * |
| 310 | * @param pidSource An enum to select the parameter. |
| 311 | */ |
| 312 | void Encoder::SetPIDSourceType(PIDSourceType pidSource) |
| 313 | { |
| 314 | m_pidSource = pidSource; |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * Implement the PIDSource interface. |
| 319 | * |
| 320 | * @return The current value of the selected source parameter. |
| 321 | */ |
| 322 | double Encoder::PIDGet() |
| 323 | { |
| 324 | switch (m_pidSource) |
| 325 | { |
| 326 | case PIDSourceType::kDisplacement: |
| 327 | return GetDistance(); |
| 328 | case PIDSourceType::kRate: |
| 329 | return GetRate(); |
| 330 | default: |
| 331 | return 0.0; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | void Encoder::UpdateTable() { |
| 336 | if (m_table != nullptr) { |
| 337 | m_table->PutNumber("Speed", GetRate()); |
| 338 | m_table->PutNumber("Distance", GetDistance()); |
| 339 | m_table->PutNumber("Distance per Tick", m_reverseDirection ? -m_distancePerPulse : m_distancePerPulse); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | void Encoder::StartLiveWindowMode() { |
| 344 | |
| 345 | } |
| 346 | |
| 347 | void Encoder::StopLiveWindowMode() { |
| 348 | |
| 349 | } |
| 350 | |
| 351 | std::string Encoder::GetSmartDashboardType() const { |
| 352 | if (m_encodingType == k4X) |
| 353 | return "Quadrature Encoder"; |
| 354 | else |
| 355 | return "Encoder"; |
| 356 | } |
| 357 | |
| 358 | void Encoder::InitTable(std::shared_ptr<ITable> subTable) { |
| 359 | m_table = subTable; |
| 360 | UpdateTable(); |
| 361 | } |
| 362 | |
| 363 | std::shared_ptr<ITable> Encoder::GetTable() const { |
| 364 | return m_table; |
| 365 | } |