Skip to content

Spot the Error, Part 2

“Hello World!” doesn’t show up in the console while the a button is pressed. Can you spot the error?

public class Robot extends OpModeRobot {
private final CommandXboxController xbox = new CommandXboxController(0);
public Robot() {
xbox.a().whileTrue(printHelloWorld());
}
@Override
public void robotPeriodic() {}
private Command printHelloWorld() {
return Command.noRequirements(coroutine -> {
while (true) {
System.out.println("Hello World!");
coroutine.yield();
}
})
.named("Hello World!");
}
}

Reveal The scheduler never runs, so scheduled commands never execute. Call Scheduler.getDefault().run() inside robotPeriodic():

@Override
public void robotPeriodic() {
Scheduler.getDefault().run();
}

This command is supposed to drive the robot using the joystick’s forward and rotation axes.

Command arcadeDrive(DoubleSupplier forwardThrottle, DoubleSupplier rotationThrottle) {
double forward = forwardThrottle.getAsDouble();
double rotation = rotationThrottle.getAsDouble();
return run(coroutine -> {
while (true) {
differentialDrive.arcadeDrive(forward, rotation);
coroutine.yield();
}
})
.named("Drive");
}

Can you spot the error?

Reveal The supplier values are read once, before the command starts running. The robot drives at the same speed and rotation forever, even as the joystick moves. Read the values from the suppliers inside the loop instead:

Command arcadeDrive(DoubleSupplier forwardThrottle, DoubleSupplier rotationThrottle) {
return run(coroutine -> {
while (true) {
differentialDrive.arcadeDrive(
forwardThrottle.getAsDouble(), rotationThrottle.getAsDouble());
coroutine.yield();
}
})
.named("Drive");
}