Class Flywheel

java.lang.Object
com.crossbotics.ftchelper.launcher.Flywheel
All Implemented Interfaces:
Subsystem

public class Flywheel extends Object implements Subsystem
A flywheel (shooter wheel) that holds a speed in RPM, says when it is ready, and notices shots. (Last season's Launcher, made general.)

 Flywheel flywheel = Flywheel.create(hardwareMap, "flywheel")
         .countsPerRev(28)            // encoder counts per FLYWHEEL turn (see below)
         .maxRpm(6000)                // the motor's top speed (a goBILDA 6000 RPM motor)
         .startingGains()             // the hub's speed control: a start, then the Flywheel Finder
         .tolerance(75)               // within 75 RPM...
         .settleTime(0.15);           // ...for 0.15 s = ready
 flywheel.setRpm(3200);
 // every loop:
 flywheel.update();
 if (flywheel.isReady()) { ...feed a ball... }
 

countsPerRev = encoder counts for ONE turn of the flywheel. Motor encoder on a 1:1 flywheel: 28 (goBILDA/REV bare motor). With gears between motor and flywheel, multiply by (motor turns per flywheel turn). An encoder on the flywheel shaft plugged into this motor's encoder port: that encoder's counts per turn (REV Through Bore: 8192). Check that the hub reads your top speed correctly (the speed on telemetry must match a tachometer or a slow-motion video).

Speed control runs on the hub (RUN_USING_ENCODER), so it is fast and smooth. A dropping battery still changes the result: the hub's F gain gives the same POWER for a speed, and the same power on a weaker battery spins the wheel slower (the I gain corrects it, but slowly). With batteryCompensation(java.util.function.DoubleSupplier, double) the F gain is scaled by (tuned volts / volts now), so the wheel gets the same push at any battery level:


 flywheel.batteryCompensation(Hw.batteryReader(hardwareMap), 12.6);   // 12.6 V: from the Flywheel Finder
 

RPM = revolutions (turns) per minute. The hub works in encoder counts per second, so this class converts between the two. Tutorial 17 builds a shooter step by step. Docs: docs/modules/launcher.md

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final double
    The battery used is the HIGHEST reading of the last this-many seconds.
    static final double
    How often the battery is read, seconds.
    static final double
    The volts the gains are for when nothing else is said: a charged battery under load (as Feedforward).
    static final double
    Below this many volts the Driver Station warns that the battery is low.
    static final double
    The F scale is kept between these, so a wrong reading cannot make the wheel run away.
    static final double
    The F scale is kept between these, so a wrong reading cannot make the wheel run away.
    static final double
    The F gain is changed only when the new scale differs by more than this share (2%).
  • Constructor Summary

    Constructors
    Constructor
    Description
    Flywheel(String name, com.qualcomm.robotcore.hardware.DcMotorEx motor)
    Wraps a motor you already set up (RUN_USING_ENCODER).
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Shows measured / target RPM and READY; shots seen and the battery (volts, F scale) in debug mode; a warning for a fault or a low battery.
    batteryCompensation(DoubleSupplier batteryVolts, double nominalVolts)
    Battery compensation: every BATTERY_READ_SEC s it reads the battery and gives the hub F x (nominalVolts / volts), kept to MIN_F_SCALE..MAX_F_SCALE, sent again only when that changes by more than 2%.
    countsPerRev(double counts)
    Encoder counts per ONE flywheel turn.
    static Flywheel
    create(com.qualcomm.robotcore.hardware.HardwareMap hardwareMap, String name)
    Gets the motor: coast (a flywheel must spin down freely), speed control on.
    follower(com.qualcomm.robotcore.hardware.HardwareMap hardwareMap, String followerName, boolean reversed)
    A second motor on the same flywheel.
    double
    The battery the F scale uses (volts): the highest reading of the last BATTERY_FILTER_SEC s.
    "" or a problem, e.g.
    double
    What F is multiplied by for the battery now (1 = as tuned, or compensation off).
    double
    The highest speed it may be asked for (see maxRpm).
    com.qualcomm.robotcore.hardware.DcMotorEx
    The FTC motor inside, for anything this helper does not do.
    double
    The measured speed (RPM).
    int
    How many shots were noticed (speed drops) since the OpMode started.
    double
    A drop of more than this many RPM counts as a shot (shotDrop(double)).
    double
    The target speed (RPM).
    double
    The ready band: within this many RPM of the target (for the settle time) = ready.
    boolean
    boolean
    True when at speed and steady.
    maxRpm(double max)
    The highest speed it may be asked for, in RPM.
    The name shown in telemetry and in the hardware check.
    Battery compensation off: the hub gets F exactly as tuned (the default).
    pidf(double p, double i, double d, double f)
    The hub's speed-control gains (P, I, D, F).
    Spins the other way for positive speeds.
    void
    setRpm(double newRpm)
    Spins to this speed in RPM (0 = coast to a stop).
    settleTime(double seconds)
    ...for at least this long, in seconds.
    shotDrop(double rpmDrop)
    A drop of more than this many RPM after being ready counts as a shot.
    spinUpStep(double targetRpmValue, double timeoutSec)
    A step (autonomous): spin up and wait until ready (or timeoutSec).
    Uses startingPidf(double) with this flywheel's maxRpm and countsPerRev (set those first): gains for a first run when you have no tuned ones yet.
    static double[]
    startingPidf(double topTicksPerSec)
    Starting speed-control gains from a top speed, before anything is tuned: {P, I, D, F}.
    void
    Coasts to a stop (no braking: a fast flywheel must spin down freely).
    tolerance(double rpmTolerance)
    Ready when within this many RPM of the target.
    void
    Once per loop: sends the speed, reads the real speed, works out "ready", and counts shots.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • DEFAULT_NOMINAL_VOLTS

      public static final double DEFAULT_NOMINAL_VOLTS
      The volts the gains are for when nothing else is said: a charged battery under load (as Feedforward).
      See Also:
    • LOW_BATTERY_VOLTS

      public static final double LOW_BATTERY_VOLTS
      Below this many volts the Driver Station warns that the battery is low.
      See Also:
    • BATTERY_READ_SEC

      public static final double BATTERY_READ_SEC
      How often the battery is read, seconds.
      See Also:
    • BATTERY_FILTER_SEC

      public static final double BATTERY_FILTER_SEC
      The battery used is the HIGHEST reading of the last this-many seconds. Why: while the drive or the flywheel itself pulls hard, the battery dips for a moment and comes back. A dip is not a weaker battery: scaling F up for it would push the wheel past its speed just as the load ends. A battery that is really lower reads low for the whole second, and then F is scaled.
      See Also:
    • RESEND_SHARE

      public static final double RESEND_SHARE
      The F gain is changed only when the new scale differs by more than this share (2%).
      See Also:
    • MIN_F_SCALE

      public static final double MIN_F_SCALE
      The F scale is kept between these, so a wrong reading cannot make the wheel run away.
      See Also:
    • MAX_F_SCALE

      public static final double MAX_F_SCALE
      The F scale is kept between these, so a wrong reading cannot make the wheel run away.
      See Also:
  • Constructor Details

    • Flywheel

      public Flywheel(String name, com.qualcomm.robotcore.hardware.DcMotorEx motor)
      Wraps a motor you already set up (RUN_USING_ENCODER).
  • Method Details

    • startingPidf

      public static double[] startingPidf(double topTicksPerSec)
      Starting speed-control gains from a top speed, before anything is tuned: {P, I, D, F}. The hub's full power is 32767, so F = 32767 / (top speed in encoder counts per second) gives the right power for any speed by itself; P = F / 10 and I = P / 10 fix what is left; D = 0. This is REV's formula (docs.revrobotics.com/first-global/fgc-pid) and the Flywheel Finder's. With the parts catalog's top speed (a goBILDA 6000 RPM motor: 6000 RPM x 28 counts / 60 = 2800 counts/s) it gives F 11.70, P 1.17, I 0.117: the wheel spins close to the asked speed on the first run. The Flywheel Finder measures the real top speed of YOUR wheel, a little lower, and gives a slightly bigger F.
      
       double[] g = Flywheel.startingPidf(6000 * 28 / 60.0);   // {1.17, 0.117, 0, 11.70}
       flywheel.pidf(g[0], g[1], g[2], g[3]);
       
      Parameters:
      topTicksPerSec - the speed at full power, encoder counts per second (sign ignored)
      Returns:
      {P, I, D, F}, or all zeros if the top speed is under 1 count per second
    • startingGains

      public Flywheel startingGains()
      Uses startingPidf(double) with this flywheel's maxRpm and countsPerRev (set those first): gains for a first run when you have no tuned ones yet. Your own pidf(double, double, double, double) replaces them.
      
       Flywheel.create(hardwareMap, "flywheel").countsPerRev(28).maxRpm(6000).startingGains();
       
      Returns:
      this flywheel
    • create

      public static Flywheel create(com.qualcomm.robotcore.hardware.HardwareMap hardwareMap, String name)
      Gets the motor: coast (a flywheel must spin down freely), speed control on.
    • reversed

      public Flywheel reversed()
      Spins the other way for positive speeds.
    • follower

      public Flywheel follower(com.qualcomm.robotcore.hardware.HardwareMap hardwareMap, String followerName, boolean reversed)
      A second motor on the same flywheel. It gets the same speed command, so it needs its own encoder cable (the hub's speed control reads it). If its encoder reads nothing while the wheel spins, the follower is switched off and a warning shows (without an encoder, its speed control would drive it to full power and fight the first motor). It gets the same pidf(double, double, double, double) gains, whether you call pidf before or after this.
    • countsPerRev

      public Flywheel countsPerRev(double counts)
      Encoder counts per ONE flywheel turn. Default 28.
    • pidf

      public Flywheel pidf(double p, double i, double d, double f)
      The hub's speed-control gains (P, I, D, F). Applied to the follower too (in any order). F is the main one: the steady power for a given speed. With batteryCompensation(java.util.function.DoubleSupplier, double), the hub gets F x (tuned volts / volts now); these numbers stay the ones you tuned.
    • batteryCompensation

      public Flywheel batteryCompensation(DoubleSupplier batteryVolts, double nominalVolts)
      Battery compensation: every BATTERY_READ_SEC s it reads the battery and gives the hub F x (nominalVolts / volts), kept to MIN_F_SCALE..MAX_F_SCALE, sent again only when that changes by more than 2%. "volts" is the highest reading of the last BATTERY_FILTER_SEC s, so a short dip while the robot pulls hard does not raise F. Why: F is a POWER per speed, and the same power spins the wheel slower on a weaker battery; scaled, the wheel gets the same push at 11.5 V as at 12.6 V, so it reaches its speed and gets back to it after a shot as fast as when it was tuned. P and I stay as tuned. Needs pidf(double, double, double, double) (nothing to scale without an F). A reading outside 7..16 V (no sensor, a bad or too old reading) is skipped and the last scale kept. Below LOW_BATTERY_VOLTS the Driver Station warns.

      Give it Hw.batteryReader(hardwareMap): it asks the hubs on the library's helper thread, so the loop never waits for them. (Asking the hubs yourself, () -> Hw.batteryVolts(hardwareMap), holds up the loop a few milliseconds every 0.25 s.)

      
       Flywheel.create(hardwareMap, "flywheel").pidf(1.17, 0.117, 0, 11.70)
               .batteryCompensation(Hw.batteryReader(hardwareMap), 12.6);
       
      Parameters:
      batteryVolts - where the volts come from, e.g. Hw.batteryReader(hardwareMap); asked every BATTERY_READ_SEC s in update(), so it must answer at once
      nominalVolts - the battery voltage the gains were tuned at (the Flywheel Finder shows it), 9 to 14 V; DEFAULT_NOMINAL_VOLTS if you do not know it
    • noBatteryCompensation

      public Flywheel noBatteryCompensation()
      Battery compensation off: the hub gets F exactly as tuned (the default).
    • tolerance

      public Flywheel tolerance(double rpmTolerance)
      Ready when within this many RPM of the target. Default 75.
    • settleTime

      public Flywheel settleTime(double seconds)
      ...for at least this long, in seconds. Stops one lucky reading from counting. Default 0.15.
    • maxRpm

      public Flywheel maxRpm(double max)
      The highest speed it may be asked for, in RPM. Default 6000.
    • shotDrop

      public Flywheel shotDrop(double rpmDrop)
      A drop of more than this many RPM after being ready counts as a shot. Default 150.
    • setRpm

      public void setRpm(double newRpm)
      Spins to this speed in RPM (0 = coast to a stop). Changing the target restarts the "ready" timer. Safe to call every loop with the same (or almost the same) speed.
    • stop

      public void stop()
      Coasts to a stop (no braking: a fast flywheel must spin down freely).
      Specified by:
      stop in interface Subsystem
    • getFault

      public String getFault()
      "" or a problem, e.g. the follower's encoder not reading.
    • update

      public void update()
      Once per loop: sends the speed, reads the real speed, works out "ready", and counts shots.
      Specified by:
      update in interface Subsystem
    • isReady

      public boolean isReady()
      True when at speed and steady.
    • getMaxRpm

      public double getMaxRpm()
      The highest speed it may be asked for (see maxRpm).
    • getShotDrop

      public double getShotDrop()
      A drop of more than this many RPM counts as a shot (shotDrop(double)).
    • getTolerance

      public double getTolerance()
      The ready band: within this many RPM of the target (for the settle time) = ready.
    • getRpm

      public double getRpm()
      The measured speed (RPM).
    • getTargetRpm

      public double getTargetRpm()
      The target speed (RPM).
    • getShotCount

      public int getShotCount()
      How many shots were noticed (speed drops) since the OpMode started.
    • getBatteryVolts

      public double getBatteryVolts()
      The battery the F scale uses (volts): the highest reading of the last BATTERY_FILTER_SEC s. NaN before the first reading or without batteryCompensation(java.util.function.DoubleSupplier, double).
    • getFScale

      public double getFScale()
      What F is multiplied by for the battery now (1 = as tuned, or compensation off).
    • hasBatteryCompensation

      public boolean hasBatteryCompensation()
    • spinUpStep

      public AutoStep spinUpStep(double targetRpmValue, double timeoutSec)
      A step (autonomous): spin up and wait until ready (or timeoutSec).
    • getMotor

      public com.qualcomm.robotcore.hardware.DcMotorEx getMotor()
      The FTC motor inside, for anything this helper does not do.
    • name

      public String name()
      Description copied from interface: Subsystem
      The name shown in telemetry and in the hardware check.
      Specified by:
      name in interface Subsystem
    • addTelemetry

      public void addTelemetry(RobotTelemetry t)
      Shows measured / target RPM and READY; shots seen and the battery (volts, F scale) in debug mode; a warning for a fault or a low battery.
      Specified by:
      addTelemetry in interface Subsystem