Building a flywheel subsystem (C++)
Goal: Create a FlyWheel subsystem in C++ with velocity control and physics simulation.
The gains in this guide are placeholders, as the overview explains. They match the Java flywheel guide.
Prerequisites
Install the Commands v2 YAMS vendordep (https://cdn.yassrobotics.com/yams_commands2.json). YAMS.hpp does not pull in the vendor wrappers, so include the wrapper for your motor controller explicitly.
#include <yams/YAMS.hpp>
// Or individually:
#include <yams/mechanisms/velocity/FlyWheel.hpp>
#include <yams/mechanisms/config/FlyWheelConfig.hpp>
#include <yams/gearing/MechanismGearing.hpp>
#include <yams/motorcontrollers/SmartMotorControllerConfig.hpp>
// Vendor wrapper (always included individually):
#include <yams/motorcontrollers/remote/TalonFXWrapper.hpp>
// WPILib 2027 headers used below:
#include <wpi/commands2/CommandPtr.hpp>
#include <wpi/commands2/SubsystemBase.hpp>
#include <wpi/commands2/button/Trigger.hpp>
#include <wpi/math/controller/SimpleMotorFeedforward.hpp>
#include <wpi/math/system/DCMotor.hpp>
#include <wpi/units/angular_velocity.hpp>Configure the motor for velocity control
Use WithFeedback for velocity PID and WithFeedforward(const wpi::math::SimpleMotorFeedforward<wpi::units::turns>&) for steady-state tracking. Set the zero power mode to COAST so the wheel decelerates freely.
Simulation note:
WithMOI(radius, mass)sets the moment of inertia for physics simulation. It belongs onSmartMotorControllerConfig, not onFlyWheelConfig.
Check it: the project builds.
Integrate into a subsystem
The wrapper and the mechanism are held in std::optional members and built with emplace at the end of the constructor, after the configs are filled in.
Check it: run the simulation and open NetworkTables -> Mechanisms -> Shooter -> mechanism in the Sim GUI. Schedule SpinUpCommand(kShootSpeed) from a button, and the wheel's line spins. Its velocity, in the telemetry next to it, climbs toward 4500 RPM.
Command factories and triggers
Run(degrees_per_second_t velocity)
CommandPtr
Spin at velocity, hold indefinitely
Run(meters_per_second_t surfaceSpeed)
CommandPtr
Spin at surface speed (requires WithRollerDiameter)
RunTo(degrees_per_second_t velocity, tolerance)
CommandPtr
Spin up, finish when within tolerance
IsNear(degrees_per_second_t velocity, within)
Trigger
True while within tolerance of velocity
Gte(degrees_per_second_t velocity)
Trigger
True when speed >= threshold
Lte(degrees_per_second_t velocity)
Trigger
True when speed <= threshold
Between(degrees_per_second_t start, end)
Trigger
True while between the two speeds
Sequencing shooter commands
Use RunTo to block until the wheel is at speed before feeding. The indexer is its own subsystem with its own YAMS FlyWheel (rollers, feeders, and indexers are flywheels too, even when run open loop).
Or bind the ReadyToShoot() trigger to allow feeding whenever the wheel is up:
Check it: in simulation, the indexer only runs once the shooter is within about 100 RPM of the shoot speed.
If it did not work
It does not build. Check the vendordep, the
#includefor the wrapper (YAMS.hppdoes not include it), and that the TalonFX and the config are members that outlive the wrapper.The wheel never spins in simulation. Check that
SimulationPeriodic()callsSimIterate(), thatWithSubsystem(this)is set, and thatWithMOI(...)is on the motor config.The wheel spins up very slowly or never reaches speed. Expected with the placeholder gains: most of a flywheel's voltage comes from
kV. Tune them as in Tune it.
Troubleshooting covers more.
Notes
Examples
Complete C++ implementations from the cpptest reference project:
Related pages
Last updated
Was this helpful?
