Brian Silverman | f7f267a | 2017-02-04 16:16:08 -0800 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) FIRST 2008-2017. All Rights Reserved. */ |
| 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 "DoubleSolenoid.h" |
| 9 | |
| 10 | #include <sstream> |
| 11 | |
| 12 | #include "HAL/HAL.h" |
| 13 | #include "HAL/Ports.h" |
| 14 | #include "HAL/Solenoid.h" |
| 15 | #include "LiveWindow/LiveWindow.h" |
| 16 | #include "WPIErrors.h" |
| 17 | |
| 18 | using namespace frc; |
| 19 | |
| 20 | /** |
| 21 | * Constructor. |
| 22 | * |
| 23 | * Uses the default PCM ID of 0. |
| 24 | * |
| 25 | * @param forwardChannel The forward channel number on the PCM (0..7). |
| 26 | * @param reverseChannel The reverse channel number on the PCM (0..7). |
| 27 | */ |
| 28 | DoubleSolenoid::DoubleSolenoid(int forwardChannel, int reverseChannel) |
| 29 | : DoubleSolenoid(GetDefaultSolenoidModule(), forwardChannel, |
| 30 | reverseChannel) {} |
| 31 | |
| 32 | /** |
| 33 | * Constructor. |
| 34 | * |
| 35 | * @param moduleNumber The CAN ID of the PCM. |
| 36 | * @param forwardChannel The forward channel on the PCM to control (0..7). |
| 37 | * @param reverseChannel The reverse channel on the PCM to control (0..7). |
| 38 | */ |
| 39 | DoubleSolenoid::DoubleSolenoid(int moduleNumber, int forwardChannel, |
| 40 | int reverseChannel) |
| 41 | : SolenoidBase(moduleNumber), |
| 42 | m_forwardChannel(forwardChannel), |
| 43 | m_reverseChannel(reverseChannel) { |
| 44 | std::stringstream buf; |
| 45 | if (!CheckSolenoidModule(m_moduleNumber)) { |
| 46 | buf << "Solenoid Module " << m_moduleNumber; |
| 47 | wpi_setWPIErrorWithContext(ModuleIndexOutOfRange, buf.str()); |
| 48 | return; |
| 49 | } |
| 50 | if (!CheckSolenoidChannel(m_forwardChannel)) { |
| 51 | buf << "Solenoid Module " << m_forwardChannel; |
| 52 | wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf.str()); |
| 53 | return; |
| 54 | } |
| 55 | if (!CheckSolenoidChannel(m_reverseChannel)) { |
| 56 | buf << "Solenoid Module " << m_reverseChannel; |
| 57 | wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf.str()); |
| 58 | return; |
| 59 | } |
| 60 | int32_t status = 0; |
| 61 | m_forwardHandle = HAL_InitializeSolenoidPort( |
| 62 | HAL_GetPortWithModule(moduleNumber, m_forwardChannel), &status); |
| 63 | if (status != 0) { |
| 64 | wpi_setErrorWithContextRange(status, 0, HAL_GetNumSolenoidChannels(), |
| 65 | forwardChannel, HAL_GetErrorMessage(status)); |
| 66 | m_forwardHandle = HAL_kInvalidHandle; |
| 67 | m_reverseHandle = HAL_kInvalidHandle; |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | m_reverseHandle = HAL_InitializeSolenoidPort( |
| 72 | HAL_GetPortWithModule(moduleNumber, m_reverseChannel), &status); |
| 73 | if (status != 0) { |
| 74 | wpi_setErrorWithContextRange(status, 0, HAL_GetNumSolenoidChannels(), |
| 75 | reverseChannel, HAL_GetErrorMessage(status)); |
| 76 | // free forward solenoid |
| 77 | HAL_FreeSolenoidPort(m_forwardHandle); |
| 78 | m_forwardHandle = HAL_kInvalidHandle; |
| 79 | m_reverseHandle = HAL_kInvalidHandle; |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | m_forwardMask = 1 << m_forwardChannel; |
| 84 | m_reverseMask = 1 << m_reverseChannel; |
| 85 | |
| 86 | HAL_Report(HALUsageReporting::kResourceType_Solenoid, m_forwardChannel, |
| 87 | m_moduleNumber); |
| 88 | HAL_Report(HALUsageReporting::kResourceType_Solenoid, m_reverseChannel, |
| 89 | m_moduleNumber); |
| 90 | LiveWindow::GetInstance()->AddActuator("DoubleSolenoid", m_moduleNumber, |
| 91 | m_forwardChannel, this); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Destructor. |
| 96 | */ |
| 97 | DoubleSolenoid::~DoubleSolenoid() { |
| 98 | HAL_FreeSolenoidPort(m_forwardHandle); |
| 99 | HAL_FreeSolenoidPort(m_reverseHandle); |
| 100 | if (m_table != nullptr) m_table->RemoveTableListener(this); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Set the value of a solenoid. |
| 105 | * |
| 106 | * @param value The value to set (Off, Forward or Reverse) |
| 107 | */ |
| 108 | void DoubleSolenoid::Set(Value value) { |
| 109 | if (StatusIsFatal()) return; |
| 110 | |
| 111 | bool forward = false; |
| 112 | bool reverse = false; |
| 113 | switch (value) { |
| 114 | case kOff: |
| 115 | forward = false; |
| 116 | reverse = false; |
| 117 | break; |
| 118 | case kForward: |
| 119 | forward = true; |
| 120 | reverse = false; |
| 121 | break; |
| 122 | case kReverse: |
| 123 | forward = false; |
| 124 | reverse = true; |
| 125 | break; |
| 126 | } |
| 127 | int fstatus = 0; |
| 128 | HAL_SetSolenoid(m_forwardHandle, forward, &fstatus); |
| 129 | int rstatus = 0; |
| 130 | HAL_SetSolenoid(m_reverseHandle, reverse, &rstatus); |
| 131 | |
| 132 | wpi_setErrorWithContext(fstatus, HAL_GetErrorMessage(fstatus)); |
| 133 | wpi_setErrorWithContext(rstatus, HAL_GetErrorMessage(rstatus)); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Read the current value of the solenoid. |
| 138 | * |
| 139 | * @return The current value of the solenoid. |
| 140 | */ |
| 141 | DoubleSolenoid::Value DoubleSolenoid::Get() const { |
| 142 | if (StatusIsFatal()) return kOff; |
| 143 | int fstatus = 0; |
| 144 | int rstatus = 0; |
| 145 | bool valueForward = HAL_GetSolenoid(m_forwardHandle, &fstatus); |
| 146 | bool valueReverse = HAL_GetSolenoid(m_reverseHandle, &rstatus); |
| 147 | |
| 148 | wpi_setErrorWithContext(fstatus, HAL_GetErrorMessage(fstatus)); |
| 149 | wpi_setErrorWithContext(rstatus, HAL_GetErrorMessage(rstatus)); |
| 150 | |
| 151 | if (valueForward) return kForward; |
| 152 | if (valueReverse) return kReverse; |
| 153 | return kOff; |
| 154 | } |
| 155 | /** |
| 156 | * Check if the forward solenoid is blacklisted. |
| 157 | * |
| 158 | * If a solenoid is shorted, it is added to the blacklist and |
| 159 | * disabled until power cycle, or until faults are cleared. |
| 160 | * @see ClearAllPCMStickyFaults() |
| 161 | * |
| 162 | * @return If solenoid is disabled due to short. |
| 163 | */ |
| 164 | bool DoubleSolenoid::IsFwdSolenoidBlackListed() const { |
| 165 | int blackList = GetPCMSolenoidBlackList(m_moduleNumber); |
| 166 | return (blackList & m_forwardMask) ? 1 : 0; |
| 167 | } |
| 168 | /** |
| 169 | * Check if the reverse solenoid is blacklisted. |
| 170 | * |
| 171 | * If a solenoid is shorted, it is added to the blacklist and |
| 172 | * disabled until power cycle, or until faults are cleared. |
| 173 | * @see ClearAllPCMStickyFaults() |
| 174 | * |
| 175 | * @return If solenoid is disabled due to short. |
| 176 | */ |
| 177 | bool DoubleSolenoid::IsRevSolenoidBlackListed() const { |
| 178 | int blackList = GetPCMSolenoidBlackList(m_moduleNumber); |
| 179 | return (blackList & m_reverseMask) ? 1 : 0; |
| 180 | } |
| 181 | |
| 182 | void DoubleSolenoid::ValueChanged(ITable* source, llvm::StringRef key, |
| 183 | std::shared_ptr<nt::Value> value, |
| 184 | bool isNew) { |
| 185 | if (!value->IsString()) return; |
| 186 | Value lvalue = kOff; |
| 187 | if (value->GetString() == "Forward") |
| 188 | lvalue = kForward; |
| 189 | else if (value->GetString() == "Reverse") |
| 190 | lvalue = kReverse; |
| 191 | Set(lvalue); |
| 192 | } |
| 193 | |
| 194 | void DoubleSolenoid::UpdateTable() { |
| 195 | if (m_table != nullptr) { |
| 196 | m_table->PutString( |
| 197 | "Value", (Get() == kForward ? "Forward" |
| 198 | : (Get() == kReverse ? "Reverse" : "Off"))); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | void DoubleSolenoid::StartLiveWindowMode() { |
| 203 | Set(kOff); |
| 204 | if (m_table != nullptr) { |
| 205 | m_table->AddTableListener("Value", this, true); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | void DoubleSolenoid::StopLiveWindowMode() { |
| 210 | Set(kOff); |
| 211 | if (m_table != nullptr) { |
| 212 | m_table->RemoveTableListener(this); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | std::string DoubleSolenoid::GetSmartDashboardType() const { |
| 217 | return "Double Solenoid"; |
| 218 | } |
| 219 | |
| 220 | void DoubleSolenoid::InitTable(std::shared_ptr<ITable> subTable) { |
| 221 | m_table = subTable; |
| 222 | UpdateTable(); |
| 223 | } |
| 224 | |
| 225 | std::shared_ptr<ITable> DoubleSolenoid::GetTable() const { return m_table; } |