WPILibC++ 2024.3.2
Relay.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <memory>
8#include <string>
9
10#include <hal/Types.h>
13
14#include "frc/MotorSafety.h"
15
16namespace frc {
17
18/**
19 * Class for Spike style relay outputs.
20 *
21 * Relays are intended to be connected to spikes or similar relays. The relay
22 * channels controls a pair of pins that are either both off, one on, the other
23 * on, or both on. This translates into two spike outputs at 0v, one at 12v and
24 * one at 0v, one at 0v and the other at 12v, or two spike outputs at 12V. This
25 * allows off, full forward, or full reverse control of motors without variable
26 * speed. It also allows the two channels (forward and reverse) to be used
27 * independently for something that does not care about voltage polarity (like
28 * a solenoid).
29 */
30class Relay : public MotorSafety,
31 public wpi::Sendable,
32 public wpi::SendableHelper<Relay> {
33 public:
34 /**
35 * The state to drive a Relay to.
36 */
37 enum Value {
38 /// Off.
40 /// On.
42 /// Forward.
44 /// Reverse.
46 };
47
48 /**
49 * The Direction(s) that a relay is configured to operate in.
50 */
51 enum Direction {
52 /// Both directions are valid.
54 /// Only forward is valid.
56 /// Only reverse is valid.
58 };
59
60 /**
61 * Relay constructor given a channel.
62 *
63 * This code initializes the relay and reserves all resources that need to be
64 * locked. Initially the relay is set to both lines at 0v.
65 *
66 * @param channel The channel number (0-3).
67 * @param direction The direction that the Relay object will control.
68 */
69 explicit Relay(int channel, Direction direction = kBothDirections);
70
71 /**
72 * Free the resource associated with a relay.
73 *
74 * The relay channels are set to free and the relay output is turned off.
75 */
76 ~Relay() override;
77
78 Relay(Relay&&) = default;
79 Relay& operator=(Relay&&) = default;
80
81 /**
82 * Set the relay state.
83 *
84 * Valid values depend on which directions of the relay are controlled by the
85 * object.
86 *
87 * When set to kBothDirections, the relay can be any of the four states:
88 * 0v-0v, 0v-12v, 12v-0v, 12v-12v
89 *
90 * When set to kForwardOnly or kReverseOnly, you can specify the constant for
91 * the direction or you can simply specify kOff and kOn. Using only kOff and
92 * kOn is recommended.
93 *
94 * @param value The state to set the relay.
95 */
96 void Set(Value value);
97
98 /**
99 * Get the Relay State
100 *
101 * Gets the current state of the relay.
102 *
103 * When set to kForwardOnly or kReverseOnly, value is returned as kOn/kOff not
104 * kForward/kReverse (per the recommendation in Set).
105 *
106 * @return The current state of the relay as a Relay::Value
107 */
108 Value Get() const;
109
110 int GetChannel() const;
111
112 // MotorSafety interface
113 void StopMotor() override;
114
115 std::string GetDescription() const override;
116
117 void InitSendable(wpi::SendableBuilder& builder) override;
118
119 private:
120 int m_channel;
121 Direction m_direction;
122
123 hal::Handle<HAL_RelayHandle> m_forwardHandle;
124 hal::Handle<HAL_RelayHandle> m_reverseHandle;
125};
126
127} // namespace frc
The Motor Safety feature acts as a watchdog timer for an individual motor.
Definition: MotorSafety.h:25
Class for Spike style relay outputs.
Definition: Relay.h:32
void InitSendable(wpi::SendableBuilder &builder) override
Initializes this Sendable object.
int GetChannel() const
Direction
The Direction(s) that a relay is configured to operate in.
Definition: Relay.h:51
@ kForwardOnly
Only forward is valid.
Definition: Relay.h:55
@ kReverseOnly
Only reverse is valid.
Definition: Relay.h:57
@ kBothDirections
Both directions are valid.
Definition: Relay.h:53
Value Get() const
Get the Relay State.
Relay & operator=(Relay &&)=default
void StopMotor() override
Called to stop the motor when the timeout expires.
std::string GetDescription() const override
Returns a description to print when an error occurs.
void Set(Value value)
Set the relay state.
Value
The state to drive a Relay to.
Definition: Relay.h:37
@ kForward
Forward.
Definition: Relay.h:43
@ kOff
Off.
Definition: Relay.h:39
@ kReverse
Reverse.
Definition: Relay.h:45
@ kOn
On.
Definition: Relay.h:41
~Relay() override
Free the resource associated with a relay.
Relay(int channel, Direction direction=kBothDirections)
Relay constructor given a channel.
Relay(Relay &&)=default
Helper class for building Sendable dashboard representations.
Definition: SendableBuilder.h:21
A helper class for use with objects that add themselves to SendableRegistry.
Definition: SendableHelper.h:19
Interface for Sendable objects.
Definition: Sendable.h:16
Definition: AprilTagPoseEstimator.h:15