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).
The key idea: you never write the loop
Section titled “The key idea: you never write the loop”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:
buttons(...)inControls.javaruns once, at INIT.- Each line in it plugs a job into a button, like snapping a script under a
when [space] key pressedhat block. Nothing runs yet. - After ▶ (start on the Driver Hub), the library’s loop watches the buttons and runs the jobs at the right moment.
@Overrideprotected 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).
The map: block to Java
Section titled “The map: block to Java”| 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::manualgets that number every loop. - You can change a number in
Tunables.javalive: in the PRACTICE profile, hold GUIDE for 1 s to open the tuning menu (Tunables.java). Then copy the good value back intoTunables.java. atStart(...)comes from the example robotflywheelbot/Robot.java: itsdeployIntakeAction drops the intake into place once, right after ▶ (never in INIT).show(...)goes insidebuttons(...), likerumbleWhen, as a line of its own. It is in the TeleOp (Controls.java) only.
FTC Blocks: the robot blocks
Section titled “FTC Blocks: the robot blocks”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 secondg2.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.
Never an if or while in buttons()
Section titled “Never an if or while in buttons()”buttons(...) runs once, at INIT. So an if or a while in it is wrong too:
// WRONG: checked once, at INIT, and never againif (gamepad2.x) robot.intake.in();
// WRONG: INIT never ends; the robot never startswhile (true) { ... }- An
ifinbuttons(...)checks the button once, at INIT, when nobody is pressing it, and never again. Nothing happens in the match. - A
whileloop inbuttons(...)never lets INIT finish: the robot is stuck in INIT. - Do not read
gamepad2.x(orgamepad1...) yourself inbuttons(...). Use theg1/g2buttons: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) |
Count with a button
Section titled “Count with a button”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 itIn 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).
Java punctuation: a short guide
Section titled “Java punctuation: a short guide”-
;ends a statement (one command), like a period. Every line insidebuttons(...)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). -
returnhands a result back.spitOutbuilds an Action andreturns it, so the button can use it. -
randrobot: these are only names. A method names its inputs in its first line:spitOut(Robot r)calls its robotr;buttons(Robot robot, Pad g1, Pad g2)calls itrobot. It is the same robot. Why shortr: Actions use it on every line. -
thismeans “this object, the one this code is in”. InRobot.java,thisis the robot, soatStart(() -> 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::outis the short way to write() -> robot.intake.out(). -
a ? b : cpicks one of two: ifais true,b, elsec. Like the Blocks “if-then-else” value block.deployAtStart ? Actions.deployIntake(this) : Action.named("Stay folded")is one Action or the other.
- Pick your start, if you have not done it yet: get the five files and drive. Then The five files.
- Controls.java: every kind of button job.
- Actions.java: steps that wait, safely.
- Controls: every way a button can work.
Something wrong on this page? Tell us with a short GitHub form.