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/Relay.h" |
| 6 | |
| 7 | #include <utility> |
| 8 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 9 | #include <fmt/format.h> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 10 | #include <hal/FRCUsageReporting.h> |
| 11 | #include <hal/HALBase.h> |
| 12 | #include <hal/Ports.h> |
| 13 | #include <hal/Relay.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 | Relay::Relay(int channel, Relay::Direction direction) |
| 24 | : m_channel(channel), m_direction(direction) { |
| 25 | if (!SensorUtil::CheckRelayChannel(m_channel)) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 26 | throw FRC_MakeError(err::ChannelIndexOutOfRange, "Channel {}", m_channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 27 | return; |
| 28 | } |
| 29 | |
| 30 | HAL_PortHandle portHandle = HAL_GetPort(channel); |
| 31 | |
| 32 | if (m_direction == kBothDirections || m_direction == kForwardOnly) { |
| 33 | int32_t status = 0; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 34 | std::string stackTrace = wpi::GetStackTrace(1); |
| 35 | m_forwardHandle = |
| 36 | HAL_InitializeRelayPort(portHandle, true, stackTrace.c_str(), &status); |
| 37 | FRC_CheckErrorStatus(status, "Channel {}", m_channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 38 | HAL_Report(HALUsageReporting::kResourceType_Relay, m_channel + 1); |
| 39 | } |
| 40 | if (m_direction == kBothDirections || m_direction == kReverseOnly) { |
| 41 | int32_t status = 0; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 42 | std::string stackTrace = wpi::GetStackTrace(1); |
| 43 | m_reverseHandle = |
| 44 | HAL_InitializeRelayPort(portHandle, false, stackTrace.c_str(), &status); |
| 45 | FRC_CheckErrorStatus(status, "Channel {}", m_channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 46 | HAL_Report(HALUsageReporting::kResourceType_Relay, m_channel + 128); |
| 47 | } |
| 48 | |
| 49 | int32_t status = 0; |
| 50 | if (m_forwardHandle != HAL_kInvalidHandle) { |
| 51 | HAL_SetRelay(m_forwardHandle, false, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 52 | FRC_CheckErrorStatus(status, "Channel {}", m_channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 53 | } |
| 54 | if (m_reverseHandle != HAL_kInvalidHandle) { |
| 55 | HAL_SetRelay(m_reverseHandle, false, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 56 | FRC_CheckErrorStatus(status, "Channel {}", m_channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 59 | wpi::SendableRegistry::AddLW(this, "Relay", m_channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | Relay::~Relay() { |
| 63 | int32_t status = 0; |
| 64 | HAL_SetRelay(m_forwardHandle, false, &status); |
| 65 | HAL_SetRelay(m_reverseHandle, false, &status); |
| 66 | // ignore errors, as we want to make sure a free happens. |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 67 | if (m_forwardHandle != HAL_kInvalidHandle) { |
| 68 | HAL_FreeRelayPort(m_forwardHandle); |
| 69 | } |
| 70 | if (m_reverseHandle != HAL_kInvalidHandle) { |
| 71 | HAL_FreeRelayPort(m_reverseHandle); |
| 72 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void Relay::Set(Relay::Value value) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 76 | int32_t status = 0; |
| 77 | |
| 78 | switch (value) { |
| 79 | case kOff: |
| 80 | if (m_direction == kBothDirections || m_direction == kForwardOnly) { |
| 81 | HAL_SetRelay(m_forwardHandle, false, &status); |
| 82 | } |
| 83 | if (m_direction == kBothDirections || m_direction == kReverseOnly) { |
| 84 | HAL_SetRelay(m_reverseHandle, false, &status); |
| 85 | } |
| 86 | break; |
| 87 | case kOn: |
| 88 | if (m_direction == kBothDirections || m_direction == kForwardOnly) { |
| 89 | HAL_SetRelay(m_forwardHandle, true, &status); |
| 90 | } |
| 91 | if (m_direction == kBothDirections || m_direction == kReverseOnly) { |
| 92 | HAL_SetRelay(m_reverseHandle, true, &status); |
| 93 | } |
| 94 | break; |
| 95 | case kForward: |
| 96 | if (m_direction == kReverseOnly) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 97 | FRC_ReportError(err::IncompatibleMode, "channel {} setting {}", |
| 98 | m_channel, "forward"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 99 | break; |
| 100 | } |
| 101 | if (m_direction == kBothDirections || m_direction == kForwardOnly) { |
| 102 | HAL_SetRelay(m_forwardHandle, true, &status); |
| 103 | } |
| 104 | if (m_direction == kBothDirections) { |
| 105 | HAL_SetRelay(m_reverseHandle, false, &status); |
| 106 | } |
| 107 | break; |
| 108 | case kReverse: |
| 109 | if (m_direction == kForwardOnly) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 110 | FRC_ReportError(err::IncompatibleMode, "channel {} setting {}", |
| 111 | m_channel, "reverse"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 112 | break; |
| 113 | } |
| 114 | if (m_direction == kBothDirections) { |
| 115 | HAL_SetRelay(m_forwardHandle, false, &status); |
| 116 | } |
| 117 | if (m_direction == kBothDirections || m_direction == kReverseOnly) { |
| 118 | HAL_SetRelay(m_reverseHandle, true, &status); |
| 119 | } |
| 120 | break; |
| 121 | } |
| 122 | |
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 | } |
| 125 | |
| 126 | Relay::Value Relay::Get() const { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 127 | Relay::Value value = kOff; |
| 128 | int32_t status = 0; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 129 | |
| 130 | if (m_direction == kForwardOnly) { |
| 131 | if (HAL_GetRelay(m_forwardHandle, &status)) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 132 | value = kOn; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 133 | } else { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 134 | value = kOff; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 135 | } |
| 136 | } else if (m_direction == kReverseOnly) { |
| 137 | if (HAL_GetRelay(m_reverseHandle, &status)) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 138 | value = kOn; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 139 | } else { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 140 | value = kOff; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 141 | } |
| 142 | } else { |
| 143 | if (HAL_GetRelay(m_forwardHandle, &status)) { |
| 144 | if (HAL_GetRelay(m_reverseHandle, &status)) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 145 | value = kOn; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 146 | } else { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 147 | value = kForward; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 148 | } |
| 149 | } else { |
| 150 | if (HAL_GetRelay(m_reverseHandle, &status)) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 151 | value = kReverse; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 152 | } else { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 153 | value = kOff; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 158 | FRC_CheckErrorStatus(status, "Channel {}", m_channel); |
| 159 | |
| 160 | return value; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 161 | } |
| 162 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 163 | int Relay::GetChannel() const { |
| 164 | return m_channel; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 165 | } |
| 166 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 167 | void Relay::StopMotor() { |
| 168 | Set(kOff); |
| 169 | } |
| 170 | |
| 171 | std::string Relay::GetDescription() const { |
| 172 | return fmt::format("Relay {}", GetChannel()); |
| 173 | } |
| 174 | |
| 175 | void Relay::InitSendable(wpi::SendableBuilder& builder) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 176 | builder.SetSmartDashboardType("Relay"); |
| 177 | builder.SetActuator(true); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 178 | builder.SetSafeState([=] { Set(kOff); }); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 179 | builder.AddSmallStringProperty( |
| 180 | "Value", |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 181 | [=](wpi::SmallVectorImpl<char>& buf) -> std::string_view { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 182 | switch (Get()) { |
| 183 | case kOn: |
| 184 | return "On"; |
| 185 | case kForward: |
| 186 | return "Forward"; |
| 187 | case kReverse: |
| 188 | return "Reverse"; |
| 189 | default: |
| 190 | return "Off"; |
| 191 | } |
| 192 | }, |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 193 | [=](std::string_view value) { |
| 194 | if (value == "Off") { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 195 | Set(kOff); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 196 | } else if (value == "Forward") { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 197 | Set(kForward); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 198 | } else if (value == "Reverse") { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 199 | Set(kReverse); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 200 | } else if (value == "On") { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 201 | Set(kOn); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 202 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 203 | }); |
| 204 | } |