Files
allwpilib/wpilibc/src/main/native/cpp/AnalogInput.cpp

266 lines
6.3 KiB
C++
Raw Normal View History

// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#include "frc/AnalogInput.h"
#include <hal/AnalogAccumulator.h>
#include <hal/AnalogInput.h>
#include <hal/FRCUsageReporting.h>
#include <hal/HALBase.h>
#include <hal/Ports.h>
#include "frc/SensorUtil.h"
#include "frc/Timer.h"
#include "frc/WPIErrors.h"
#include "frc/smartdashboard/SendableBuilder.h"
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
AnalogInput::AnalogInput(int channel) {
if (!SensorUtil::CheckAnalogInputChannel(channel)) {
2017-12-01 21:50:24 -08:00
wpi_setWPIErrorWithContext(ChannelIndexOutOfRange,
"Analog Input " + wpi::Twine(channel));
return;
}
m_channel = channel;
HAL_PortHandle port = HAL_GetPort(channel);
int32_t status = 0;
m_port = HAL_InitializeAnalogInputPort(port, &status);
if (status != 0) {
wpi_setHALErrorWithRange(status, 0, HAL_GetNumAnalogInputs(), channel);
m_channel = std::numeric_limits<int>::max();
m_port = HAL_kInvalidHandle;
return;
}
HAL_Report(HALUsageReporting::kResourceType_AnalogChannel, channel + 1);
SendableRegistry::GetInstance().AddLW(this, "AnalogInput", channel);
}
AnalogInput::~AnalogInput() {
HAL_FreeAnalogInputPort(m_port);
}
int AnalogInput::GetValue() const {
if (StatusIsFatal()) {
return 0;
}
int32_t status = 0;
int value = HAL_GetAnalogValue(m_port, &status);
wpi_setHALError(status);
return value;
}
int AnalogInput::GetAverageValue() const {
if (StatusIsFatal()) {
return 0;
}
int32_t status = 0;
int value = HAL_GetAnalogAverageValue(m_port, &status);
wpi_setHALError(status);
return value;
}
double AnalogInput::GetVoltage() const {
if (StatusIsFatal()) {
return 0.0;
}
int32_t status = 0;
double voltage = HAL_GetAnalogVoltage(m_port, &status);
wpi_setHALError(status);
return voltage;
}
double AnalogInput::GetAverageVoltage() const {
if (StatusIsFatal()) {
return 0.0;
}
int32_t status = 0;
double voltage = HAL_GetAnalogAverageVoltage(m_port, &status);
wpi_setHALError(status);
return voltage;
}
int AnalogInput::GetChannel() const {
if (StatusIsFatal()) {
return 0;
}
return m_channel;
}
void AnalogInput::SetAverageBits(int bits) {
if (StatusIsFatal()) {
return;
}
int32_t status = 0;
HAL_SetAnalogAverageBits(m_port, bits, &status);
wpi_setHALError(status);
}
int AnalogInput::GetAverageBits() const {
int32_t status = 0;
int averageBits = HAL_GetAnalogAverageBits(m_port, &status);
wpi_setHALError(status);
return averageBits;
}
void AnalogInput::SetOversampleBits(int bits) {
if (StatusIsFatal()) {
return;
}
int32_t status = 0;
HAL_SetAnalogOversampleBits(m_port, bits, &status);
wpi_setHALError(status);
}
int AnalogInput::GetOversampleBits() const {
if (StatusIsFatal()) {
return 0;
}
int32_t status = 0;
int oversampleBits = HAL_GetAnalogOversampleBits(m_port, &status);
wpi_setHALError(status);
return oversampleBits;
}
int AnalogInput::GetLSBWeight() const {
if (StatusIsFatal()) {
return 0;
}
int32_t status = 0;
int lsbWeight = HAL_GetAnalogLSBWeight(m_port, &status);
wpi_setHALError(status);
return lsbWeight;
}
int AnalogInput::GetOffset() const {
if (StatusIsFatal()) {
return 0;
}
int32_t status = 0;
int offset = HAL_GetAnalogOffset(m_port, &status);
wpi_setHALError(status);
return offset;
}
bool AnalogInput::IsAccumulatorChannel() const {
if (StatusIsFatal()) {
return false;
}
int32_t status = 0;
bool isAccum = HAL_IsAccumulatorChannel(m_port, &status);
wpi_setHALError(status);
return isAccum;
}
void AnalogInput::InitAccumulator() {
if (StatusIsFatal()) {
return;
}
m_accumulatorOffset = 0;
int32_t status = 0;
HAL_InitAccumulator(m_port, &status);
wpi_setHALError(status);
}
void AnalogInput::SetAccumulatorInitialValue(int64_t initialValue) {
if (StatusIsFatal()) {
return;
}
m_accumulatorOffset = initialValue;
}
void AnalogInput::ResetAccumulator() {
if (StatusIsFatal()) {
return;
}
int32_t status = 0;
HAL_ResetAccumulator(m_port, &status);
wpi_setHALError(status);
if (!StatusIsFatal()) {
// Wait until the next sample, so the next call to GetAccumulator*()
// won't have old values.
const double sampleTime = 1.0 / GetSampleRate();
const double overSamples = 1 << GetOversampleBits();
const double averageSamples = 1 << GetAverageBits();
Wait(sampleTime * overSamples * averageSamples);
}
}
void AnalogInput::SetAccumulatorCenter(int center) {
if (StatusIsFatal()) {
return;
}
int32_t status = 0;
HAL_SetAccumulatorCenter(m_port, center, &status);
wpi_setHALError(status);
}
void AnalogInput::SetAccumulatorDeadband(int deadband) {
if (StatusIsFatal()) {
return;
}
int32_t status = 0;
HAL_SetAccumulatorDeadband(m_port, deadband, &status);
wpi_setHALError(status);
}
int64_t AnalogInput::GetAccumulatorValue() const {
if (StatusIsFatal()) {
return 0;
}
int32_t status = 0;
int64_t value = HAL_GetAccumulatorValue(m_port, &status);
wpi_setHALError(status);
return value + m_accumulatorOffset;
}
int64_t AnalogInput::GetAccumulatorCount() const {
if (StatusIsFatal()) {
return 0;
}
int32_t status = 0;
int64_t count = HAL_GetAccumulatorCount(m_port, &status);
wpi_setHALError(status);
return count;
}
void AnalogInput::GetAccumulatorOutput(int64_t& value, int64_t& count) const {
if (StatusIsFatal()) {
return;
}
int32_t status = 0;
HAL_GetAccumulatorOutput(m_port, &value, &count, &status);
wpi_setHALError(status);
artf4154: Get rid of raw pointers in C++. This deals with the majority of the user-facing code in wpilibC++Devices and a substantial portion of it in wpilibC++. wpilibC++Sim and wpilibC++IntegrationTests are untouched except where it is necessary to make them work with the rest of the libraries. There is still a lot to do in the following areas: -The HAL (which we may not want to touch at all). -The I2C, Serial, and SPI interfaces in wpilibC++Devices, which I haven't gotten around to doing yet. -Most wpilibC++Devices classes have void* pointers for interacting with the HAL. -InterruptableSensorBase passes a void *params for the interrupt handler. -I haven't converted all the const char* to std::strings. -There are plenty of other cases of raw pointers still existing. -This doesn't fall directly under raw pointer stuff, but move syntax and rvalue references could be introduced in many places. -I haven't touched vision code. -The Resource classes conflict (one is in the hal, the other in wpilibC++). Someone should figure out a more permanent fix (eg, just renaming them), then doing what I did (making a new namespace for one of them, essentially the same as renaming it). A few other things: -I created a NullDeleter class which is marked as deprecated. What this does is it can be passed as the deleter to a std::shared_ptr so that when you are converting raw pointers to shared_ptrs the shared_ptr doesn't do any deletion if someone else owns the raw pointer. This should only be used in making old raw pointer UIs. -I had to alter the build.gradle so that it did not emit errors when deprecated functions called deprecated functions. Unfortunately, gradle doesn't appear to be actually printing out gcc warnigns for some reason. The best way I have found to fix this is to patch the toolchains (https://bitbucket.org/byteit101/toolchain-builder/pull-request/5/make-gcc-not-throw-warnings-for-nested/diff) so that a deprecated function calling a deprecated function is fine but a non-deprecated function calling a deprecated function will throw a warning (which we then elevate with -Werror). I believe that clang deals with this properly, although I have not tried it myself. Change-Id: Ib8090c66893576fe73654f4e9d268f9d37be06a2
2015-06-30 15:01:20 -04:00
value += m_accumulatorOffset;
}
void AnalogInput::SetSampleRate(double samplesPerSecond) {
int32_t status = 0;
HAL_SetAnalogSampleRate(samplesPerSecond, &status);
wpi_setGlobalHALError(status);
}
double AnalogInput::GetSampleRate() {
int32_t status = 0;
double sampleRate = HAL_GetAnalogSampleRate(&status);
wpi_setGlobalHALError(status);
return sampleRate;
}
void AnalogInput::SetSimDevice(HAL_SimDeviceHandle device) {
HAL_SetAnalogInputSimDevice(m_port, device);
}
void AnalogInput::InitSendable(SendableBuilder& builder) {
builder.SetSmartDashboardType("Analog Input");
builder.AddDoubleProperty(
"Value", [=]() { return GetAverageVoltage(); }, nullptr);
}