001/**
002 * Copyright 2024 The Space Cookies : Girl Scout Troop #62868 and FRC Team #1868
003 * Open Source Software; you may modify and/or share it under the terms of
004 * the 3-Clause BSD License found in the root directory of this project.
005 */
006
007/**
008 * Copyright 2024 The Space Cookies : Girl Scout Troop #62868 and FRC Team #1868
009 * Open Source Software; you may modify and/or share it under the terms of
010 * the 3-Clause BSD License found in the root directory of this project.
011 */
012
013package tagalong.units;
014
015/**
016 * Distance units enum identifiers.
017 * Used for configuration and robot setup instead of the WPILib Java units to
018 * avoid reallocations and garbage collections in match.
019 */
020public enum DistanceUnits {
021  /**
022   * Meter
023   */
024  METER(false, 1.0),
025  /**
026   * Foot
027   */
028  FEET(false, 0.3048),
029  /**
030   * Inch
031   */
032  INCH(false, 0.0254),
033  /**
034   * Rotation
035   */
036  ROTATION(true, 1.0),
037  /**
038   * Radian
039   */
040  RADIAN(true, 0.5 / Math.PI),
041  /**
042   * Degree
043   */
044  DEGREE(true, 1.0 / 360.0);
045
046  /**
047   * True if unit measures rotational distance (degree, radians, rotation)
048   * False if unit measures linear distance (inch, foot, meter)
049   */
050  public final boolean isRotational;
051  /**
052   * If rotational, rotations per unit (conversion l.t. 1 if smaller than a
053   * rotation)
054   * If linear, meters per unit (conversion l.t. 1 if smaller than a meter)
055   */
056  public final double conversionToBase;
057
058  /**
059   *
060   * @param rotationalUnit  True if it is a rotational unit
061   * @param perUnitDist     Conversion rate to the common unit
062   */
063  DistanceUnits(boolean rotationalUnit, double perUnitDist) {
064    isRotational = rotationalUnit;
065    conversionToBase = perUnitDist;
066  }
067
068  /**
069   *
070   * @param x          double to be converted
071   * @param targetUnit target units
072   * @return x converted to the target units
073   */
074  public double convertX(double x, DistanceUnits targetUnit) {
075    if (isRotational != targetUnit.isRotational) {
076      System.err.println("Incompatible conversion between rotational and linear distances");
077      System.exit(1);
078      return 0.0;
079    } else {
080      return x * conversionToBase / targetUnit.conversionToBase;
081    }
082  }
083}