Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/ |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 2 | /* Copyright (c) FIRST 2008-2016. All Rights Reserved. */ |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #include "Relay.h" |
| 9 | |
| 10 | #include "MotorSafetyHelper.h" |
| 11 | //#include "NetworkCommunication/UsageReporting.h" |
| 12 | #include "WPIErrors.h" |
| 13 | #include "LiveWindow/LiveWindow.h" |
| 14 | |
| 15 | /** |
| 16 | * Relay constructor given a channel. |
| 17 | * |
| 18 | * This code initializes the relay and reserves all resources that need to be |
| 19 | * locked. Initially the relay is set to both lines at 0v. |
| 20 | * @param channel The channel number (0-3). |
| 21 | * @param direction The direction that the Relay object will control. |
| 22 | */ |
| 23 | Relay::Relay(uint32_t channel, Relay::Direction direction) |
| 24 | : m_channel (channel) |
| 25 | , m_direction (direction) |
| 26 | { |
| 27 | char buf[64]; |
| 28 | if (!SensorBase::CheckRelayChannel(m_channel)) |
| 29 | { |
| 30 | snprintf(buf, 64, "Relay Channel %d", m_channel); |
| 31 | wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf); |
| 32 | return; |
| 33 | } |
| 34 | |
| 35 | m_safetyHelper = std::make_unique<MotorSafetyHelper>(this); |
| 36 | m_safetyHelper->SetSafetyEnabled(false); |
| 37 | |
| 38 | sprintf(buf, "relay/%d", m_channel); |
| 39 | impl = new SimContinuousOutput(buf); // TODO: Allow two different relays (targetting the different halves of a relay) to be combined to control one motor. |
| 40 | LiveWindow::GetInstance()->AddActuator("Relay", 1, m_channel, this); |
| 41 | go_pos = go_neg = false; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Free the resource associated with a relay. |
| 46 | * The relay channels are set to free and the relay output is turned off. |
| 47 | */ |
| 48 | Relay::~Relay() |
| 49 | { |
| 50 | impl->Set(0); |
| 51 | if (m_table != nullptr) m_table->RemoveTableListener(this); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Set the relay state. |
| 56 | * |
| 57 | * Valid values depend on which directions of the relay are controlled by the object. |
| 58 | * |
| 59 | * When set to kBothDirections, the relay can be any of the four states: |
| 60 | * 0v-0v, 0v-12v, 12v-0v, 12v-12v |
| 61 | * |
| 62 | * When set to kForwardOnly or kReverseOnly, you can specify the constant for the |
| 63 | * direction or you can simply specify kOff and kOn. Using only kOff and kOn is |
| 64 | * recommended. |
| 65 | * |
| 66 | * @param value The state to set the relay. |
| 67 | */ |
| 68 | void Relay::Set(Relay::Value value) |
| 69 | { |
| 70 | switch (value) |
| 71 | { |
| 72 | case kOff: |
| 73 | if (m_direction == kBothDirections || m_direction == kForwardOnly) |
| 74 | { |
| 75 | go_pos = false; |
| 76 | } |
| 77 | if (m_direction == kBothDirections || m_direction == kReverseOnly) |
| 78 | { |
| 79 | go_neg = false; |
| 80 | } |
| 81 | break; |
| 82 | case kOn: |
| 83 | if (m_direction == kBothDirections || m_direction == kForwardOnly) |
| 84 | { |
| 85 | go_pos = true; |
| 86 | } |
| 87 | if (m_direction == kBothDirections || m_direction == kReverseOnly) |
| 88 | { |
| 89 | go_neg = true; |
| 90 | } |
| 91 | break; |
| 92 | case kForward: |
| 93 | if (m_direction == kReverseOnly) |
| 94 | { |
| 95 | wpi_setWPIError(IncompatibleMode); |
| 96 | break; |
| 97 | } |
| 98 | if (m_direction == kBothDirections || m_direction == kForwardOnly) |
| 99 | { |
| 100 | go_pos = true; |
| 101 | } |
| 102 | if (m_direction == kBothDirections) |
| 103 | { |
| 104 | go_neg = false; |
| 105 | } |
| 106 | break; |
| 107 | case kReverse: |
| 108 | if (m_direction == kForwardOnly) |
| 109 | { |
| 110 | wpi_setWPIError(IncompatibleMode); |
| 111 | break; |
| 112 | } |
| 113 | if (m_direction == kBothDirections) |
| 114 | { |
| 115 | go_pos = false; |
| 116 | } |
| 117 | if (m_direction == kBothDirections || m_direction == kReverseOnly) |
| 118 | { |
| 119 | go_neg = true; |
| 120 | } |
| 121 | break; |
| 122 | } |
| 123 | impl->Set((go_pos ? 1 : 0) + (go_neg ? -1 : 0)); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Get the Relay State |
| 128 | * |
| 129 | * Gets the current state of the relay. |
| 130 | * |
| 131 | * When set to kForwardOnly or kReverseOnly, value is returned as kOn/kOff not |
| 132 | * kForward/kReverse (per the recommendation in Set) |
| 133 | * |
| 134 | * @return The current state of the relay as a Relay::Value |
| 135 | */ |
| 136 | Relay::Value Relay::Get() const { |
| 137 | // TODO: Don't assume that the go_pos and go_neg fields are correct? |
| 138 | if ((go_pos || m_direction == kReverseOnly) && (go_neg || m_direction == kForwardOnly)) { |
| 139 | return kOn; |
| 140 | } else if (go_pos) { |
| 141 | return kForward; |
| 142 | } else if (go_neg) { |
| 143 | return kReverse; |
| 144 | } else { |
| 145 | return kOff; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | uint32_t Relay::GetChannel() const { |
| 150 | return m_channel; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Set the expiration time for the Relay object |
| 155 | * @param timeout The timeout (in seconds) for this relay object |
| 156 | */ |
| 157 | void Relay::SetExpiration(float timeout) { |
| 158 | m_safetyHelper->SetExpiration(timeout); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Return the expiration time for the relay object. |
| 163 | * @return The expiration time value. |
| 164 | */ |
| 165 | float Relay::GetExpiration() const { return m_safetyHelper->GetExpiration(); } |
| 166 | |
| 167 | /** |
| 168 | * Check if the relay object is currently alive or stopped due to a timeout. |
| 169 | * @returns a bool value that is true if the motor has NOT timed out and should |
| 170 | * still be running. |
| 171 | */ |
| 172 | bool Relay::IsAlive() const { return m_safetyHelper->IsAlive(); } |
| 173 | |
| 174 | /** |
| 175 | * Stop the motor associated with this PWM object. |
| 176 | * This is called by the MotorSafetyHelper object when it has a timeout for this |
| 177 | * relay and needs to stop it from running. |
| 178 | */ |
| 179 | void Relay::StopMotor() { Set(kOff); } |
| 180 | |
| 181 | /** |
| 182 | * Enable/disable motor safety for this device |
| 183 | * Turn on and off the motor safety option for this relay object. |
| 184 | * @param enabled True if motor safety is enforced for this object |
| 185 | */ |
| 186 | void Relay::SetSafetyEnabled(bool enabled) { |
| 187 | m_safetyHelper->SetSafetyEnabled(enabled); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Check if motor safety is enabled for this object |
| 192 | * @returns True if motor safety is enforced for this object |
| 193 | */ |
| 194 | bool Relay::IsSafetyEnabled() const { |
| 195 | return m_safetyHelper->IsSafetyEnabled(); |
| 196 | } |
| 197 | |
| 198 | void Relay::GetDescription(std::ostringstream& desc) const { |
| 199 | desc << "Relay " << GetChannel(); |
| 200 | } |
| 201 | |
| 202 | void Relay::ValueChanged(ITable* source, llvm::StringRef key, |
| 203 | std::shared_ptr<nt::Value> value, bool isNew) { |
| 204 | if (!value->IsString()) return; |
| 205 | if (value->GetString() == "Off") Set(kOff); |
| 206 | else if (value->GetString() == "Forward") Set(kForward); |
| 207 | else if (value->GetString() == "Reverse") Set(kReverse); |
| 208 | } |
| 209 | |
| 210 | void Relay::UpdateTable() { |
| 211 | if(m_table != nullptr){ |
| 212 | if (Get() == kOn) { |
| 213 | m_table->PutString("Value", "On"); |
| 214 | } |
| 215 | else if (Get() == kForward) { |
| 216 | m_table->PutString("Value", "Forward"); |
| 217 | } |
| 218 | else if (Get() == kReverse) { |
| 219 | m_table->PutString("Value", "Reverse"); |
| 220 | } |
| 221 | else { |
| 222 | m_table->PutString("Value", "Off"); |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | void Relay::StartLiveWindowMode() { |
| 228 | if(m_table != nullptr){ |
| 229 | m_table->AddTableListener("Value", this, true); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | void Relay::StopLiveWindowMode() { |
| 234 | if(m_table != nullptr){ |
| 235 | m_table->RemoveTableListener(this); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | std::string Relay::GetSmartDashboardType() const { |
| 240 | return "Relay"; |
| 241 | } |
| 242 | |
| 243 | void Relay::InitTable(std::shared_ptr<ITable> subTable) { |
| 244 | m_table = subTable; |
| 245 | UpdateTable(); |
| 246 | } |
| 247 | |
| 248 | std::shared_ptr<ITable> Relay::GetTable() const { |
| 249 | return m_table; |
| 250 | } |