Skip to content

Commit 64b2408

Browse files
auscompgeekLucienMorey
authored andcommitted
Refactor REV Through Bore Encoder duty cycle config into helper
1 parent c16dbbe commit 64b2408

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

components/intake.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from wpimath.trajectory import TrapezoidProfile
1414

1515
from ids import DioChannel, SparkId, TalonId
16+
from utilities.rev import configure_through_bore_encoder
1617

1718

1819
class IntakeComponent:
@@ -38,8 +39,7 @@ def __init__(self, intake_mech_root: wpilib.MechanismRoot2d) -> None:
3839

3940
self.arm_motor = SparkMax(SparkId.INTAKE_ARM, SparkMax.MotorType.kBrushless)
4041
self.encoder = DutyCycleEncoder(DioChannel.INTAKE_ENCODER, math.tau, 0)
41-
self.encoder.setAssumedFrequency(975.6)
42-
self.encoder.setDutyCycleRange(1 / 1025, 1024 / 1025)
42+
configure_through_bore_encoder(self.encoder)
4343
self.encoder.setInverted(True)
4444

4545
spark_config = SparkMaxConfig()

components/vision.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from components.chassis import ChassisComponent
2424
from utilities.caching import HasPerLoopCache, cache_per_loop
2525
from utilities.game import APRILTAGS, apriltag_layout
26+
from utilities.rev import configure_through_bore_encoder
2627
from utilities.scalers import scale_value
2728

2829

@@ -88,8 +89,7 @@ def __init__(
8889
super().__init__()
8990
self.camera = PhotonCamera(name)
9091
self.encoder = wpilib.DutyCycleEncoder(encoder_id, math.tau, 0)
91-
self.encoder.setAssumedFrequency(975.6)
92-
self.encoder.setDutyCycleRange(1 / 1025, 1024 / 1025)
92+
configure_through_bore_encoder(self.encoder)
9393
# Offset of encoder in radians when facing forwards (the desired zero)
9494
# To find this value, manually point the camera forwards and record the encoder value
9595
# This has nothing to do with the servo - do it by hand!!

utilities/rev.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import rev
2+
import wpilib
23

34

45
def configure_spark_ephemeral(motor: rev.SparkBase, config: rev.SparkBaseConfig):
@@ -17,3 +18,20 @@ def configure_spark_reset_and_persist(
1718
rev.SparkBase.ResetMode.kResetSafeParameters,
1819
rev.SparkBase.PersistMode.kPersistParameters,
1920
)
21+
22+
23+
def configure_through_bore_encoder(
24+
enc: wpilib.DutyCycleEncoder, freq: float = 975.6
25+
) -> None:
26+
"""Configure a REV Through Bore Encoder absolute pulse output.
27+
28+
This avoids the roboRIO duty cycle frequency computation, which will
29+
be inaccurate at startup.
30+
31+
Args:
32+
enc: The DutyCycleEncoder to configure.
33+
freq: The assumed frequency of the encoder in Hz. Defaults to
34+
the encoder's nominal frequency per the datasheet.
35+
"""
36+
enc.setAssumedFrequency(freq)
37+
enc.setDutyCycleRange(1 / 1025, 1024 / 1025)

0 commit comments

Comments
 (0)