/** * Get a scaled sample straight from the channel on this module. * * The value is scaled to units of Volts using the calibrated scaling data from GetLSBWeight() and GetOffset(). * * @param analog_port_pointer Pointer to the analog port to use. * @return A scaled sample straight from the channel on this module. */ float getAnalogVoltage(void* analog_port_pointer, int32_t *status) { int16_t value = getAnalogValue(analog_port_pointer, status); uint32_t LSBWeight = getAnalogLSBWeight(analog_port_pointer, status); int32_t offset = getAnalogOffset(analog_port_pointer, status); float voltage = LSBWeight * 1.0e-9 * value - offset * 1.0e-9; return voltage; }
/** * Get the factory scaling offset constant. * * Volts = ((LSB_Weight * 1e-9) * raw) - (Offset * 1e-9) * * @return Offset constant. */ int32_t AnalogInput::GetOffset() const { if (StatusIsFatal()) return 0; int32_t status = 0; int32_t offset = getAnalogOffset(m_port, &status); wpi_setErrorWithContext(status, getHALErrorMessage(status)); return offset; }
/** * 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: getAnalogOffset * Signature: (Ljava/nio/ByteBuffer;Ljava/nio/IntBuffer;)I */ JNIEXPORT jint JNICALL Java_edu_wpi_first_wpilibj_hal_AnalogJNI_getAnalogOffset (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 = getAnalogOffset(*javaId, statusPtr); ANALOGJNI_LOG(logDEBUG) << "Status = " << *statusPtr; ANALOGJNI_LOG(logDEBUG) << "AnalogOffset = " << returnValue; return returnValue; }
/** * Convert a voltage to a raw value for a specified channel. * * This process depends on the calibration of each channel, so the channel * must be specified. * * @todo This assumes raw values. Oversampling not supported as is. * * @param analog_port_pointer Pointer to the analog port to use. * @param voltage The voltage to convert. * @return The raw value for the channel. */ int32_t getAnalogVoltsToValue(void* analog_port_pointer, double voltage, int32_t *status) { if (voltage > 5.0) { voltage = 5.0; *status = VOLTAGE_OUT_OF_RANGE; } if (voltage < 0.0) { voltage = 0.0; *status = VOLTAGE_OUT_OF_RANGE; } uint32_t LSBWeight = getAnalogLSBWeight(analog_port_pointer, status); int32_t offset = getAnalogOffset(analog_port_pointer, status); int32_t value = (int32_t) ((voltage + offset * 1.0e-9) / (LSBWeight * 1.0e-9)); return value; }