2014-06-12 18:07:45 -04:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2020-06-27 20:39:00 -07:00
|
|
|
/* Copyright (c) 2014-2020 FIRST. All Rights Reserved. */
|
2014-06-12 18:07:45 -04:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
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>
|
2017-08-27 00:11:52 -07:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/SensorUtil.h"
|
|
|
|
|
#include "frc/WPIErrors.h"
|
|
|
|
|
#include "frc/smartdashboard/SendableBuilder.h"
|
2019-09-14 15:22:54 -05:00
|
|
|
#include "frc/smartdashboard/SendableRegistry.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)) {
|
2017-12-01 21:50:24 -08:00
|
|
|
wpi_setWPIErrorWithContext(ChannelIndexOutOfRange,
|
2018-04-29 23:33:19 -07:00
|
|
|
"analog output " + wpi::Twine(channel));
|
2016-09-06 00:01:45 -07:00
|
|
|
m_channel = std::numeric_limits<int>::max();
|
2016-07-09 00:24:26 -07:00
|
|
|
m_port = HAL_kInvalidHandle;
|
2015-06-25 15:07:55 -04:00
|
|
|
return;
|
|
|
|
|
}
|
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;
|
2016-07-09 00:24:26 -07:00
|
|
|
m_port = HAL_InitializeAnalogOutputPort(port, &status);
|
2016-06-27 11:32:40 -07:00
|
|
|
if (status != 0) {
|
2019-11-08 22:53:20 -08:00
|
|
|
wpi_setHALErrorWithRange(status, 0, HAL_GetNumAnalogOutputs(), channel);
|
2016-09-06 00:01:45 -07:00
|
|
|
m_channel = std::numeric_limits<int>::max();
|
2016-07-09 00:24:26 -07:00
|
|
|
m_port = HAL_kInvalidHandle;
|
2016-06-27 11:32:40 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2014-06-12 18:07:45 -04:00
|
|
|
|
2019-10-29 21:34:10 -07:00
|
|
|
HAL_Report(HALUsageReporting::kResourceType_AnalogOutput, m_channel + 1);
|
2019-09-14 15:22:54 -05:00
|
|
|
SendableRegistry::GetInstance().AddLW(this, "AnalogOutput", m_channel);
|
2014-06-12 18:07:45 -04:00
|
|
|
}
|
|
|
|
|
|
2016-07-09 00:24:26 -07: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);
|
2014-06-12 18:07:45 -04:00
|
|
|
|
2019-11-08 22:53:20 -08:00
|
|
|
wpi_setHALError(status);
|
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);
|
2014-06-12 18:07:45 -04:00
|
|
|
|
2019-11-08 22:53:20 -08:00
|
|
|
wpi_setHALError(status);
|
2014-06-12 18:07:45 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
return voltage;
|
2014-06-12 18:07:45 -04:00
|
|
|
}
|
|
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
int AnalogOutput::GetChannel() { return m_channel; }
|
|
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
void AnalogOutput::InitSendable(SendableBuilder& builder) {
|
|
|
|
|
builder.SetSmartDashboardType("Analog Output");
|
2020-06-27 20:39:00 -07:00
|
|
|
builder.AddDoubleProperty(
|
|
|
|
|
"Value", [=]() { return GetVoltage(); },
|
|
|
|
|
[=](double value) { SetVoltage(value); });
|
2014-06-12 18:07:45 -04:00
|
|
|
}
|