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