Lesson 2.4: Using start() and stop() for One-Time Robot Actions
Context
start() and stop() improve robot reliability by handling transitions. start() is often used to reset the getRuntime() timer exactly when the match begins, ensuring autonomous timing is accurate. stop() acts as a fail-safe, allowing you to force motors to zero power or close files when the match ends, preventing hardware damage or data corruption.
start() and stop() Functions
While init() and loop() are required, start() and stop() are optional lifecycle methods.
start() is called once the instant the "Play" button is pressed, making it more precise than init() for resetting counters.
stop() is called once when the "Stop" button is hit or the match timer expires. This is the professional way to handle cleanup logic, such as ensuring a high-speed flywheel is commanded to setPower(0) before the program terminates.
Annotated Code
@Autonomous(name="Transition_Demo")
public class TransitionDemo extends OpMode {
@Override
public void init() {
telemetry.addData("Hub", "Standby");
}
@Override
public void start() {
// Resetting the internal clock the moment the match begins
resetRuntime();
}
@Override
public void loop() {
telemetry.addData("Match Time", getRuntime());
}
@Override
public void stop() {
// Cleanup: Ensures no logic persists after the program ends
telemetry.addData("Match", "Complete");
}
}
Fill-in-the-Blank Practice
- The
__________method is called exactly once when the driver presses the "Play" button. - To ensure all motors are deactivated at the end of a match, logic should be placed in the
__________method. - The method that runs repeatedly between the "Init" and "Play" buttons being pressed is
__________.
Show answers
start()stop()init_loop()
Challenge
Your team needs to reset the robot's timer at the start of the match and display a "Robot Shutdown" message when the program is stopped.
Show answer
@Override
public void start() {
resetRuntime();
}
@Override
public void stop() {
telemetry.addData("Status", "Robot Shutdown");
}
Ready to move on?
Sign in with Google to save your progress with Telemark, or continue without saving.