/** * Get a sample from the output of the oversample and average engine for this * channel. * The sample is 12-bit + the bits configured in SetOversampleBits(). * The value configured in SetAverageBits() will cause this value to be averaged * 2**bits number of samples. * This is not a sliding window. The sample will not change until * 2**(OversampleBits + AverageBits) samples * have been acquired from the module on this channel. * Use GetAverageVoltage() to get the analog value in calibrated units. * @return A sample from the oversample and average engine for this channel. */ int32_t AnalogInput::GetAverageValue() const { if (StatusIsFatal()) return 0; int32_t status = 0; int32_t value = getAnalogAverageValue(m_port, &status); wpi_setErrorWithContext(status, getHALErrorMessage(status)); return value; }
/** * Get a scaled sample from the output of the oversample and average engine for the channel. * * The value is scaled to units of Volts using the calibrated scaling data from GetLSBWeight() and GetOffset(). * Using oversampling will cause this value to be higher resolution, but it will update more slowly. * Using averaging will cause this value to be more stable, but it will update more slowly. * * @param analog_port_pointer Pointer to the analog port to use. * @return A scaled sample from the output of the oversample and average engine for the channel. */ float getAnalogAverageVoltage(void* analog_port_pointer, int32_t *status) { int32_t value = getAnalogAverageValue(analog_port_pointer, status); uint32_t LSBWeight = getAnalogLSBWeight(analog_port_pointer, status); int32_t offset = getAnalogOffset(analog_port_pointer, status); uint32_t oversampleBits = getAnalogOversampleBits(analog_port_pointer, status); float voltage = ((LSBWeight * 1.0e-9 * value) / (float)(1 << oversampleBits)) - offset * 1.0e-9; return voltage; }
/* * Class: edu_wpi_first_wpilibj_hal_AnalogJNI * Method: getAnalogAverageValue * Signature: (Ljava/nio/ByteBuffer;Ljava/nio/IntBuffer;)I */ JNIEXPORT jint JNICALL Java_edu_wpi_first_wpilibj_hal_AnalogJNI_getAnalogAverageValue (JNIEnv * env, jclass, jobject id, jobject status) { void ** javaId = (void**)env->GetDirectBufferAddress(id); ANALOGJNI_LOG(logDEBUG) << "Analog Ptr = " << *javaId; jint * statusPtr = (jint*)env->GetDirectBufferAddress(status); jint returnValue = getAnalogAverageValue( *javaId, statusPtr ); ANALOGJNI_LOG(logDEBUG) << "Status = " << *statusPtr; ANALOGJNI_LOG(logDEBUG) << "AverageValue = " << returnValue; return returnValue; }