Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/ |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame] | 2 | /* Copyright (c) 2014-2019 FIRST. All Rights Reserved. */ |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #include "frc/AnalogOutput.h" |
| 9 | |
| 10 | #include <limits> |
| 11 | #include <utility> |
| 12 | |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame^] | 13 | #include <hal/AnalogOutput.h> |
| 14 | #include <hal/FRCUsageReporting.h> |
| 15 | #include <hal/HALBase.h> |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 16 | #include <hal/Ports.h> |
| 17 | |
| 18 | #include "frc/SensorUtil.h" |
| 19 | #include "frc/WPIErrors.h" |
| 20 | #include "frc/smartdashboard/SendableBuilder.h" |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame] | 21 | #include "frc/smartdashboard/SendableRegistry.h" |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 22 | |
| 23 | using namespace frc; |
| 24 | |
| 25 | AnalogOutput::AnalogOutput(int channel) { |
| 26 | if (!SensorUtil::CheckAnalogOutputChannel(channel)) { |
| 27 | wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, |
| 28 | "analog output " + wpi::Twine(channel)); |
| 29 | m_channel = std::numeric_limits<int>::max(); |
| 30 | m_port = HAL_kInvalidHandle; |
| 31 | return; |
| 32 | } |
| 33 | |
| 34 | m_channel = channel; |
| 35 | |
| 36 | HAL_PortHandle port = HAL_GetPort(m_channel); |
| 37 | int32_t status = 0; |
| 38 | m_port = HAL_InitializeAnalogOutputPort(port, &status); |
| 39 | if (status != 0) { |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame^] | 40 | wpi_setHALErrorWithRange(status, 0, HAL_GetNumAnalogOutputs(), channel); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 41 | m_channel = std::numeric_limits<int>::max(); |
| 42 | m_port = HAL_kInvalidHandle; |
| 43 | return; |
| 44 | } |
| 45 | |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame^] | 46 | HAL_Report(HALUsageReporting::kResourceType_AnalogOutput, m_channel + 1); |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame] | 47 | SendableRegistry::GetInstance().AddLW(this, "AnalogOutput", m_channel); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | AnalogOutput::~AnalogOutput() { HAL_FreeAnalogOutputPort(m_port); } |
| 51 | |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 52 | void AnalogOutput::SetVoltage(double voltage) { |
| 53 | int32_t status = 0; |
| 54 | HAL_SetAnalogOutput(m_port, voltage, &status); |
| 55 | |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame^] | 56 | wpi_setHALError(status); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | double AnalogOutput::GetVoltage() const { |
| 60 | int32_t status = 0; |
| 61 | double voltage = HAL_GetAnalogOutput(m_port, &status); |
| 62 | |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame^] | 63 | wpi_setHALError(status); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 64 | |
| 65 | return voltage; |
| 66 | } |
| 67 | |
| 68 | int AnalogOutput::GetChannel() { return m_channel; } |
| 69 | |
| 70 | void AnalogOutput::InitSendable(SendableBuilder& builder) { |
| 71 | builder.SetSmartDashboardType("Analog Output"); |
| 72 | builder.AddDoubleProperty("Value", [=]() { return GetVoltage(); }, |
| 73 | [=](double value) { SetVoltage(value); }); |
| 74 | } |