WPILibC++ 2025.3.1
Loading...
Searching...
No Matches
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/Relay.h>
11#include <hal/Types.h>
14
15#include "frc/MotorSafety.h"
16
17namespace frc {
18
19/**
20 * Class for Spike style relay outputs.
21 *
22 * Relays are intended to be connected to spikes or similar relays. The relay
23 * channels controls a pair of pins that are either both off, one on, the other
24 * on, or both on. This translates into two spike outputs at 0v, one at 12v and
25 * one at 0v, one at 0v and the other at 12v, or two spike outputs at 12V. This
26 * allows off, full forward, or full reverse control of motors without variable
27 * speed. It also allows the two channels (forward and reverse) to be used
28 * independently for something that does not care about voltage polarity (like
29 * a solenoid).
30 */
31class Relay : public MotorSafety,
32 public wpi::Sendable,
33 public wpi::SendableHelper<Relay> {
34 public:
35 /**
36 * The state to drive a Relay to.
37 */
38 enum Value {
39 /// Off.
41 /// On.
43 /// Forward.
45 /// Reverse.
47 };
48
49 /**
50 * The Direction(s) that a relay is configured to operate in.
51 */
52 enum Direction {
53 /// Both directions are valid.
55 /// Only forward is valid.
57 /// Only reverse is valid.
59 };
60
61 /**
62 * Relay constructor given a channel.
63 *
64 * This code initializes the relay and reserves all resources that need to be
65 * locked. Initially the relay is set to both lines at 0v.
66 *
67 * @param channel The channel number (0-3).
68 * @param direction The direction that the Relay object will control.
69 */
70 explicit Relay(int channel, Direction direction = kBothDirections);
71
72 Relay(Relay&&) = default;
73 Relay& operator=(Relay&&) = default;
74
75 /**
76 * Free the resource associated with a relay.
77 *
78 * The relay channels are set to free and the relay output is turned off.
79 */
80 ~Relay() override;
81
82 /**
83 * Set the relay state.
84 *
85 * Valid values depend on which directions of the relay are controlled by the
86 * object.
87 *
88 * When set to kBothDirections, the relay can be any of the four states:
89 * 0v-0v, 0v-12v, 12v-0v, 12v-12v
90 *
91 * When set to kForwardOnly or kReverseOnly, you can specify the constant for
92 * the direction or you can simply specify kOff and kOn. Using only kOff and
93 * kOn is recommended.
94 *
95 * @param value The state to set the relay.
96 */
97 void Set(Value value);
98
99 /**
100 * Get the Relay State
101 *
102 * Gets the current state of the relay.
103 *
104 * When set to kForwardOnly or kReverseOnly, value is returned as kOn/kOff not
105 * kForward/kReverse (per the recommendation in Set).
106 *
107 * @return The current state of the relay as a Relay::Value
108 */
109 Value Get() const;
110
111 int GetChannel() const;
112
113 // MotorSafety interface
114 void StopMotor() override;
115
116 std::string GetDescription() const override;
117
118 void InitSendable(wpi::SendableBuilder& builder) override;
119
120 private:
121 int m_channel;
122 Direction m_direction;
123
126};
127
128} // 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:33
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:52
@ kForwardOnly
Only forward is valid.
Definition Relay.h:56
@ kReverseOnly
Only reverse is valid.
Definition Relay.h:58
@ kBothDirections
Both directions are valid.
Definition Relay.h:54
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:38
@ kForward
Forward.
Definition Relay.h:44
@ kOff
Off.
Definition Relay.h:40
@ kReverse
Reverse.
Definition Relay.h:46
@ kOn
On.
Definition Relay.h:42
~Relay() override
Free the resource associated with a relay.
Relay(int channel, Direction direction=kBothDirections)
Relay constructor given a channel.
Relay(Relay &&)=default
A move-only C++ wrapper around a HAL handle.
Definition Types.h:96
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:21
Interface for Sendable objects.
Definition Sendable.h:16
Definition CAN.h:11