Skip to content

Commit df6ccb7

Browse files
committed
Update limit switch example
1 parent 0638d87 commit df6ccb7

File tree

2 files changed

+58
-22
lines changed

2 files changed

+58
-22
lines changed

examples/limit-switch/robot.py

Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,48 +16,84 @@
1616
class Robot(wpilib.TimedRobot):
1717
def robotInit(self):
1818
# Create motor
19-
self.motor = rev.CANSparkMax(1, rev.CANSparkMax.MotorType.kBrushless)
19+
self.motor = rev.SparkMax(1, rev.SparkMax.MotorType.kBrushless)
2020

2121
self.joystick = wpilib.Joystick(0)
2222

23-
# A CANDigitalInput object is constructed using the
24-
# GetForwardLimitSwitch() or
25-
# GetReverseLimitSwitch() method on an existing CANSparkMax object,
23+
# A SparkLimitSwitch object is constructed using the
24+
# getForwardLimitSwitch() or
25+
# getReverseLimitSwitch() method on an existing CANSparkMax object,
2626
# depending on which direction you would like to limit
2727
#
2828
# Limit switches can be configured to one of two polarities:
29-
# rev.CANDigitalInput.LimitSwitchPolarity.kNormallyOpen
30-
# rev.CANDigitalInput.LimitSwitchPolarity.kNormallyClosed
31-
self.forwardLimit = self.motor.getForwardLimitSwitch(
32-
rev.SparkLimitSwitch.Type.kNormallyClosed
29+
# rev.LimitSwitchConfig.Type.kNormallyOpen
30+
# rev.LimitSwitchConfig.Type.kNormallyClosed
31+
self.forwardLimit = self.motor.getForwardLimitSwitch()
32+
self.reverseLimit = self.motor.getReverseLimitSwitch()
33+
34+
self.limitConfig = rev.SparkMaxConfig()
35+
self.limitConfig.limitSwitch.forwardLimitSwitchType(
36+
rev.LimitSwitchConfig.Type.kNormallyClosed
37+
).forwardLimitSwitchEnabled(False).reverseLimitSwitchType(
38+
rev.LimitSwitchConfig.Type.kNormallyClosed
39+
).reverseLimitSwitchEnabled(
40+
False
3341
)
34-
self.reverseLimit = self.motor.getReverseLimitSwitch(
35-
rev.SparkLimitSwitch.Type.kNormallyClosed
42+
self.motor.configure(
43+
self.limitConfig,
44+
rev.SparkBase.ResetMode.kResetSafeParameters,
45+
rev.SparkBase.PersistMode.kNoPersistParameters,
3646
)
3747

38-
self.forwardLimit.enableLimitSwitch(False)
39-
self.reverseLimit.enableLimitSwitch(False)
48+
self.prevForwardLimitEnabled = (
49+
self.motor.configAccessor.limitSwitch.getForwardLimitSwitchEnabled()
50+
)
51+
self.prevReverseLimitEnabled = (
52+
self.motor.configAccessor.limitSwitch.getReverseLimitSwitchEnabled()
53+
)
4054

4155
wpilib.SmartDashboard.putBoolean(
42-
"Forward Limit Enabled", self.forwardLimit.isLimitSwitchEnabled()
56+
"Forward Limit Enabled", self.prevForwardLimitEnabled
4357
)
4458
wpilib.SmartDashboard.putBoolean(
45-
"Reverse Limit Enabled", self.forwardLimit.isLimitSwitchEnabled()
59+
"Reverse Limit Enabled", self.prevReverseLimitEnabled
4660
)
4761

4862
def teleopPeriodic(self):
4963
# Pair motor and the joystick's Y Axis
5064
self.motor.set(self.joystick.getY())
5165

5266
# enable/disable limit switches based on value read from SmartDashboard
53-
self.forwardLimit.enableLimitSwitch(
54-
wpilib.SmartDashboard.getBoolean("Forward Limit Enabled", False)
55-
)
56-
self.reverseLimit.enableLimitSwitch(
57-
wpilib.SmartDashboard.getBoolean("Reverse Limit Enabled", False)
58-
)
67+
if self.prevForwardLimitEnabled != wpilib.SmartDashboard.getBoolean(
68+
"Forward Limit Enabled", False
69+
):
70+
self.prevForwardLimitEnabled = wpilib.SmartDashboard.getBoolean(
71+
"Forward Limit Enabled", False
72+
)
73+
self.limitConfig.limitSwitch.forwardLimitSwitchEnabled(
74+
self.prevForwardLimitEnabled
75+
)
76+
self.motor.configure(
77+
self.limitConfig,
78+
rev.SparkBase.ResetMode.kResetSafeParameters,
79+
rev.SparkBase.PersistMode.kNoPersistParameters,
80+
)
81+
if self.prevReverseLimitEnabled != wpilib.SmartDashboard.getBoolean(
82+
"Reverse Limit Enabled", False
83+
):
84+
self.prevReverseLimitEnabled = wpilib.SmartDashboard.getBoolean(
85+
"Reverse Limit Enabled", False
86+
)
87+
self.limitConfig.limitSwitch.reverseLimitSwitchEnabled(
88+
self.prevReverseLimitEnabled
89+
)
90+
self.motor.configure(
91+
self.limitConfig,
92+
rev.SparkBase.ResetMode.kResetSafeParameters,
93+
rev.SparkBase.PersistMode.kNoPersistParameters,
94+
)
5995

60-
# The get() method can be used on a CANDigitalInput object to read the
96+
# The get() method can be used on a SparkLimitSwitch object to read the
6197
# state of the switch.
6298
#
6399
# In this example, the polarity of the switches are set to normally

examples/run_tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ BASE_TESTS="
77
can-arcade-drive
88
can-tank-drive
99
getting-started
10+
limit-switch
1011
"
1112

1213
IGNORED_TESTS="
1314
color_match
1415
get-set-params
15-
limit-switch
1616
maxswerve
1717
position-pid-control
1818
read_rgb_values

0 commit comments

Comments
 (0)