Timing
Control when actions happen in your robot code.
Timing is important when programming robot actions.
Examples:
- Waiting for a mechanism to move
- Adding delays between actions
- Running actions for a specific amount of time
- Creating autonomous sequences
sleep()
Thread.sleep() pauses the current thread.
Example:
sleep(1000);This waits for:
1000 milliseconds = 1 secondExample:
claw.open();
sleep(500);
claw.close();Problems with sleep()
Although useful, sleep() blocks your program.
During a sleep:
- The robot does not run other code
- Inputs are not checked
- Updates may be delayed
Avoid using long sleeps in TeleOp programs.
ElapsedTime
ElapsedTime tracks how much time has passed.
Create a timer:
ElapsedTime timer = new ElapsedTime();Reset it:
timer.reset();Read the time:
double time = timer.seconds();Example:
timer.reset();
while (opModeIsActive()) {
if (timer.seconds() > 2) {
intake.stop();
}
}Timers in Autonomous
Timers are often used for sequencing actions.
Example:
if (timer.seconds() > 5) {
shooter.start();
}State Machines
Advanced FTC code often uses timers with states.
Example:
START
↓
MOVE_ARM
↓
WAIT
↓
DROP_OBJECTEach state performs an action when certain conditions are met.
Avoid Blocking Code
Bad:
sleep(5000);The robot does nothing for five seconds.
Better:
if (timer.seconds() > 5) {
continueAction();
}The program can continue running while waiting.
Key Takeaways
- Timing controls when actions happen.
sleep()pauses your program.ElapsedTimetracks time without blocking.- Timers are useful for autonomous and state machines.
- Avoid long blocking delays.
Was this resource helpful?
