virtuald
23-03-2014, 11:27
Continuing some work from the last few years with Team 2423's code, I've been developing generic python modules that will allow python teams to develop autonomous modes that have the following characteristics:
Dynamic detection of autonomous modes at runtime
Automatic support of selection of which autonomous mode to use via SmartDashboard
Supports simple built-in tuning of autonomous mode parameters via SmartDashboard
Easy to create autonomous modes that support state machine or time-based operation
Autonomous modes that are easy to read and understand
Team 1418 has been using this framework this year with great success, and at some point in the near future I'll be releasing it as part of pyfrc. New features in pyfrc will allow this (and other useful libraries) to be installed directly to the robot.
Here's one of Team 1418's autonomous modes that can score two balls.
try:
import wpilib
except ImportError:
from pyfrc import wpilib
from common.autonomous_helper import StatefulAutonomous, timed_state
class TwoBall(StatefulAutonomous):
MODE_NAME = 'Two balls'
DEFAULT = True
def __init__(self, components):
super().__init__(components)
self.register_sd_var('drive_speed', 0.5)
def on_enable(self):
super().on_enable()
def update(self, tm):
if tm > 0.3:
self.catapult.pulldown()
super().update(tm)
@timed_state(duration=1.2, next_state='drive', first=True)
def drive_wait(self, tm, state_tm):
'''Wait some period before we start driving'''
self.intake.armDown()
@timed_state(duration=1.4, next_state='launch')
def drive(self, tm, state_tm):
'''Start the launch sequence! Drive slowly forward for N seconds'''
self.drive.move(0, self.drive_speed, 0)
self.intake.armDown()
@timed_state(duration=1, next_state='go_back')
def launch(self, tm):
'''Fire and keep firing for 1 seconds'''
self.catapult.launchNoSensor()
@timed_state(duration=2.9,next_state='drive2')
def go_back(self):
'''Go back to get the next ball'''
self.drive.move(0, -1*self.drive_speed, 0)
self.intake.ballIn()
@timed_state(duration=2.5, next_state='launch2')
def drive2(self, tm, state_tm):
'''Once we get it, drive forward'''
self.drive.move(0, self.drive_speed, 0)
self.intake.ballIn()
@timed_state(duration=1)
def launch2(self, tm):
'''And shoot!'''
self.catapult.launchNoSensor()
self.intake.ballIn()
Seems too simple to work? Well, here's a video of this autonomous mode (https://www.youtube.com/watch?v=c1rzUWEyUFs) in the Virginia Regional scoring both balls in team 1418's last qualifying match.
Dynamic detection of autonomous modes at runtime
Automatic support of selection of which autonomous mode to use via SmartDashboard
Supports simple built-in tuning of autonomous mode parameters via SmartDashboard
Easy to create autonomous modes that support state machine or time-based operation
Autonomous modes that are easy to read and understand
Team 1418 has been using this framework this year with great success, and at some point in the near future I'll be releasing it as part of pyfrc. New features in pyfrc will allow this (and other useful libraries) to be installed directly to the robot.
Here's one of Team 1418's autonomous modes that can score two balls.
try:
import wpilib
except ImportError:
from pyfrc import wpilib
from common.autonomous_helper import StatefulAutonomous, timed_state
class TwoBall(StatefulAutonomous):
MODE_NAME = 'Two balls'
DEFAULT = True
def __init__(self, components):
super().__init__(components)
self.register_sd_var('drive_speed', 0.5)
def on_enable(self):
super().on_enable()
def update(self, tm):
if tm > 0.3:
self.catapult.pulldown()
super().update(tm)
@timed_state(duration=1.2, next_state='drive', first=True)
def drive_wait(self, tm, state_tm):
'''Wait some period before we start driving'''
self.intake.armDown()
@timed_state(duration=1.4, next_state='launch')
def drive(self, tm, state_tm):
'''Start the launch sequence! Drive slowly forward for N seconds'''
self.drive.move(0, self.drive_speed, 0)
self.intake.armDown()
@timed_state(duration=1, next_state='go_back')
def launch(self, tm):
'''Fire and keep firing for 1 seconds'''
self.catapult.launchNoSensor()
@timed_state(duration=2.9,next_state='drive2')
def go_back(self):
'''Go back to get the next ball'''
self.drive.move(0, -1*self.drive_speed, 0)
self.intake.ballIn()
@timed_state(duration=2.5, next_state='launch2')
def drive2(self, tm, state_tm):
'''Once we get it, drive forward'''
self.drive.move(0, self.drive_speed, 0)
self.intake.ballIn()
@timed_state(duration=1)
def launch2(self, tm):
'''And shoot!'''
self.catapult.launchNoSensor()
self.intake.ballIn()
Seems too simple to work? Well, here's a video of this autonomous mode (https://www.youtube.com/watch?v=c1rzUWEyUFs) in the Virginia Regional scoring both balls in team 1418's last qualifying match.