Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added relay example. #80

Merged
merged 4 commits into from
Dec 3, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added relay example.
  • Loading branch information
mbardoe committed Dec 2, 2023
commit 5798d7b4e16f85641de502a31ff4e111e3420c47
48 changes: 48 additions & 0 deletions relay/robot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env python3

import wpilib


class MyRobot(wpilib.TimedRobot):
"""This is a sample program which uses joystick buttons to control a relay.
A Relay (generally a spike) has two outputs, each of which can be at either
0V or 12V and so can be used for actions such as turning a motor off, full
forwards, or full reverse, and is generally used on the compressor. This
program uses two buttons on a joystick and each button corresponds to one
output; pressing the button sets the output to 12V and releasing sets it to 0V.
"""

def robotInit(self):
"""Robot initialization function"""
# create Relay object
self.relay = wpilib.Relay(0)

# create joystick object
self.joystickChannel = 0 # usb number in DriverStation
self.joystick = wpilib.Joystick(self.joystickChannel)

# create variables to define the buttons
self.relayForwardButton = 1
self.relayReverseButton = 2

def teleopPeriodic(self):
"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment isn't present in the original example, so let's omit it.

The relay can be set several ways. It has two outputs which each can be set for either 0V or 12V.
This code uses buttons on a joystick to set each of the outputs.
"""

forward = self.joystick.getRawButton(self.relayForwardButton)
reverse = self.joystick.getRawButton(self.relayReverseButton)

if forward and reverse:
self.relay.set(wpilib.Relay.Value.kOn)
elif forward:
self.relay.set(wpilib.Relay.Value.kForward)
elif reverse:
self.relay.set(wpilib.Relay.Value.kReverse)
else:
self.relay.set(wpilib.Relay.Value.kOff)


if __name__ == "__main__":
wpilib.run(MyRobot)