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.

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.

For your robot: Set the length of your arm, and where its hard stops are.
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'speriodic()here, beforescheduler.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'ssimulationPeriodic()here withstartSimulation(...), which runs it in its ownNotifierevery 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.
@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();
}/** 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();
}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?
