blob: eac39673f8f5196686399000b748816d316ea458 [file] [log] [blame]
Brian Silvermanf7f267a2017-02-04 16:16:08 -08001/*----------------------------------------------------------------------------*/
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#pragma once
9
10#include <memory>
11#include <string>
12
13#include "HAL/Types.h"
14#include "LiveWindow/LiveWindowSendable.h"
15#include "MotorSafety.h"
16#include "SensorBase.h"
17#include "tables/ITable.h"
18#include "tables/ITableListener.h"
19
20namespace frc {
21
22class MotorSafetyHelper;
23
24/**
25 * Class for Spike style relay outputs.
26 * Relays are intended to be connected to spikes or similar relays. The relay
27 * channels controls a pair of pins that are either both off, one on, the other
28 * on, or both on. This translates into two spike outputs at 0v, one at 12v and
29 * one at 0v, one at 0v and the other at 12v, or two spike outputs at 12V. This
30 * allows off, full forward, or full reverse control of motors without variable
31 * speed. It also allows the two channels (forward and reverse) to be used
32 * independently for something that does not care about voltage polarity (like
33 * a solenoid).
34 */
35class Relay : public MotorSafety,
36 public SensorBase,
37 public ITableListener,
38 public LiveWindowSendable {
39 public:
40 enum Value { kOff, kOn, kForward, kReverse };
41 enum Direction { kBothDirections, kForwardOnly, kReverseOnly };
42
43 explicit Relay(int channel, Direction direction = kBothDirections);
44 virtual ~Relay();
45
46 void Set(Value value);
47 Value Get() const;
48 int GetChannel() const;
49
50 void SetExpiration(double timeout) override;
51 double GetExpiration() const override;
52 bool IsAlive() const override;
53 void StopMotor() override;
54 bool IsSafetyEnabled() const override;
55 void SetSafetyEnabled(bool enabled) override;
56 void GetDescription(std::ostringstream& desc) const override;
57
58 void ValueChanged(ITable* source, llvm::StringRef key,
59 std::shared_ptr<nt::Value> value, bool isNew) override;
60 void UpdateTable() override;
61 void StartLiveWindowMode() override;
62 void StopLiveWindowMode() override;
63 std::string GetSmartDashboardType() const override;
64 void InitTable(std::shared_ptr<ITable> subTable) override;
65 std::shared_ptr<ITable> GetTable() const override;
66
67 std::shared_ptr<ITable> m_table;
68
69 private:
70 int m_channel;
71 Direction m_direction;
72
73 HAL_RelayHandle m_forwardHandle = HAL_kInvalidHandle;
74 HAL_RelayHandle m_reverseHandle = HAL_kInvalidHandle;
75
76 std::unique_ptr<MotorSafetyHelper> m_safetyHelper;
77};
78
79} // namespace frc