Mechanisms
A Mechanism represents a subsystem on your robot, an arm, a claw, a drivetrain, bundling the hardware it
owns with the commands and periodic logic that control it.
interface Mechanism { fun periodic() {} fun instant(action: Runnable): Command fun infinite(action: Runnable): Command}public interface Mechanism { default void periodic() {} Command instant(Runnable action); Command infinite(Runnable action);}periodic()
Section titled “periodic()”Called once per loop for every mechanism registered on your NextRobot’s mechanisms set.
Use it for anything that needs to run continuously regardless of what commands are active,
such as reading a sensor, running a background PID loop to hold position, and so on.
instant and infinite
Section titled “instant and infinite”Both build an Ivy command scoped to the mechanism,
automatically calling requiring(this) so the scheduler treats it as exclusive to that mechanism.
This means that two commands can’t fight over the same hardware at once.
instant runs its action once;
infinite re-runs its action every loop until cancelled.
Example: a simple mechanism
Section titled “Example: a simple mechanism”class Claw : Mechanism { val servo = NextServo("clawServo")
fun open() = instant { servo.position = 0.2 } fun close() = instant { servo.position = 0.8 }}public class Claw implements Mechanism { NextServo servo = new NextServo("clawServo");
public Command open() { return instant(() -> servo.setPosition(0.2)); } public Command close() { return instant(() -> servo.setPosition(0.8)); }}Example: using periodic() for continuous logic
Section titled “Example: using periodic() for continuous logic”A mechanism can use periodic() for anything that needs to run every loop,
independent of whatever command is currently active.
For example, an intake that automatically stops once a sensor reports it has a piece:
class Intake : Mechanism { val motor = NextMotor("intakeMotor") val sensor = NextDistanceSensor("intakeSensor")
override fun periodic() { sensor.update() if (sensor.isWithinDistance(2.0)) { motor.throttle = 0.0 } }
fun run() = instant { motor.throttle = 1.0 }}public class Intake implements Mechanism { NextMotor motor = new NextMotor("intakeMotor"); NextDistanceSensor sensor = new NextDistanceSensor("intakeSensor");
@Override public void periodic() { sensor.update(); if (sensor.isWithinDistance(2.0)) { motor.setThrottle(0.0); } }
public Command run() { return instant(() -> motor.setThrottle(1.0)); }}Register the mechanism on your NextRobot so its periodic() gets called every loop.

