Skip to content

Instantly share code, notes, and snippets.

@virtuald
Created February 15, 2015 00:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save virtuald/f08d5b9493e60bfd2496 to your computer and use it in GitHub Desktop.
Save virtuald/f08d5b9493e60bfd2496 to your computer and use it in GitHub Desktop.
Command sample that works
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