Building positional mechanisms (C++)
Goal: Create Arm, Elevator, and Pivot subsystems in C++ with position control and physics simulation.
The gains in this guide are placeholders, as the overview explains. They match the Java guides, so you can compare the two line by line.
Prerequisites
Install the Commands v2 YAMS vendordep (https://cdn.yassrobotics.com/yams_commands2.json); C++ is not part of the Commands v3 vendordep. Then include the main YAMS header or the specific mechanism headers. 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/positional/Arm.hpp>
#include <yams/mechanisms/positional/Elevator.hpp>
#include <yams/mechanisms/positional/Pivot.hpp>
#include <yams/mechanisms/config/ArmConfig.hpp>
#include <yams/mechanisms/config/ElevatorConfig.hpp>
#include <yams/mechanisms/config/PivotConfig.hpp>
#include <yams/gearing/GearBox.hpp>
#include <yams/gearing/MechanismGearing.hpp>
#include <yams/motorcontrollers/SmartMotorControllerConfig.hpp>
// Vendor wrappers (always included individually):
#include <yams/motorcontrollers/local/SparkWrapper.hpp>
#include <yams/motorcontrollers/local/NovaWrapper.hpp>
#include <yams/motorcontrollers/local/A301Wrapper.hpp>
#include <yams/motorcontrollers/remote/TalonFXWrapper.hpp>
#include <yams/motorcontrollers/remote/TalonFXSWrapper.hpp>WPILib 2027 C++ types live in the wpi:: namespaces (wpi::units, wpi::math, wpi::cmd) with headers under <wpi/...>:
Building an Arm
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 -> Shoulder -> mechanism in the Sim GUI. The arm is drawn at 0 degrees. Schedule SetAngleCommand(45_deg) from a button, and the arm moves to 45 degrees.
Command factories and triggers
Run(degree_t angle)
CommandPtr
Move to angle and hold indefinitely
RunTo(degree_t angle, degree_t tolerance)
CommandPtr
Move to angle, finish when within tolerance
IsNear(degree_t angle, degree_t within)
Trigger
True while within tolerance of angle
Gte(degree_t angle)
Trigger
True when current angle >= threshold
Lte(degree_t angle)
Trigger
True when current angle <= threshold
Between(degree_t start, degree_t end)
Trigger
True while between the two angles
Max()
Trigger
True when at or past upper soft limit
Min()
Trigger
True when at or past lower soft limit
Building an Elevator
Configure the motor for linear travel
Drum circumference converts rotations to linear distance. Set it on SmartMotorControllerConfig, before the linear limits and starting position that depend on it.
Check it: the project builds.
Note: Drum radius does NOT go on
ElevatorConfig; it's set onSmartMotorControllerConfigviaWithMechanismCircumference,WithMechanismDiameter, orWithMechanismRadius.
Construct the Elevator
Build the motor controller as for the arm, in Construct the motor controller, then the elevator:
Check it: run the simulation. The code starts without an ElevatorConfigurationException.
Command factories and triggers
Run(meter_t height)
CommandPtr
Move to height and hold indefinitely
RunTo(meter_t height, meter_t tolerance)
CommandPtr
Move to height, finish when within tolerance
IsNear(meter_t height, meter_t within)
Trigger
True while within tolerance of height
Gte(meter_t height)
Trigger
True when current height >= threshold
Lte(meter_t height)
Trigger
True when current height <= threshold
Between(meter_t start, meter_t end)
Trigger
True while between the two heights
Check it: open NetworkTables -> Mechanisms -> Elevator -> mechanism in the Sim GUI. Schedule elevator_->Run(1.0_m) from a button, and the carriage rises to 1 m.
Building a Pivot
A Pivot is a single-jointed rotational mechanism, like Arm, but without gravity acting on it. Use it for turrets, wrists, and other joints that turn in a level plane. It takes no arm length: give its moment of inertia directly with WithMOI on the motor config.
Notes
If it did not work
It does not build. Check the vendordep, the
#includefor your motor controller's wrapper (YAMS.hppdoes not include it), and that the hardware object and the config are members that outlive the wrapper.It throws when the code starts in simulation. The exception names the missing value, as the warning above lists.
The mechanism never moves in simulation. Check that
SimulationPeriodic()callsSimIterate(), and thatWithSubsystem(this)is set.It sags or overshoots. Expected with the placeholder gains. Tune them as in Tune it.
Troubleshooting covers more.
Examples
Complete C++ implementations from the cpptest reference project:
Related pages
Last updated
Was this helpful?
