Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 1 | // Copyright (c) FIRST and other WPILib contributors. |
| 2 | // Open Source Software; you can modify and/or share it under the terms of |
| 3 | // the WPILib BSD license file in the root directory of this project. |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 4 | |
| 5 | #include "frc/AnalogOutput.h" |
| 6 | |
| 7 | #include <limits> |
| 8 | #include <utility> |
| 9 | |
| 10 | #include <hal/AnalogOutput.h> |
| 11 | #include <hal/FRCUsageReporting.h> |
| 12 | #include <hal/HALBase.h> |
| 13 | #include <hal/Ports.h> |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 14 | #include <wpi/StackTrace.h> |
| 15 | #include <wpi/sendable/SendableBuilder.h> |
| 16 | #include <wpi/sendable/SendableRegistry.h> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 17 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 18 | #include "frc/Errors.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 19 | #include "frc/SensorUtil.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 20 | |
| 21 | using namespace frc; |
| 22 | |
| 23 | AnalogOutput::AnalogOutput(int channel) { |
| 24 | if (!SensorUtil::CheckAnalogOutputChannel(channel)) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 25 | throw FRC_MakeError(err::ChannelIndexOutOfRange, "Channel {}", channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | m_channel = channel; |
| 29 | |
| 30 | HAL_PortHandle port = HAL_GetPort(m_channel); |
| 31 | int32_t status = 0; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 32 | std::string stackTrace = wpi::GetStackTrace(1); |
| 33 | m_port = HAL_InitializeAnalogOutputPort(port, stackTrace.c_str(), &status); |
| 34 | FRC_CheckErrorStatus(status, "Channel {}", channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 35 | |
| 36 | HAL_Report(HALUsageReporting::kResourceType_AnalogOutput, m_channel + 1); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 37 | wpi::SendableRegistry::AddLW(this, "AnalogOutput", m_channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 40 | AnalogOutput::~AnalogOutput() { |
| 41 | HAL_FreeAnalogOutputPort(m_port); |
| 42 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 43 | |
| 44 | void AnalogOutput::SetVoltage(double voltage) { |
| 45 | int32_t status = 0; |
| 46 | HAL_SetAnalogOutput(m_port, voltage, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 47 | FRC_CheckErrorStatus(status, "Channel {}", m_channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | double AnalogOutput::GetVoltage() const { |
| 51 | int32_t status = 0; |
| 52 | double voltage = HAL_GetAnalogOutput(m_port, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 53 | FRC_CheckErrorStatus(status, "Channel {}", m_channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 54 | return voltage; |
| 55 | } |
| 56 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 57 | int AnalogOutput::GetChannel() const { |
| 58 | return m_channel; |
| 59 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 60 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 61 | void AnalogOutput::InitSendable(wpi::SendableBuilder& builder) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 62 | builder.SetSmartDashboardType("Analog Output"); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 63 | builder.AddDoubleProperty( |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 64 | "Value", [=] { return GetVoltage(); }, |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 65 | [=](double value) { SetVoltage(value); }); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 66 | } |