Skip to content
Work in progress. This library and its docs are new and still changing, and some parts are not yet tested on a real robot. If something is wrong or unclear,tell us with a short GitHub form.

From Blocks to Java: where each block went

Read this page if you have made programs with blocks (Scratch, FTC Blocks, or similar) but not with Java. Each block you know has a place in the robot's five files. Come back to this page with Controls.java.

Before this: Set up (if you are new to Android Studio). Read this page before Pick your start: it gives you the five files this page talks about. The reading order: Set up, Read Java and make your first edit, then this page, then Pick your start, then The five files, then Add to your robot (only the parts your robot needs).

In Scratch, a game is a forever loop: every time around, it checks the keys and moves the sprite. A robot program is the same inside: a loop that runs many times a second. Each time around (each “loop”), it reads the gamepads, tells every motor what to do, and updates the Driver Station.

With this library, you do not write that loop. The library runs it for you. Your job is different:

  1. buttons(...) in Controls.java runs once, at INIT.
  2. Each line in it plugs a job into a button, like snapping a script under a when [space] key pressed hat block. Nothing runs yet.
  3. After ▶ (start on the Driver Hub), the library’s loop watches the buttons and runs the jobs at the right moment.
@Override
protected void buttons(Robot robot, Pad g1, Pad g2) {
g2.a.press(robot.claw::open).says("open claw"); // when G2 A pressed: open the claw
g2.rt.hold(robot.intake::in, robot.intake::stop); // while G2 RT held: in; let go: stop
}

(The claw line needs a claw in Robot.java. The quickstart example robot has only an intake.)

Why it is built this way: in one loop, many jobs share the robot. If one job stops to wait, the whole robot stops: no driving, no other button. So a job must never wait. Waits happen only inside an Action (a list of steps that runs a little every loop, Actions.java) and in autonomous steps (The autos).

Block In Java with this library Where
forever / repeat until / while opModeIsActive() Written for you: the library’s loop. Of your lines, only hold(...), sticks’ use(...), rumbleWhen(...) and show(...) run every loop. inside ControlMap (the library)
when green flag clicked (the match starts) atStart(() -> Actions.deployIntake(this)); Robot.java, in define()
when [key] pressed g2.a.press(job); Controls.java, in buttons(...)
when [key] released g2.a.release(job); Controls.java
if <key pressed?> then ... else stop (while held) g2.rt.hold(robot.intake::in, robot.intake::stop); Controls.java
on / off switch (toggle) g2.b.toggle(robot.claw); Controls.java
wait 0.5 seconds (in TeleOp) Not sleep(500). In an Action: Action.named("Spit out").then(r.intake::out).waitSeconds(0.5).then(r.intake::stop) Actions.java
wait until <...> .waitUntil("lift at the top", r.lift::isAtTarget, 2.0) (2.0 = give up after 2 s) Actions.java
a stack of blocks, run in order An Action: Action.named("...").then(a).waitSeconds(t).then(b). In an autonomous: s.start(...).move(...).action(...) Actions.java, autos/
define my block (a custom block) public static Action spitOut(Robot r) { return ...; } Actions.java
the slot inside a C-block a lambda () -> { a(); b(); }, or a method reference robot.intake::out anywhere a job goes
set [maxPower] to 0.4 (a number variable) public static double maxPower = 0.4; read as Tunables.Drive.maxPower Tunables.java
set motor power / run motor robot.intake.in(), robot.lift.goTo("high"). The drive motors need no code. jobs and Actions
joystick (a reporter block) g2.leftStickY.use(robot.lift::manual); (the stick’s value, -1 to 1, every loop) Controls.java
if <sensor> then rumble g2.rumbleWhen(robot.intake::isRunning); (a buzz each time it becomes true) Controls.java
say / show variable show("Lift ticks", robot.lift::position); (the value on the Driver Station, every loop) Controls.java, in buttons(...)
x position / y position / direction (where am I?) robot.pose(): .x, .y (inches) and .headingDeg, from the odometry; show it with show("Where", robot::pose); (Add odometry) Controls.java, autonomous
go to x: y: s.driveTo("To the goal", -30, -30, 45, 0.6) (x, y, heading, power), with odometry autos/
if <sensor> then ... not an if. Pick one: g2.rumbleWhen(...) (a buzz when it becomes true), .waitUntil(...) in an Action (wait for it, then go on), or a hold(...) (runs while a button is held) Controls.java, Actions.java
if ... else inside an action not yet in an Action. Use two actions on two buttons, one for each case Actions.java, Controls.java
repeat 3 times a for loop that adds the steps to the Action 3 times, when the action is made (cookbook: repeat); or press the button again (it runs the action again) Actions.java
change [count] by 1 a variable (a field) in Controls.java, changed by a button job: see Count with a button Controls.java
broadcast / when I receive not needed: a button job or an Action calls the part’s method itself (robot.intake.in())
stop all Tap gamepad 1 BACK (pause; tap again to go on), or STOP on the Driver Station. the drive team

