Does anyone know of any method to call to disable the robot through code, say at the push of a button on the robot itself?
If not does anyone know of a way I could perhaps crash the code when a button is pressed causing an automatic disable?
Does anyone know of any method to call to disable the robot through code, say at the push of a button on the robot itself?
If not does anyone know of a way I could perhaps crash the code when a button is pressed causing an automatic disable?
Interesting question.
You definitely can do this if you want. I would be interested in knowing the reason, though.
If you use Java, you can do anything that will cause an unhandled exception, and the code will automatically crash and the robot will be disabled:
// TeleopPeriodic
if (my_button_is_pressed)
{
throw new java.lang.Exception("Intentional crash");
}
It’s for a Engineering project we’re working on. My team hasn’t been denying the DriveBase kit from our KOPs and so we’ve gotten a couple of them. We’re planning on having it where several robots are on a small arena we’ll have, where if 1 robot can hit another’s kill switch, that robot is disabled.
This does not appear to be working, it asks me to put a “try/catch” bracket around the throw new java.lang.Exception(“crash”); and then with that implemented using the code that it default generates pushing the button doesn’t crash the program nor throw an error to the roborio console
If you throw a RuntimeException, then you should not have to add a try/catch.
System.exit(0); might work too, but I think you’d have to manually reboot code.
The runtime exception doesn’t need a try/catch loop, so it may work but currently I have no access to a roborio to test on.
When I added a System.exit(0); line, the roborio could would rash and reboot no matter the state of the button as soon as Teleop was enabled.
If that line isn’t executing, there’s no reason it would crash. Make sure it’s only being called if the right button is pressed. Can you post the code?
The button might be normally closed and sourcing, or normally open and sinking, so the input will be high when the switch is not pressed.
If you are using the Command Based Robot and can be sure all your commands are end-safe (they stop whatever actuators they are using), I’d say the best way to simulate this may be to disable the Scheduler.
Please note I have never actually tried this. I don’t know if it will actually work, it just looks like it would from the wpilibj source.
// Will End() all running Commands (NOT Interrupt())
Scheduler.getInstance().removeAll();
// Scheduler will not run any more commands
Scheduler.getInstance().disable();
Is your intent that the robot will be disabled for the rest of the match? If so, throwing an exception or using the System.exit(0); will not work, as you witnessed. The robot application automatically restarts itself and will become enabled.
I would recommend adding a boolean flag to control whether the bot is enabled or not and then do what gixxy recommended. Something to the likes of:
public class MyRobot extends TimedRobot {
private boolean enabled = true;
private boolean finishedDisable = false;
private DigitalInput killSwitch = new DigitalInput(0);
@Override
public void robotPeriodic() {
// Usually these inputs return false when pressed and true when not
// If this disables the bot immediately, use this commented version instead
// Note: you can check the "killSwitchActive" field on the smart dashboard
boolean killSwitchActive = !killSwitch.get();
// boolean killSwitchActive = killSwitch.get();
if (killSwitchActive) {
enabled = false;
}
if (enabled) {
Scheduler.getInstance().run();
} else if (!finishedDisable) {
System.out.println("Disabling robot...");
// gixxy's code:
// Will End() all running Commands (NOT Interrupt())
Scheduler.getInstance().removeAll();
// Scheduler will not run any more commands
Scheduler.getInstance().disable();
finishedDisable = true;
}
SmartDashboard.putBoolean("enabled", enabled);
SmartDashboard.putBoolean("killSwitchActive", killSwitchActive);
}
}
Restarting the robot code will reset this and the bot will no longer be disabled. Also, this is assuming you are using Command based programming btw.
Two changes you will need to make: