001// Copyright (c) FIRST and other WPILib contributors.
002// Open Source Software; you can modify and/or share it under the terms of
003// the WPILib BSD license file in the root directory of this project.
004
005package edu.wpi.first.hal;
006
007/**
008 * Encoder JNI Functions.
009 *
010 * @see "hal/Encoder.h"
011 */
012public class EncoderJNI extends JNIWrapper {
013  /**
014   * Initializes an encoder.
015   *
016   * @param aChannel the A channel
017   * @param bChannel the B channel
018   * @param reverseDirection true to reverse the counting direction from standard, otherwise false
019   * @param encodingType the encoding type
020   * @return the created encoder handle
021   * @see "HAL_InitializeEncoder"
022   */
023  public static native int initializeEncoder(
024      int aChannel, int bChannel, boolean reverseDirection, int encodingType);
025
026  /**
027   * Frees an encoder.
028   *
029   * @param encoderHandle the encoder handle
030   * @see "HAL_FreeEncoder"
031   */
032  public static native void freeEncoder(int encoderHandle);
033
034  /**
035   * Indicates the encoder is used by a simulated device.
036   *
037   * @param handle the encoder handle
038   * @param device simulated device handle
039   * @see "HAL_SetEncoderSimDevice"
040   */
041  public static native void setEncoderSimDevice(int handle, int device);
042
043  /**
044   * Gets the current counts of the encoder after encoding type scaling.
045   *
046   * <p>This is scaled by the value passed during initialization to encodingType.
047   *
048   * @param encoderHandle the encoder handle
049   * @return the current scaled count
050   * @see "HAL_GetEncoder"
051   */
052  public static native int getEncoder(int encoderHandle);
053
054  /**
055   * Gets the raw counts of the encoder.
056   *
057   * <p>This is not scaled by any values.
058   *
059   * @param encoderHandle the encoder handle
060   * @return the raw encoder count
061   * @see "HAL_GetEncoderRaw"
062   */
063  public static native int getEncoderRaw(int encoderHandle);
064
065  /**
066   * Gets the encoder scale value.
067   *
068   * <p>This is set by the value passed during initialization to encodingType.
069   *
070   * @param encoderHandle the encoder handle
071   * @return the encoder scale value
072   * @see "HAL_GetEncoderEncodingScale"
073   */
074  public static native int getEncodingScaleFactor(int encoderHandle);
075
076  /**
077   * Reads the current encoder value.
078   *
079   * <p>Read the value at this instant. It may still be running, so it reflects the current value.
080   * Next time it is read, it might have a different value.
081   *
082   * @param encoderHandle the encoder handle
083   * @see "HAL_ResetEncoder"
084   */
085  public static native void resetEncoder(int encoderHandle);
086
087  /**
088   * Gets the Period of the most recent count.
089   *
090   * <p>Returns the time interval of the most recent count. This can be used for velocity
091   * calculations to determine shaft speed.
092   *
093   * @param encoderHandle the encoder handle
094   * @return the period of the last two pulses in units of seconds
095   * @see "HAL_GetEncoderPeriod"
096   */
097  public static native double getEncoderPeriod(int encoderHandle);
098
099  /**
100   * Sets the maximum period where the device is still considered "moving".
101   *
102   * <p>Sets the maximum period where the device is considered moving. This value is used to
103   * determine the "stopped" state of the encoder using the getEncoderStopped method.
104   *
105   * @param encoderHandle the encoder handle
106   * @param maxPeriod the maximum period where the counted device is considered moving in seconds
107   * @see "HAL_SetEncoderMaxPeriod"
108   */
109  public static native void setEncoderMaxPeriod(int encoderHandle, double maxPeriod);
110
111  /**
112   * Determines if the clock is stopped.
113   *
114   * <p>Determines if the clocked input is stopped based on the MaxPeriod value set using the
115   * SetMaxPeriod method. If the clock exceeds the MaxPeriod, then the device (and encoder) are
116   * assumed to be stopped and it returns true.
117   *
118   * @param encoderHandle the encoder handle
119   * @return true if the most recent encoder period exceeds the MaxPeriod value set by SetMaxPeriod
120   * @see "HAL_GetEncoderStopped"
121   */
122  public static native boolean getEncoderStopped(int encoderHandle);
123
124  /**
125   * Gets the last direction the encoder value changed.
126   *
127   * @param encoderHandle the encoder handle
128   * @return the last direction the encoder value changed
129   * @see "HAL_GetEncoderDirection"
130   */
131  public static native boolean getEncoderDirection(int encoderHandle);
132
133  /**
134   * Gets the current distance traveled by the encoder.
135   *
136   * <p>This is the encoder count scaled by the distance per pulse set for the encoder.
137   *
138   * @param encoderHandle the encoder handle
139   * @return the encoder distance (units are determined by the units passed to
140   *     setEncoderDistancePerPulse)
141   * @see "HAL_GetEncoderDistance"
142   */
143  public static native double getEncoderDistance(int encoderHandle);
144
145  /**
146   * Gets the current rate of the encoder.
147   *
148   * <p>This is the encoder period scaled by the distance per pulse set for the encoder.
149   *
150   * @param encoderHandle the encoder handle
151   * @return the encoder rate (units are determined by the units passed to
152   *     setEncoderDistancePerPulse, time value is seconds)
153   * @see "HAL_GetEncoderRate"
154   */
155  public static native double getEncoderRate(int encoderHandle);
156
157  /**
158   * Sets the minimum rate to be considered moving by the encoder.
159   *
160   * <p>Units need to match what is set by setEncoderDistancePerPulse, with time as seconds.
161   *
162   * @param encoderHandle the encoder handle
163   * @param minRate the minimum rate to be considered moving (units are determined by the units
164   *     passed to setEncoderDistancePerPulse, time value is seconds)
165   * @see "HAL_SetEncoderMinRate"
166   */
167  public static native void setEncoderMinRate(int encoderHandle, double minRate);
168
169  /**
170   * Sets the distance traveled per encoder pulse. This is used as a scaling factor for the rate and
171   * distance calls.
172   *
173   * @param encoderHandle the encoder handle
174   * @param distancePerPulse the distance traveled per encoder pulse (units user defined)
175   * @see "HAL_SetEncoderDistancePerPulse"
176   */
177  public static native void setEncoderDistancePerPulse(int encoderHandle, double distancePerPulse);
178
179  /**
180   * Sets if to reverse the direction of the encoder.
181   *
182   * <p>Note that this is not a toggle. It is an absolute set.
183   *
184   * @param encoderHandle the encoder handle
185   * @param reverseDirection true to reverse the direction, false to not.
186   * @see "HAL_SetEncoderReverseDirection"
187   */
188  public static native void setEncoderReverseDirection(int encoderHandle, boolean reverseDirection);
189
190  /**
191   * Sets the number of encoder samples to average when calculating encoder rate.
192   *
193   * @param encoderHandle the encoder handle
194   * @param samplesToAverage the number of samples to average
195   * @see "HAL_SetEncoderSamplesToAverage"
196   */
197  public static native void setEncoderSamplesToAverage(int encoderHandle, int samplesToAverage);
198
199  /**
200   * Gets the current samples to average value.
201   *
202   * @param encoderHandle the encoder handle
203   * @return the current samples to average value
204   * @see "HAL_GetEncoderSamplesToAverage"
205   */
206  public static native int getEncoderSamplesToAverage(int encoderHandle);
207
208  /**
209   * Gets the FPGA index of the encoder.
210   *
211   * @param encoderHandle the encoder handle
212   * @return the FPGA index of the encoder
213   * @see "HAL_GetEncoderFPGAIndex"
214   */
215  public static native int getEncoderFPGAIndex(int encoderHandle);
216
217  /**
218   * Gets the encoder scale value.
219   *
220   * <p>This is set by the value passed during initialization to encodingType.
221   *
222   * @param encoderHandle the encoder handle
223   * @return the encoder scale value
224   * @see "HAL_GetEncoderEncodingScale"
225   */
226  public static native int getEncoderEncodingScale(int encoderHandle);
227
228  /**
229   * Gets the decoding scale factor of the encoder.
230   *
231   * <p>This is used to perform the scaling from raw to type scaled values.
232   *
233   * @param encoderHandle the encoder handle
234   * @return the scale value for the encoder
235   * @see "HAL_GetEncoderDecodingScaleFactor"
236   */
237  public static native double getEncoderDecodingScaleFactor(int encoderHandle);
238
239  /**
240   * Gets the user set distance per pulse of the encoder.
241   *
242   * @param encoderHandle the encoder handle
243   * @return the set distance per pulse
244   * @see "HAL_GetEncoderDistancePerPulse"
245   */
246  public static native double getEncoderDistancePerPulse(int encoderHandle);
247
248  /**
249   * Gets the encoding type of the encoder.
250   *
251   * @param encoderHandle the encoder handle
252   * @return the encoding type
253   * @see "HAL_GetEncoderEncodingType"
254   */
255  public static native int getEncoderEncodingType(int encoderHandle);
256
257  /** Utility class. */
258  private EncoderJNI() {}
259}