Files
allwpilib/wpilibc/wpilibC++Devices/src/AnalogInput.cpp

443 lines
12 KiB
C++
Raw Normal View History

/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008. 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 $(WIND_BASE)/WPILib. */
/*----------------------------------------------------------------------------*/
#include "AnalogInput.h"
//#include "NetworkCommunication/UsageReporting.h"
#include "Resource.h"
#include "Timer.h"
#include "WPIErrors.h"
#include "LiveWindow/LiveWindow.h"
static Resource *inputs = NULL;
const uint8_t AnalogInput::kAccumulatorModuleNumber;
const uint32_t AnalogInput::kAccumulatorNumChannels;
const uint32_t AnalogInput::kAccumulatorChannels[] = {0, 1};
/**
* Common initialization.
*/
void AnalogInput::InitAnalogInput(uint32_t channel)
{
m_table = NULL;
char buf[64];
Resource::CreateResourceObject(&inputs, kAnalogInputs);
if (!checkAnalogInputChannel(channel))
{
snprintf(buf, 64, "analog input %d", channel);
wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf);
return;
}
snprintf(buf, 64, "Analog Input %d", channel);
if (inputs->Allocate(channel, buf) == ~0ul)
{
CloneError(inputs);
return;
}
m_channel = channel;
void* port = getPort(channel);
int32_t status = 0;
m_port = initializeAnalogInputPort(port, &status);
wpi_setErrorWithContext(status, getHALErrorMessage(status));
LiveWindow::GetInstance()->AddSensor("AnalogInput", channel, this);
HALReport(HALUsageReporting::kResourceType_AnalogChannel, channel);
}
/**
* Construct an analog input.
*
* @param channel The channel number to represent.
*/
AnalogInput::AnalogInput(uint32_t channel)
{
InitAnalogInput(channel);
}
/**
* Channel destructor.
*/
AnalogInput::~AnalogInput()
{
inputs->Free(m_channel);
}
/**
* Get a sample straight from this channel.
* 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.
* @return A sample straight from this channel.
*/
int16_t AnalogInput::GetValue()
{
if (StatusIsFatal()) return 0;
int32_t status = 0;
int16_t value = getAnalogValue(m_port, &status);
wpi_setErrorWithContext(status, getHALErrorMessage(status));
return value;
}
/**
* Get a sample from the output of the oversample and average engine for this 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.
* @return A sample from the oversample and average engine for this channel.
*/
int32_t AnalogInput::GetAverageValue()
{
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 straight from this channel.
* The value is scaled to units of Volts using the calibrated scaling data from GetLSBWeight() and GetOffset().
* @return A scaled sample straight from this channel.
*/
float AnalogInput::GetVoltage()
{
if (StatusIsFatal()) return 0.0f;
int32_t status = 0;
float voltage = getAnalogVoltage(m_port, &status);
wpi_setErrorWithContext(status, getHALErrorMessage(status));
return voltage;
}
/**
* Get a scaled sample from the output of the oversample and average engine for this 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.
* @return A scaled sample from the output of the oversample and average engine for this channel.
*/
float AnalogInput::GetAverageVoltage()
{
if (StatusIsFatal()) return 0.0f;
int32_t status = 0;
float voltage = getAnalogAverageVoltage(m_port, &status);
wpi_setErrorWithContext(status, getHALErrorMessage(status));
return voltage;
}
/**
* Get the factory scaling least significant bit weight constant.
*
* Volts = ((LSB_Weight * 1e-9) * raw) - (Offset * 1e-9)
*
* @return Least significant bit weight.
*/
uint32_t AnalogInput::GetLSBWeight()
{
if (StatusIsFatal()) return 0;
int32_t status = 0;
int32_t lsbWeight = getAnalogLSBWeight(m_port, &status);
wpi_setErrorWithContext(status, getHALErrorMessage(status));
return lsbWeight;
}
/**
* Get the factory scaling offset constant.
*
* Volts = ((LSB_Weight * 1e-9) * raw) - (Offset * 1e-9)
*
* @return Offset constant.
*/
int32_t AnalogInput::GetOffset()
{
if (StatusIsFatal()) return 0;
int32_t status = 0;
int32_t offset = getAnalogOffset(m_port, &status);
wpi_setErrorWithContext(status, getHALErrorMessage(status));
return offset;
}
/**
* Get the channel number.
* @return The channel number.
*/
uint32_t AnalogInput::GetChannel()
{
if (StatusIsFatal()) return 0;
return m_channel;
}
/**
* 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.
*
* @param bits Number of bits of averaging.
*/
void AnalogInput::SetAverageBits(uint32_t bits)
{
if (StatusIsFatal()) return;
int32_t status = 0;
setAnalogAverageBits(m_port, bits, &status);
wpi_setErrorWithContext(status, getHALErrorMessage(status));
}
/**
* Get the number of averaging bits previously configured.
* 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.
*
* @return Number of bits of averaging previously configured.
*/
uint32_t AnalogInput::GetAverageBits()
{
int32_t status = 0;
int32_t averageBits = getAnalogAverageBits(m_port, &status);
wpi_setErrorWithContext(status, getHALErrorMessage(status));
return averageBits;
}
/**
* 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.
*
* @param bits Number of bits of oversampling.
*/
void AnalogInput::SetOversampleBits(uint32_t bits)
{
if (StatusIsFatal()) return;
int32_t status = 0;
setAnalogOversampleBits(m_port, bits, &status);
wpi_setErrorWithContext(status, getHALErrorMessage(status));
}
/**
* Get the number of oversample bits previously configured.
* 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.
*
* @return Number of bits of oversampling previously configured.
*/
uint32_t AnalogInput::GetOversampleBits()
{
if (StatusIsFatal()) return 0;
int32_t status = 0;
int32_t oversampleBits = getAnalogOversampleBits(m_port, &status);
wpi_setErrorWithContext(status, getHALErrorMessage(status));
return oversampleBits;
}
/**
* Is the channel attached to an accumulator.
*
* @return The analog input is attached to an accumulator.
*/
bool AnalogInput::IsAccumulatorChannel()
{
if (StatusIsFatal()) return false;
int32_t status = 0;
bool isAccum = isAccumulatorChannel(m_port, &status);
wpi_setErrorWithContext(status, getHALErrorMessage(status));
return isAccum;
}
/**
* Initialize the accumulator.
*/
void AnalogInput::InitAccumulator()
{
if (StatusIsFatal()) return;
m_accumulatorOffset = 0;
int32_t status = 0;
initAccumulator(m_port, &status);
wpi_setErrorWithContext(status, getHALErrorMessage(status));
}
/**
* Set an inital value for the accumulator.
*
* This will be added to all values returned to the user.
* @param initialValue The value that the accumulator should start from when reset.
*/
void AnalogInput::SetAccumulatorInitialValue(int64_t initialValue)
{
if (StatusIsFatal()) return;
m_accumulatorOffset = initialValue;
}
/**
* Resets the accumulator to the initial value.
*/
void AnalogInput::ResetAccumulator()
{
if (StatusIsFatal()) return;
int32_t status = 0;
resetAccumulator(m_port, &status);
wpi_setErrorWithContext(status, getHALErrorMessage(status));
if(!StatusIsFatal())
{
// Wait until the next sample, so the next call to GetAccumulator*()
// won't have old values.
const float sampleTime = 1.0f / GetSampleRate();
const float overSamples = 1 << GetOversampleBits();
const float averageSamples = 1 << GetAverageBits();
Wait(sampleTime * overSamples * averageSamples);
}
}
/**
* Set the center value of the accumulator.
*
* The center value is subtracted from each A/D value before it is added to the accumulator. This
* is used for the center value of devices like gyros and accelerometers to make integration work
* and to take the device offset into account when integrating.
*
* This center value is based on the output of the oversampled and averaged source from channel 1.
* Because of this, any non-zero oversample bits will affect the size of the value for this field.
*/
void AnalogInput::SetAccumulatorCenter(int32_t center)
{
if (StatusIsFatal()) return;
int32_t status = 0;
setAccumulatorCenter(m_port, center, &status);
wpi_setErrorWithContext(status, getHALErrorMessage(status));
}
/**
* Set the accumulator's deadband.
*/
void AnalogInput::SetAccumulatorDeadband(int32_t deadband)
{
if (StatusIsFatal()) return;
int32_t status = 0;
setAccumulatorDeadband(m_port, deadband, &status);
wpi_setErrorWithContext(status, getHALErrorMessage(status));
}
/**
* Read the accumulated value.
*
* Read the value that has been accumulating on channel 1.
* The accumulator is attached after the oversample and average engine.
*
* @return The 64-bit value accumulated since the last Reset().
*/
int64_t AnalogInput::GetAccumulatorValue()
{
if (StatusIsFatal()) return 0;
int32_t status = 0;
int64_t value = getAccumulatorValue(m_port, &status);
wpi_setErrorWithContext(status, getHALErrorMessage(status));
return value + m_accumulatorOffset;
}
/**
* Read the number of accumulated values.
*
* Read the count of the accumulated values since the accumulator was last Reset().
*
* @return The number of times samples from the channel were accumulated.
*/
uint32_t AnalogInput::GetAccumulatorCount()
{
if (StatusIsFatal()) return 0;
int32_t status = 0;
uint32_t count = getAccumulatorCount(m_port, &status);
wpi_setErrorWithContext(status, getHALErrorMessage(status));
return count;
}
/**
* Read the accumulated value and the number of accumulated values atomically.
*
* This function reads the value and count from the FPGA atomically.
* This can be used for averaging.
*
* @param value Pointer to the 64-bit accumulated output.
* @param count Pointer to the number of accumulation cycles.
*/
void AnalogInput::GetAccumulatorOutput(int64_t *value, uint32_t *count)
{
if (StatusIsFatal()) return;
int32_t status = 0;
getAccumulatorOutput(m_port, value, count, &status);
wpi_setErrorWithContext(status, getHALErrorMessage(status));
*value += m_accumulatorOffset;
}
/**
* Set the sample rate for all analog channels.
*
* @param samplesPerSecond The number of samples per second.
*/
void AnalogInput::SetSampleRate(float samplesPerSecond)
{
int32_t status = 0;
setAnalogSampleRate(samplesPerSecond, &status);
Pass errors to DS in C++ and Java Squashed commit of the following: commit f317b3522e312cf7e7bb9eb0494f2f96a7f6363c Author: Kevin O'Connor <koconnor@usfirst.org> Date: Mon Oct 20 17:15:46 2014 -0400 Send unhandled exceptions back to the DS. Change-Id: I0e658fdb6d43593ee20457f20f71f4f4cd2d21c3 commit f834ef8c791945697ad483c27b4167eb917ac242 Author: Kevin O'Connor <koconnor@usfirst.org> Date: Mon Oct 20 16:05:24 2014 -0400 Add StackTrace to Java errors Change-Id: I83b162afcc5f294703705770fbcd8623b0895539 commit 02e040b0c79067ce046ada29e26004e0460fceb0 Author: Kevin O'Connor <koconnor@usfirst.org> Date: Mon Oct 20 15:07:44 2014 -0400 HAL Errors to DS in Java Change-Id: I5fb51e4066bbc26ea59ca513c03c5ec5ace98831 commit 03775ddc42b129c27fdf403f17f0796009311c3c Author: Kevin O'Connor <koconnor@usfirst.org> Date: Mon Oct 20 13:38:18 2014 -0400 Update AnalogInput to report errors for getting and setting sample rate Change-Id: I00eb78f52fc5b17a60bc84456f0ec9842cc40ef7 commit 4c10cb79612ae81e3cbb6bd4d6da8cf3b8955821 Author: Kevin O'Connor <koconnor@usfirst.org> Date: Mon Oct 20 11:46:03 2014 -0400 Define errors in HAL Change-Id: I96595472e42ba61f0f3d0da17caf01a748d0422a commit 56cb5dcd93e5e849a016f63ac9d0dc245a23eb2b Author: Kevin O'Connor <koconnor@usfirst.org> Date: Fri Oct 17 10:59:29 2014 -0400 Throttle errors (1 report per second per error code) and fix issue with GetTime conflicting with GetTime from Timer.h/Timer.cpp Change-Id: Ibe4dc2e400fc4671b240b876a46959256ea65ad7 commit 71c78826e548682ecd0c1548255f8a6552cece32 Author: Kevin O'Connor <koconnor@usfirst.org> Date: Thu Oct 16 16:41:04 2014 -0400 Feed errors to DS from C++ Change-Id: I009a7798499fd93e9fdd976ff00aa74c0bd094ae commit 81030c6cee7f18a5ddf0e95c4e402a6cf7b5de6c Author: Kevin O'Connor <koconnor@usfirst.org> Date: Thu Oct 16 16:40:50 2014 -0400 Don't try to de-mangle lines without any symbols in them Change-Id: Icea02494b68f2ec9116d6cbf20a35a3a132234f8 Change-Id: If7717025b03914183736ccd95da5c9d49819a6f3
2014-10-20 17:19:28 -04:00
wpi_setGlobalErrorWithContext(status, getHALErrorMessage(status));
}
/**
* Get the current sample rate for all channels
*
* @return Sample rate.
*/
float AnalogInput::GetSampleRate()
{
int32_t status = 0;
float sampleRate = getAnalogSampleRate(&status);
Pass errors to DS in C++ and Java Squashed commit of the following: commit f317b3522e312cf7e7bb9eb0494f2f96a7f6363c Author: Kevin O'Connor <koconnor@usfirst.org> Date: Mon Oct 20 17:15:46 2014 -0400 Send unhandled exceptions back to the DS. Change-Id: I0e658fdb6d43593ee20457f20f71f4f4cd2d21c3 commit f834ef8c791945697ad483c27b4167eb917ac242 Author: Kevin O'Connor <koconnor@usfirst.org> Date: Mon Oct 20 16:05:24 2014 -0400 Add StackTrace to Java errors Change-Id: I83b162afcc5f294703705770fbcd8623b0895539 commit 02e040b0c79067ce046ada29e26004e0460fceb0 Author: Kevin O'Connor <koconnor@usfirst.org> Date: Mon Oct 20 15:07:44 2014 -0400 HAL Errors to DS in Java Change-Id: I5fb51e4066bbc26ea59ca513c03c5ec5ace98831 commit 03775ddc42b129c27fdf403f17f0796009311c3c Author: Kevin O'Connor <koconnor@usfirst.org> Date: Mon Oct 20 13:38:18 2014 -0400 Update AnalogInput to report errors for getting and setting sample rate Change-Id: I00eb78f52fc5b17a60bc84456f0ec9842cc40ef7 commit 4c10cb79612ae81e3cbb6bd4d6da8cf3b8955821 Author: Kevin O'Connor <koconnor@usfirst.org> Date: Mon Oct 20 11:46:03 2014 -0400 Define errors in HAL Change-Id: I96595472e42ba61f0f3d0da17caf01a748d0422a commit 56cb5dcd93e5e849a016f63ac9d0dc245a23eb2b Author: Kevin O'Connor <koconnor@usfirst.org> Date: Fri Oct 17 10:59:29 2014 -0400 Throttle errors (1 report per second per error code) and fix issue with GetTime conflicting with GetTime from Timer.h/Timer.cpp Change-Id: Ibe4dc2e400fc4671b240b876a46959256ea65ad7 commit 71c78826e548682ecd0c1548255f8a6552cece32 Author: Kevin O'Connor <koconnor@usfirst.org> Date: Thu Oct 16 16:41:04 2014 -0400 Feed errors to DS from C++ Change-Id: I009a7798499fd93e9fdd976ff00aa74c0bd094ae commit 81030c6cee7f18a5ddf0e95c4e402a6cf7b5de6c Author: Kevin O'Connor <koconnor@usfirst.org> Date: Thu Oct 16 16:40:50 2014 -0400 Don't try to de-mangle lines without any symbols in them Change-Id: Icea02494b68f2ec9116d6cbf20a35a3a132234f8 Change-Id: If7717025b03914183736ccd95da5c9d49819a6f3
2014-10-20 17:19:28 -04:00
wpi_setGlobalErrorWithContext(status, getHALErrorMessage(status));
return sampleRate;
}
/**
* Get the Average value for the PID Source base object.
*
* @return The average voltage.
*/
double AnalogInput::PIDGet()
{
if (StatusIsFatal()) return 0.0;
return GetAverageVoltage();
}
void AnalogInput::UpdateTable() {
if (m_table != NULL) {
m_table->PutNumber("Value", GetAverageVoltage());
}
}
void AnalogInput::StartLiveWindowMode() {
}
void AnalogInput::StopLiveWindowMode() {
}
std::string AnalogInput::GetSmartDashboardType() {
return "Analog Input";
}
void AnalogInput::InitTable(ITable *subTable) {
m_table = subTable;
UpdateTable();
}
ITable * AnalogInput::GetTable() {
return m_table;
}