Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) FIRST 2008. 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 $(WIND_BASE)/WPILib. */ |
| 5 | /*----------------------------------------------------------------------------*/ |
| 6 | |
| 7 | #include "DoubleSolenoid.h" |
| 8 | #include "WPIErrors.h" |
| 9 | #include "LiveWindow/LiveWindow.h" |
| 10 | |
| 11 | #include <sstream> |
| 12 | |
| 13 | /** |
| 14 | * Constructor. |
| 15 | * Uses the default PCM ID of 0 |
| 16 | * @param forwardChannel The forward channel number on the PCM (0..7). |
| 17 | * @param reverseChannel The reverse channel number on the PCM (0..7). |
| 18 | */ |
| 19 | DoubleSolenoid::DoubleSolenoid(uint32_t forwardChannel, uint32_t reverseChannel) |
| 20 | : DoubleSolenoid(GetDefaultSolenoidModule(), forwardChannel, reverseChannel) {} |
| 21 | |
| 22 | /** |
| 23 | * Constructor. |
| 24 | * |
| 25 | * @param moduleNumber The CAN ID of the PCM. |
| 26 | * @param forwardChannel The forward channel on the PCM to control (0..7). |
| 27 | * @param reverseChannel The reverse channel on the PCM to control (0..7). |
| 28 | */ |
| 29 | DoubleSolenoid::DoubleSolenoid(uint8_t moduleNumber, uint32_t forwardChannel, |
| 30 | uint32_t reverseChannel) |
| 31 | : SolenoidBase(moduleNumber), |
| 32 | m_forwardChannel(forwardChannel), |
| 33 | m_reverseChannel(reverseChannel) { |
| 34 | std::stringstream buf; |
| 35 | if (!CheckSolenoidModule(m_moduleNumber)) { |
| 36 | buf << "Solenoid Module " << m_moduleNumber; |
| 37 | wpi_setWPIErrorWithContext(ModuleIndexOutOfRange, buf.str()); |
| 38 | return; |
| 39 | } |
| 40 | if (!CheckSolenoidChannel(m_forwardChannel)) { |
| 41 | buf << "Solenoid Module " << m_forwardChannel; |
| 42 | wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf.str()); |
| 43 | return; |
| 44 | } |
| 45 | if (!CheckSolenoidChannel(m_reverseChannel)) { |
| 46 | buf << "Solenoid Module " << m_reverseChannel; |
| 47 | wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf.str()); |
| 48 | return; |
| 49 | } |
| 50 | Resource::CreateResourceObject( |
| 51 | m_allocated, m_maxModules * m_maxPorts); |
| 52 | |
| 53 | buf << "Solenoid " << m_forwardChannel << " (Module: " << m_moduleNumber |
| 54 | << ")"; |
| 55 | if (m_allocated->Allocate( |
| 56 | m_moduleNumber * kSolenoidChannels + m_forwardChannel, buf.str()) == |
| 57 | std::numeric_limits<uint32_t>::max()) { |
| 58 | CloneError(*m_allocated); |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | buf << "Solenoid " << m_reverseChannel << " (Module: " << m_moduleNumber |
| 63 | << ")"; |
| 64 | if (m_allocated->Allocate( |
| 65 | m_moduleNumber * kSolenoidChannels + m_reverseChannel, buf.str()) == |
| 66 | std::numeric_limits<uint32_t>::max()) { |
| 67 | CloneError(*m_allocated); |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | m_forwardMask = 1 << m_forwardChannel; |
| 72 | m_reverseMask = 1 << m_reverseChannel; |
| 73 | HALReport(HALUsageReporting::kResourceType_Solenoid, m_forwardChannel, |
| 74 | m_moduleNumber); |
| 75 | HALReport(HALUsageReporting::kResourceType_Solenoid, m_reverseChannel, |
| 76 | m_moduleNumber); |
| 77 | LiveWindow::GetInstance()->AddActuator("DoubleSolenoid", m_moduleNumber, |
| 78 | m_forwardChannel, this); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Destructor. |
| 83 | */ |
| 84 | DoubleSolenoid::~DoubleSolenoid() { |
| 85 | if (CheckSolenoidModule(m_moduleNumber)) { |
| 86 | m_allocated->Free(m_moduleNumber * kSolenoidChannels + m_forwardChannel); |
| 87 | m_allocated->Free(m_moduleNumber * kSolenoidChannels + m_reverseChannel); |
| 88 | } |
| 89 | if (m_table != nullptr) m_table->RemoveTableListener(this); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Set the value of a solenoid. |
| 94 | * |
| 95 | * @param value The value to set (Off, Forward or Reverse) |
| 96 | */ |
| 97 | void DoubleSolenoid::Set(Value value) { |
| 98 | if (StatusIsFatal()) return; |
| 99 | uint8_t rawValue = 0x00; |
| 100 | |
| 101 | switch (value) { |
| 102 | case kOff: |
| 103 | rawValue = 0x00; |
| 104 | break; |
| 105 | case kForward: |
| 106 | rawValue = m_forwardMask; |
| 107 | break; |
| 108 | case kReverse: |
| 109 | rawValue = m_reverseMask; |
| 110 | break; |
| 111 | } |
| 112 | |
| 113 | SolenoidBase::Set(rawValue, m_forwardMask | m_reverseMask, m_moduleNumber); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Read the current value of the solenoid. |
| 118 | * |
| 119 | * @return The current value of the solenoid. |
| 120 | */ |
| 121 | DoubleSolenoid::Value DoubleSolenoid::Get() const { |
| 122 | if (StatusIsFatal()) return kOff; |
| 123 | uint8_t value = GetAll(m_moduleNumber); |
| 124 | |
| 125 | if (value & m_forwardMask) return kForward; |
| 126 | if (value & m_reverseMask) return kReverse; |
| 127 | return kOff; |
| 128 | } |
| 129 | /** |
| 130 | * Check if the forward solenoid is blacklisted. |
| 131 | * If a solenoid is shorted, it is added to the blacklist and |
| 132 | * disabled until power cycle, or until faults are cleared. |
| 133 | * @see ClearAllPCMStickyFaults() |
| 134 | * |
| 135 | * @return If solenoid is disabled due to short. |
| 136 | */ |
| 137 | bool DoubleSolenoid::IsFwdSolenoidBlackListed() const { |
| 138 | int blackList = GetPCMSolenoidBlackList(m_moduleNumber); |
| 139 | return (blackList & m_forwardMask) ? 1 : 0; |
| 140 | } |
| 141 | /** |
| 142 | * Check if the reverse solenoid is blacklisted. |
| 143 | * If a solenoid is shorted, it is added to the blacklist and |
| 144 | * disabled until power cycle, or until faults are cleared. |
| 145 | * @see ClearAllPCMStickyFaults() |
| 146 | * |
| 147 | * @return If solenoid is disabled due to short. |
| 148 | */ |
| 149 | bool DoubleSolenoid::IsRevSolenoidBlackListed() const { |
| 150 | int blackList = GetPCMSolenoidBlackList(m_moduleNumber); |
| 151 | return (blackList & m_reverseMask) ? 1 : 0; |
| 152 | } |
| 153 | |
| 154 | void DoubleSolenoid::ValueChanged(ITable* source, llvm::StringRef key, |
| 155 | std::shared_ptr<nt::Value> value, |
| 156 | bool isNew) { |
| 157 | if (!value->IsString()) return; |
| 158 | Value lvalue = kOff; |
| 159 | if (value->GetString() == "Forward") |
| 160 | lvalue = kForward; |
| 161 | else if (value->GetString() == "Reverse") |
| 162 | lvalue = kReverse; |
| 163 | Set(lvalue); |
| 164 | } |
| 165 | |
| 166 | void DoubleSolenoid::UpdateTable() { |
| 167 | if (m_table != nullptr) { |
| 168 | m_table->PutString( |
| 169 | "Value", (Get() == kForward ? "Forward" |
| 170 | : (Get() == kReverse ? "Reverse" : "Off"))); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | void DoubleSolenoid::StartLiveWindowMode() { |
| 175 | Set(kOff); |
| 176 | if (m_table != nullptr) { |
| 177 | m_table->AddTableListener("Value", this, true); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | void DoubleSolenoid::StopLiveWindowMode() { |
| 182 | Set(kOff); |
| 183 | if (m_table != nullptr) { |
| 184 | m_table->RemoveTableListener(this); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | std::string DoubleSolenoid::GetSmartDashboardType() const { |
| 189 | return "Double Solenoid"; |
| 190 | } |
| 191 | |
| 192 | void DoubleSolenoid::InitTable(std::shared_ptr<ITable> subTable) { |
| 193 | m_table = subTable; |
| 194 | UpdateTable(); |
| 195 | } |
| 196 | |
| 197 | std::shared_ptr<ITable> DoubleSolenoid::GetTable() const { return m_table; } |