blob: 5290d8b826b42ccf3ac1ddb71a5817f4e42da997 [file] [log] [blame]
Brian Silverman26e4e522015-12-17 01:56:40 -05001/*----------------------------------------------------------------------------*/
Brian Silverman1a675112016-02-20 20:42:49 -05002/* Copyright (c) FIRST 2008-2016. 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. */
Brian Silverman26e4e522015-12-17 01:56:40 -05006/*----------------------------------------------------------------------------*/
7
8#include "Relay.h"
9
10#include "MotorSafetyHelper.h"
11#include "Resource.h"
12#include "WPIErrors.h"
13#include "LiveWindow/LiveWindow.h"
14#include "HAL/HAL.hpp"
15
16#include <sstream>
17
18// Allocate each direction separately.
19static std::unique_ptr<Resource> relayChannels;
20
21/**
22 * Relay constructor given a channel.
23 *
24 * This code initializes the relay and reserves all resources that need to be
25 * locked. Initially the relay is set to both lines at 0v.
26 * @param channel The channel number (0-3).
27 * @param direction The direction that the Relay object will control.
28 */
29Relay::Relay(uint32_t channel, Relay::Direction direction)
30 : m_channel(channel), m_direction(direction) {
31 std::stringstream buf;
32 Resource::CreateResourceObject(relayChannels,
33 dio_kNumSystems * kRelayChannels * 2);
34 if (!SensorBase::CheckRelayChannel(m_channel)) {
35 buf << "Relay Channel " << m_channel;
36 wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf.str());
37 return;
38 }
39
40 if (m_direction == kBothDirections || m_direction == kForwardOnly) {
41 buf << "Forward Relay " << m_channel;
42 if (relayChannels->Allocate(m_channel * 2, buf.str()) ==
43 std::numeric_limits<uint32_t>::max()) {
44 CloneError(*relayChannels);
45 return;
46 }
47
48 HALReport(HALUsageReporting::kResourceType_Relay, m_channel);
49 }
50 if (m_direction == kBothDirections || m_direction == kReverseOnly) {
51 buf << "Reverse Relay " << m_channel;
52 if (relayChannels->Allocate(m_channel * 2 + 1, buf.str()) ==
53 std::numeric_limits<uint32_t>::max()) {
54 CloneError(*relayChannels);
55 return;
56 }
57
58 HALReport(HALUsageReporting::kResourceType_Relay, m_channel + 128);
59 }
60
61 int32_t status = 0;
62 setRelayForward(m_relay_ports[m_channel], false, &status);
63 setRelayReverse(m_relay_ports[m_channel], false, &status);
64 wpi_setErrorWithContext(status, getHALErrorMessage(status));
65
66 m_safetyHelper = std::make_unique<MotorSafetyHelper>(this);
67 m_safetyHelper->SetSafetyEnabled(false);
68
69 LiveWindow::GetInstance()->AddActuator("Relay", 1, m_channel, this);
70}
71
72/**
73 * Free the resource associated with a relay.
74 * The relay channels are set to free and the relay output is turned off.
75 */
76Relay::~Relay() {
77 int32_t status = 0;
78 setRelayForward(m_relay_ports[m_channel], false, &status);
79 setRelayReverse(m_relay_ports[m_channel], false, &status);
80 wpi_setErrorWithContext(status, getHALErrorMessage(status));
81
82 if (m_direction == kBothDirections || m_direction == kForwardOnly) {
83 relayChannels->Free(m_channel * 2);
84 }
85 if (m_direction == kBothDirections || m_direction == kReverseOnly) {
86 relayChannels->Free(m_channel * 2 + 1);
87 }
88 if (m_table != nullptr) m_table->RemoveTableListener(this);
89}
90
91/**
92 * Set the relay state.
93 *
94 * Valid values depend on which directions of the relay are controlled by the
95 * object.
96 *
97 * When set to kBothDirections, the relay can be any of the four states:
98 * 0v-0v, 0v-12v, 12v-0v, 12v-12v
99 *
100 * When set to kForwardOnly or kReverseOnly, you can specify the constant for
101 * the
102 * direction or you can simply specify kOff and kOn. Using only kOff and
103 * kOn is
104 * recommended.
105 *
106 * @param value The state to set the relay.
107 */
108void Relay::Set(Relay::Value value) {
109 if (StatusIsFatal()) return;
110
111 int32_t status = 0;
112
113 switch (value) {
114 case kOff:
115 if (m_direction == kBothDirections || m_direction == kForwardOnly) {
116 setRelayForward(m_relay_ports[m_channel], false, &status);
117 }
118 if (m_direction == kBothDirections || m_direction == kReverseOnly) {
119 setRelayReverse(m_relay_ports[m_channel], false, &status);
120 }
121 break;
122 case kOn:
123 if (m_direction == kBothDirections || m_direction == kForwardOnly) {
124 setRelayForward(m_relay_ports[m_channel], true, &status);
125 }
126 if (m_direction == kBothDirections || m_direction == kReverseOnly) {
127 setRelayReverse(m_relay_ports[m_channel], true, &status);
128 }
129 break;
130 case kForward:
131 if (m_direction == kReverseOnly) {
132 wpi_setWPIError(IncompatibleMode);
133 break;
134 }
135 if (m_direction == kBothDirections || m_direction == kForwardOnly) {
136 setRelayForward(m_relay_ports[m_channel], true, &status);
137 }
138 if (m_direction == kBothDirections) {
139 setRelayReverse(m_relay_ports[m_channel], false, &status);
140 }
141 break;
142 case kReverse:
143 if (m_direction == kForwardOnly) {
144 wpi_setWPIError(IncompatibleMode);
145 break;
146 }
147 if (m_direction == kBothDirections) {
148 setRelayForward(m_relay_ports[m_channel], false, &status);
149 }
150 if (m_direction == kBothDirections || m_direction == kReverseOnly) {
151 setRelayReverse(m_relay_ports[m_channel], true, &status);
152 }
153 break;
154 }
155
156 wpi_setErrorWithContext(status, getHALErrorMessage(status));
157}
158
159/**
160 * Get the Relay State
161 *
162 * Gets the current state of the relay.
163 *
164 * When set to kForwardOnly or kReverseOnly, value is returned as kOn/kOff not
165 * kForward/kReverse (per the recommendation in Set)
166 *
167 * @return The current state of the relay as a Relay::Value
168 */
169Relay::Value Relay::Get() const {
170 int32_t status;
171
172 if (getRelayForward(m_relay_ports[m_channel], &status)) {
173 if (getRelayReverse(m_relay_ports[m_channel], &status)) {
174 return kOn;
175 } else {
176 if (m_direction == kForwardOnly) {
177 return kOn;
178 } else {
179 return kForward;
180 }
181 }
182 } else {
183 if (getRelayReverse(m_relay_ports[m_channel], &status)) {
184 if (m_direction == kReverseOnly) {
185 return kOn;
186 } else {
187 return kReverse;
188 }
189 } else {
190 return kOff;
191 }
192 }
193
194 wpi_setErrorWithContext(status, getHALErrorMessage(status));
195}
196
197uint32_t Relay::GetChannel() const {
198 return m_channel;
199}
200
201/**
202 * Set the expiration time for the Relay object
203 * @param timeout The timeout (in seconds) for this relay object
204 */
205void Relay::SetExpiration(float timeout) {
206 m_safetyHelper->SetExpiration(timeout);
207}
208
209/**
210 * Return the expiration time for the relay object.
211 * @return The expiration time value.
212 */
213float Relay::GetExpiration() const { return m_safetyHelper->GetExpiration(); }
214
215/**
216 * Check if the relay object is currently alive or stopped due to a timeout.
217 * @returns a bool value that is true if the motor has NOT timed out and should
218 * still be running.
219 */
220bool Relay::IsAlive() const { return m_safetyHelper->IsAlive(); }
221
222/**
223 * Stop the motor associated with this PWM object.
224 * This is called by the MotorSafetyHelper object when it has a timeout for this
225 * relay and needs to stop it from running.
226 */
227void Relay::StopMotor() { Set(kOff); }
228
229/**
230 * Enable/disable motor safety for this device
231 * Turn on and off the motor safety option for this relay object.
232 * @param enabled True if motor safety is enforced for this object
233 */
234void Relay::SetSafetyEnabled(bool enabled) {
235 m_safetyHelper->SetSafetyEnabled(enabled);
236}
237
238/**
239 * Check if motor safety is enabled for this object
240 * @returns True if motor safety is enforced for this object
241 */
242bool Relay::IsSafetyEnabled() const {
243 return m_safetyHelper->IsSafetyEnabled();
244}
245
246void Relay::GetDescription(std::ostringstream& desc) const {
247 desc << "Relay " << GetChannel();
248}
249
250void Relay::ValueChanged(ITable* source, llvm::StringRef key,
251 std::shared_ptr<nt::Value> value, bool isNew) {
252 if (!value->IsString()) return;
253 if (value->GetString() == "Off") Set(kOff);
254 else if (value->GetString() == "Forward") Set(kForward);
255 else if (value->GetString() == "Reverse") Set(kReverse);
256 else if (value->GetString() == "On") Set(kOn);
257}
258
259void Relay::UpdateTable() {
260 if (m_table != nullptr) {
261 if (Get() == kOn) {
262 m_table->PutString("Value", "On");
263 } else if (Get() == kForward) {
264 m_table->PutString("Value", "Forward");
265 } else if (Get() == kReverse) {
266 m_table->PutString("Value", "Reverse");
267 } else {
268 m_table->PutString("Value", "Off");
269 }
270 }
271}
272
273void Relay::StartLiveWindowMode() {
274 if (m_table != nullptr) {
275 m_table->AddTableListener("Value", this, true);
276 }
277}
278
279void Relay::StopLiveWindowMode() {
280 if (m_table != nullptr) {
281 m_table->RemoveTableListener(this);
282 }
283}
284
285std::string Relay::GetSmartDashboardType() const { return "Relay"; }
286
287void Relay::InitTable(std::shared_ptr<ITable> subTable) {
288 m_table = subTable;
289 UpdateTable();
290}
291
292std::shared_ptr<ITable> Relay::GetTable() const { return m_table; }