forked from robotpy/robotpy-rev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobot.py
76 lines (63 loc) · 2.89 KB
/
robot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# ----------------------------------------------------------------------------
# Copyright (c) 2017-2018 FIRST. All Rights Reserved.
# Open Source Software - may be modified and shared by FRC teams. The code
# must be accompanied by the FIRST BSD license file in the root directory of
# the project.
# ----------------------------------------------------------------------------
import rev
import wpilib
# Before Running:
# Open Shuffleboard, select File->Load Layout and select the
# shuffleboard.json that is in the root directory of this example
class Robot(wpilib.TimedRobot):
def robotInit(self):
# Create motor
self.motor = rev.CANSparkMax(1, rev.MotorType.kBrushless)
self.joystick = wpilib.Joystick(0)
# A CANDigitalInput object is constructed using the
# GetForwardLimitSwitch() or
# GetReverseLimitSwitch() method on an existing CANSparkMax object,
# depending on which direction you would like to limit
#
# Limit switches can be configured to one of two polarities:
# rev.CANDigitalInput.LimitSwitchPolarity.kNormallyOpen
# rev.CANDigitalInput.LimitSwitchPolarity.kNormallyClosed
self.forwardLimit = self.motor.getForwardLimitSwitch(
rev.LimitSwitchPolarity.kNormallyClosed
)
self.reverseLimit = self.motor.getReverseLimitSwitch(
rev.LimitSwitchPolarity.kNormallyClosed
)
self.forwardLimit.enableLimitSwitch(False)
self.reverseLimit.enableLimitSwitch(False)
wpilib.SmartDashboard.putBoolean(
"Forward Limit Enabled", self.forwardLimit.isLimitSwitchEnabled()
)
wpilib.SmartDashboard.putBoolean(
"Reverse Limit Enabled", self.forwardLimit.isLimitSwitchEnabled()
)
def teleopPeriodic(self):
# Pair motor and the joystick's Y Axis
self.motor.set(self.joystick.getY())
# enable/disable limit switches based on value read from SmartDashboard
self.forwardLimit.enableLimitSwitch(
wpilib.SmartDashboard.getBoolean("Forward Limit Enabled", False)
)
self.reverseLimit.enableLimitSwitch(
wpilib.SmartDashboard.getBoolean("Reverse Limit Enabled", False)
)
# The get() method can be used on a CANDigitalInput object to read the
# state of the switch.
#
# In this example, the polarity of the switches are set to normally
# closed. In this case, get() will return true if the switch is
# pressed. It will also return true if you do not have a switch
# connected. get() will return false when the switch is released.
wpilib.SmartDashboard.putBoolean(
"Forward Limit Switch", self.forwardLimit.get()
)
wpilib.SmartDashboard.putBoolean(
"Reverse Limit Switch", self.reverseLimit.get()
)
if __name__ == "__main__":
wpilib.run(Robot)