|
16 | 16 | class Robot(wpilib.TimedRobot): |
17 | 17 | def robotInit(self): |
18 | 18 | # Create motor |
19 | | - self.motor = rev.CANSparkMax(1, rev.CANSparkMax.MotorType.kBrushless) |
| 19 | + self.motor = rev.SparkMax(1, rev.SparkMax.MotorType.kBrushless) |
20 | 20 |
|
21 | 21 | self.joystick = wpilib.Joystick(0) |
22 | 22 |
|
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, |
26 | 26 | # depending on which direction you would like to limit |
27 | 27 | # |
28 | 28 | # 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 |
33 | 41 | ) |
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, |
36 | 46 | ) |
37 | 47 |
|
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 | + ) |
40 | 54 |
|
41 | 55 | wpilib.SmartDashboard.putBoolean( |
42 | | - "Forward Limit Enabled", self.forwardLimit.isLimitSwitchEnabled() |
| 56 | + "Forward Limit Enabled", self.prevForwardLimitEnabled |
43 | 57 | ) |
44 | 58 | wpilib.SmartDashboard.putBoolean( |
45 | | - "Reverse Limit Enabled", self.forwardLimit.isLimitSwitchEnabled() |
| 59 | + "Reverse Limit Enabled", self.prevReverseLimitEnabled |
46 | 60 | ) |
47 | 61 |
|
48 | 62 | def teleopPeriodic(self): |
49 | 63 | # Pair motor and the joystick's Y Axis |
50 | 64 | self.motor.set(self.joystick.getY()) |
51 | 65 |
|
52 | 66 | # 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 | + ) |
59 | 95 |
|
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 |
61 | 97 | # state of the switch. |
62 | 98 | # |
63 | 99 | # In this example, the polarity of the switches are set to normally |
|
0 commit comments