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.
|
2014-06-12 18:07:45 -04:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/AnalogOutput.h"
|
2014-06-12 18:07:45 -04:00
|
|
|
|
2015-11-15 14:49:50 -08:00
|
|
|
#include <limits>
|
2018-09-24 00:08:25 -07:00
|
|
|
#include <utility>
|
2015-06-30 15:01:20 -04:00
|
|
|
|
2019-11-08 22:53:20 -08:00
|
|
|
#include <hal/AnalogOutput.h>
|
|
|
|
|
#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"
|
2016-09-05 13:55:31 -07:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
using namespace frc;
|
|
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
AnalogOutput::AnalogOutput(int channel) {
|
2018-05-23 20:22:30 -07:00
|
|
|
if (!SensorUtil::CheckAnalogOutputChannel(channel)) {
|
2021-05-23 19:33:33 -07:00
|
|
|
throw FRC_MakeError(err::ChannelIndexOutOfRange, "Channel {}", channel);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2014-06-12 18:07:45 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
m_channel = channel;
|
2014-06-12 18:07:45 -04:00
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_PortHandle port = HAL_GetPort(m_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_InitializeAnalogOutputPort(port, stackTrace.c_str(), &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Channel {}", channel);
|
2014-06-12 18:07:45 -04:00
|
|
|
|
2019-10-29 21:34:10 -07:00
|
|
|
HAL_Report(HALUsageReporting::kResourceType_AnalogOutput, m_channel + 1);
|
2021-06-15 23:06:03 -07:00
|
|
|
wpi::SendableRegistry::AddLW(this, "AnalogOutput", m_channel);
|
2014-06-12 18:07:45 -04:00
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
AnalogOutput::~AnalogOutput() {
|
|
|
|
|
HAL_FreeAnalogOutputPort(m_port);
|
|
|
|
|
}
|
2014-06-12 18:07:45 -04:00
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
void AnalogOutput::SetVoltage(double voltage) {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_SetAnalogOutput(m_port, voltage, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Channel {}", m_channel);
|
2014-06-12 18:07:45 -04:00
|
|
|
}
|
|
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
double AnalogOutput::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_GetAnalogOutput(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;
|
2014-06-12 18:07:45 -04:00
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
int AnalogOutput::GetChannel() const {
|
|
|
|
|
return m_channel;
|
|
|
|
|
}
|
2018-05-31 20:47:15 -07:00
|
|
|
|
2021-06-13 16:38:05 -07:00
|
|
|
void AnalogOutput::InitSendable(wpi::SendableBuilder& builder) {
|
2017-12-04 23:28:33 -08:00
|
|
|
builder.SetSmartDashboardType("Analog Output");
|
2020-06-27 20:39:00 -07:00
|
|
|
builder.AddDoubleProperty(
|
2022-10-15 16:33:14 -07:00
|
|
|
"Value", [=, this] { return GetVoltage(); },
|
|
|
|
|
[=, this](double value) { SetVoltage(value); });
|
2014-06-12 18:07:45 -04:00
|
|
|
}
|