Created
February 15, 2015 00:50
-
-
Save virtuald/f08d5b9493e60bfd2496 to your computer and use it in GitHub Desktop.
Command sample that works
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import wpilib | |
from wpilib.buttons import JoystickButton | |
from wpilib.command import Command, CommandGroup, Scheduler | |
class DoTheThing(Command): | |
def __init__(self, robot): | |
super().__init__() | |
self.robot = robot | |
def execute(self): | |
print("DOING IT") | |
def end(self): | |
print("I do get called") | |
def isFinished(self): | |
return False | |
def interrupted(self): | |
print("interrupted!") | |
self.end() | |
def cancel(self): | |
self.end() | |
super().cancel() | |
class DoAllThings(CommandGroup): | |
def __init__(self, robot): | |
super().__init__() | |
self.robot = robot | |
self.addSequential(DoTheThing(self.robot)) | |
class CancelCommand(Command): | |
def __init__(self, thing): | |
super().__init__() | |
self.thing = thing | |
def execute(self): | |
print("cancelling the task") | |
self.thing.cancel() | |
def isFinished(self): | |
return True | |
class MyRobot(wpilib.IterativeRobot): | |
def robotInit(self): | |
self.group = DoAllThings(self) | |
self.joy = wpilib.Joystick(0) | |
button = JoystickButton(self.joy, 1) | |
button.whenPressed(self.group) | |
button = JoystickButton(self.joy, 2) | |
button.whenPressed(CancelCommand(self.group)) | |
def teleopPeriodic(self): | |
"""This function is called periodically during operator control.""" | |
Scheduler.getInstance().run() | |
def testPeriodic(self): | |
"""This function is called periodically during test mode.""" | |
wpilib.LiveWindow.run() | |
if __name__ == "__main__": | |
wpilib.run(MyRobot) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment