How do I set an arm's target angle?
Goal: send your Arm to an angle from a button, an autonomous routine or your own code.
This page assumes you already have an Arm in a subsystem. If you do not, build one with the Arm tutorial first; it ends with complete files you can copy.
YAMS drives the arm to a target with closed loop control: you give it an angle, and its controller keeps adjusting the voltage until the encoder says the arm is there. Open loop commands (set and setVoltage) send an output straight to the motor instead. Open loop and closed loop explains both.
An arm's target is an Angle (org.wpilib.units.measure.Angle), for example Degrees.of(25). YAMS gives you three ways to set it:
arm.run(angle)
Hold an angle for as long as a button is held, or as a default command.
No. It runs until something interrupts it.
arm.runTo(angle, tolerance)
A step in a sequence or autonomous routine: go there, then move on.
Yes, once the arm has stayed within tolerance of the angle for 0.1 seconds.
arm.setMechanismPositionSetpoint(angle)
Your own code, inside a command you already wrote.
It is a method, not a command. It sets the target once.
All three start the closed loop controller again if an open loop command stopped it. When mechanism commands end explains run and runTo with the default command.
Add the commands to your subsystem
These methods go in the subsystem (Commands v2) or mechanism (Commands v3) that owns the arm field. They are fragments: the rest of the class is in the Arm tutorial.
/** Go to an angle and hold it. Never ends on its own. */
public Command run(Angle angle) {
return arm.run(angle);
}
/** Go to an angle, then end once the arm is within tolerance. */
public Command runTo(Angle angle, Angle tolerance) {
return arm.runTo(angle, tolerance);
}
/** Set the target from your own code. */
public void setAngleSetpoint(Angle angle) {
arm.setMechanismPositionSetpoint(angle);
}/** Go to an angle and hold it. Never ends on its own. */
public Command setAngle(Angle angle) {
return arm.run(angle);
}
/** Go to an angle, then end once the arm is within tolerance. */
public Command moveTo(Angle angle, Angle tolerance) {
return arm.runTo(angle, tolerance);
}
/** Hold an angle at the lowest priority, so any other command can take over. For the default command. */
public Command hold(Angle angle) {
return run(coroutine -> coroutine.await(arm.run(angle)))
.withPriority(Command.LOWEST_PRIORITY)
.named("Arm Hold");
}
/** Set the target from your own code. */
public void setAngleSetpoint(Angle angle) {
arm.setMechanismPositionSetpoint(angle);
}Declare the three in ArmSubsystem.hpp, as the Arm tutorial does for Run and RunTo.
Bind them to buttons
Every target must be inside your soft limits. The Arm tutorial sets .withSoftLimits(Degrees.of(-20), Degrees.of(30)) in the SmartMotorControllerConfig, so -5 and 25 degrees are both reachable.
In Commands v3 a Mechanism already has a run(...) method that takes a coroutine, so the wrappers are called setAngle, moveTo and hold, as in the tutorial.
If the target comes from a Supplier<Angle> (for example an aiming calculation), run(supplier) and runTo(supplier, tolerance) follow the supplier every loop. runTo ends once the arm has been within the tolerance of the supplier's current value for 0.1 seconds.
Check it
Start the simulation (or enable the robot) and open NetworkTables > Mechanisms > Arm > mechanism in your dashboard.
Armis the name you gaveArmConfig.withTelemetry.Hold B. The drawn arm should swing to 25 degrees and stay there.
Release B. The default command takes over and the arm returns to 0 degrees.
If it did not work
The arm stops short of the target. The target is outside your soft limits. YAMS clamps the setpoint to the nearest limit and prints a warning ("Setpoint is higher than Mechanism ... upper limit") in the Driver Station. Widen the soft limits or pick a target inside them. A
runToto a clamped target never ends, because the arm never gets within tolerance.The arm sags below the target, or falls when you let go.
kGis still 0. The gains in the tutorial are placeholders; findkGand thenkPwith Step 5: Tune it.runTonever ends. The tolerance is tighter than the arm can hold. Try 2 or 3 degrees, and tunekP.Something else. See Troubleshooting.
Read more
Tune the arm so it reaches the target quickly and holds it: Arm tutorial: Tune it.
Last updated
Was this helpful?
