2016-05-26 22:14:25 -07:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
/* Copyright (c) FIRST 2016. All Rights Reserved. */
|
|
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#include "HAL/AnalogInput.h"
|
|
|
|
|
|
|
|
|
|
#include <mutex>
|
|
|
|
|
|
|
|
|
|
#include "AnalogInternal.h"
|
|
|
|
|
#include "FRC_NetworkCommunication/AICalibration.h"
|
|
|
|
|
#include "HAL/AnalogAccumulator.h"
|
|
|
|
|
#include "HAL/HAL.h"
|
|
|
|
|
#include "HAL/cpp/priority_mutex.h"
|
2016-07-13 20:29:28 -07:00
|
|
|
#include "HAL/handles/HandlesInternal.h"
|
2016-07-02 23:19:14 -07:00
|
|
|
#include "PortsInternal.h"
|
2016-05-26 22:14:25 -07:00
|
|
|
|
|
|
|
|
using namespace hal;
|
|
|
|
|
|
|
|
|
|
static bool analogSampleRateSet = false;
|
|
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
/**
|
|
|
|
|
* Initialize the analog input port using the given port object.
|
2016-08-12 13:45:28 -07:00
|
|
|
*
|
|
|
|
|
* @param portHandle Handle to the port to initialize.
|
2016-05-26 22:14:25 -07:00
|
|
|
*/
|
2016-08-12 13:45:28 -07:00
|
|
|
HAL_AnalogInputHandle HAL_InitializeAnalogInputPort(HAL_PortHandle portHandle,
|
2016-07-09 00:24:26 -07:00
|
|
|
int32_t* status) {
|
2016-05-26 22:14:25 -07:00
|
|
|
initializeAnalog(status);
|
2016-06-05 15:23:58 -07:00
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
if (*status != 0) return HAL_kInvalidHandle;
|
2016-06-05 15:23:58 -07:00
|
|
|
|
2016-08-12 13:45:28 -07:00
|
|
|
int16_t channel = getPortHandleChannel(portHandle);
|
|
|
|
|
if (channel == InvalidHandleIndex) {
|
2016-06-05 15:23:58 -07:00
|
|
|
*status = PARAMETER_OUT_OF_RANGE;
|
2016-07-09 00:24:26 -07:00
|
|
|
return HAL_kInvalidHandle;
|
2016-06-05 15:23:58 -07:00
|
|
|
}
|
2016-05-26 22:14:25 -07:00
|
|
|
|
2016-08-12 13:45:28 -07:00
|
|
|
HAL_AnalogInputHandle handle = analogInputHandles.Allocate(channel, status);
|
2016-06-27 21:32:30 -07:00
|
|
|
|
|
|
|
|
if (*status != 0)
|
2016-07-09 00:24:26 -07:00
|
|
|
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
|
2016-06-27 21:32:30 -07:00
|
|
|
|
2016-05-26 22:14:25 -07:00
|
|
|
// Initialize port structure
|
2016-06-27 21:32:30 -07:00
|
|
|
auto analog_port = analogInputHandles.Get(handle);
|
|
|
|
|
if (analog_port == nullptr) { // would only error on thread issue
|
2016-07-03 17:27:06 -07:00
|
|
|
*status = HAL_HANDLE_ERROR;
|
2016-07-09 00:24:26 -07:00
|
|
|
return HAL_kInvalidHandle;
|
2016-06-27 21:32:30 -07:00
|
|
|
}
|
2016-08-12 13:45:28 -07:00
|
|
|
analog_port->channel = static_cast<uint8_t>(channel);
|
2016-07-09 00:24:26 -07:00
|
|
|
if (HAL_IsAccumulatorChannel(handle, status)) {
|
2016-08-12 13:45:28 -07:00
|
|
|
analog_port->accumulator.reset(tAccumulator::create(channel, status));
|
2016-07-10 17:47:44 -07:00
|
|
|
} else {
|
2016-05-26 22:14:25 -07:00
|
|
|
analog_port->accumulator = nullptr;
|
2016-07-10 17:47:44 -07:00
|
|
|
}
|
2016-05-26 22:14:25 -07:00
|
|
|
|
|
|
|
|
// Set default configuration
|
2016-08-12 13:45:28 -07:00
|
|
|
analogInputSystem->writeScanList(channel, channel, status);
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_SetAnalogAverageBits(handle, kDefaultAverageBits, status);
|
|
|
|
|
HAL_SetAnalogOversampleBits(handle, kDefaultOversampleBits, status);
|
2016-06-27 21:32:30 -07:00
|
|
|
return handle;
|
2016-05-26 22:14:25 -07:00
|
|
|
}
|
|
|
|
|
|
2016-08-12 13:45:28 -07:00
|
|
|
/**
|
|
|
|
|
* @param analogPortHandle Handle to the analog port.
|
|
|
|
|
*/
|
|
|
|
|
void HAL_FreeAnalogInputPort(HAL_AnalogInputHandle analogPortHandle) {
|
2016-06-27 21:32:30 -07:00
|
|
|
// no status, so no need to check for a proper free.
|
2016-08-12 13:45:28 -07:00
|
|
|
analogInputHandles.Free(analogPortHandle);
|
2016-05-26 22:14:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check that the analog module number is valid.
|
|
|
|
|
*
|
2016-08-12 13:45:28 -07:00
|
|
|
* @param module The analog module number.
|
2016-05-26 22:14:25 -07:00
|
|
|
* @return Analog module is valid and present
|
|
|
|
|
*/
|
2016-07-12 10:45:14 -07:00
|
|
|
HAL_Bool HAL_CheckAnalogModule(int32_t module) { return module == 1; }
|
2016-05-26 22:14:25 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check that the analog output channel number is value.
|
|
|
|
|
* Verify that the analog channel number is one of the legal channel numbers.
|
|
|
|
|
* Channel numbers are 0-based.
|
|
|
|
|
*
|
2016-08-12 13:45:28 -07:00
|
|
|
* @param channel The analog output channel number.
|
2016-05-26 22:14:25 -07:00
|
|
|
* @return Analog channel is valid
|
|
|
|
|
*/
|
2016-08-12 13:45:28 -07:00
|
|
|
HAL_Bool HAL_CheckAnalogInputChannel(int32_t channel) {
|
|
|
|
|
return channel < kNumAnalogInputs && channel >= 0;
|
2016-05-26 22:14:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the sample rate.
|
|
|
|
|
*
|
|
|
|
|
* This is a global setting for the Athena and effects all channels.
|
|
|
|
|
*
|
|
|
|
|
* @param samplesPerSecond The number of samples per channel per second.
|
|
|
|
|
*/
|
2016-07-09 00:24:26 -07:00
|
|
|
void HAL_SetAnalogSampleRate(double samplesPerSecond, int32_t* status) {
|
2016-05-26 22:14:25 -07:00
|
|
|
// TODO: This will change when variable size scan lists are implemented.
|
|
|
|
|
// TODO: Need float comparison with epsilon.
|
|
|
|
|
// wpi_assert(!sampleRateSet || GetSampleRate() == samplesPerSecond);
|
|
|
|
|
analogSampleRateSet = true;
|
|
|
|
|
|
|
|
|
|
// Compute the convert rate
|
2016-07-10 17:47:44 -07:00
|
|
|
uint32_t ticksPerSample =
|
2016-07-12 10:45:14 -07:00
|
|
|
static_cast<uint32_t>(static_cast<double>(kTimebase) / samplesPerSecond);
|
2016-05-26 22:14:25 -07:00
|
|
|
uint32_t ticksPerConversion =
|
|
|
|
|
ticksPerSample / getAnalogNumChannelsToActivate(status);
|
|
|
|
|
// ticksPerConversion must be at least 80
|
|
|
|
|
if (ticksPerConversion < 80) {
|
|
|
|
|
if ((*status) >= 0) *status = SAMPLE_RATE_TOO_HIGH;
|
|
|
|
|
ticksPerConversion = 80;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Atomically set the scan size and the convert rate so that the sample rate
|
|
|
|
|
// is constant
|
|
|
|
|
tAI::tConfig config;
|
|
|
|
|
config.ScanSize = getAnalogNumChannelsToActivate(status);
|
|
|
|
|
config.ConvertRate = ticksPerConversion;
|
|
|
|
|
analogInputSystem->writeConfig(config, status);
|
|
|
|
|
|
|
|
|
|
// Indicate that the scan size has been commited to hardware.
|
|
|
|
|
setAnalogNumChannelsToActivate(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the current sample rate.
|
|
|
|
|
*
|
|
|
|
|
* This assumes one entry in the scan list.
|
|
|
|
|
* This is a global setting for the Athena and effects all channels.
|
|
|
|
|
*
|
|
|
|
|
* @return Sample rate.
|
|
|
|
|
*/
|
2016-07-12 10:45:14 -07:00
|
|
|
double HAL_GetAnalogSampleRate(int32_t* status) {
|
2016-05-26 22:14:25 -07:00
|
|
|
uint32_t ticksPerConversion = analogInputSystem->readLoopTiming(status);
|
|
|
|
|
uint32_t ticksPerSample =
|
|
|
|
|
ticksPerConversion * getAnalogNumActiveChannels(status);
|
2016-07-12 10:45:14 -07:00
|
|
|
return static_cast<double>(kTimebase) / static_cast<double>(ticksPerSample);
|
2016-05-26 22:14:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the number of averaging bits.
|
|
|
|
|
*
|
|
|
|
|
* This sets the number of averaging bits. The actual number of averaged samples
|
|
|
|
|
* is 2**bits. Use averaging to improve the stability of your measurement at the
|
|
|
|
|
* expense of sampling rate. The averaging is done automatically in the FPGA.
|
|
|
|
|
*
|
2016-08-12 13:45:28 -07:00
|
|
|
* @param analogPortHandle Handle to the analog port to configure.
|
2016-05-26 22:14:25 -07:00
|
|
|
* @param bits Number of bits to average.
|
|
|
|
|
*/
|
2016-08-12 13:45:28 -07:00
|
|
|
void HAL_SetAnalogAverageBits(HAL_AnalogInputHandle analogPortHandle,
|
2016-07-12 10:45:14 -07:00
|
|
|
int32_t bits, int32_t* status) {
|
2016-08-12 13:45:28 -07:00
|
|
|
auto port = analogInputHandles.Get(analogPortHandle);
|
2016-06-27 21:32:30 -07:00
|
|
|
if (port == nullptr) {
|
2016-07-03 17:27:06 -07:00
|
|
|
*status = HAL_HANDLE_ERROR;
|
2016-06-27 21:32:30 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2016-08-12 13:45:28 -07:00
|
|
|
analogInputSystem->writeAverageBits(port->channel, static_cast<uint8_t>(bits),
|
2016-07-12 10:45:14 -07:00
|
|
|
status);
|
2016-05-26 22:14:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the number of averaging bits.
|
|
|
|
|
*
|
|
|
|
|
* This gets the number of averaging bits from the FPGA. The actual number of
|
|
|
|
|
* averaged samples is 2**bits. The averaging is done automatically in the FPGA.
|
|
|
|
|
*
|
2016-08-12 13:45:28 -07:00
|
|
|
* @param analogPortHandle Handle to the analog port to use.
|
2016-05-26 22:14:25 -07:00
|
|
|
* @return Bits to average.
|
|
|
|
|
*/
|
2016-08-12 13:45:28 -07:00
|
|
|
int32_t HAL_GetAnalogAverageBits(HAL_AnalogInputHandle analogPortHandle,
|
2016-07-12 10:45:14 -07:00
|
|
|
int32_t* status) {
|
2016-08-12 13:45:28 -07:00
|
|
|
auto port = analogInputHandles.Get(analogPortHandle);
|
2016-06-27 21:32:30 -07:00
|
|
|
if (port == nullptr) {
|
2016-07-03 17:27:06 -07:00
|
|
|
*status = HAL_HANDLE_ERROR;
|
2016-06-27 21:32:30 -07:00
|
|
|
return kDefaultAverageBits;
|
|
|
|
|
}
|
2016-08-12 13:45:28 -07:00
|
|
|
uint8_t result = analogInputSystem->readAverageBits(port->channel, status);
|
2016-05-26 22:14:25 -07:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the number of oversample bits.
|
|
|
|
|
*
|
|
|
|
|
* This sets the number of oversample bits. The actual number of oversampled
|
|
|
|
|
* values is 2**bits. Use oversampling to improve the resolution of your
|
|
|
|
|
* measurements at the expense of sampling rate. The oversampling is done
|
|
|
|
|
* automatically in the FPGA.
|
|
|
|
|
*
|
2016-08-12 13:45:28 -07:00
|
|
|
* @param analogPortHandle Handle to the analog port to use.
|
2016-05-26 22:14:25 -07:00
|
|
|
* @param bits Number of bits to oversample.
|
|
|
|
|
*/
|
2016-08-12 13:45:28 -07:00
|
|
|
void HAL_SetAnalogOversampleBits(HAL_AnalogInputHandle analogPortHandle,
|
2016-07-12 10:45:14 -07:00
|
|
|
int32_t bits, int32_t* status) {
|
2016-08-12 13:45:28 -07:00
|
|
|
auto port = analogInputHandles.Get(analogPortHandle);
|
2016-06-27 21:32:30 -07:00
|
|
|
if (port == nullptr) {
|
2016-07-03 17:27:06 -07:00
|
|
|
*status = HAL_HANDLE_ERROR;
|
2016-06-27 21:32:30 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2016-08-12 13:45:28 -07:00
|
|
|
analogInputSystem->writeOversampleBits(port->channel,
|
|
|
|
|
static_cast<uint8_t>(bits), status);
|
2016-05-26 22:14:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the number of oversample bits.
|
|
|
|
|
*
|
|
|
|
|
* This gets the number of oversample bits from the FPGA. The actual number of
|
|
|
|
|
* oversampled values is 2**bits. The oversampling is done automatically in the
|
|
|
|
|
* FPGA.
|
|
|
|
|
*
|
2016-08-12 13:45:28 -07:00
|
|
|
* @param analogPortHandle Handle to the analog port to use.
|
2016-05-26 22:14:25 -07:00
|
|
|
* @return Bits to oversample.
|
|
|
|
|
*/
|
2016-08-12 13:45:28 -07:00
|
|
|
int32_t HAL_GetAnalogOversampleBits(HAL_AnalogInputHandle analogPortHandle,
|
2016-07-12 10:45:14 -07:00
|
|
|
int32_t* status) {
|
2016-08-12 13:45:28 -07:00
|
|
|
auto port = analogInputHandles.Get(analogPortHandle);
|
2016-06-27 21:32:30 -07:00
|
|
|
if (port == nullptr) {
|
2016-07-03 17:27:06 -07:00
|
|
|
*status = HAL_HANDLE_ERROR;
|
2016-06-27 21:32:30 -07:00
|
|
|
return kDefaultOversampleBits;
|
|
|
|
|
}
|
2016-08-12 13:45:28 -07:00
|
|
|
uint8_t result = analogInputSystem->readOversampleBits(port->channel, status);
|
2016-05-26 22:14:25 -07:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a sample straight from the channel on this module.
|
|
|
|
|
*
|
|
|
|
|
* The sample is a 12-bit value representing the 0V to 5V range of the A/D
|
|
|
|
|
* converter in the module. The units are in A/D converter codes. Use
|
|
|
|
|
* GetVoltage() to get the analog value in calibrated units.
|
|
|
|
|
*
|
2016-08-12 13:45:28 -07:00
|
|
|
* @param analogPortHandle Handle to the analog port to use.
|
2016-05-26 22:14:25 -07:00
|
|
|
* @return A sample straight from the channel on this module.
|
|
|
|
|
*/
|
2016-08-12 13:45:28 -07:00
|
|
|
int32_t HAL_GetAnalogValue(HAL_AnalogInputHandle analogPortHandle,
|
2016-07-09 00:24:26 -07:00
|
|
|
int32_t* status) {
|
2016-08-12 13:45:28 -07:00
|
|
|
auto port = analogInputHandles.Get(analogPortHandle);
|
2016-06-27 21:32:30 -07:00
|
|
|
if (port == nullptr) {
|
2016-07-03 17:27:06 -07:00
|
|
|
*status = HAL_HANDLE_ERROR;
|
2016-06-27 21:32:30 -07:00
|
|
|
return 0;
|
|
|
|
|
}
|
2016-05-26 22:14:25 -07:00
|
|
|
|
|
|
|
|
tAI::tReadSelect readSelect;
|
2016-08-12 13:45:28 -07:00
|
|
|
readSelect.Channel = port->channel;
|
2016-05-26 22:14:25 -07:00
|
|
|
readSelect.Averaged = false;
|
|
|
|
|
|
2016-07-09 17:38:18 -07:00
|
|
|
std::lock_guard<priority_recursive_mutex> sync(analogRegisterWindowMutex);
|
|
|
|
|
analogInputSystem->writeReadSelect(readSelect, status);
|
|
|
|
|
analogInputSystem->strobeLatchOutput(status);
|
|
|
|
|
return static_cast<int16_t>(analogInputSystem->readOutput(status));
|
2016-05-26 22:14:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a sample from the output of the oversample and average engine for the
|
|
|
|
|
* channel.
|
|
|
|
|
*
|
|
|
|
|
* The sample is 12-bit + the value 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**(OversamplBits + AverageBits) samples have been acquired from
|
|
|
|
|
* the module on this channel. Use GetAverageVoltage() to get the analog value
|
|
|
|
|
* in calibrated units.
|
|
|
|
|
*
|
2016-08-12 13:45:28 -07:00
|
|
|
* @param analogPortHandle Handle to the analog port to use.
|
2016-05-26 22:14:25 -07:00
|
|
|
* @return A sample from the oversample and average engine for the channel.
|
|
|
|
|
*/
|
2016-08-12 13:45:28 -07:00
|
|
|
int32_t HAL_GetAnalogAverageValue(HAL_AnalogInputHandle analogPortHandle,
|
2016-07-09 00:24:26 -07:00
|
|
|
int32_t* status) {
|
2016-08-12 13:45:28 -07:00
|
|
|
auto port = analogInputHandles.Get(analogPortHandle);
|
2016-06-27 21:32:30 -07:00
|
|
|
if (port == nullptr) {
|
2016-07-03 17:27:06 -07:00
|
|
|
*status = HAL_HANDLE_ERROR;
|
2016-06-27 21:32:30 -07:00
|
|
|
return 0;
|
|
|
|
|
}
|
2016-05-26 22:14:25 -07:00
|
|
|
tAI::tReadSelect readSelect;
|
2016-08-12 13:45:28 -07:00
|
|
|
readSelect.Channel = port->channel;
|
2016-05-26 22:14:25 -07:00
|
|
|
readSelect.Averaged = true;
|
|
|
|
|
|
2016-07-09 17:38:18 -07:00
|
|
|
std::lock_guard<priority_recursive_mutex> sync(analogRegisterWindowMutex);
|
|
|
|
|
analogInputSystem->writeReadSelect(readSelect, status);
|
|
|
|
|
analogInputSystem->strobeLatchOutput(status);
|
|
|
|
|
return static_cast<int32_t>(analogInputSystem->readOutput(status));
|
2016-05-26 22:14:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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().
|
|
|
|
|
*
|
2016-08-12 13:45:28 -07:00
|
|
|
* @param analogPortHandle Handle to the analog port to use.
|
2016-05-26 22:14:25 -07:00
|
|
|
* @return A scaled sample straight from the channel on this module.
|
|
|
|
|
*/
|
2016-08-12 13:45:28 -07:00
|
|
|
double HAL_GetAnalogVoltage(HAL_AnalogInputHandle analogPortHandle,
|
2016-07-12 10:45:14 -07:00
|
|
|
int32_t* status) {
|
2016-08-12 13:45:28 -07:00
|
|
|
int32_t value = HAL_GetAnalogValue(analogPortHandle, status);
|
|
|
|
|
int32_t LSBWeight = HAL_GetAnalogLSBWeight(analogPortHandle, status);
|
|
|
|
|
int32_t offset = HAL_GetAnalogOffset(analogPortHandle, status);
|
2016-07-12 10:45:14 -07:00
|
|
|
double voltage = LSBWeight * 1.0e-9 * value - offset * 1.0e-9;
|
2016-05-26 22:14:25 -07:00
|
|
|
return voltage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
2016-08-12 13:45:28 -07:00
|
|
|
* @param analogPortHandle Handle to the analog port to use.
|
2016-05-26 22:14:25 -07:00
|
|
|
* @return A scaled sample from the output of the oversample and average engine
|
|
|
|
|
* for the channel.
|
|
|
|
|
*/
|
2016-08-12 13:45:28 -07:00
|
|
|
double HAL_GetAnalogAverageVoltage(HAL_AnalogInputHandle analogPortHandle,
|
2016-07-12 10:45:14 -07:00
|
|
|
int32_t* status) {
|
2016-08-12 13:45:28 -07:00
|
|
|
int32_t value = HAL_GetAnalogAverageValue(analogPortHandle, status);
|
|
|
|
|
int32_t LSBWeight = HAL_GetAnalogLSBWeight(analogPortHandle, status);
|
|
|
|
|
int32_t offset = HAL_GetAnalogOffset(analogPortHandle, status);
|
2016-07-12 10:45:14 -07:00
|
|
|
int32_t oversampleBits =
|
2016-08-12 13:45:28 -07:00
|
|
|
HAL_GetAnalogOversampleBits(analogPortHandle, status);
|
2016-07-12 10:45:14 -07:00
|
|
|
double voltage =
|
|
|
|
|
LSBWeight * 1.0e-9 * value / static_cast<double>(1 << oversampleBits) -
|
2016-05-26 22:14:25 -07:00
|
|
|
offset * 1.0e-9;
|
|
|
|
|
return voltage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convert a voltage to a raw value for a specified channel.
|
|
|
|
|
*
|
2016-08-12 13:45:28 -07:00
|
|
|
* This process depends on the calibration of each channel, so the channel must
|
|
|
|
|
* be specified.
|
2016-05-26 22:14:25 -07:00
|
|
|
*
|
|
|
|
|
* @todo This assumes raw values. Oversampling not supported as is.
|
|
|
|
|
*
|
2016-08-12 13:45:28 -07:00
|
|
|
* @param analogPortHandle Handle to the analog port to use.
|
2016-05-26 22:14:25 -07:00
|
|
|
* @param voltage The voltage to convert.
|
|
|
|
|
* @return The raw value for the channel.
|
|
|
|
|
*/
|
2016-08-12 13:45:28 -07:00
|
|
|
int32_t HAL_GetAnalogVoltsToValue(HAL_AnalogInputHandle analogPortHandle,
|
2016-07-09 00:24:26 -07:00
|
|
|
double voltage, int32_t* status) {
|
2016-05-26 22:14:25 -07:00
|
|
|
if (voltage > 5.0) {
|
|
|
|
|
voltage = 5.0;
|
|
|
|
|
*status = VOLTAGE_OUT_OF_RANGE;
|
|
|
|
|
}
|
|
|
|
|
if (voltage < 0.0) {
|
|
|
|
|
voltage = 0.0;
|
|
|
|
|
*status = VOLTAGE_OUT_OF_RANGE;
|
|
|
|
|
}
|
2016-08-12 13:45:28 -07:00
|
|
|
int32_t LSBWeight = HAL_GetAnalogLSBWeight(analogPortHandle, status);
|
|
|
|
|
int32_t offset = HAL_GetAnalogOffset(analogPortHandle, status);
|
2016-07-12 10:45:14 -07:00
|
|
|
int32_t value =
|
|
|
|
|
static_cast<int32_t>((voltage + offset * 1.0e-9) / (LSBWeight * 1.0e-9));
|
2016-05-26 22:14:25 -07:00
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the factory scaling least significant bit weight constant.
|
|
|
|
|
* The least significant bit weight constant for the channel that was calibrated
|
|
|
|
|
* in manufacturing and stored in an eeprom in the module.
|
|
|
|
|
*
|
|
|
|
|
* Volts = ((LSB_Weight * 1e-9) * raw) - (Offset * 1e-9)
|
|
|
|
|
*
|
2016-08-12 13:45:28 -07:00
|
|
|
* @param analogPortHandle Handle to the analog port to use.
|
2016-05-26 22:14:25 -07:00
|
|
|
* @return Least significant bit weight.
|
|
|
|
|
*/
|
2016-08-12 13:45:28 -07:00
|
|
|
int32_t HAL_GetAnalogLSBWeight(HAL_AnalogInputHandle analogPortHandle,
|
2016-07-12 10:45:14 -07:00
|
|
|
int32_t* status) {
|
2016-08-12 13:45:28 -07:00
|
|
|
auto port = analogInputHandles.Get(analogPortHandle);
|
2016-06-27 21:32:30 -07:00
|
|
|
if (port == nullptr) {
|
2016-07-03 17:27:06 -07:00
|
|
|
*status = HAL_HANDLE_ERROR;
|
2016-06-27 21:32:30 -07:00
|
|
|
return 0;
|
|
|
|
|
}
|
2016-05-26 22:14:25 -07:00
|
|
|
uint32_t lsbWeight = FRC_NetworkCommunication_nAICalibration_getLSBWeight(
|
2016-08-12 13:45:28 -07:00
|
|
|
0, port->channel, status); // XXX: aiSystemIndex == 0?
|
2016-05-26 22:14:25 -07:00
|
|
|
return lsbWeight;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the factory scaling offset constant.
|
|
|
|
|
* The offset constant for the channel that was calibrated in manufacturing and
|
|
|
|
|
* stored in an eeprom in the module.
|
|
|
|
|
*
|
|
|
|
|
* Volts = ((LSB_Weight * 1e-9) * raw) - (Offset * 1e-9)
|
|
|
|
|
*
|
2016-08-12 13:45:28 -07:00
|
|
|
* @param analogPortHandle Handle to the analog port to use.
|
2016-05-26 22:14:25 -07:00
|
|
|
* @return Offset constant.
|
|
|
|
|
*/
|
2016-08-12 13:45:28 -07:00
|
|
|
int32_t HAL_GetAnalogOffset(HAL_AnalogInputHandle analogPortHandle,
|
2016-07-09 00:24:26 -07:00
|
|
|
int32_t* status) {
|
2016-08-12 13:45:28 -07:00
|
|
|
auto port = analogInputHandles.Get(analogPortHandle);
|
2016-06-27 21:32:30 -07:00
|
|
|
if (port == nullptr) {
|
2016-07-03 17:27:06 -07:00
|
|
|
*status = HAL_HANDLE_ERROR;
|
2016-06-27 21:32:30 -07:00
|
|
|
return 0;
|
|
|
|
|
}
|
2016-05-26 22:14:25 -07:00
|
|
|
int32_t offset = FRC_NetworkCommunication_nAICalibration_getOffset(
|
2016-08-12 13:45:28 -07:00
|
|
|
0, port->channel, status); // XXX: aiSystemIndex == 0?
|
2016-05-26 22:14:25 -07:00
|
|
|
return offset;
|
|
|
|
|
}
|
|
|
|
|
}
|