Class Flywheel
- All Implemented Interfaces:
Subsystem
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
FieldsModifier and TypeFieldDescriptionstatic final doubleThe battery used is the HIGHEST reading of the last this-many seconds.static final doubleHow often the battery is read, seconds.static final doubleThe volts the gains are for when nothing else is said: a charged battery under load (asFeedforward).static final doubleBelow this many volts the Driver Station warns that the battery is low.static final doubleThe F scale is kept between these, so a wrong reading cannot make the wheel run away.static final doubleThe F scale is kept between these, so a wrong reading cannot make the wheel run away.static final doubleThe F gain is changed only when the new scale differs by more than this share (2%). -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidShows 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: everyBATTERY_READ_SECs it reads the battery and gives the hub F x (nominalVolts / volts), kept toMIN_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 FlywheelGets 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.doubleThe battery the F scale uses (volts): the highest reading of the lastBATTERY_FILTER_SECs.getFault()"" or a problem, e.g.doubleWhat F is multiplied by for the battery now (1 = as tuned, or compensation off).doubleThe highest speed it may be asked for (seemaxRpm).com.qualcomm.robotcore.hardware.DcMotorExgetMotor()The FTC motor inside, for anything this helper does not do.doublegetRpm()The measured speed (RPM).intHow many shots were noticed (speed drops) since the OpMode started.doubleA drop of more than this many RPM counts as a shot (shotDrop(double)).doubleThe target speed (RPM).doubleThe ready band: within this many RPM of the target (for the settle time) = ready.booleanTrue whilebatteryCompensation(java.util.function.DoubleSupplier, double)is on.booleanisReady()True when at speed and steady.maxRpm(double max) The highest speed it may be asked for, in RPM.name()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).reversed()Spins the other way for positive speeds.voidsetRpm(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 (ortimeoutSec).UsesstartingPidf(double)with this flywheel'smaxRpmandcountsPerRev(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}.voidstop()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.voidupdate()Once per loop: sends the speed, reads the real speed, works out "ready", and counts shots.
-
Field Details
-
DEFAULT_NOMINAL_VOLTS
public static final double DEFAULT_NOMINAL_VOLTSThe volts the gains are for when nothing else is said: a charged battery under load (asFeedforward).- See Also:
-
LOW_BATTERY_VOLTS
public static final double LOW_BATTERY_VOLTSBelow this many volts the Driver Station warns that the battery is low.- See Also:
-
BATTERY_READ_SEC
public static final double BATTERY_READ_SECHow often the battery is read, seconds.- See Also:
-
BATTERY_FILTER_SEC
public static final double BATTERY_FILTER_SECThe 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_SHAREThe 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_SCALEThe 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_SCALEThe F scale is kept between these, so a wrong reading cannot make the wheel run away.- See Also:
-
-
Constructor Details
-
Flywheel
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
UsesstartingPidf(double)with this flywheel'smaxRpmandcountsPerRev(set those first): gains for a first run when you have no tuned ones yet. Your ownpidf(double, double, double, double)replaces them.Flywheel.create(hardwareMap, "flywheel").countsPerRev(28).maxRpm(6000).startingGains();- Returns:
- this flywheel
-
create
Gets the motor: coast (a flywheel must spin down freely), speed control on. -
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 samepidf(double, double, double, double)gains, whether you call pidf before or after this. -
countsPerRev
Encoder counts per ONE flywheel turn. Default 28. -
pidf
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. WithbatteryCompensation(java.util.function.DoubleSupplier, double), the hub gets F x (tuned volts / volts now); these numbers stay the ones you tuned. -
batteryCompensation
Battery compensation: everyBATTERY_READ_SECs it reads the battery and gives the hub F x (nominalVolts / volts), kept toMIN_F_SCALE..MAX_F_SCALE, sent again only when that changes by more than 2%. "volts" is the highest reading of the lastBATTERY_FILTER_SECs, 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. Needspidf(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. BelowLOW_BATTERY_VOLTSthe 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 everyBATTERY_READ_SECs inupdate(), so it must answer at oncenominalVolts- the battery voltage the gains were tuned at (the Flywheel Finder shows it), 9 to 14 V;DEFAULT_NOMINAL_VOLTSif you do not know it
-
noBatteryCompensation
Battery compensation off: the hub gets F exactly as tuned (the default). -
tolerance
Ready when within this many RPM of the target. Default 75. -
settleTime
...for at least this long, in seconds. Stops one lucky reading from counting. Default 0.15. -
maxRpm
The highest speed it may be asked for, in RPM. Default 6000. -
shotDrop
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). -
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. -
isReady
public boolean isReady()True when at speed and steady. -
getMaxRpm
public double getMaxRpm()The highest speed it may be asked for (seemaxRpm). -
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 lastBATTERY_FILTER_SECs. NaN before the first reading or withoutbatteryCompensation(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()True whilebatteryCompensation(java.util.function.DoubleSupplier, double)is on. -
spinUpStep
A step (autonomous): spin up and wait until ready (ortimeoutSec). -
getMotor
public com.qualcomm.robotcore.hardware.DcMotorEx getMotor()The FTC motor inside, for anything this helper does not do. -
name
Description copied from interface:SubsystemThe name shown in telemetry and in the hardware check. -
addTelemetry
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:
addTelemetryin interfaceSubsystem
-