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

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

1

Configure the SmartMotorControllerConfig

Use WithFeedforward(const wpi::math::ArmFeedforward&) and angular position limits. The motor config must know the gear ratio for accurate position tracking.

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 an ArmConfig

WithArmLength, WithMinAngle, and WithMaxAngle are required for simulation. All other fields are optional.

Check it: the project builds.

4

Construct the Arm

Check it: run the simulation. The code starts without an ArmConfigurationException. If it throws, the message names the missing setting.

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 -> 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

Method
Returns
Description

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

In C++ the Trigger factory is IsNear(...). The Java equivalent is near(...).


Building an Elevator

1

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 on SmartMotorControllerConfig via WithMechanismCircumference, WithMechanismDiameter, or WithMechanismRadius.

2

Create an ElevatorConfig

WithCarriageMass, WithMinimumHeight, and WithMaximumHeight are required for simulation.

Check it: the project builds.

3

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.

4

Command factories and triggers

Method
Returns
Description

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.

1

Configure the motor

Check it: the project builds.

2

Create a PivotConfig

WithMinAngle and WithMaxAngle are required for simulation.

Check it: the project builds.

3

Construct the Pivot

Check it: run the simulation. The code starts without a PivotConfigurationException.

4

Command factories and triggers

Same interface as Arm: Run(), RunTo(), IsNear(), Gte(), Lte(), Between(), Max(), Min().

Check it: open NetworkTables -> Mechanisms -> Wrist -> mechanism in the Sim GUI. Schedule pivot_->Run(90_deg) from a button, and the pivot turns to 90 degrees.


Notes

Run(angle) holds the setpoint indefinitely. Use it for default positions or when the mechanism should stay put until another command interrupts. RunTo(angle, tolerance) ends once the mechanism is within tolerance; use it in command sequences to gate the next step.

If it did not work

  • It does not build. Check the vendordep, the #include for your motor controller's wrapper (YAMS.hpp does 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() calls SimIterate(), and that WithSubsystem(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:

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

Last updated

Was this helpful?