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
|
|
|
|
2024-09-20 17:43:39 -07:00
|
|
|
#include <string>
|
|
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#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
|
|
|
|
2025-01-25 10:52:19 -08:00
|
|
|
wpi::SendableRegistry::Add(this, "AnalogInput", channel);
|
2013-12-15 18:30:16 -05: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;
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
2022-10-19 10:49:27 -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);
|
2022-10-19 10:49:27 -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(
|
2022-10-15 16:33:14 -07:00
|
|
|
"Value", [=, this] { return GetAverageVoltage(); }, nullptr);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|