Package yams.units

Class EasyCRTConfig

java.lang.Object
yams.units.EasyCRTConfig

public class EasyCRTConfig extends Object
Configuration for the EasyCRT solver. Made by team 6911.
  • Constructor Details

    • EasyCRTConfig

      public EasyCRTConfig(Supplier<Angle> absoluteEncoder1AngleSupplier, Supplier<Angle> absoluteEncoder2AngleSupplier)
      Creates a configuration with suppliers for both absolute encoders.
      Parameters:
      absoluteEncoder1AngleSupplier - supplier that returns the angle for encoder 1
      absoluteEncoder2AngleSupplier - supplier that returns the angle for encoder 2
  • Method Details

    • withEncoderRatios

      public EasyCRTConfig withEncoderRatios(double encoder1RotPerMechanismRot, double encoder2RotPerMechanismRot)
      Sets encoder rotations per mechanism rotation directly for encoder 1 and encoder 2.

      Use this when you already know the ratios and do not want to describe the gear train. Positive ratios mean the encoder increases with positive mechanism rotation. If an encoder is mounted in reverse, and you cannot set inversion on-device, use withAbsoluteEncoder1Inverted(boolean) or withAbsoluteEncoder2Inverted(boolean) to flip the sign.

      Parameters:
      encoder1RotPerMechanismRot - rotations per mechanism rotation for encoder 1
      encoder2RotPerMechanismRot - rotations per mechanism rotation for encoder 2
      Returns:
      this configuration for chaining
    • withCommonDriveGear

      public EasyCRTConfig withCommonDriveGear(double commonRatio, int driveGearTeeth, int absoluteEncoder1PinionTeeth, int absoluteEncoder2PinionTeeth)
      Sets ratios using a shared drive gear stage.

      Formula per encoder: ratio = commonRatio * (driveGearTeeth / encoderPinionTeeth). This assumes both encoders are driven off the same drive gear after the shared reduction.

      Example: turret gearbox is 12:50 -> 10:110 and the encoders are driven by 30t and 31t gears The common ratio is 110/10, the drive gear is 50T.

      If the encoder gears are driven by the turret gear itself, the common ratio is 1, and the drive gear is the turret gear teeth.

      Also seeds CRT gear recommendation inputs (stage1 gear teeth + stage2 ratio) so you can call withCrtGearRecommendationConstraints(double, int, int, int) afterward.

      Parameters:
      commonRatio - ratio between mechanism and drive gear
      driveGearTeeth - tooth count on the gear that drives both encoder pinions
      absoluteEncoder1PinionTeeth - tooth count on encoder 1 pinion
      absoluteEncoder2PinionTeeth - tooth count on encoder 2 pinion
      Returns:
      this configuration for chaining
    • withAbsoluteEncoderOffsets

      public EasyCRTConfig withAbsoluteEncoderOffsets(Angle encoder1Offset, Angle encoder2Offset)
      Sets encoder offsets added to raw absolute readings before wrap (in rotations).

      If your encoder supports on device zeroing/offsets, configure it there and keep these offsets at zero to avoid double-offsetting.

      Parameters:
      encoder1Offset - offset to apply to encoder 1 reading
      encoder2Offset - offset to apply to encoder 2 reading
      Returns:
      this configuration for chaining
    • withMechanismRange

      public EasyCRTConfig withMechanismRange(Angle minAngle, Angle maxAngle)
      Sets the allowed mechanism angle range (in rotations).
      Parameters:
      minAngle - minimum allowed mechanism angle
      maxAngle - maximum allowed mechanism angle
      Returns:
      this configuration for chaining
    • withMatchTolerance

      public EasyCRTConfig withMatchTolerance(Angle tolerance)
      Sets the match tolerance for encoder 2 modular error (in rotations).

      Lower values are reasonable when backlash and sensor noise are low, but too small can yield no solution; high backlash mechanisms may need larger values. When backlash is the main source of error and not noise, the tolerance can often be close to the backlash.

      Experimenting with always pushing the mechanism to one side of the backlash can allow a reduced tolerance, but teams should validate this to ensure it will always solve with their specified tolerance. Tune based on getLastErrorRotations().

      Example: mechanism backlash to the encoders is about 1 degree. commonRatio = 11, driveGearTeeth = 50, and encoder2PinionTeeth = 30. Ratio = 18.3333. 1 degree of mechanism backlash is about 0.0509 rotations of the encoder. A tolerance of 0.06 rotations corresponds to about 1.18 degrees at the mechanism (0.06 / 18.33333 * 360), which is only slightly higher than the backlash.

      Parameters:
      tolerance - allowable modular error for encoder 2
      Returns:
      this configuration for chaining
    • withAbsoluteEncoder1Inverted

      public EasyCRTConfig withAbsoluteEncoder1Inverted(boolean inverted)
      If true, flips encoder 1 ratio sign (use when the sensor is mounted reversed).

      If your encoder supports on-device direction/inversion, configure it there and keep this false to avoid double-inversion.

      Parameters:
      inverted - whether encoder 1 should be inverted
      Returns:
      this configuration for chaining
    • withAbsoluteEncoder2Inverted

      public EasyCRTConfig withAbsoluteEncoder2Inverted(boolean inverted)
      If true, flips encoder 2 ratio sign (use when the sensor is mounted reversed).

      If your encoder supports on-device direction/inversion, configure it there and keep this false to avoid double-inversion.

      Parameters:
      inverted - whether encoder 2 should be inverted
      Returns:
      this configuration for chaining
    • withAbsoluteEncoderInversions

      public EasyCRTConfig withAbsoluteEncoderInversions(boolean encoder1Inverted, boolean encoder2Inverted)
      Sets inversion for both encoders in one call.
      Parameters:
      encoder1Inverted - whether encoder 1 should be inverted
      encoder2Inverted - whether encoder 2 should be inverted
      Returns:
      this configuration for chaining
    • withCrtGearRecommendationInputs

      public EasyCRTConfig withCrtGearRecommendationInputs(int stage1GearTeeth, double stage2Ratio)
      Sets the stage1 gear teeth and stage2 ratio inputs used for gear recommendations.

      Call withCrtGearRecommendationConstraints(double, int, int, int) to configure coverage and search bounds.

      stage1GearTeeth is the gear that drives both encoder pinions, and stage2Ratio is the shared "common ratio" between the mechanism and that drive gear.

      Parameters:
      stage1GearTeeth - tooth count on the gear that drives both encoders
      stage2Ratio - common ratio between mechanism and drive gear
      Returns:
      this configuration for chaining
    • withCrtGearRecommendationConstraints

      public EasyCRTConfig withCrtGearRecommendationConstraints(double coverageMargin, int minTeeth, int maxTeeth, int maxIterationsLimit)
      Sets CRT gear recommendation constraints (coverage margin + search bounds).

      Call this after withCommonDriveGear(double, int, int, int) or withCrtGearRecommendationInputs(int, double).

      No-op when not running in simulation to avoid extraneous calculations on a real robot.

      Parameters:
      coverageMargin - additional coverage margin multiplier
      minTeeth - minimum tooth count to search
      maxTeeth - maximum tooth count to search
      maxIterationsLimit - maximum iterations per gear to consider valid
      Returns:
      this configuration for chaining
    • withAbsoluteEncoder1Gearing

      public EasyCRTConfig withAbsoluteEncoder1Gearing(int... teethChain)
      Defines a meshed gear chain for encoder 1, ordered from mechanism drive gear to encoder.

      Example: withAbsoluteEncoder1Gearing(50, 20, 40) means 50T drives 20T, which drives 40T on encoder 1, for a ratio of (50/20) * (20/40) = 50/40. A simpler one-stage chain would be withAbsoluteEncoder1Gearing(72, 24) for a 3:1 reduction.

      Not valid for compound same-shaft trains; use withAbsoluteEncoder1GearingStages(int...) instead.

      Parameters:
      teethChain - ordered teeth counts from mechanism gear to encoder 1 pinion
      Returns:
      this configuration for chaining
    • withAbsoluteEncoder2Gearing

      public EasyCRTConfig withAbsoluteEncoder2Gearing(int... teethChain)
      Defines a meshed gear chain for encoder 2, ordered from mechanism drive gear to encoder.

      Example: withAbsoluteEncoder2Gearing(50, 20, 40) means 50T drives 20T, which drives 40T on encoder 2, for a ratio of (50/20) * (20/40) = 50/40. A single mesh could be withAbsoluteEncoder2Gearing(60, 20) for a 3:1 reduction.

      Not valid for compound same-shaft trains; use withAbsoluteEncoder2GearingStages(int...) instead.

      Parameters:
      teethChain - ordered teeth counts from mechanism gear to encoder 2 pinion
      Returns:
      this configuration for chaining
    • withAbsoluteEncoder1GearingStages

      public EasyCRTConfig withAbsoluteEncoder1GearingStages(int... driverDrivenPairs)
      Defines explicit mesh stages for encoder 1 as (driverTeeth, drivenTeeth) pairs.

      Use this for compound trains or when you need same-shaft gears represented by separate stages. Example: withAbsoluteEncoder1GearingStages(12, 36, 18, 60) means 12T drives 36T, then 18T drives 60T. A single stage would be withAbsoluteEncoder1GearingStages(12, 60).

      Parameters:
      driverDrivenPairs - alternating driver and driven teeth counts for each stage
      Returns:
      this configuration for chaining
    • withAbsoluteEncoder2GearingStages

      public EasyCRTConfig withAbsoluteEncoder2GearingStages(int... driverDrivenPairs)
      Defines explicit mesh stages for encoder 2 as (driverTeeth, drivenTeeth) pairs.

      Use this for compound trains or when you need same-shaft gears represented by separate stages. Example: withAbsoluteEncoder2GearingStages(12, 36, 18, 60) means 12T drives 36T, then 18T drives 60T.

      Parameters:
      driverDrivenPairs - alternating driver and driven teeth counts for each stage
      Returns:
      this configuration for chaining
    • getAbsoluteEncoder1Gearing

      public MechanismGearing getAbsoluteEncoder1Gearing()
      Builds a MechanismGearing for encoder 1 from the configured chain or stages.
      Returns:
      gearing representation for encoder 1
    • getAbsoluteEncoder2Gearing

      public MechanismGearing getAbsoluteEncoder2Gearing()
      Builds a MechanismGearing for encoder 2 from the configured chain or stages.
      Returns:
      gearing representation for encoder 2
    • getAbsoluteEncoder1Angle

      public Angle getAbsoluteEncoder1Angle()
      Returns the current angle for absolute encoder 1.
      Returns:
      angle for encoder 1, or NaN rotation if supplier returns null
    • getAbsoluteEncoder2Angle

      public Angle getAbsoluteEncoder2Angle()
      Returns the current angle for absolute encoder 2.
      Returns:
      angle for encoder 2, or NaN rotation if supplier returns null
    • getAbsoluteEncoder1Offset

      public Angle getAbsoluteEncoder1Offset()
      Returns the configured offset for encoder 1 in rotations.
      Returns:
      offset applied to encoder 1
    • getAbsoluteEncoder2Offset

      public Angle getAbsoluteEncoder2Offset()
      Returns the configured offset for encoder 2 in rotations.
      Returns:
      offset applied to encoder 2
    • getMinMechanismAngle

      public Angle getMinMechanismAngle()
      Returns the minimum allowed mechanism rotations.
      Returns:
      minimum rotations for the mechanism
    • getMaxMechanismAngle

      public Angle getMaxMechanismAngle()
      Returns the maximum allowed mechanism rotations.
      Returns:
      maximum rotations for the mechanism
    • getMechanismRange

      public Angle getMechanismRange()
      Allowed mechanism travel in rotations (max - min).
      Returns:
      range of allowed mechanism motion
    • getMatchTolerance

      public Angle getMatchTolerance()
      Returns the maximum allowed modular error between predicted and measured encoder 2.
      Returns:
      tolerance in rotations
    • getEncoder1RotationsPerMechanismRotation

      public double getEncoder1RotationsPerMechanismRotation()
      Returns encoder 1 rotations per mechanism rotation (includes inversion if enabled).
      Returns:
      ratio for encoder 1
    • getEncoder2RotationsPerMechanismRotation

      public double getEncoder2RotationsPerMechanismRotation()
      Returns encoder 2 rotations per mechanism rotation (includes inversion if enabled).
      Returns:
      ratio for encoder 2
    • ratioFromChain

      public static double ratioFromChain(int... teethChain)
      Computes ratio from a simple meshed gear chain (driver/gear/.../encoder).

      Returns encoder rotations per mechanism rotation.

      Parameters:
      teethChain - ordered tooth counts from mechanism to encoder
      Returns:
      encoder rotations per mechanism rotation
    • ratioFromDriverDrivenPairs

      public static double ratioFromDriverDrivenPairs(int... driverDrivenPairs)
      Computes ratio from explicit (driver, driven) stage pairs.

      Returns encoder rotations per mechanism rotation.

      Parameters:
      driverDrivenPairs - alternating driver and driven teeth counts for each stage
      Returns:
      encoder rotations per mechanism rotation
    • ratioFromCommonDrive

      public static double ratioFromCommonDrive(double commonRatio, int driveGearTeeth, int encoderTeeth)
      Computes ratio from a shared drive stage: commonRatio * (driveGear / encoderGear).

      Returns encoder rotations per mechanism rotation.

      Parameters:
      commonRatio - ratio between mechanism and drive gear
      driveGearTeeth - tooth count on the gear that drives encoder pinions
      encoderTeeth - tooth count on the encoder pinion
      Returns:
      encoder rotations per mechanism rotation
    • getUniqueCoverage

      public Optional<Angle> getUniqueCoverage()
      Returns unique coverage in mechanism angle, if prime teeth + common scale are set.
      Returns:
      optional coverage range for the mechanism
    • coverageSatisfiesRange

      public boolean coverageSatisfiesRange()
      Returns true when the unique coverage meets or exceeds the configured maximum mechanism range.

      Returns false if coverage inputs are missing or non-finite.

      Returns:
      whether coverage satisfies the configured mechanism travel
    • isCoprime

      public static boolean isCoprime(int a, int b)
      Checks whether two integers are coprime.
      Parameters:
      a - first integer
      b - second integer
      Returns:
      true when the numbers share no common factors beyond 1
    • getRecommendedCrtGearPair

      public Optional<EasyCRTConfig.CrtGearPair> getRecommendedCrtGearPair()
      Returns a recommended CRT gear pair if gear-search inputs are configured.
      Returns:
      optional gear pair recommendation
    • findSmallestCrtGearPair

      public static EasyCRTConfig.CrtGearPair findSmallestCrtGearPair(int stage1GearTeeth, double stage2Ratio, Angle maxMechanismAngle, double coverageMargin, int minTeeth, int maxTeeth, int maxIterationsLimit)
      Finds the smallest gear pair that satisfies coverage and iteration limits.
      Parameters:
      stage1GearTeeth - tooth count on the gear that drives both encoders
      stage2Ratio - ratio between mechanism and drive gear
      maxMechanismAngle - maximum mechanism rotations to cover
      coverageMargin - multiplier to ensure enough unique coverage
      minTeeth - minimum teeth to consider
      maxTeeth - maximum teeth to consider
      maxIterationsLimit - maximum iterations allowed for a gear
      Returns:
      gear pair that meets the criteria, or null if none found