Class OpModeRobot
- All Implemented Interfaces:
AutoCloseable
- Direct Known Subclasses:
Robot
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(RobotMode mode, String name, String group, String description, Class<? extends OpMode> cls) Adds an opmode for an opmode class.voidAdds an opmode using a factory function that creates the opmode.voidaddOpMode(RobotMode mode, String name, String group, String description, Color textColor, Color backgroundColor, Class<? extends OpMode> cls) Adds an opmode for an opmode class.voidaddOpMode(RobotMode mode, String name, String group, String description, Color textColor, Color backgroundColor, Supplier<OpMode> factory) Adds an opmode using a factory function that creates the opmode.voidAdds an opmode using a factory function that creates the opmode.voidAdds 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 nanoseconds 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, getRobotName, getRuntimeType, isAutonomous, isAutonomousEnabled, isDisabled, isEnabled, isReal, isSimulation, isTeleop, isTeleopEnabled, isUtility, isUtilityEnabled, startRobot, suppressExitWarning
-
Field Details
-
DEFAULT_PERIOD
public static final double DEFAULT_PERIODDefault loop period.- See Also:
-
-
Constructor Details
-
OpModeRobot
public OpModeRobot()Constructor with default period. -
OpModeRobot
public OpModeRobot(double period) Constructor with specified period.- Parameters:
period- the period at which to run the robot and opmode periodic callbacks.
-
-
Method Details
-
addOpMode
public void addOpMode(RobotMode mode, String name, String group, String description, Color textColor, Color backgroundColor, Supplier<OpMode> factory) 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:
mode- 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 defaultfactory- factory function to create the opmode- Throws:
IllegalArgumentException- if class does not meet criteria
-
addOpMode
public void addOpMode(RobotMode mode, String name, String group, String description, Supplier<OpMode> factory) 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:
mode- robot modename- name of the operating modegroup- group of the operating modedescription- description of the operating modefactory- factory function to create the opmode- Throws:
IllegalArgumentException- if class does not meet criteria
-
addOpMode
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:
mode- robot modename- name of the operating modegroup- group of the operating modefactory- factory function to create the opmode- Throws:
IllegalArgumentException- if class does not meet criteria
-
addOpMode
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:
mode- robot modename- name of the operating modefactory- factory function to create the opmode- Throws:
IllegalArgumentException- if class does not meet criteria
-
addOpMode
public void addOpMode(RobotMode mode, String name, String group, String description, Color textColor, Color backgroundColor, Class<? extends OpMode> cls) 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:
mode- 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 defaultcls- class to add- Throws:
IllegalArgumentException- if class does not meet criteria
-
addOpMode
public void addOpMode(RobotMode mode, String name, String group, String description, Class<? extends OpMode> cls) 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:
mode- robot modename- name of the operating modegroup- group of the operating modedescription- description of the operating modecls- class to add- 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:
mode- robot modename- name of the operating modegroup- group of the operating modecls- class to add- 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:
mode- robot modename- name of the operating modecls- class to add- 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
-
publishOpModes
public void publishOpModes()Publishes the operating mode options to the driver station. -
clearOpModes
public void 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.This callback will be registered with the framework immediately when this method is called and will begin executing as soon as it is registered.
- Parameters:
callback- The callback to run.period- The period at which to run the callback.
-
getPeriod
public double 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
public void 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
public void robotPeriodic()Function called periodically every loop, regardless of enabled state or OpMode selection. -
simulationInit
public void simulationInit()Function called once during robot initialization in simulation. -
simulationPeriodic
public void simulationPeriodic()Function called periodically in simulation. -
disabledInit
public void disabledInit()Function called once when the robot becomes disabled. -
disabledPeriodic
public void disabledPeriodic()Function called periodically while the robot is disabled. -
disabledExit
public void disabledExit()Function called once when the robot exits disabled state. -
nonePeriodic
public void nonePeriodic()Function called periodically anytime when no opmode is selected, including when the Driver Station is disconnected. -
getLoopStartTime
public long getLoopStartTime()Return the system clock time in nanoseconds 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 nanoseconds, as of the start of the current periodic function.
-
startCompetition
public final void startCompetition()Provide an alternate "main loop" via startCompetition().- Specified by:
startCompetitionin classRobotBase
-
close
public void close()- Specified by:
closein interfaceAutoCloseable- Overrides:
closein classRobotBase
-
endCompetition
public final void endCompetition()Ends the main loop in startCompetition().- Specified by:
endCompetitionin classRobotBase
-
printWatchdogEpochs
public void printWatchdogEpochs()Prints list of epochs added so far and their times.
-