Notes on the table:

  • Autonomous steps DO run in order and wait, like a stack of blocks. s.move(...) finishes before the next step starts. Why the difference: in an autonomous, no driver is waiting for a button. The library still keeps every part working while a step waits.
  • Sticks give numbers. Up and right are +, from -1 to 1. robot.lift::manual gets that number every loop.
  • You can change a number in Tunables.java live: in the PRACTICE profile, hold GUIDE for 1 s to open the tuning menu (Tunables.java). Then copy the good value back into Tunables.java.
  • atStart(...) comes from the example robot flywheelbot/Robot.java: its deployIntake Action drops the intake into place once, right after ▶ (never in INIT).
  • show(...) goes inside buttons(...), like rumbleWhen, as a line of its own. It is in the TeleOp (Controls.java) only.

The FTC Blocks editor has blocks for the robot itself. Each one has a place in Java:

FTC Blocks block In Java with this library Where
waitForStart nothing: the library waits for ▶. Something right after ▶: atStart(...) (cookbook) Robot.java, in define()
repeat while opModeIsActive nothing: the library’s loop
telemetry.addData("key", value) and telemetry.update show("Lift ticks", robot.lift::position); once, in buttons(...). The library shows it every loop as Lift ticks : 350. You need no update Controls.java
gamepad1.left_stick_y (and the other sticks) driving is built in. For your own part: g2.leftStickY.use(robot.lift::manual); Controls.java
if gamepad2.y then ... g2.y.press(...), one line, once (Controls.java) Controls.java
set servo.Position to 0.75 a named position in Tunables.java (open = 0.20, closed = 0.75 in Tunables.Claw), then robot.claw.close(); any servo: robot.intakeDeploy.goTo("deployed") Tunables.java, then Controls.java or Actions.java
set motor.Direction to REVERSE drive motors: .reversed("front_left", "back_left") in the drive(...) line; a part: .reversed() on its line Robot.java
set motor.Power to 1 a command of the part: robot.intake.in(), robot.lift.goTo("high") Controls.java, Actions.java
sleep 500 .waitSeconds(0.5) in an Action or an autonomous Actions.java, autos/
if the drivers chose something before the match onlyIf(...) with a menu choice (cookbook) an autonomous

The stick-up sign. In FTC Blocks (and in the plain FTC SDK) a stick pushed up gives a negative number. So Blocks programs put a “negative” block (or -1 *) in front of gamepad1.left_stick_y. Here the sign is already fixed: every stick value the library hands you (use(...), the driving) is + when pushed up or right. Do not add the minus, or the part moves the wrong way. (You need the minus only when you read gamepad1.left_stick_y yourself, outside the library: signs and units.)

Never sleep() or a while loop in a button job

Section titled “Never sleep() or a while loop in a button job”

This compiles, but it is wrong:

// WRONG: the whole robot freezes for half a second
g2.dpadLeft.press(() -> { robot.intake.out(); sleep(500); robot.intake.stop(); });

sleep(500) stops the library’s loop for 0.5 s. During that time, nothing else runs: no driving, no other button, no safety checks. A while (...) { } loop in a job does the same, maybe forever. The Driver Station then shows a warning like this:

