For the complete documentation index, see llms.txt. This page is also available as Markdown.

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>

1

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 on SmartMotorControllerConfig, not on FlyWheelConfig.

Check it: the project builds.

2

Construct the motor controller

The wrapper takes pointers to the vendor hardware object and to the config. Both must outlive the wrapper, so keep them as subsystem members. The wrapper applies the config when it is constructed, so finish configuring motorConfig first.

Check it: the project builds.

3

Create a FlyWheelConfig

WithRollerDiameter enables surface speed calculations. All fields are optional.

Moment of inertia is set on SmartMotorControllerConfig via WithMOI(), not on FlyWheelConfig. FlyWheelConfig only holds the roller diameter, telemetry name, and sim color.

Check it: the project builds.

4

Construct the FlyWheel

Check it: run the simulation. The code starts without an exception.

5

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.

6

Command factories and triggers

Method
Returns
Description

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

7

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 #include for the wrapper (YAMS.hpp does 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() calls SimIterate(), that WithSubsystem(this) is set, and that WithMOI(...) 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

RunTo(velocity, tolerance) ends once the wheel reaches the target. Use it in command sequences where the next step should not begin until the wheel is at speed.

IsNear(velocity, tolerance) (as a Trigger) stays true while the wheel remains within tolerance; use it for continuously gated logic such as a conveyor that feeds whenever the shooter is ready. (The Java equivalent is near(...).)


Examples

Complete C++ implementations from the cpptest reference project:

https://github.com/Yet-Another-Software-Suite/YAMS/blob/master/examples/cpptest/src/main/cpp/subsystems/ShooterSubsystem.cpp

Last updated

Was this helpful?