Class OpModeRobot
- All Implemented Interfaces:
AutoCloseable
The OpModeRobot class is intended to be subclassed by a user creating a robot program.
Classes annotated with Autonomous, Teleop, and Utility in the same
package or subpackages as the user's subclass are automatically registered as autonomous, teleop,
and utility opmodes respectively.
Opmodes are constructed when selected on the driver station. While selected and disabled,
OpMode.disabledPeriodic() is called. When enabled, OpMode.start()
is called once and OpMode.periodic() runs at the rate from getPeriod().
On disable or mode switch while enabled, OpMode.end() is called asynchronously
and the opmode is then closed and discarded. When no opmode is selected, nonePeriodic()
is called. driverStationConnected() is called once when the DS first connects.
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionConstructor with default period.OpModeRobot(double period) Constructor with specified period. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddAnnotatedOpMode(Class<? extends OpMode> cls) voidScans for classes in the specified package and all nested packages that are annotated withAutonomous,Teleop, orUtilityand registers them.voidAdds an opmode for an opmode class.voidAdds an opmode for an opmode class.voidaddOpMode(Class<? extends OpMode> cls, RobotMode mode, String name, String group, String description) Adds an opmode for an opmode class.voidaddOpMode(Class<? extends OpMode> cls, RobotMode mode, String name, String group, String description, Color textColor, Color backgroundColor) Adds an opmode for an opmode class.voidaddOpModeFactory(Supplier<OpMode> factory, RobotMode mode, String name) Adds an opmode using a factory function that creates the opmode.voidAdds an opmode using a factory function that creates the opmode.voidaddOpModeFactory(Supplier<OpMode> factory, RobotMode mode, String name, String group, String description) Adds an opmode using a factory function that creates the opmode.voidaddOpModeFactory(Supplier<OpMode> factory, RobotMode mode, String name, String group, String description, Color textColor, Color backgroundColor) Adds an opmode using a factory function that creates the opmode.voidaddPeriodic(Runnable callback, double period) Add a callback to run at a specific period.voidClears all operating mode options and publishes an empty list to the driver station.voidclose()voidFunction called once when the robot exits disabled state.voidFunction called once when the robot becomes disabled.voidFunction called periodically while the robot is disabled.voidCode that needs to know the DS state should go here.final voidEnds the main loop in startCompetition().longReturn the system clock time in microseconds for the start of the current periodic loop.doubleGet the period at which robot and opmode periodic callbacks are run.voidFunction called periodically anytime when no opmode is selected, including when the Driver Station is disconnected.voidPrints list of epochs added so far and their times.voidPublishes the operating mode options to the driver station.voidremoveOpMode(RobotMode mode, String name) Removes an operating mode option.voidFunction called periodically every loop, regardless of enabled state or OpMode selection.voidFunction called once during robot initialization in simulation.voidFunction called periodically in simulation.final voidProvide an alternate "main loop" via startCompetition().Methods inherited from class RobotBase
getMainThreadId, getOpMode, getOpModeId, getRuntimeType, isAutonomous, isAutonomousEnabled, isDisabled, isEnabled, isReal, isSimulation, isTeleop, isTeleopEnabled, isUtility, isUtilityEnabled, startRobot, suppressExitWarning
-
Field Details
-
DEFAULT_PERIOD
-
-
Constructor Details
-
OpModeRobot
public OpModeRobot()Constructor with default period. -
OpModeRobot
Constructor with specified period.- Parameters:
period- the period at which to run the robot and opmode periodic callbacks.
-
-
Method Details
-
addOpModeFactory
public void addOpModeFactory(Supplier<OpMode> factory, RobotMode mode, String name, String group, String description, Color textColor, Color backgroundColor) Adds an opmode using a factory function that creates the opmode. It's necessary to call publishOpModes() to make the added mode visible to the driver station.- Parameters:
factory- factory function to create the opmodemode- robot modename- name of the operating modegroup- group of the operating modedescription- description of the operating modetextColor- text color, or null for defaultbackgroundColor- background color, or null for default- Throws:
IllegalArgumentException- if class does not meet criteria
-
addOpModeFactory
public void addOpModeFactory(Supplier<OpMode> factory, RobotMode mode, String name, String group, String description) Adds an opmode using a factory function that creates the opmode. It's necessary to call publishOpModes() to make the added mode visible to the driver station.- Parameters:
factory- factory function to create the opmodemode- robot modename- name of the operating modegroup- group of the operating modedescription- description of the operating mode- Throws:
IllegalArgumentException- if class does not meet criteria
-
addOpModeFactory
Adds an opmode using a factory function that creates the opmode. It's necessary to call publishOpModes() to make the added mode visible to the driver station.- Parameters:
factory- factory function to create the opmodemode- robot modename- name of the operating modegroup- group of the operating mode- Throws:
IllegalArgumentException- if class does not meet criteria
-
addOpModeFactory
Adds an opmode using a factory function that creates the opmode. It's necessary to call publishOpModes() to make the added mode visible to the driver station.- Parameters:
factory- factory function to create the opmodemode- robot modename- name of the operating mode- Throws:
IllegalArgumentException- if class does not meet criteria
-
addOpMode
public void addOpMode(Class<? extends OpMode> cls, RobotMode mode, String name, String group, String description, Color textColor, Color backgroundColor) Adds an opmode for an opmode class. The class must be a public, non-abstract subclass of OpMode with a public constructor that either takes no arguments or accepts a single argument assignable from this robot class type (the latter is preferred; if multiple match, the most specific parameter type is used). It's necessary to call publishOpModes() to make the added mode visible to the driver station.- Parameters:
cls- class to addmode- robot modename- name of the operating modegroup- group of the operating modedescription- description of the operating modetextColor- text color, or null for defaultbackgroundColor- background color, or null for default- Throws:
IllegalArgumentException- if class does not meet criteria
-
addOpMode
public void addOpMode(Class<? extends OpMode> cls, RobotMode mode, String name, String group, String description) Adds an opmode for an opmode class. The class must be a public, non-abstract subclass of OpMode with a public constructor that either takes no arguments or accepts a single argument assignable from this robot class type (the latter is preferred; if multiple match, the most specific parameter type is used). It's necessary to call publishOpModes() to make the added mode visible to the driver station.- Parameters:
cls- class to addmode- robot modename- name of the operating modegroup- group of the operating modedescription- description of the operating mode- Throws:
IllegalArgumentException- if class does not meet criteria
-
addOpMode
Adds an opmode for an opmode class. The class must be a public, non-abstract subclass of OpMode with a public constructor that either takes no arguments or accepts a single argument assignable from this robot class type (the latter is preferred; if multiple match, the most specific parameter type is used). It's necessary to call publishOpModes() to make the added mode visible to the driver station.- Parameters:
cls- class to addmode- robot modename- name of the operating modegroup- group of the operating mode- Throws:
IllegalArgumentException- if class does not meet criteria
-
addOpMode
Adds an opmode for an opmode class. The class must be a public, non-abstract subclass of OpMode with a public constructor that either takes no arguments or accepts a single argument assignable from this robot class type (the latter is preferred; if multiple match, the most specific parameter type is used). It's necessary to call publishOpModes() to make the added mode visible to the driver station.- Parameters:
cls- class to addmode- robot modename- name of the operating mode- Throws:
IllegalArgumentException- if class does not meet criteria
-
addAnnotatedOpMode
Adds an opmode for an opmode class annotated withAutonomous,Teleop, orUtility. The class must be a public, non-abstract subclass of OpMode with a public constructor that either takes no arguments or accepts a single argument assignable from this robot class type (if multiple match, the most specific parameter type is used). It's necessary to call publishOpModes() to make the added mode visible to the driver station.- Parameters:
cls- class to add- Throws:
IllegalArgumentException- if class does not meet criteria
-
addAnnotatedOpModeClasses
Scans for classes in the specified package and all nested packages that are annotated withAutonomous,Teleop, orUtilityand registers them. It's necessary to call publishOpModes() to make the added modes visible to the driver station.- Parameters:
pkg- package to scan
-
removeOpMode
Removes an operating mode option. It's necessary to call publishOpModes() to make the removed mode no longer visible to the driver station.- Parameters:
mode- robot modename- name of the operating mode
-
publishOpModes
Publishes the operating mode options to the driver station. -
clearOpModes
Clears all operating mode options and publishes an empty list to the driver station. -
addPeriodic
Add a callback to run at a specific period.- Parameters:
callback- The callback to run.period- The period at which to run the callback.
-
getPeriod
Get the period at which robot and opmode periodic callbacks are run.- Returns:
- The period at which robot and opmode periodic callbacks are run.
-
driverStationConnected
Code that needs to know the DS state should go here.Users should override this method for initialization that needs to occur after the DS is connected, such as needing the alliance information.
-
robotPeriodic
Function called periodically every loop, regardless of enabled state or OpMode selection. -
simulationInit
Function called once during robot initialization in simulation. -
simulationPeriodic
Function called periodically in simulation. -
disabledInit
Function called once when the robot becomes disabled. -
disabledPeriodic
Function called periodically while the robot is disabled. -
disabledExit
Function called once when the robot exits disabled state. -
nonePeriodic
Function called periodically anytime when no opmode is selected, including when the Driver Station is disconnected. -
getLoopStartTime
Return the system clock time in microseconds for the start of the current periodic loop. This is in the same time base as Timer.getMonotonicTimestamp(), but is stable through a loop. It is updated at the beginning of every periodic callback (including the normal periodic loop).- Returns:
- Robot running time in microseconds, as of the start of the current periodic function.
-
startCompetition
Provide an alternate "main loop" via startCompetition().- Specified by:
startCompetitionin classRobotBase
-
close
- Specified by:
closein interfaceAutoCloseable- Overrides:
closein classRobotBase
-
endCompetition
Ends the main loop in startCompetition().- Specified by:
endCompetitionin classRobotBase
-
printWatchdogEpochs
Prints list of epochs added so far and their times.
-