Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Commit

Permalink
Porting.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjiboy50fonz committed May 15, 2021
1 parent d7ab473 commit c574e1e
Show file tree
Hide file tree
Showing 22 changed files with 342 additions and 136 deletions.
45 changes: 45 additions & 0 deletions commands/autoconfig.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from commands2 import SequentialCommandGroup

from commands.autonomouscommandgroup import AutonomousCommandGroup

from custom import driverhud

from networktables import NetworkTables

excludedMethods = [
"interrupted"
] # Methods that aren't from the parents but aren't autos. If you want to exclude a program because it might not work, add it here.

table = NetworkTables.getTable("Autonomous")
autoVars = [var.lower() for var in dir(AutonomousCommandGroup)]

definedAutos = []
for auto in autoVars:
if (
auto[0] != "_" # Make sure it's not one of Python's methods.
and auto
not in [
var.lower() for var in dir(SequentialCommandGroup)
] # Make sure it's not from the parent.
and auto
not in excludedMethods # Other methods to exclude that are in the Auto command group.
):
definedAutos.append(auto)


def init():
print(definedAutos)
table.putStringArray("autos", [a.lower() for a in definedAutos])
try:
table.putString("selectedAuto", definedAutos[0].lower())
except(IndexError):
table.putString("selectedAuto", "NO AUTO FOUND")

def getAutoProgram():
"""
Returns the first defined auto if none are selected.
"""
try:
return table.getString("selectedAuto", (definedAutos[0]).lower())
except(IndexError):
raise Exception("You don't have any autos defined!")
13 changes: 12 additions & 1 deletion commands/autonomouscommandgroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@


class AutonomousCommandGroup(SequentialCommandGroup):
"""
Name the methods for the autos the name you want
to appear in the driver station.
"""

def __init__(self):
super().__init__('Autonomous')
super().__init__()


def Example(self):
"""
This is a sample autonomous program.
"""
pass
15 changes: 4 additions & 11 deletions commands/drivetrain/drivecommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,15 @@
logicalaxes.registerAxis('driveRotate')

class DriveCommand(CommandBase):
def __init__(self, speedLimit):
super().__init__('DriveCommand %s' % speedLimit)

self.requires(robot.drivetrain)
self.speedLimit = speedLimit
def __init__(self):
super().__init__()

self.addRequirements(robot.drivetrain)

def initialize(self):
robot.drivetrain.stop()
robot.drivetrain.setProfile(0)
try:
robot.drivetrain.setSpeedLimit(self.speedLimit)
except (ValueError, MissingConfigError):
print('Could not set speed to %s' % self.speedLimit)
driverhud.showAlert('Drive Train is not configured')


self.lastY = None
self.slowed = False

Expand Down
4 changes: 2 additions & 2 deletions commands/drivetrain/movecommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ def __init__(self, distance, avoidCollisions=True, name=None):
if name is None:
name = 'Move %f inches' % distance

super().__init__(name, 0.2)
super().__init__()

self.distance = distance
self.blocked = False
self.avoidCollisions = avoidCollisions
self.requires(robot.drivetrain)
self.addRequirements(robot.drivetrain)


def _initialize(self):
Expand Down
8 changes: 3 additions & 5 deletions commands/drivetrain/resettiltcommand.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
from wpilib.command import InstantCommand
from commands2 import InstantCommand

import robot

class ResetTiltCommand(InstantCommand):

def __init__(self):
super().__init__('Set Tilt to 0')

self.requires(robot.drivetrain)
self.setRunWhenDisabled(True)
super().__init__()

self.addRequirements(robot.drivetrain)

def initialize(self):
robot.drivetrain.resetTilt()
2 changes: 1 addition & 1 deletion commands/drivetrain/setspeedcommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class SetSpeedCommand(InstantCommand):
'''Changes the max speed of the drive subsystem.'''

def __init__(self, speed):
super().__init__('Set Speed To %s' % speed)
super().__init__()
self.speed = speed


Expand Down
5 changes: 1 addition & 4 deletions commands/network/alertcommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ class AlertCommand(InstantCommand):

def __init__(self, msg, type='Alerts'):
'''Show an alert on the dashboard'''
super().__init__('Alert: %s' % msg)

self.setRunWhenDisabled(True)
super().__init__()

self.msg = msg
self.type = type
Expand All @@ -17,6 +15,5 @@ def __init__(self, msg, type='Alerts'):
def initialize(self):
driverhud.showAlert(self.msg, self.type)


def setMessage(self, msg):
self.msg = msg
15 changes: 9 additions & 6 deletions commands/startupcommandgroup.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
from wpilib.command import CommandGroup
import commandbased.flowcontrol as fc
from commands2 import ParallelCommandGroup

import robot

from .drivetrain.resettiltcommand import ResetTiltCommand

class StartUpCommandGroup(CommandGroup):
class StartUpCommandGroup(ParallelCommandGroup):

def __init__(self):
super().__init__('Start Up')
self.setRunWhenDisabled(True)
super().__init__()

self.addParallel(ResetTiltCommand())
robot.drivetrain.initDefaultCommand()

self.addCommands(ResetTiltCommand())

def runsWhenDisabled(self):
return True
4 changes: 2 additions & 2 deletions commands/tools/calculateerrorcommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ class CalculateErrorCommand(MoveCommand):
errors = []

def __init__(self, direction=1):
super().__init__(30 * direction, 'Calculate Error')
super().__init__(30 * direction)

self.requires(robot.drivetrain)
self.addRequirements(robot.drivetrain)
Config('DriveTrain/wheelDiameter', 8)
self.table = NetworkTables.getTable('DriveTrain/Speed')

