Skip to content

Commit 3534978

Browse files
authored
Merge pull request #80 from mbardoeChoate/relay
Added relay example.
2 parents 616e7ab + e5e2e14 commit 3534978

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

relay/robot.py

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

run_tests.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ BASE_TESTS="
2626
physics-4wheel/src
2727
physics-mecanum/src
2828
physics-spi/src
29+
relay
2930
shuffleboard
3031
stateful-autonomous
3132
state-space-flywheel

0 commit comments

Comments
 (0)