Shooters
A shooter is a FlyWheel in YAMS, and so is anything else that spins: rollers, intakes, feeders and indexers.
How a flywheel works

Where an arm or an elevator goes to a position, a flywheel goes to a speed. Most of the voltage comes from the kV term of SimpleMotorFeedforward, which is the voltage that speed needs, and a small kP corrects what is left and brings the wheel back after a game piece slows it.
A roller that only ever needs a voltage is still a FlyWheel: put its motor in ControlMode.OPEN_LOOP and drive it with set or setVoltage, as in Intakes, rollers and feeders below.
Creating a flywheel
This is the shape of a shooter class, not a complete file: the imports and the motor controller object (spark, a SparkMax) are left out. Step 2 of the Shooter/FlyWheels tutorial builds the complete file. The code is the same for Commands v2 and Commands v3 except for the package you import from and the class that owns the mechanism.
this is the Subsystem. The scheduler calls periodic() and simulationPeriodic() for you.
this is the Commands v3 Mechanism. A Mechanism has no periodic hook, so call periodic() yourself from Robot.robotPeriodic(), and run simulationPeriodic() in its own Notifier, started from Robot.simulationInit(). Where the tutorials put things shows both.
The options in motorConfig are explained in SmartMotorControllerConfig, and the options in config in FlyWheelConfig.
In the rest of your code, shooter is the ShooterSubsystem (or ShooterMechanism), and flywheel is the YAMS FlyWheel inside it.
Controlling a flywheel
A flywheel takes speed targets: flywheel.run(RPM.of(3000)) spins at 3000 RPM until interrupted, flywheel.runTo(RPM.of(3000), RPM.of(100)) ends once it is at speed, and flywheel.getSpeed() reads how fast it is going. Controlling a mechanism lists every command, setpoint function and check, and When mechanism commands end explains how they work with a default command.
A shooter subsystem usually wraps them in a few command factories:
Make set(0) the default command, not run(RPM.of(0)). A speed of zero is a closed loop target, so the motor would brake the wheel to a stop against its momentum, drawing current to do it. set(0) turns the closed loop off and lets the wheel coast down, with MotorMode.COAST.
Shooting
A shot is two flywheels working together: the shooter spins up, and a feeder pushes the game piece in once the shooter is at speed. Gate the feeder on the shooter's atSpeed trigger:
While the trigger is held the shooter holds 3000 RPM, and the feeder only runs while the shooter is within 100 RPM of it. On release, both default commands take over. How do I shoot with a shooter? walks through it, and Double FlyWheel covers two wheels spinning at different speeds.
Shooting on the move
A shooter on a moving robot needs a lead: How do I shoot on the move? lists where to learn the math and which YAMS call does each part, and Shoot on the move: worked example builds a complete robot. This section explains the idea, and what the Swerve Input Stream does for you.
The idea in one picture
A game piece keeps the robot's velocity when it leaves the shooter, so a robot that aims straight at the target while driving sideways misses. The sources listed in How do I shoot on the move? all solve the same problem: work out a lead that cancels the robot's motion.

A primer: vector subtraction
Read: Eeshwar's Shooting on the Fly and Shooting on the Fly, Part 2: The LUT Approach.
Before you pick a firing strategy, it helps to see the lead as one subtraction, which is how Eeshwar's posts explain it:
The time of flight in your lookup table gives the horizontal speed of the standing shot: the distance divided by the time.
Point a velocity of that speed at the target. This is the velocity the game piece needs over the ground.
Subtract the robot's velocity. What is left is the velocity the shooter has to give the game piece.
The direction of that vector is where to aim, and its length is the speed the shot needs.

Two things follow from this, and every firing strategy has to deal with both. The aim changes when the robot moves, and so does the speed of the shot: drive toward the target and the shooter needs less, drive away and it needs more. The firing strategies in the worked example are different ways of working out that same lead from the time of flight in your table.

What SwerveInputStream does, and what it leaves to you
The stream's aim mode uses the simplest method there is, point at the pose. withAim takes a target pose, works out the direction from the robot to it, and lets the drive's rotation PID controller turn the front of the robot that way while the left stick keeps translating.

withAim: the left stick drives, and the robot keeps facing the target it is given.Four options refine it: withAimLookahead, withAimFeedforward, withAimHeadingOffset and aimLock. The Swerve Input Stream page explains each one.

withAimLookahead: the robot turns to the direction it will need a moment from now.Even with all four, the stream on its own does not shoot on the move the way you might expect:
The lookahead is one fixed time. Aiming from where the robot will be in 0.3 seconds is the same as leading the target by 0.3 seconds of travel. That is only the right lead when the time of flight is 0.3 seconds, which is true at one distance. Use the lookahead for what the sources call latency: the short, constant delay before the game piece leaves the robot.
It only turns the robot. A lead changes the distance of the shot as well as its direction. The stream knows nothing about your flywheel, your hood or your tables, so it cannot change the shot to match.
It assumes a straight line. The lookahead takes the robot's velocity right now and assumes it stays the same.
Without a feedforward, the rotation lags. The PID controller only reacts to error, so while the robot drives past the target it trails a little behind.
So let the stream do the turning, and give it the right thing to turn to. The lead itself comes from one of the methods in the sources, which is the part you write.
Intakes, rollers and feeders
A roller that only needs to push a game piece in or out does not need a speed. Make it a FlyWheel in its own subsystem, with its motor in open loop, and give it a voltage:
set takes a duty cycle from -1 to 1, and setVoltage a voltage. A voltage gives the same push as the battery drains, so prefer it. An intake is the same class with an intake() command. In Commands v3, write stop() as a lowest priority default command, as for the shooter above. How do I set up an intake roller? has complete files for both.
When a flywheel will not reach its speed
A flywheel that reaches low speeds but not high ones, or that slows down more with every shot, is usually short of power and not badly tuned: see when tuning does not help.
Configuring and tuning a flywheel, step by step
Do the configuration steps first, in simulation, then tune the real flywheel with live tuning. Tuning something that is configured wrong wastes the time.
Configure it
Set the gearing
Give the reduction from the motor to the wheel with withGearing. An overdrive, where the wheel spins faster than the motor, is a number below 1.
Choose the positive direction
Positive should be the direction that shoots or intakes. Flip withMotorInverted if it is not.
Let it coast
Use MotorMode.COAST so the wheel is not braked to a stop every time the command ends, and set a stator current limit.
Set the diameter
Set withDiameter if you want to ask for surface speeds.
Describe it to the simulator
Set withMomentOfInertia on the motor config.
What is different when tuning a flywheel
The Shooter/FlyWheels tutorial walks through tuning a flywheel, and Tuning order lists the order with the keys to watch. What sets a flywheel apart:
kV does most of the work. It is the voltage the speed needs. Find kS, then raise kV until
mechanism/velocitysettles on the setpoint with no help from kP, and check it at a low and a high speed.kP only needs to be small. Its job is to bring the wheel back after a game piece slows it. Too much makes the speed ripple.
There is no kG and no position. Nothing to hold up, and no soft limits.
Choose the tolerance by shooting. Shoot at the edges of your
atSpeedtolerance, and use the widest one where the shots still land, so the shooter is ready sooner.A brownout is not a tuning problem. If spinning up browns out the robot, add a velocity profile or a current limit, and test it against the battery simulation.
FlyWheelConfig options
Every FlyWheelConfig option is on the FlyWheelConfig page under this one, with a graphic and an example for each. Options shared by every mechanism are in Shared options.
A FlyWheel needs nothing on its config to be simulated. Its moment of inertia is on the motor config.
Code reference
Last updated
Was this helpful?
