Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) FIRST 2008-2017. All Rights Reserved. */ |
| 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 "frc971/wpilib/ahal/Encoder.h" |
| 9 | |
Austin Schuh | f6b9463 | 2019-02-02 22:11:27 -0800 | [diff] [blame] | 10 | #include "hal/HAL.h" |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 11 | #include "frc971/wpilib/ahal/DigitalInput.h" |
| 12 | |
| 13 | using namespace frc; |
| 14 | |
| 15 | #define HAL_FATAL_WITH_STATUS(status) |
| 16 | |
| 17 | /** |
| 18 | * Common initialization code for Encoders. |
| 19 | * |
| 20 | * This code allocates resources for Encoders and is common to all constructors. |
| 21 | * |
| 22 | * The counter will start counting immediately. |
| 23 | * |
| 24 | * @param reverseDirection If true, counts down instead of up (this is all |
| 25 | * relative) |
| 26 | * @param encodingType either k1X, k2X, or k4X to indicate 1X, 2X or 4X |
| 27 | * decoding. If 4X is selected, then an encoder FPGA |
| 28 | * object is used and the returned counts will be 4x |
| 29 | * the encoder spec'd value since all rising and |
| 30 | * falling edges are counted. If 1X or 2X are selected |
| 31 | * then a counter object will be used and the returned |
| 32 | * value will either exactly match the spec'd count or |
| 33 | * be double (2x) the spec'd count. |
| 34 | */ |
| 35 | void Encoder::InitEncoder(bool reverseDirection, EncodingType encodingType) { |
| 36 | int32_t status = 0; |
| 37 | m_encoder = HAL_InitializeEncoder( |
| 38 | m_aSource->GetPortHandleForRouting(), |
| 39 | (HAL_AnalogTriggerType)m_aSource->GetAnalogTriggerTypeForRouting(), |
| 40 | m_bSource->GetPortHandleForRouting(), |
| 41 | (HAL_AnalogTriggerType)m_bSource->GetAnalogTriggerTypeForRouting(), |
| 42 | reverseDirection, (HAL_EncoderEncodingType)encodingType, &status); |
| 43 | HAL_FATAL_WITH_STATUS(status); |
| 44 | |
| 45 | HAL_Report(HALUsageReporting::kResourceType_Encoder, GetFPGAIndex(), |
| 46 | encodingType); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Encoder constructor. |
| 51 | * |
| 52 | * Construct a Encoder given a and b channels. |
| 53 | * |
| 54 | * The counter will start counting immediately. |
| 55 | * |
| 56 | * @param aChannel The a channel DIO channel. 0-9 are on-board, 10-25 |
| 57 | * are on the MXP port |
| 58 | * @param bChannel The b channel DIO channel. 0-9 are on-board, 10-25 |
| 59 | * are on the MXP port |
| 60 | * @param reverseDirection represents the orientation of the encoder and |
| 61 | * inverts the output values if necessary so forward |
| 62 | * represents positive values. |
| 63 | * @param encodingType either k1X, k2X, or k4X to indicate 1X, 2X or 4X |
| 64 | * decoding. If 4X is selected, then an encoder FPGA |
| 65 | * object is used and the returned counts will be 4x |
| 66 | * the encoder spec'd value since all rising and |
| 67 | * falling edges are counted. If 1X or 2X are selected |
| 68 | * then a counter object will be used and the returned |
| 69 | * value will either exactly match the spec'd count or |
| 70 | * be double (2x) the spec'd count. |
| 71 | */ |
| 72 | Encoder::Encoder(int aChannel, int bChannel, bool reverseDirection, |
| 73 | EncodingType encodingType) { |
| 74 | m_aSource = std::make_shared<DigitalInput>(aChannel); |
| 75 | m_bSource = std::make_shared<DigitalInput>(bChannel); |
| 76 | InitEncoder(reverseDirection, encodingType); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Free the resources for an Encoder. |
| 81 | * |
| 82 | * Frees the FPGA resources associated with an Encoder. |
| 83 | */ |
| 84 | Encoder::~Encoder() { |
| 85 | int32_t status = 0; |
| 86 | HAL_FreeEncoder(m_encoder, &status); |
| 87 | HAL_FATAL_WITH_STATUS(status); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * The encoding scale factor 1x, 2x, or 4x, per the requested encodingType. |
| 92 | * |
| 93 | * Used to divide raw edge counts down to spec'd counts. |
| 94 | */ |
| 95 | int Encoder::GetEncodingScale() const { |
| 96 | int32_t status = 0; |
| 97 | int val = HAL_GetEncoderEncodingScale(m_encoder, &status); |
| 98 | wpi_setErrorWithContext(status, HAL_GetErrorMessage(status)); |
| 99 | return val; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Gets the raw value from the encoder. |
| 104 | * |
| 105 | * The raw value is the actual count unscaled by the 1x, 2x, or 4x scale |
| 106 | * factor. |
| 107 | * |
| 108 | * @return Current raw count from the encoder |
| 109 | */ |
| 110 | int Encoder::GetRaw() const { |
| 111 | int32_t status = 0; |
| 112 | int value = HAL_GetEncoderRaw(m_encoder, &status); |
| 113 | HAL_FATAL_WITH_STATUS(status); |
| 114 | return value; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Returns the period of the most recent pulse. |
| 119 | * |
| 120 | * Returns the period of the most recent Encoder pulse in seconds. |
| 121 | * This method compensates for the decoding type. |
| 122 | * |
| 123 | * @deprecated Use GetRate() in favor of this method. This returns unscaled |
| 124 | * periods and GetRate() scales using value from |
| 125 | * SetDistancePerPulse(). |
| 126 | * |
| 127 | * @return Period in seconds of the most recent pulse. |
| 128 | */ |
| 129 | double Encoder::GetPeriod() const { |
| 130 | int32_t status = 0; |
| 131 | double value = HAL_GetEncoderPeriod(m_encoder, &status); |
| 132 | HAL_FATAL_WITH_STATUS(status); |
| 133 | return value; |
| 134 | } |
| 135 | /** |
| 136 | * Sets the maximum period for stopped detection. |
| 137 | * |
| 138 | * Sets the value that represents the maximum period of the Encoder before it |
| 139 | * will assume that the attached device is stopped. This timeout allows users |
| 140 | * to determine if the wheels or other shaft has stopped rotating. |
| 141 | * This method compensates for the decoding type. |
| 142 | * |
| 143 | * @deprecated Use SetMinRate() in favor of this method. This takes unscaled |
| 144 | * periods and SetMinRate() scales using value from |
| 145 | * SetDistancePerPulse(). |
| 146 | * |
| 147 | * @param maxPeriod The maximum time between rising and falling edges before |
| 148 | * the FPGA will report the device stopped. This is expressed |
| 149 | * in seconds. |
| 150 | */ |
| 151 | void Encoder::SetMaxPeriod(double maxPeriod) { |
| 152 | int32_t status = 0; |
| 153 | HAL_SetEncoderMaxPeriod(m_encoder, maxPeriod, &status); |
| 154 | HAL_FATAL_WITH_STATUS(status); |
| 155 | } |
| 156 | |
| 157 | int Encoder::GetFPGAIndex() const { |
| 158 | int32_t status = 0; |
| 159 | int val = HAL_GetEncoderFPGAIndex(m_encoder, &status); |
| 160 | HAL_FATAL_WITH_STATUS(status); |
| 161 | return val; |
| 162 | } |