Class PhoenixOdometryThread
- All Implemented Interfaces:
Runnable
This singleton thread runs at a configurable odometry Hz (typically 250Hz), continuously
sampling drive/turn encoder positions and gyro yaw from TalonFX and Pigeon2 devices. Samples are
buffered in thread-safe BlockingQueues and drained by SwerveDrive.periodic() each robot loop iteration.
Thread Safety: All signal registration and data access is guarded by a ReentrantLock. The queues use a fixed capacity of 50 to bound memory usage.
Zero-Allocation Contract: All drain methods (getSyncData(int), getGyroYawData()) write into pre-allocated fixed-capacity arrays and return a valid sample count.
No heap allocations occur on the hot path.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classContainer for pre-allocated gyro yaw data.static classContainer for a batch of synchronized odometry samples from a single module.Nested classes/interfaces inherited from class java.lang.Thread
Thread.State, Thread.UncaughtExceptionHandler -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intMaximum number of odometry samples buffered between drains (250Hz / 50Hz = 5 typical).Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionDrains all buffered high-frequency gyro yaw samples since the last call.static PhoenixOdometryThreadReturns the singleton instance, creating and starting the thread on first access.getSyncData(int moduleId) Drains all buffered odometry samples for a specific module since the last call.voidregisterGyro(com.ctre.phoenix6.BaseStatusSignal yawPos, double odometryHz) Registers the gyro yaw signal for high-frequency sampling alongside module signals.intregisterModule(com.ctre.phoenix6.BaseStatusSignal drivePosition, com.ctre.phoenix6.BaseStatusSignal turnPosition, double odometryHz) Registers a swerve module's drive and turn position signals for high-frequency sampling.static voidStops the current instance and clears the singleton reference.voidrun()Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, countStackFrames, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, onSpinWait, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, suspend, toString, yield
-
Field Details
-
MAX_SAMPLES
public static final int MAX_SAMPLESMaximum number of odometry samples buffered between drains (250Hz / 50Hz = 5 typical).- See Also:
-
-
Constructor Details
-
PhoenixOdometryThread
public PhoenixOdometryThread()
-
-
Method Details
-
getInstance
Returns the singleton instance, creating and starting the thread on first access.- Returns:
- The global
PhoenixOdometryThreadinstance.
-
resetInstance
public static void resetInstance()Stops the current instance and clears the singleton reference. Only used for unit testing. -
registerModule
public int registerModule(com.ctre.phoenix6.BaseStatusSignal drivePosition, com.ctre.phoenix6.BaseStatusSignal turnPosition, double odometryHz) Registers a swerve module's drive and turn position signals for high-frequency sampling.- Parameters:
drivePosition- The drive motor's positionBaseStatusSignal.turnPosition- The turn motor's positionBaseStatusSignal.odometryHz- The required update frequency for these signals.- Returns:
- The module ID used to retrieve synchronized data via
getSyncData(int).
-
getSyncData
Drains all buffered odometry samples for a specific module since the last call.Zero-allocation: Returns a pre-allocated
PhoenixOdometryThread.SyncDatawith fixed-capacity arrays. CheckPhoenixOdometryThread.SyncData.validCountfor the number of populated entries. The returned object is reused — do not store references across ticks.- Parameters:
moduleId- The module ID returned byregisterModule(com.ctre.phoenix6.BaseStatusSignal, com.ctre.phoenix6.BaseStatusSignal, double).- Returns:
- A pre-allocated
PhoenixOdometryThread.SyncDatacontaining synchronized drive/turn positions and timestamps.
-
registerGyro
public void registerGyro(com.ctre.phoenix6.BaseStatusSignal yawPos, double odometryHz) Registers the gyro yaw signal for high-frequency sampling alongside module signals.- Parameters:
yawPos- The Pigeon2 yaw positionBaseStatusSignal.odometryHz- The required update frequency.
-
getGyroYawData
Drains all buffered high-frequency gyro yaw samples since the last call.Zero-allocation: Returns a pre-allocated
PhoenixOdometryThread.GyroYawDatawith a fixed-capacity array. CheckPhoenixOdometryThread.GyroYawData.validCountfor the number of populated entries. The returned object is reused — do not store references across ticks.- Returns:
- Pre-allocated container with yaw positions (rotations) accumulated since the last drain.
-
run
public void run()
-