2016-05-26 22:14:25 -07:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2019-07-07 19:17:14 -07:00
|
|
|
/* Copyright (c) 2016-2019 FIRST. All Rights Reserved. */
|
2016-05-26 22:14:25 -07:00
|
|
|
/* 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. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "hal/AnalogInput.h"
|
2016-05-26 22:14:25 -07:00
|
|
|
|
2017-08-27 00:11:52 -07:00
|
|
|
#include <FRC_NetworkCommunication/AICalibration.h>
|
2018-04-29 23:33:19 -07:00
|
|
|
#include <wpi/mutex.h>
|
2017-08-27 00:11:52 -07:00
|
|
|
|
2016-05-26 22:14:25 -07:00
|
|
|
#include "AnalogInternal.h"
|
2018-05-13 22:02:47 -07:00
|
|
|
#include "HALInitializer.h"
|
2016-07-02 23:19:14 -07:00
|
|
|
#include "PortsInternal.h"
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "hal/AnalogAccumulator.h"
|
|
|
|
|
#include "hal/handles/HandlesInternal.h"
|
2016-05-26 22:14:25 -07:00
|
|
|
|
2017-12-10 18:02:07 -08:00
|
|
|
namespace hal {
|
|
|
|
|
namespace init {
|
|
|
|
|
void InitializeAnalogInput() {}
|
|
|
|
|
} // namespace init
|
|
|
|
|
} // namespace hal
|
|
|
|
|
|
2016-05-26 22:14:25 -07:00
|
|
|
using namespace hal;
|
|
|
|
|
|
|
|
|
|
extern "C" {
|
2017-08-23 22:07:46 -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) {
|
2018-05-13 22:02:47 -07:00
|
|
|
hal::init::CheckInit();
|
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
|
|
|
|
2017-12-10 18:02:07 -08: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
|
2017-12-10 18:02:07 -08:00
|
|
|
auto analog_port = analogInputHandles->Get(handle);
|
2016-06-27 21:32:30 -07:00
|
|
|
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
|
|
|
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.
|
2017-12-10 18:02:07 -08:00
|
|
|
analogInputHandles->Free(analogPortHandle);
|
2016-05-26 22:14:25 -07:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
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.
|
2016-11-20 07:25:03 -08:00
|
|
|
// TODO: Need double comparison with epsilon.
|
2016-05-26 22:14:25 -07:00
|
|
|
// wpi_assert(!sampleRateSet || GetSampleRate() == samplesPerSecond);
|
2016-10-23 10:00:34 -07:00
|
|
|
initializeAnalog(status);
|
|
|
|
|
if (*status != 0) return;
|
|
|
|
|
setAnalogSampleRate(samplesPerSecond, status);
|
2016-05-26 22:14:25 -07:00
|
|
|
}
|
|
|
|
|
|
2016-07-12 10:45:14 -07:00
|
|
|
double HAL_GetAnalogSampleRate(int32_t* status) {
|
2016-10-23 10:00:34 -07:00
|
|
|
initializeAnalog(status);
|
|
|
|
|
if (*status != 0) return 0;
|
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
|
|
|
}
|
|
|
|
|
|
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) {
|
2017-12-10 18:02:07 -08: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
|
|
|
}
|
|
|
|
|
|
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) {
|
2017-12-10 18:02:07 -08: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;
|
|
|
|
|
}
|
|
|
|
|
|
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) {
|
2017-12-10 18:02:07 -08: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
|
|
|
}
|
|
|
|
|
|
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) {
|
2017-12-10 18:02:07 -08: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;
|
|
|
|
|
}
|
|
|
|
|
|
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) {
|
2017-12-10 18:02:07 -08: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;
|
|
|
|
|
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(analogRegisterWindowMutex);
|
2016-07-09 17:38:18 -07:00
|
|
|
analogInputSystem->writeReadSelect(readSelect, status);
|
|
|
|
|
analogInputSystem->strobeLatchOutput(status);
|
|
|
|
|
return static_cast<int16_t>(analogInputSystem->readOutput(status));
|
2016-05-26 22:14:25 -07:00
|
|
|
}
|
|
|
|
|
|
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) {
|
2017-12-10 18:02:07 -08: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;
|
|
|
|
|
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(analogRegisterWindowMutex);
|
2016-07-09 17:38:18 -07:00
|
|
|
analogInputSystem->writeReadSelect(readSelect, status);
|
|
|
|
|
analogInputSystem->strobeLatchOutput(status);
|
|
|
|
|
return static_cast<int32_t>(analogInputSystem->readOutput(status));
|
2016-05-26 22:14:25 -07:00
|
|
|
}
|
|
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
int32_t HAL_GetAnalogVoltsToValue(HAL_AnalogInputHandle analogPortHandle,
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
int32_t LSBWeight = HAL_GetAnalogLSBWeight(analogPortHandle, status);
|
|
|
|
|
int32_t offset = HAL_GetAnalogOffset(analogPortHandle, status);
|
|
|
|
|
int32_t value =
|
|
|
|
|
static_cast<int32_t>((voltage + offset * 1.0e-9) / (LSBWeight * 1.0e-9));
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
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) {
|
2017-12-10 18:02:07 -08: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-09-06 19:39:28 -07:00
|
|
|
int32_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;
|
|
|
|
|
}
|
|
|
|
|
|
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) {
|
2017-12-10 18:02:07 -08: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;
|
|
|
|
|
}
|
2017-08-23 22:07:46 -07:00
|
|
|
|
|
|
|
|
} // extern "C"
|