Skip to content

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
}

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.

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.

class Claw : Mechanism {
val servo = NextServo("clawServo")
fun open() = instant { servo.position = 0.2 }
fun close() = instant { servo.position = 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 }
}

Register the mechanism on your NextRobot so its periodic() gets called every loop.