|
| 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) |
0 commit comments