WPILibC++ 2024.1.1-beta-4
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:
36
37 /**
38 * Relay constructor given a channel.
39 *
40 * This code initializes the relay and reserves all resources that need to be
41 * locked. Initially the relay is set to both lines at 0v.
42 *
43 * @param channel The channel number (0-3).
44 * @param direction The direction that the Relay object will control.
45 */
46 explicit Relay(int channel, Direction direction = kBothDirections);
47
48 /**
49 * Free the resource associated with a relay.
50 *
51 * The relay channels are set to free and the relay output is turned off.
52 */
53 ~Relay() override;
54
55 Relay(Relay&&) = default;
56 Relay& operator=(Relay&&) = default;
57
58 /**
59 * Set the relay state.
60 *
61 * Valid values depend on which directions of the relay are controlled by the
62 * object.
63 *
64 * When set to kBothDirections, the relay can be any of the four states:
65 * 0v-0v, 0v-12v, 12v-0v, 12v-12v
66 *
67 * When set to kForwardOnly or kReverseOnly, you can specify the constant for
68 * the direction or you can simply specify kOff and kOn. Using only kOff and
69 * kOn is recommended.
70 *
71 * @param value The state to set the relay.
72 */
73 void Set(Value value);
74
75 /**
76 * Get the Relay State
77 *
78 * Gets the current state of the relay.
79 *
80 * When set to kForwardOnly or kReverseOnly, value is returned as kOn/kOff not
81 * kForward/kReverse (per the recommendation in Set).
82 *
83 * @return The current state of the relay as a Relay::Value
84 */
85 Value Get() const;
86
87 int GetChannel() const;
88
89 // MotorSafety interface
90 void StopMotor() override;
91
92 std::string GetDescription() const override;
93
94 void InitSendable(wpi::SendableBuilder& builder) override;
95
96 private:
97 int m_channel;
98 Direction m_direction;
99
100 hal::Handle<HAL_RelayHandle> m_forwardHandle;
101 hal::Handle<HAL_RelayHandle> m_reverseHandle;
102};
103
104} // 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
Definition: Relay.h:35
@ kForwardOnly
Definition: Relay.h:35
@ kReverseOnly
Definition: Relay.h:35
@ kBothDirections
Definition: Relay.h:35
Value Get() const
Get the Relay State.
Relay & operator=(Relay &&)=default
void StopMotor() override
std::string GetDescription() const override
The return value from this method is printed out when an error occurs.
void Set(Value value)
Set the relay state.
Value
Definition: Relay.h:34
@ kForward
Definition: Relay.h:34
@ kOff
Definition: Relay.h:34
@ kReverse
Definition: Relay.h:34
@ kOn
Definition: Relay.h:34
~Relay() override
Free the resource associated with a relay.
Relay(int channel, Direction direction=kBothDirections)
Relay constructor given a channel.
Relay(Relay &&)=default
Definition: SendableBuilder.h:18
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