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/DigitalOutput.h" |
| 6 | |
| 7 | #include <limits> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 8 | |
| 9 | #include <hal/DIO.h> |
| 10 | #include <hal/FRCUsageReporting.h> |
| 11 | #include <hal/HALBase.h> |
| 12 | #include <hal/Ports.h> |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 13 | #include <wpi/StackTrace.h> |
| 14 | #include <wpi/sendable/SendableBuilder.h> |
| 15 | #include <wpi/sendable/SendableRegistry.h> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 16 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 17 | #include "frc/Errors.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 18 | #include "frc/SensorUtil.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 19 | |
| 20 | using namespace frc; |
| 21 | |
| 22 | DigitalOutput::DigitalOutput(int channel) { |
| 23 | m_pwmGenerator = HAL_kInvalidHandle; |
| 24 | if (!SensorUtil::CheckDigitalChannel(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 | m_channel = channel; |
| 28 | |
| 29 | int32_t status = 0; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 30 | std::string stackTrace = wpi::GetStackTrace(1); |
| 31 | m_handle = HAL_InitializeDIOPort(HAL_GetPort(channel), false, |
| 32 | stackTrace.c_str(), &status); |
| 33 | FRC_CheckErrorStatus(status, "Channel {}", channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 34 | |
| 35 | HAL_Report(HALUsageReporting::kResourceType_DigitalOutput, channel + 1); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 36 | wpi::SendableRegistry::AddLW(this, "DigitalOutput", channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | DigitalOutput::~DigitalOutput() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 40 | // Disable the PWM in case it was running. |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 41 | try { |
| 42 | DisablePWM(); |
| 43 | } catch (const RuntimeError& e) { |
| 44 | e.Report(); |
| 45 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 46 | |
| 47 | HAL_FreeDIOPort(m_handle); |
| 48 | } |
| 49 | |
| 50 | void DigitalOutput::Set(bool value) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 51 | int32_t status = 0; |
| 52 | HAL_SetDIO(m_handle, value, &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 | } |
| 55 | |
| 56 | bool DigitalOutput::Get() const { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 57 | int32_t status = 0; |
| 58 | bool val = HAL_GetDIO(m_handle, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 59 | FRC_CheckErrorStatus(status, "Channel {}", m_channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 60 | return val; |
| 61 | } |
| 62 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 63 | HAL_Handle DigitalOutput::GetPortHandleForRouting() const { |
| 64 | return m_handle; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 67 | AnalogTriggerType DigitalOutput::GetAnalogTriggerTypeForRouting() const { |
| 68 | return static_cast<AnalogTriggerType>(0); |
| 69 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 70 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 71 | bool DigitalOutput::IsAnalogTrigger() const { |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | int DigitalOutput::GetChannel() const { |
| 76 | return m_channel; |
| 77 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 78 | |
| 79 | void DigitalOutput::Pulse(double length) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 80 | int32_t status = 0; |
| 81 | HAL_Pulse(m_handle, length, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 82 | FRC_CheckErrorStatus(status, "Channel {}", m_channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | bool DigitalOutput::IsPulsing() const { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 86 | int32_t status = 0; |
| 87 | bool value = HAL_IsPulsing(m_handle, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 88 | FRC_CheckErrorStatus(status, "Channel {}", m_channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 89 | return value; |
| 90 | } |
| 91 | |
| 92 | void DigitalOutput::SetPWMRate(double rate) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 93 | int32_t status = 0; |
| 94 | HAL_SetDigitalPWMRate(rate, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 95 | FRC_CheckErrorStatus(status, "Channel {}", m_channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void DigitalOutput::EnablePWM(double initialDutyCycle) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 99 | if (m_pwmGenerator != HAL_kInvalidHandle) { |
| 100 | return; |
| 101 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 102 | |
| 103 | int32_t status = 0; |
| 104 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 105 | m_pwmGenerator = HAL_AllocateDigitalPWM(&status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 106 | FRC_CheckErrorStatus(status, "Channel {}", m_channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 107 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 108 | HAL_SetDigitalPWMDutyCycle(m_pwmGenerator, initialDutyCycle, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 109 | FRC_CheckErrorStatus(status, "Channel {}", m_channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 110 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 111 | HAL_SetDigitalPWMOutputChannel(m_pwmGenerator, m_channel, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 112 | FRC_CheckErrorStatus(status, "Channel {}", m_channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | void DigitalOutput::DisablePWM() { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 116 | if (m_pwmGenerator == HAL_kInvalidHandle) { |
| 117 | return; |
| 118 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 119 | |
| 120 | int32_t status = 0; |
| 121 | |
| 122 | // Disable the output by routing to a dead bit. |
| 123 | HAL_SetDigitalPWMOutputChannel(m_pwmGenerator, SensorUtil::kDigitalChannels, |
| 124 | &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 125 | FRC_CheckErrorStatus(status, "Channel {}", m_channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 126 | |
| 127 | HAL_FreeDigitalPWM(m_pwmGenerator, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 128 | FRC_CheckErrorStatus(status, "Channel {}", m_channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 129 | |
| 130 | m_pwmGenerator = HAL_kInvalidHandle; |
| 131 | } |
| 132 | |
| 133 | void DigitalOutput::UpdateDutyCycle(double dutyCycle) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 134 | int32_t status = 0; |
| 135 | HAL_SetDigitalPWMDutyCycle(m_pwmGenerator, dutyCycle, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 136 | FRC_CheckErrorStatus(status, "Channel {}", m_channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | void DigitalOutput::SetSimDevice(HAL_SimDeviceHandle device) { |
| 140 | HAL_SetDIOSimDevice(m_handle, device); |
| 141 | } |
| 142 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 143 | void DigitalOutput::InitSendable(wpi::SendableBuilder& builder) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 144 | builder.SetSmartDashboardType("Digital Output"); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 145 | builder.AddBooleanProperty( |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 146 | "Value", [=] { return Get(); }, [=](bool value) { Set(value); }); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 147 | } |