Gray Matter
WorkshopMotion Magic in Code
WPILib 2027 is still in alpha: these pages change as the APIs settle.
LESSON 15

Motion Magic in Code

You have already run Motion Magic from the Control drop-down in Tuner X. This lesson sends the same request from code.

Branchmech-3-MotionMagic6 minutes
You’ll need
  • Buttons moving your mechanism, from Hardware Simulation.
  • Gains tuned on the bench in PID Tuning in Tuner X and Motion Magic in Tuner X.

What mechanism are you working on?

The lesson below is written for the one you pick. Switch back any time to read it for the other.

The request from Tuner X

On Motion Magic in Tuner X you set the Control drop-down to Motion Magic VoltageMotion Magic Velocity Voltage, gave it a target, and watched the armflywheel follow a profile. That drop-down picks a control request, and the Java class has the same name. Swap the VoltageOut field for it.

Arm.java: the request field
// Moves the arm to a target angle along a smooth Motion Magic ramp.
private final MotionMagicVoltage positionOut = new MotionMagicVoltage(0);
Flywheel.java: the request field
// Asks the motor to ramp to a target speed instead of jumping to it.
private final MotionMagicVelocityVoltage velocityOut = new MotionMagicVelocityVoltage(0);

Your editor offers its import.

Name targets, not volts

Replace setVoltage with setPosition, and replace runSlow and runFast with two position commands, vertical and horizontal. Keep stop and stopMotor: you may still want to stop the arm while it is moving to a target.

Arm.java: the commands
/**
* Move to vertical, 0.25 rotations or 90 degrees, and hold it there. This is the stowed
* position for transport. Never finishes.
*/
public Command vertical() {
return runRepeatedly(() -> setPosition(0.25)).named("vertical");
}
 
/**
* Move to horizontal, 0.5 rotations or 180 degrees, and hold it there. This is the ground
* intake position. Never finishes.
*/
public Command horizontal() {
return runRepeatedly(() -> setPosition(0.5)).named("horizontal");
}
 
private void setPosition(double rotations) {
motor.setControl(positionOut.withPosition(rotations));
}

The flywheel keeps all three commands and stopMotor. Replace setVoltage with setVelocity, and change the two numbers from volts to rotations per second.

Flywheel.java: what changes
/** Spin the flywheel at 25 rotations per second and hold it. Never finishes. */
public Command runSlow() {
return runRepeatedly(() -> setVelocity(25.0)).named("runSlow");
}
 
/** Spin the flywheel at 75 rotations per second and hold it. Never finishes. */
public Command runFast() {
return runRepeatedly(() -> setVelocity(75.0)).named("runFast");
}
 
private void setVelocity(double rps) {
motor.setControl(velocityOut.withVelocity(RotationsPerSecond.of(rps)));
}

Update the bindings

With closed-loop control, we often want the motor to keep holding its position. In MyTeleop.java, switch runFast to vertical and delete the whileFalse(robot.arm.stop()).

MyTeleop.java: the arm binding
// Hold the left trigger to drive the arm to its vertical position. Releasing cancels the
// command; the position request stays applied, so the arm finishes the move and holds there.
robot.driver.leftTrigger().whileTrue(robot.arm.vertical());

Releasing the trigger cancels the command, and the motor keeps applying the last request it received. On a voltage request that would have caused problems, because the motor would never have stopped turning. On a position request it is what you want: the arm holds the target against gravity.

The flywheel bindings in MyTeleop.java do not change. A wheel left on its last velocity request keeps spinning, so they keep their whileFalse.

Check your work

Start Hardware Sim Robot Code, pick Teleoperated and your OpMode, enable, and hold the binding. The build runs on the way, so a compile error shows up here.

Check

You should see

  • The arm drive to vertical and stop there, however long you hold.
  • The arm stay at vertical when you release.
  • The wheel come up to 75 rotations per second and hold it on the right trigger, then settle at 25 when you release. Plot its velocity in Tuner X to read the numbers.

A armflywheel that does not move at all may still have 0.0 values in its config, so repeat the paste from Mechanisms. One that overshoots and hunts is a tuning problem. Fix it in Tuner X with PID Tuning in Tuner X, then generate and paste again.

Check yourself

You enable, hold the binding, and the mechanism does not move. The build was clean. What do you check first?

Arm still has stop(). Why does the left-trigger binding no longer end in whileFalse(robot.arm.stop())?

You release the left trigger halfway through the move to vertical. What does the arm do?

The mechanism passes its target, comes back, and passes it again before it settles. Where does the fix go?

Pick an answer for each.