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

Step 2: The arm

Describe the arm and create the Arm mechanism.

The SmartMotorController from step 1 is configured, but nothing tells it to move yet. An Arm adds what makes it an arm: a physics simulation, a drawing of the mechanism for your dashboard, and the commands and triggers you will use in step 3.

A diagram. A mechanism config, which says what the mechanism is, such as its length, mass, hard stops and how it is drawn, and a SmartMotorControllerConfig, which says how its motor behaves, such as gearing, PID, feedforward, soft limits and current, both go into the mechanism class. The mechanism then drives the simulation's physics and Mechanism2d, and the real robot's commands and telemetry.
Two configs go into one mechanism.
1

Describe the arm

ArmConfig describes the arm itself: its length and its hard limits. The simulation uses both. The hard limits are where the arm physically stops, which is different from the soft limits you set in step 1.

A side view of the arm and the angles used in this tutorial. The hard limits are minus 30 and 40 degrees: where the arm physically stops, set on ArmConfig and used by the simulation. The soft limits are minus 20 and 30 degrees: as far as the closed loop will go, set on SmartMotorControllerConfig. Inside them are the setpoints: minus 5 degrees for the A button, which is also where the arm starts, 15 degrees for the B button, and 0 degrees for the default command.
The soft limits from step 1 sit inside the hard limits from this step.

For your robot: Set the length of your arm, and where its hard stops are.

Want the details? Arms and ArmConfig

2

Create the Arm

Hand the config, and the SmartMotorController from step 1, to the Arm.

subsystems/ArmSubsystem.java
// The arm mechanism.
private final Arm arm = new Arm(armConfig, motor);
mechanisms/ArmMechanism.java
// The arm mechanism.
private final Arm arm = new Arm(armConfig, motor);
cpp/subsystems/ArmSubsystem.cpp
// The arm mechanism.
m_arm.emplace(&m_armConfig, &m_motor.value());
3

Update it every loop

Two calls keep the mechanism alive. updateTelemetry() publishes its telemetry and applies anything you change through live tuning. simIterate() runs the physics simulation, and only needs to run in simulation.

In Commands v2 and C++ the scheduler calls a subsystem's periodic methods for you, so there is nothing more to do.

In Commands v3 nothing calls a Mechanism every loop, so Robot.java does it. Robot.java has the two functions you need:

  • robotPeriodic() runs every loop, on the robot and in simulation. Call the mechanism's periodic() here, before scheduler.run(), so that the commands see fresh values.

  • simulationInit() runs once, when the simulation starts. The template does not have it yet, so add it, and start the mechanism's simulationPeriodic() here with startSimulation(...), which runs it in its own Notifier every loop, in simulation only. That keeps the simulation from adding to the robot loop's time, as Where the tutorials put things explains.

Every mechanism you add to the robot gets one line in each of the two.

Two smaller changes tidy up the template while you are here. Keep Scheduler.getDefault() in a scheduler attribute instead of asking for it every loop, and publish the scheduler with Telemetry.log(...) after it runs, so that a dashboard shows which commands are running and which mechanisms they hold.

subsystems/ArmSubsystem.java
@Override
public void periodic() {
  // Publishes the arm's telemetry, and applies anything changed through live tuning.
  arm.updateTelemetry();
}

@Override
public void simulationPeriodic() {
  // Runs the arm's physics simulation.
  arm.simIterate();
}
mechanisms/ArmMechanism.java
/** Call this from Robot.robotPeriodic(), before the scheduler runs. */
public void periodic() {
  // Publishes the arm's telemetry, and applies anything changed through live tuning.
  arm.updateTelemetry();
}

/** Robot.simulationInit() runs this in its own Notifier, every robot loop. */
public void simulationPeriodic() {
  // Runs the arm's physics simulation.
  arm.simIterate();
}
Robot.java
import java.util.ArrayList;
import java.util.List;
import org.wpilib.system.Notifier;
import org.wpilib.telemetry.Telemetry;
import first.robot.mechanisms.ArmMechanism;

public class Robot extends OpModeRobot {
  // ...
  final ArmMechanism arm = new ArmMechanism();
  // ...

  // The scheduler that runs every command.
  private final Scheduler scheduler = Scheduler.getDefault();
  // Each mechanism's simulation Notifier, kept so that it is not garbage collected.
  private final List<Notifier> simulationNotifiers = new ArrayList<>();

  // ...

  @Override
  public void robotPeriodic() {
    // Runs every loop, on the robot and in simulation.
    // Update the mechanism before the scheduler runs its commands.
    arm.periodic();
    scheduler.run();
    // Publish the scheduler, so that a dashboard shows which commands are running.
    Telemetry.log("Scheduler", scheduler, Scheduler.proto);
  }

  @Override
  public void simulationInit() {
    // Runs once, when the simulation starts.
    // Run each mechanism's simulation in its own Notifier, every robot loop.
    startSimulation("Arm", arm::simulationPeriodic);
  }

  /** Runs a mechanism's simulation in its own Notifier, every robot loop period. */
  private void startSimulation(String name, Runnable simulationPeriodic) {
    Notifier notifier = new Notifier(simulationPeriodic);
    notifier.setName(name + " Simulation");
    notifier.startPeriodic(getPeriod());
    simulationNotifiers.add(notifier);
  }
}

The complete file

The highlighted lines are new in this step.

ArmSubsystem extends SubsystemBase, so the scheduler calls periodic() and simulationPeriodic() for you.

The highlighted lines are new in this step.

Nothing calls a periodic method on a Commands v3 Mechanism, so give the class its own periodic() and simulationPeriodic() and create the mechanism in Robot.java. Then call its periodic() from Robot.robotPeriodic(), before the scheduler runs, and start its simulationPeriodic() in its own Notifier from Robot.simulationInit().

The highlighted lines are new in this step.

ArmConfig in C++ takes the two hard limits separately, with WithMinAngle and WithMaxAngle, and WithArmLength is the C++ name of withLength. The C++ mechanism configs take a telemetry name only.

Check it

Build the code again. You can already run the simulation as in step 4: the arm is drawn at -5 degrees. It does not move yet, because nothing gives it a setpoint.

Last updated

Was this helpful?