G2:DPAD_LEFT button job took 512 ms: the whole robot stopped that long. No sleep() or while loop in a button job: use an Action with waitSeconds (docs: Actions.java)

(The warning starts with the button, G2:DPAD_LEFT here. For a job on a gesture or a “buzz when”, it starts with a button job took ... instead.)

Do this instead (the quickstart example’s spitOut in Actions.java):

public static Action spitOut(Robot r) {
return Action.named("Spit out")
.then(r.intake::out)
.waitSeconds(0.5)
.then(r.intake::stop);
}

In Controls.java, add g2.dpadLeft.press(Actions.spitOut(robot)).says("spit out");. Why it works: the Action waits “inside itself”. Each loop it checks the time and lets the loop go on, so the robot keeps driving.

buttons(...) runs once, at INIT. So an if or a while in it is wrong too:

// WRONG: checked once, at INIT, and never again
if (gamepad2.x) robot.intake.in();
// WRONG: INIT never ends; the robot never starts
while (true) { ... }
  • An if in buttons(...) checks the button once, at INIT, when nobody is pressing it, and never again. Nothing happens in the match.
  • A while loop in buttons(...) never lets INIT finish: the robot is stuck in INIT.
  • Do not read gamepad2.x (or gamepad1...) yourself in buttons(...). Use the g1/g2 buttons: g2.x.press(robot.intake::in);. Why: the library checks those buttons every loop for you.

What to use instead of an if:

You want Use
do something when a button is pressed g2.x.press(job)
do something while a button is held g2.x.hold(job, stopJob)
a buzz when a sensor becomes true g2.rumbleWhen(robot.intake::isRunning)
wait for something, then go on .waitUntil("lift at the top", r.lift::isAtTarget, 2.0) in an Action
“if A then this, else that” two actions on two buttons (an Action has no if/else yet)

A Blocks change [count] by 1 is a field in Java: a variable that belongs to the whole file, so it keeps its value between loops. Put it in Controls.java, inside the class, above buttons(...):

private int count = 0; // a whole number, starts at 0; private = only this file uses it

In buttons(...), add a button job that changes it, and a show(...) line to see it:

g2.x.press(() -> count++); // X: count goes up by 1 (count++ means "count = count + 1")
show("Count", () -> count); // the Driver Station shows "Count : 3"

Why a field, not a variable inside buttons(...): a variable made inside buttons(...) is gone when it ends (at INIT), and Java does not let a job change it. A field stays as long as the TeleOp runs.

Check it in a simulator test (Record a test): a show(...) line is on the screen as Label : value. So after one press of X, check expect screen has "Count : 1". Check a true/false value the same way: expect screen has "Intake on : true" (Scenario files).

  • ; ends a statement (one command), like a period. Every line inside buttons(...) ends with one.

  • A chain goes on over several lines. A line that starts with . continues the line above it:

    return Action.named("Spit out") // no ; here: the statement goes on
    .then(r.intake::out)
    .waitSeconds(0.5)
    .then(r.intake::stop); // the ; is at the very end
  • { } hold a group of lines together, like the inside of a C-block. Every { has its own }.

  • "quotes" are text, used as it is: a name on the Driver Station, a motor name, a lift height ("high"). Words without quotes are names in the code (robot, intake, goTo).

  • return hands a result back. spitOut builds an Action and returns it, so the button can use it.

  • r and robot: these are only names. A method names its inputs in its first line: spitOut(Robot r) calls its robot r; buttons(Robot robot, Pad g1, Pad g2) calls it robot. It is the same robot. Why short r: Actions use it on every line.

  • this means “this object, the one this code is in”. In Robot.java, this is the robot, so atStart(() -> Actions.deployIntake(this)) hands the robot to the Action.

  • () -> starts a lambda: code you hand over now, to run later. :: is a method reference: robot.intake::out is the short way to write () -> robot.intake.out().

  • a ? b : c picks one of two: if a is true, b, else c. Like the Blocks “if-then-else” value block. deployAtStart ? Actions.deployIntake(this) : Action.named("Stay folded") is one Action or the other.

Something wrong on this page? Tell us with a short GitHub form.