Skip to content

Commit 5798d7b

Browse files
committed
Added relay example.
1 parent 616e7ab commit 5798d7b

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

relay/robot.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env python3
2+
3+
import wpilib
4+
5+
6+
class MyRobot(wpilib.TimedRobot):
7+
"""This is a sample program which uses joystick buttons to control a relay.
8+
A Relay (generally a spike) has two outputs, each of which can be at either
9+
0V or 12V and so can be used for actions such as turning a motor off, full
10+
forwards, or full reverse, and is generally used on the compressor. This
11+
program uses two buttons on a joystick and each button corresponds to one
12+
output; pressing the button sets the output to 12V and releasing sets it to 0V.
13+
"""
14+
15+
def robotInit(self):
16+
"""Robot initialization function"""
17+
# create Relay object
18+
self.relay = wpilib.Relay(0)
19+
20+
# create joystick object
21+
self.joystickChannel = 0 # usb number in DriverStation
22+
self.joystick = wpilib.Joystick(self.joystickChannel)
23+
24+
# create variables to define the buttons
25+
self.relayForwardButton = 1
26+
self.relayReverseButton = 2
27+
28+
def teleopPeriodic(self):
29+
"""
30+
The relay can be set several ways. It has two outputs which each can be set for either 0V or 12V.
31+
This code uses buttons on a joystick to set each of the outputs.
32+
"""
33+
34+
forward = self.joystick.getRawButton(self.relayForwardButton)
35+
reverse = self.joystick.getRawButton(self.relayReverseButton)
36+
37+
if forward and reverse:
38+
self.relay.set(wpilib.Relay.Value.kOn)
39+
elif forward:
40+
self.relay.set(wpilib.Relay.Value.kForward)
41+
elif reverse:
42+
self.relay.set(wpilib.Relay.Value.kReverse)
43+
else:
44+
self.relay.set(wpilib.Relay.Value.kOff)
45+
46+
47+
if __name__ == "__main__":
48+
wpilib.run(MyRobot)

0 commit comments

Comments
 (0)