mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-24 01:31:46 +00:00
[wpilib] Add TimedRobot functions for running code on mode exit (#3499)
Currently, we have functions like TeleopInit() for running code on mode entry, but no such functions for running code on mode exit, and it's cumbersome to add those in user code without making a custom robot class. This PR adds exit functions to TimedRobot. Some example use cases include DisabledExit() for operations when the robot enables (whether that be into teleop, autonomous, or test) and AutonomousExit() for disabling feedback controllers.
This commit is contained in:
@@ -23,13 +23,34 @@ import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
|
||||
* <p>robotInit() -- provide for initialization at robot power-on
|
||||
*
|
||||
* <p>init() functions -- each of the following functions is called once when the appropriate mode
|
||||
* is entered: - disabledInit() -- called each and every time disabled is entered from another mode
|
||||
* - autonomousInit() -- called each and every time autonomous is entered from another mode -
|
||||
* teleopInit() -- called each and every time teleop is entered from another mode - testInit() --
|
||||
* called each and every time test is entered from another mode
|
||||
* is entered:
|
||||
*
|
||||
* <p>periodic() functions -- each of these functions is called on an interval: - robotPeriodic() -
|
||||
* disabledPeriodic() - autonomousPeriodic() - teleopPeriodic() - testPeriodic()
|
||||
* <ul>
|
||||
* <li>disabledInit() -- called each and every time disabled is entered from another mode
|
||||
* <li>autonomousInit() -- called each and every time autonomous is entered from another mode
|
||||
* <li>teleopInit() -- called each and every time teleop is entered from another mode
|
||||
* <li>testInit() -- called each and every time test is entered from another mode
|
||||
* </ul>
|
||||
*
|
||||
* <p>periodic() functions -- each of these functions is called on an interval:
|
||||
*
|
||||
* <ul>
|
||||
* <li>robotPeriodic()
|
||||
* <li>disabledPeriodic()
|
||||
* <li>autonomousPeriodic()
|
||||
* <li>teleopPeriodic()
|
||||
* <li>testPeriodic()
|
||||
* </ul>
|
||||
*
|
||||
* <p>final() functions -- each of the following functions is called once when the appropriate mode
|
||||
* is exited:
|
||||
*
|
||||
* <ul>
|
||||
* <li>disabledExit() -- called each and every time disabled is exited
|
||||
* <li>autonomousExit() -- called each and every time autonomous is exited
|
||||
* <li>teleopExit() -- called each and every time teleop is exited
|
||||
* <li>testExit() -- called each and every time test is exited
|
||||
* </ul>
|
||||
*/
|
||||
public abstract class IterativeRobotBase extends RobotBase {
|
||||
private enum Mode {
|
||||
@@ -194,6 +215,39 @@ public abstract class IterativeRobotBase extends RobotBase {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Exit code for disabled mode should go here.
|
||||
*
|
||||
* <p>Users should override this method for code which will be called each time the robot exits
|
||||
* disabled mode.
|
||||
*/
|
||||
public void disabledExit() {}
|
||||
|
||||
/**
|
||||
* Exit code for autonomous mode should go here.
|
||||
*
|
||||
* <p>Users should override this method for code which will be called each time the robot exits
|
||||
* autonomous mode.
|
||||
*/
|
||||
public void autonomousExit() {}
|
||||
|
||||
/**
|
||||
* Exit code for teleop mode should go here.
|
||||
*
|
||||
* <p>Users should override this method for code which will be called each time the robot exits
|
||||
* teleop mode.
|
||||
*/
|
||||
public void teleopExit() {}
|
||||
|
||||
/**
|
||||
* Exit code for test mode should go here.
|
||||
*
|
||||
* <p>Users should override this method for code which will be called each time the robot exits
|
||||
* test mode.
|
||||
*/
|
||||
@SuppressWarnings("PMD.JUnit4TestShouldUseTestAnnotation")
|
||||
public void testExit() {}
|
||||
|
||||
/**
|
||||
* Enables or disables flushing NetworkTables every loop iteration. By default, this is disabled.
|
||||
*
|
||||
@@ -215,60 +269,67 @@ public abstract class IterativeRobotBase extends RobotBase {
|
||||
protected void loopFunc() {
|
||||
m_watchdog.reset();
|
||||
|
||||
// Call the appropriate function depending upon the current robot mode
|
||||
// Get current mode
|
||||
Mode mode = Mode.kNone;
|
||||
if (isDisabled()) {
|
||||
// Call DisabledInit() if we are now just entering disabled mode from either a different mode
|
||||
// or from power-on.
|
||||
if (m_lastMode != Mode.kDisabled) {
|
||||
mode = Mode.kDisabled;
|
||||
} else if (isAutonomous()) {
|
||||
mode = Mode.kAutonomous;
|
||||
} else if (isOperatorControl()) {
|
||||
mode = Mode.kTeleop;
|
||||
} else if (isTest()) {
|
||||
mode = Mode.kTest;
|
||||
}
|
||||
|
||||
// If mode changed, call mode exit and entry functions
|
||||
if (m_lastMode != mode) {
|
||||
// Call last mode's exit function
|
||||
if (m_lastMode == Mode.kDisabled) {
|
||||
disabledExit();
|
||||
} else if (m_lastMode == Mode.kAutonomous) {
|
||||
autonomousExit();
|
||||
} else if (m_lastMode == Mode.kTeleop) {
|
||||
teleopExit();
|
||||
} else if (m_lastMode == Mode.kTest) {
|
||||
LiveWindow.setEnabled(false);
|
||||
Shuffleboard.disableActuatorWidgets();
|
||||
testExit();
|
||||
}
|
||||
|
||||
// Call current mode's entry function
|
||||
if (mode == Mode.kDisabled) {
|
||||
disabledInit();
|
||||
m_watchdog.addEpoch("disabledInit()");
|
||||
m_lastMode = Mode.kDisabled;
|
||||
}
|
||||
|
||||
HAL.observeUserProgramDisabled();
|
||||
disabledPeriodic();
|
||||
m_watchdog.addEpoch("disablePeriodic()");
|
||||
} else if (isAutonomous()) {
|
||||
// Call AutonomousInit() if we are now just entering autonomous mode from either a different
|
||||
// mode or from power-on.
|
||||
if (m_lastMode != Mode.kAutonomous) {
|
||||
LiveWindow.setEnabled(false);
|
||||
Shuffleboard.disableActuatorWidgets();
|
||||
} else if (mode == Mode.kAutonomous) {
|
||||
autonomousInit();
|
||||
m_watchdog.addEpoch("autonomousInit()");
|
||||
m_lastMode = Mode.kAutonomous;
|
||||
}
|
||||
|
||||
HAL.observeUserProgramAutonomous();
|
||||
autonomousPeriodic();
|
||||
m_watchdog.addEpoch("autonomousPeriodic()");
|
||||
} else if (isOperatorControl()) {
|
||||
// Call TeleopInit() if we are now just entering teleop mode from either a different mode or
|
||||
// from power-on.
|
||||
if (m_lastMode != Mode.kTeleop) {
|
||||
LiveWindow.setEnabled(false);
|
||||
Shuffleboard.disableActuatorWidgets();
|
||||
} else if (mode == Mode.kTeleop) {
|
||||
teleopInit();
|
||||
m_watchdog.addEpoch("teleopInit()");
|
||||
m_lastMode = Mode.kTeleop;
|
||||
}
|
||||
|
||||
HAL.observeUserProgramTeleop();
|
||||
teleopPeriodic();
|
||||
m_watchdog.addEpoch("teleopPeriodic()");
|
||||
} else {
|
||||
// Call TestInit() if we are now just entering test mode from either a different mode or from
|
||||
// power-on.
|
||||
if (m_lastMode != Mode.kTest) {
|
||||
} else if (mode == Mode.kTest) {
|
||||
LiveWindow.setEnabled(true);
|
||||
Shuffleboard.enableActuatorWidgets();
|
||||
testInit();
|
||||
m_watchdog.addEpoch("testInit()");
|
||||
m_lastMode = Mode.kTest;
|
||||
}
|
||||
|
||||
m_lastMode = mode;
|
||||
}
|
||||
|
||||
// Call the appropriate function depending upon the current robot mode
|
||||
if (mode == Mode.kDisabled) {
|
||||
HAL.observeUserProgramDisabled();
|
||||
disabledPeriodic();
|
||||
m_watchdog.addEpoch("disabledPeriodic()");
|
||||
} else if (mode == Mode.kAutonomous) {
|
||||
HAL.observeUserProgramAutonomous();
|
||||
autonomousPeriodic();
|
||||
m_watchdog.addEpoch("autonomousPeriodic()");
|
||||
} else if (mode == Mode.kTeleop) {
|
||||
HAL.observeUserProgramTeleop();
|
||||
teleopPeriodic();
|
||||
m_watchdog.addEpoch("teleopPeriodic()");
|
||||
} else {
|
||||
HAL.observeUserProgramTest();
|
||||
testPeriodic();
|
||||
m_watchdog.addEpoch("testPeriodic()");
|
||||
|
||||
Reference in New Issue
Block a user