Expand Down
4 changes: 2 additions & 2 deletions commands/tools/calculatemaxspeedcommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ class CalculateMaxSpeedCommand(InstantCommand):
measuredSpeeds = []

def __init__(self):
super().__init__('Calculate Max Speed')
super().__init__()

self.requires(robot.drivetrain)
self.addRequirements(robot.drivetrain)
self.table = NetworkTables.getTable('DriveTrain')


Expand Down
80 changes: 39 additions & 41 deletions commands/tools/configurepidcommandgroup.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from commands2 import CommandGroup, WaitCommand, PrintCommand
import commandbased.flowcontrol as fc
from commands2 import SequentialCommandGroup, WaitCommand, PrintCommand, InstantCommand

from .setuseencoderscommand import SetUseEncodersCommand
from .moveycommand import MoveYCommand
from .resetpidcommand import ResetPIDCommand
from .calculatemaxspeedcommand import CalculateMaxSpeedCommand
from .calculateerrorcommand import CalculateErrorCommand
Expand All @@ -11,10 +9,12 @@

from custom.config import Config

class ConfigurePIDCommandGroup(CommandGroup):
import robot

class ConfigurePIDCommandGroup(SequentialCommandGroup):

def __init__(self):
super().__init__('Configure PID')
super().__init__()

output = '''
Your F and P values have been configured. However, P is probably too small.
Expand All @@ -31,40 +31,38 @@ def __init__(self):
SRX Software Reference Manual.
'''

self.addSequential(AlertCommand('Do not disable the robot!'))
self.addSequential(WaitCommand(1))
self.addSequential(
AlertCommand('Enable netconsole for details', 'Info')
)
self.addSequential(WaitCommand(2))
self.addSequential(PrintCommand('Zeroing PID Values'))
self.addSequential(ResetPIDCommand())
self.addSequential(PrintCommand('Calculating Max Speed'))
self.addSequential(SetUseEncodersCommand(False))
self.addSequential(MoveYCommand(1))
self.addSequential(WaitCommand(2))
self.addSequential(CalculateMaxSpeedCommand())
self.addSequential(MoveYCommand(0))
self.addSequential(WaitCommand(2))
self.addSequential(MoveYCommand(-1))
self.addSequential(WaitCommand(2))
self.addSequential(CalculateMaxSpeedCommand())
self.addSequential(PrintCommand('Testing PID driving'))
self.addSequential(SetUseEncodersCommand(True))
self.addSequential(SetSpeedCommand(Config('DriveTrain/normalSpeed')))
self.addSequential(MoveYCommand(1))
self.addSequential(WaitCommand(2))
self.addSequential(MoveYCommand(0))
self.addSequential(WaitCommand(2))
self.addSequential(SetSpeedCommand(Config('DriveTrain/preciseSpeed')))
self.addSequential(MoveYCommand(-1))
self.addSequential(WaitCommand(2))
self.addSequential(MoveYCommand(0))
self.addSequential(WaitCommand(2))
self.addSequential(PrintCommand('Generating starting P value'))
self.addSequential(CalculateErrorCommand(1))
self.addSequential(CalculateErrorCommand(-1))
self.addSequential(PrintCommand(output.strip()))
self.addSequential(
self.addCommands([
AlertCommand('Do not disable the robot!'),
WaitCommand(1),
AlertCommand('Enable netconsole for details', 'Info'),
WaitCommand(2),
PrintCommand('Zeroing PID Values'),
ResetPIDCommand(),
PrintCommand('Calculating Max Speed'),
SetUseEncodersCommand(False),
InstantCommand(lambda: robot.drivetrain.move(0, 1, 0), robot.drivetrain),
WaitCommand(2),
CalculateMaxSpeedCommand(),
InstantCommand(lambda: robot.drivetrain.stop(), robot.drivetrain),
WaitCommand(2),
InstantCommand(lambda: robot.drivetrain.move(0, -1, 0), robot.drivetrain),
WaitCommand(2),
CalculateMaxSpeedCommand(),
PrintCommand('Testing PID driving'),
SetUseEncodersCommand(True),
SetSpeedCommand(Config('DriveTrain/normalSpeed')),
InstantCommand(lambda: robot.drivetrain.move(0, 1, 0), robot.drivetrain),
WaitCommand(2),
InstantCommand(lambda: robot.drivetrain.stop(), robot.drivetrain),
WaitCommand(2),
SetSpeedCommand(Config('DriveTrain/preciseSpeed')),
InstantCommand(lambda: robot.drivetrain.move(0, -1, 0), robot.drivetrain),
WaitCommand(2),
InstantCommand(lambda: robot.drivetrain.stop(), robot.drivetrain),
WaitCommand(2),
PrintCommand('Generating starting P value'),
CalculateErrorCommand(1),
CalculateErrorCommand(-1),
PrintCommand(output.strip()),
AlertCommand('You may now disable the robot', 'Info')
)
])
15 changes: 0 additions & 15 deletions commands/tools/moveycommand.py

This file was deleted.

4 changes: 2 additions & 2 deletions commands/tools/resetpidcommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
class ResetPIDCommand(InstantCommand):

def __init__(self):
super().__init__('Reset PID values')
super().__init__()

self.requires(robot.drivetrain)
self.addRequirements(robot.drivetrain)


def initialize(self):
Expand Down
4 changes: 2 additions & 2 deletions commands/tools/setuseencoderscommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
class SetUseEncodersCommand(InstantCommand):

def __init__(self, encodersEnabled):
super().__init__('Set Use Encoders')
super().__init__()

self.requires(robot.drivetrain)
self.addRequirements(robot.drivetrain)
self.encodersEnabled = encodersEnabled


Expand Down
Loading

0 comments on commit c574e1e

Please sign in to comment.