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 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 79 | void DigitalOutput::Pulse(units::second_t pulseLength) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 80 | int32_t status = 0; |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 81 | HAL_Pulse(m_handle, pulseLength.to<double>(), &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 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 98 | void DigitalOutput::EnablePPS(double dutyCycle) { |
| 99 | if (m_pwmGenerator != HAL_kInvalidHandle) { |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | int32_t status = 0; |
| 104 | |
| 105 | m_pwmGenerator = HAL_AllocateDigitalPWM(&status); |
| 106 | FRC_CheckErrorStatus(status, "Channel {}", m_channel); |
| 107 | |
| 108 | HAL_SetDigitalPWMPPS(m_pwmGenerator, dutyCycle, &status); |
| 109 | FRC_CheckErrorStatus(status, "Channel {}", m_channel); |
| 110 | |
| 111 | HAL_SetDigitalPWMOutputChannel(m_pwmGenerator, m_channel, &status); |
| 112 | FRC_CheckErrorStatus(status, "Channel {}", m_channel); |
| 113 | } |
| 114 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 115 | void DigitalOutput::EnablePWM(double initialDutyCycle) { |
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 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 122 | m_pwmGenerator = HAL_AllocateDigitalPWM(&status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 123 | FRC_CheckErrorStatus(status, "Channel {}", m_channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 124 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 125 | HAL_SetDigitalPWMDutyCycle(m_pwmGenerator, initialDutyCycle, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 126 | FRC_CheckErrorStatus(status, "Channel {}", m_channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 127 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 128 | HAL_SetDigitalPWMOutputChannel(m_pwmGenerator, m_channel, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 129 | FRC_CheckErrorStatus(status, "Channel {}", m_channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void DigitalOutput::DisablePWM() { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 133 | if (m_pwmGenerator == HAL_kInvalidHandle) { |
| 134 | return; |
| 135 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 136 | |
| 137 | int32_t status = 0; |
| 138 | |
| 139 | // Disable the output by routing to a dead bit. |
| 140 | HAL_SetDigitalPWMOutputChannel(m_pwmGenerator, SensorUtil::kDigitalChannels, |
| 141 | &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 142 | FRC_CheckErrorStatus(status, "Channel {}", m_channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 143 | |
| 144 | HAL_FreeDigitalPWM(m_pwmGenerator, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 145 | FRC_CheckErrorStatus(status, "Channel {}", m_channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 146 | |
| 147 | m_pwmGenerator = HAL_kInvalidHandle; |
| 148 | } |
| 149 | |
| 150 | void DigitalOutput::UpdateDutyCycle(double dutyCycle) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 151 | int32_t status = 0; |
| 152 | HAL_SetDigitalPWMDutyCycle(m_pwmGenerator, dutyCycle, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 153 | FRC_CheckErrorStatus(status, "Channel {}", m_channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | void DigitalOutput::SetSimDevice(HAL_SimDeviceHandle device) { |
| 157 | HAL_SetDIOSimDevice(m_handle, device); |
| 158 | } |
| 159 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 160 | void DigitalOutput::InitSendable(wpi::SendableBuilder& builder) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 161 | builder.SetSmartDashboardType("Digital Output"); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 162 | builder.AddBooleanProperty( |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 163 | "Value", [=, this] { return Get(); }, |
| 164 | [=, this](bool value) { Set(value); }); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 165 | } |