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 "LiveWindow/LiveWindow.h" |
| 11 | #include "WPIErrors.h" |
| 12 | |
| 13 | using namespace frc; |
| 14 | |
| 15 | /** |
| 16 | * Constructor. |
| 17 | * |
| 18 | * @param forwardChannel The forward channel on the module to control. |
| 19 | * @param reverseChannel The reverse channel on the module to control. |
| 20 | */ |
| 21 | DoubleSolenoid::DoubleSolenoid(int forwardChannel, int reverseChannel) |
| 22 | : DoubleSolenoid(1, forwardChannel, reverseChannel) {} |
| 23 | |
| 24 | /** |
| 25 | * Constructor. |
| 26 | * |
| 27 | * @param moduleNumber The solenoid module (1 or 2). |
| 28 | * @param forwardChannel The forward channel on the module to control. |
| 29 | * @param reverseChannel The reverse channel on the module to control. |
| 30 | */ |
| 31 | DoubleSolenoid::DoubleSolenoid(int moduleNumber, int forwardChannel, |
| 32 | int reverseChannel) { |
| 33 | m_reversed = false; |
| 34 | if (reverseChannel < forwardChannel) { // Swap ports to get the right address |
| 35 | int channel = reverseChannel; |
| 36 | reverseChannel = forwardChannel; |
| 37 | forwardChannel = channel; |
| 38 | m_reversed = true; |
| 39 | } |
| 40 | std::stringstream ss; |
| 41 | ss << "pneumatic/" << moduleNumber << "/" << forwardChannel << "/" |
| 42 | << moduleNumber << "/" << reverseChannel; |
| 43 | m_impl = new SimContinuousOutput(ss.str()); |
| 44 | |
| 45 | LiveWindow::GetInstance()->AddActuator("DoubleSolenoid", moduleNumber, |
| 46 | forwardChannel, this); |
| 47 | } |
| 48 | |
| 49 | DoubleSolenoid::~DoubleSolenoid() { |
| 50 | if (m_table != nullptr) m_table->RemoveTableListener(this); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Set the value of a solenoid. |
| 55 | * |
| 56 | * @param value Move the solenoid to forward, reverse, or don't move it. |
| 57 | */ |
| 58 | void DoubleSolenoid::Set(Value value) { |
| 59 | m_value = value; |
| 60 | switch (value) { |
| 61 | case kOff: |
| 62 | m_impl->Set(0); |
| 63 | break; |
| 64 | case kForward: |
| 65 | m_impl->Set(m_reversed ? -1 : 1); |
| 66 | break; |
| 67 | case kReverse: |
| 68 | m_impl->Set(m_reversed ? 1 : -1); |
| 69 | break; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Read the current value of the solenoid. |
| 75 | * |
| 76 | * @return The current value of the solenoid. |
| 77 | */ |
| 78 | DoubleSolenoid::Value DoubleSolenoid::Get() const { return m_value; } |
| 79 | |
| 80 | void DoubleSolenoid::ValueChanged(ITable* source, llvm::StringRef key, |
| 81 | std::shared_ptr<nt::Value> value, |
| 82 | bool isNew) { |
| 83 | if (!value->IsString()) return; |
| 84 | Value lvalue = kOff; |
| 85 | if (value->GetString() == "Forward") |
| 86 | lvalue = kForward; |
| 87 | else if (value->GetString() == "Reverse") |
| 88 | lvalue = kReverse; |
| 89 | Set(lvalue); |
| 90 | } |
| 91 | |
| 92 | void DoubleSolenoid::UpdateTable() { |
| 93 | if (m_table != nullptr) { |
| 94 | m_table->PutString( |
| 95 | "Value", (Get() == kForward ? "Forward" |
| 96 | : (Get() == kReverse ? "Reverse" : "Off"))); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | void DoubleSolenoid::StartLiveWindowMode() { |
| 101 | Set(kOff); |
| 102 | if (m_table != nullptr) { |
| 103 | m_table->AddTableListener("Value", this, true); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void DoubleSolenoid::StopLiveWindowMode() { |
| 108 | Set(kOff); |
| 109 | if (m_table != nullptr) { |
| 110 | m_table->RemoveTableListener(this); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | std::string DoubleSolenoid::GetSmartDashboardType() const { |
| 115 | return "Double Solenoid"; |
| 116 | } |
| 117 | |
| 118 | void DoubleSolenoid::InitTable(std::shared_ptr<ITable> subTable) { |
| 119 | m_table = subTable; |
| 120 | UpdateTable(); |
| 121 | } |
| 122 | |
| 123 | std::shared_ptr<ITable> DoubleSolenoid::GetTable() const { return m_table; } |