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 * Analog Input / Output / Accumulator / Trigger JNI Functions. 009 * 010 * @see "hal/AnalogInput.h" 011 * @see "hal/AnalogOutput.h" 012 * @see "hal/AnalogAccumulator.h" 013 * @see "hal/AnalogTrigger.h" 014 */ 015public class AnalogJNI extends JNIWrapper { 016 /** 017 * <i>native declaration : AthenaJava\target\native\include\HAL\Analog.h:58</i><br> 018 * enum values 019 */ 020 public interface AnalogTriggerType { 021 /** <i>native declaration : AthenaJava\target\native\include\HAL\Analog.h:54</i> */ 022 int kInWindow = 0; 023 024 /** <i>native declaration : AthenaJava\target\native\include\HAL\Analog.h:55</i> */ 025 int kState = 1; 026 027 /** <i>native declaration : AthenaJava\target\native\include\HAL\Analog.h:56</i> */ 028 int kRisingPulse = 2; 029 030 /** <i>native declaration : AthenaJava\target\native\include\HAL\Analog.h:57</i> */ 031 int kFallingPulse = 3; 032 } 033 034 /** 035 * Initializes the analog input port using the given port object. 036 * 037 * @param halPortHandle Handle to the port to initialize. 038 * @return the created analog input handle 039 * @see "HAL_InitializeAnalogInputPort" 040 */ 041 public static native int initializeAnalogInputPort(int halPortHandle); 042 043 /** 044 * Frees an analog input port. 045 * 046 * @param portHandle Handle to the analog port. 047 * @see "HAL_FreeAnalogInputPort" 048 */ 049 public static native void freeAnalogInputPort(int portHandle); 050 051 /** 052 * Initializes the analog output port using the given port object. 053 * 054 * @param halPortHandle handle to the port 055 * @return the created analog output handle 056 * @see "HAL_InitializeAnalogOutputPort" 057 */ 058 public static native int initializeAnalogOutputPort(int halPortHandle); 059 060 /** 061 * Frees an analog output port. 062 * 063 * @param portHandle the analog output handle 064 * @see "HAL_FreeAnalogOutputPort" 065 */ 066 public static native void freeAnalogOutputPort(int portHandle); 067 068 /** 069 * Checks that the analog module number is valid. 070 * 071 * @param module The analog module number. 072 * @return Analog module is valid and present 073 * @see "HAL_CheckAnalogModule" 074 */ 075 public static native boolean checkAnalogModule(byte module); 076 077 /** 078 * Checks that the analog output channel number is valid. Verifies that the analog channel number 079 * is one of the legal channel numbers. Channel numbers are 0-based. 080 * 081 * @param channel The analog output channel number. 082 * @return Analog channel is valid 083 * @see "HAL_CheckAnalogInputChannel" 084 */ 085 public static native boolean checkAnalogInputChannel(int channel); 086 087 public static native boolean checkAnalogOutputChannel(int channel); 088 089 /** 090 * Indicates the analog input is used by a simulated device. 091 * 092 * @param handle the analog input handle 093 * @param device simulated device handle 094 * @see "HAL_SetAnalogInputSimDevice" 095 */ 096 public static native void setAnalogInputSimDevice(int handle, int device); 097 098 public static native void setAnalogOutput(int portHandle, double voltage); 099 100 public static native double getAnalogOutput(int portHandle); 101 102 /** 103 * Sets the sample rate. 104 * 105 * <p>This is a global setting for the Athena and effects all channels. 106 * 107 * @param samplesPerSecond The number of samples per channel per second. 108 * @see "HAL_SetAnalogSampleRate" 109 */ 110 public static native void setAnalogSampleRate(double samplesPerSecond); 111 112 /** 113 * Gets the current sample rate. 114 * 115 * <p>This assumes one entry in the scan list. This is a global setting for the Athena and effects 116 * all channels. 117 * 118 * @return Sample rate. 119 * @see "HAL_GetAnalogSampleRate" 120 */ 121 public static native double getAnalogSampleRate(); 122 123 /** 124 * Sets the number of averaging bits. 125 * 126 * <p>This sets the number of averaging bits. The actual number of averaged samples is 2**bits. 127 * Use averaging to improve the stability of your measurement at the expense of sampling rate. The 128 * averaging is done automatically in the FPGA. 129 * 130 * @param analogPortHandle Handle to the analog port to configure. 131 * @param bits Number of bits to average. 132 * @see "HAL_SetAnalogAverageBits" 133 */ 134 public static native void setAnalogAverageBits(int analogPortHandle, int bits); 135 136 /** 137 * Gets the number of averaging bits. 138 * 139 * <p>This gets the number of averaging bits from the FPGA. The actual number of averaged samples 140 * is 2**bits. The averaging is done automatically in the FPGA. 141 * 142 * @param analogPortHandle Handle to the analog port to use. 143 * @return Bits to average. 144 * @see "HAL_GetAnalogAverageBits" 145 */ 146 public static native int getAnalogAverageBits(int analogPortHandle); 147 148 /** 149 * Sets the number of oversample bits. 150 * 151 * <p>This sets the number of oversample bits. The actual number of oversampled values is 2**bits. 152 * Use oversampling to improve the resolution of your measurements at the expense of sampling 153 * rate. The oversampling is done automatically in the FPGA. 154 * 155 * @param analogPortHandle Handle to the analog port to use. 156 * @param bits Number of bits to oversample. 157 * @see "HAL_SetAnalogOversampleBits" 158 */ 159 public static native void setAnalogOversampleBits(int analogPortHandle, int bits); 160 161 /** 162 * Gets the number of oversample bits. 163 * 164 * <p>This gets the number of oversample bits from the FPGA. The actual number of oversampled 165 * values is 2**bits. The oversampling is done automatically in the FPGA. 166 * 167 * @param analogPortHandle Handle to the analog port to use. 168 * @return Bits to oversample. 169 * @see "HAL_GetAnalogOversampleBits" 170 */ 171 public static native int getAnalogOversampleBits(int analogPortHandle); 172 173 /** 174 * Gets a sample straight from the channel on this module. 175 * 176 * <p>The sample is a 12-bit value representing the 0V to 5V range of the A/D converter in the 177 * module. The units are in A/D converter codes. Use GetVoltage() to get the analog value in 178 * calibrated units. 179 * 180 * @param analogPortHandle Handle to the analog port to use. 181 * @return A sample straight from the channel on this module. 182 * @see "HAL_GetAnalogValue" 183 */ 184 public static native short getAnalogValue(int analogPortHandle); 185 186 /** 187 * Gets a sample from the output of the oversample and average engine for the channel. 188 * 189 * <p>The sample is 12-bit + the value configured in SetOversampleBits(). The value configured in 190 * SetAverageBits() will cause this value to be averaged 2**bits number of samples. This is not a 191 * sliding window. The sample will not change until 2**(OversampleBits + AverageBits) samples have 192 * been acquired from the module on this channel. Use GetAverageVoltage() to get the analog value 193 * in calibrated units. 194 * 195 * @param analogPortHandle Handle to the analog port to use. 196 * @return A sample from the oversample and average engine for the channel. 197 * @see "HAL_GetAnalogAverageValue" 198 */ 199 public static native int getAnalogAverageValue(int analogPortHandle); 200 201 /** 202 * Converts a voltage to a raw value for a specified channel. 203 * 204 * <p>This process depends on the calibration of each channel, so the channel must be specified. 205 * 206 * <p>todo This assumes raw values. Oversampling not supported as is. 207 * 208 * @param analogPortHandle Handle to the analog port to use. 209 * @param voltage The voltage to convert. 210 * @return The raw value for the channel. 211 * @see "HAL_GetAnalogVoltsToValue" 212 */ 213 public static native int getAnalogVoltsToValue(int analogPortHandle, double voltage); 214 215 /** 216 * Get the analog voltage from a raw value. 217 * 218 * @param analogPortHandle Handle to the analog port the values were read from. 219 * @param value The raw analog value 220 * @return The voltage relating to the value 221 * @see "HAL_GetAnalogValueToVolts" 222 */ 223 public static native double getAnalogValueToVolts(int analogPortHandle, int value); 224 225 /** 226 * Gets a scaled sample straight from the channel on this module. 227 * 228 * <p>The value is scaled to units of Volts using the calibrated scaling data from GetLSBWeight() 229 * and GetOffset(). 230 * 231 * @param analogPortHandle Handle to the analog port to use. 232 * @return A scaled sample straight from the channel on this module. 233 * @see "HAL_GetAnalogVoltage" 234 */ 235 public static native double getAnalogVoltage(int analogPortHandle); 236 237 /** 238 * Gets a scaled sample from the output of the oversample and average engine for the channel. 239 * 240 * <p>The value is scaled to units of Volts using the calibrated scaling data from GetLSBWeight() 241 * and GetOffset(). Using oversampling will cause this value to be higher resolution, but it will 242 * update more slowly. Using averaging will cause this value to be more stable, but it will update 243 * more slowly. 244 * 245 * @param analogPortHandle Handle to the analog port to use. 246 * @return A scaled sample from the output of the oversample and average engine for the channel. 247 * @see "HAL_GetAnalogAverageVoltage" 248 */ 249 public static native double getAnalogAverageVoltage(int analogPortHandle); 250 251 /** 252 * Gets the factory scaling least significant bit weight constant. The least significant bit 253 * weight constant for the channel that was calibrated in manufacturing and stored in an eeprom in 254 * the module. 255 * 256 * <p>Volts = ((LSB_Weight * 1e-9) * raw) - (Offset * 1e-9) 257 * 258 * @param analogPortHandle Handle to the analog port to use. 259 * @return Least significant bit weight. 260 * @see "HAL_GetAnalogLSBWeight" 261 */ 262 public static native int getAnalogLSBWeight(int analogPortHandle); 263 264 /** 265 * Gets the factory scaling offset constant. The offset constant for the channel that was 266 * calibrated in manufacturing and stored in an eeprom in the module. 267 * 268 * <p>Volts = ((LSB_Weight * 1e-9) * raw) - (Offset * 1e-9) 269 * 270 * @param analogPortHandle Handle to the analog port to use. 271 * @return Offset constant. 272 * @see "HAL_GetAnalogOffset" 273 */ 274 public static native int getAnalogOffset(int analogPortHandle); 275 276 /** 277 * Is the channel attached to an accumulator. 278 * 279 * @param analogPortHandle Handle to the analog port. 280 * @return The analog channel is attached to an accumulator. 281 * @see "HAL_IsAccumulatorChannel" 282 */ 283 public static native boolean isAccumulatorChannel(int analogPortHandle); 284 285 /** 286 * Initialize the accumulator. 287 * 288 * @param analogPortHandle Handle to the analog port. 289 * @see "HAL_InitAccumulator" 290 */ 291 public static native void initAccumulator(int analogPortHandle); 292 293 /** 294 * Resets the accumulator to the initial value. 295 * 296 * @param analogPortHandle Handle to the analog port. 297 * @see "HAL_ResetAccumulator" 298 */ 299 public static native void resetAccumulator(int analogPortHandle); 300 301 /** 302 * Set the center value of the accumulator. 303 * 304 * <p>The center value is subtracted from each A/D value before it is added to the accumulator. 305 * This is used for the center value of devices like gyros and accelerometers to make integration 306 * work and to take the device offset into account when integrating. 307 * 308 * <p>This center value is based on the output of the oversampled and averaged source from channel 309 * 1. Because of this, any non-zero oversample bits will affect the size of the value for this 310 * field. 311 * 312 * @param analogPortHandle Handle to the analog port. 313 * @param center The center value of the accumulator. 314 * @see "HAL_SetAccumulatorCenter" 315 */ 316 public static native void setAccumulatorCenter(int analogPortHandle, int center); 317 318 /** 319 * Set the accumulator's deadband. 320 * 321 * @param analogPortHandle Handle to the analog port. 322 * @param deadband The deadband of the accumulator. 323 * @see "HAL_SetAccumulatorDeadband" 324 */ 325 public static native void setAccumulatorDeadband(int analogPortHandle, int deadband); 326 327 /** 328 * Read the accumulated value. 329 * 330 * <p>Read the value that has been accumulating on channel 1. The accumulator is attached after 331 * the oversample and average engine. 332 * 333 * @param analogPortHandle Handle to the analog port. 334 * @return The 64-bit value accumulated since the last Reset(). 335 * @see "HAL_GetAccumulatorValue" 336 */ 337 public static native long getAccumulatorValue(int analogPortHandle); 338 339 /** 340 * Read the number of accumulated values. 341 * 342 * <p>Read the count of the accumulated values since the accumulator was last Reset(). 343 * 344 * @param analogPortHandle Handle to the analog port. 345 * @return The number of times samples from the channel were accumulated. 346 * @see "HAL_GetAccumulatorCount" 347 */ 348 public static native int getAccumulatorCount(int analogPortHandle); 349 350 /** 351 * Read the accumulated value and the number of accumulated values atomically. 352 * 353 * <p>This function reads the value and count from the FPGA atomically. This can be used for 354 * averaging. 355 * 356 * @param analogPortHandle Handle to the analog port. 357 * @param result Accumulator result. 358 * @see "HAL_GetAccumulatorOutput" 359 */ 360 public static native void getAccumulatorOutput(int analogPortHandle, AccumulatorResult result); 361 362 /** 363 * Initializes an analog trigger. 364 * 365 * @param analogInputHandle the analog input to use for triggering 366 * @return the created analog trigger handle 367 * @see "HAL_InitializeAnalogTrigger" 368 */ 369 public static native int initializeAnalogTrigger(int analogInputHandle); 370 371 /** 372 * Initializes an analog trigger with a Duty Cycle input. 373 * 374 * @param dutyCycleHandle the analog input to use for duty cycle 375 * @return tbe created analog trigger handle 376 * @see "HAL_InitializeAnalogTriggerDutyCycle" 377 */ 378 public static native int initializeAnalogTriggerDutyCycle(int dutyCycleHandle); 379 380 /** 381 * Frees an analog trigger. 382 * 383 * @param analogTriggerHandle the trigger handle 384 * @see "HAL_CleanAnalogTrigger" 385 */ 386 public static native void cleanAnalogTrigger(int analogTriggerHandle); 387 388 /** 389 * Sets the raw ADC upper and lower limits of the analog trigger. 390 * 391 * <p>HAL_SetAnalogTriggerLimitsVoltage or HAL_SetAnalogTriggerLimitsDutyCycle is likely better in 392 * most cases. 393 * 394 * @param analogTriggerHandle the trigger handle 395 * @param lower the lower ADC value 396 * @param upper the upper ADC value 397 * @see "HAL_SetAnalogTriggerLimitsRaw" 398 */ 399 public static native void setAnalogTriggerLimitsRaw( 400 int analogTriggerHandle, int lower, int upper); 401 402 /** 403 * Sets the upper and lower limits of the analog trigger. 404 * 405 * <p>The limits are given as floating point duty cycle values. 406 * 407 * @param analogTriggerHandle the trigger handle 408 * @param lower the lower duty cycle value 409 * @param higher the upper duty cycle value 410 * @see "HAL_SetAnalogTriggerLimitsDutyCycle" 411 */ 412 public static native void setAnalogTriggerLimitsDutyCycle( 413 int analogTriggerHandle, double lower, double higher); 414 415 /** 416 * Sets the upper and lower limits of the analog trigger. 417 * 418 * <p>The limits are given as floating point voltage values. 419 * 420 * @param analogTriggerHandle the trigger handle 421 * @param lower the lower voltage value 422 * @param upper the upper voltage value 423 * @see "HAL_SetAnalogTriggerLimitsVoltage" 424 */ 425 public static native void setAnalogTriggerLimitsVoltage( 426 int analogTriggerHandle, double lower, double upper); 427 428 /** 429 * Configures the analog trigger to use the averaged vs. raw values. 430 * 431 * <p>If the value is true, then the averaged value is selected for the analog trigger, otherwise 432 * the immediate value is used. 433 * 434 * <p>This is not allowed to be used if filtered mode is set. This is not allowed to be used with 435 * Duty Cycle based inputs. 436 * 437 * @param analogTriggerHandle the trigger handle 438 * @param useAveragedValue true to use averaged values, false for raw 439 * @see "HAL_SetAnalogTriggerAveraged" 440 */ 441 public static native void setAnalogTriggerAveraged( 442 int analogTriggerHandle, boolean useAveragedValue); 443 444 /** 445 * Configures the analog trigger to use a filtered value. 446 * 447 * <p>The analog trigger will operate with a 3 point average rejection filter. This is designed to 448 * help with 360 degree pot applications for the period where the pot crosses through zero. 449 * 450 * <p>This is not allowed to be used if averaged mode is set. 451 * 452 * @param analogTriggerHandle the trigger handle 453 * @param useFilteredValue true to use filtered values, false for average or raw 454 * @see "HAL_SetAnalogTriggerFiltered" 455 */ 456 public static native void setAnalogTriggerFiltered( 457 int analogTriggerHandle, boolean useFilteredValue); 458 459 /** 460 * Returns the InWindow output of the analog trigger. 461 * 462 * <p>True if the analog input is between the upper and lower limits. 463 * 464 * @param analogTriggerHandle the trigger handle 465 * @return the InWindow output of the analog trigger 466 * @see "HAL_GetAnalogTriggerInWindow" 467 */ 468 public static native boolean getAnalogTriggerInWindow(int analogTriggerHandle); 469 470 /** 471 * Returns the TriggerState output of the analog trigger. 472 * 473 * <p>True if above upper limit. False if below lower limit. If in Hysteresis, maintain previous 474 * state. 475 * 476 * @param analogTriggerHandle the trigger handle 477 * @return the TriggerState output of the analog trigger 478 * @see "HAL_GetAnalogTriggerTriggerState" 479 */ 480 public static native boolean getAnalogTriggerTriggerState(int analogTriggerHandle); 481 482 /** 483 * Gets the state of the analog trigger output. 484 * 485 * @param analogTriggerHandle the trigger handle 486 * @param type the type of trigger to trigger on 487 * @return the state of the analog trigger output 488 * @see "HAL_GetAnalogTriggerOutput" 489 */ 490 public static native boolean getAnalogTriggerOutput(int analogTriggerHandle, int type); 491 492 /** 493 * Get the FPGA index for the AnlogTrigger. 494 * 495 * @param analogTriggerHandle the trigger handle 496 * @return the FPGA index 497 * @see "HAL_GetAnalogTriggerFPGAIndex" 498 */ 499 public static native int getAnalogTriggerFPGAIndex(int analogTriggerHandle); 500}