2020-12-26 14:12:05 -08:00
|
|
|
// 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.
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/AnalogInput.h"
|
2016-09-05 13:55:31 -07:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include <hal/AnalogAccumulator.h>
|
|
|
|
|
#include <hal/AnalogInput.h>
|
2019-11-08 22:53:20 -08:00
|
|
|
#include <hal/FRCUsageReporting.h>
|
|
|
|
|
#include <hal/HALBase.h>
|
2018-07-20 00:03:45 -07:00
|
|
|
#include <hal/Ports.h>
|
2021-05-01 10:28:30 -07:00
|
|
|
#include <wpi/StackTrace.h>
|
2021-06-13 16:38:05 -07:00
|
|
|
#include <wpi/sendable/SendableBuilder.h>
|
|
|
|
|
#include <wpi/sendable/SendableRegistry.h>
|
2017-08-27 00:11:52 -07:00
|
|
|
|
2021-04-18 20:35:29 -07:00
|
|
|
#include "frc/Errors.h"
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/SensorUtil.h"
|
|
|
|
|
#include "frc/Timer.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
using namespace frc;
|
|
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
AnalogInput::AnalogInput(int channel) {
|
2018-05-23 20:22:30 -07:00
|
|
|
if (!SensorUtil::CheckAnalogInputChannel(channel)) {
|
2021-05-23 19:33:33 -07:00
|
|
|
throw FRC_MakeError(err::ChannelIndexOutOfRange, "Channel {}", channel);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_channel = channel;
|
|
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_PortHandle port = HAL_GetPort(channel);
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2021-05-01 10:28:30 -07:00
|
|
|
std::string stackTrace = wpi::GetStackTrace(1);
|
|
|
|
|
m_port = HAL_InitializeAnalogInputPort(port, stackTrace.c_str(), &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Channel {}", channel);
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2019-10-29 21:34:10 -07:00
|
|
|
HAL_Report(HALUsageReporting::kResourceType_AnalogChannel, channel + 1);
|
2019-09-14 15:22:54 -05:00
|
|
|
|
2021-06-15 23:06:03 -07:00
|
|
|
wpi::SendableRegistry::AddLW(this, "AnalogInput", channel);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
AnalogInput::~AnalogInput() {
|
|
|
|
|
HAL_FreeAnalogInputPort(m_port);
|
|
|
|
|
}
|
2018-09-24 00:08:25 -07:00
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
int AnalogInput::GetValue() const {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2016-09-06 00:01:45 -07:00
|
|
|
int value = HAL_GetAnalogValue(m_port, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Channel {}", m_channel);
|
2015-06-25 15:07:55 -04:00
|
|
|
return value;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
int AnalogInput::GetAverageValue() const {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2016-09-06 00:01:45 -07:00
|
|
|
int value = HAL_GetAnalogAverageValue(m_port, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Channel {}", m_channel);
|
2015-06-25 15:07:55 -04:00
|
|
|
return value;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
double AnalogInput::GetVoltage() const {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2016-11-20 07:25:03 -08:00
|
|
|
double voltage = HAL_GetAnalogVoltage(m_port, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Channel {}", m_channel);
|
2015-06-25 15:07:55 -04:00
|
|
|
return voltage;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
double AnalogInput::GetAverageVoltage() const {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2016-11-20 07:25:03 -08:00
|
|
|
double voltage = HAL_GetAnalogAverageVoltage(m_port, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Channel {}", m_channel);
|
2015-06-25 15:07:55 -04:00
|
|
|
return voltage;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
int AnalogInput::GetChannel() const {
|
2015-06-25 15:07:55 -04:00
|
|
|
return m_channel;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
void AnalogInput::SetAverageBits(int bits) {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_SetAnalogAverageBits(m_port, bits, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Channel {}", m_channel);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
int AnalogInput::GetAverageBits() const {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2016-09-06 00:01:45 -07:00
|
|
|
int averageBits = HAL_GetAnalogAverageBits(m_port, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Channel {}", m_channel);
|
2015-06-25 15:07:55 -04:00
|
|
|
return averageBits;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
void AnalogInput::SetOversampleBits(int bits) {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_SetAnalogOversampleBits(m_port, bits, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Channel {}", m_channel);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
int AnalogInput::GetOversampleBits() const {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2016-09-06 00:01:45 -07:00
|
|
|
int oversampleBits = HAL_GetAnalogOversampleBits(m_port, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Channel {}", m_channel);
|
2015-06-25 15:07:55 -04:00
|
|
|
return oversampleBits;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
int AnalogInput::GetLSBWeight() const {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
int lsbWeight = HAL_GetAnalogLSBWeight(m_port, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Channel {}", m_channel);
|
2018-05-31 20:47:15 -07:00
|
|
|
return lsbWeight;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int AnalogInput::GetOffset() const {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
int offset = HAL_GetAnalogOffset(m_port, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Channel {}", m_channel);
|
2018-05-31 20:47:15 -07:00
|
|
|
return offset;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
bool AnalogInput::IsAccumulatorChannel() const {
|
|
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
bool isAccum = HAL_IsAccumulatorChannel(m_port, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Channel {}", m_channel);
|
2015-06-25 15:07:55 -04:00
|
|
|
return isAccum;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void AnalogInput::InitAccumulator() {
|
|
|
|
|
m_accumulatorOffset = 0;
|
|
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_InitAccumulator(m_port, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Channel {}", m_channel);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void AnalogInput::SetAccumulatorInitialValue(int64_t initialValue) {
|
|
|
|
|
m_accumulatorOffset = initialValue;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void AnalogInput::ResetAccumulator() {
|
|
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_ResetAccumulator(m_port, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Channel {}", m_channel);
|
2021-04-18 20:35:29 -07:00
|
|
|
|
|
|
|
|
// 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();
|
2021-05-28 22:06:59 -07:00
|
|
|
Wait(units::second_t{sampleTime * overSamples * averageSamples});
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
void AnalogInput::SetAccumulatorCenter(int center) {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_SetAccumulatorCenter(m_port, center, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Channel {}", m_channel);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
void AnalogInput::SetAccumulatorDeadband(int deadband) {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_SetAccumulatorDeadband(m_port, deadband, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Channel {}", m_channel);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
int64_t AnalogInput::GetAccumulatorValue() const {
|
|
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
int64_t value = HAL_GetAccumulatorValue(m_port, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Channel {}", m_channel);
|
2015-06-25 15:07:55 -04:00
|
|
|
return value + m_accumulatorOffset;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-07-12 10:45:14 -07:00
|
|
|
int64_t AnalogInput::GetAccumulatorCount() const {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2016-07-12 10:45:14 -07:00
|
|
|
int64_t count = HAL_GetAccumulatorCount(m_port, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Channel {}", m_channel);
|
2015-06-25 15:07:55 -04:00
|
|
|
return count;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-07-12 10:45:14 -07:00
|
|
|
void AnalogInput::GetAccumulatorOutput(int64_t& value, int64_t& count) const {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_GetAccumulatorOutput(m_port, &value, &count, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Channel {}", m_channel);
|
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;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
void AnalogInput::SetSampleRate(double samplesPerSecond) {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_SetAnalogSampleRate(samplesPerSecond, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "{}", "SetSampleRate");
|
2014-07-21 16:32:36 -04:00
|
|
|
}
|
|
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
double AnalogInput::GetSampleRate() {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2016-11-20 07:25:03 -08:00
|
|
|
double sampleRate = HAL_GetAnalogSampleRate(&status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "{}", "GetSampleRate");
|
2015-06-25 15:07:55 -04:00
|
|
|
return sampleRate;
|
2014-07-21 16:32:36 -04:00
|
|
|
}
|
|
|
|
|
|
2019-10-04 22:56:24 -07:00
|
|
|
void AnalogInput::SetSimDevice(HAL_SimDeviceHandle device) {
|
|
|
|
|
HAL_SetAnalogInputSimDevice(m_port, device);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 16:38:05 -07:00
|
|
|
void AnalogInput::InitSendable(wpi::SendableBuilder& builder) {
|
2017-12-04 23:28:33 -08:00
|
|
|
builder.SetSmartDashboardType("Analog Input");
|
2020-06-27 20:39:00 -07:00
|
|
|
builder.AddDoubleProperty(
|
2021-06-12 08:06:45 -07:00
|
|
|
"Value", [=] { return GetAverageVoltage(); }, nullptr);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|