Skip to content
This repository has been archived by the owner on Jan 15, 2018. It is now read-only.

Commit

Permalink
Finished up commenting everything.
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBoesen committed Jun 27, 2016
1 parent 74c78eb commit 5685e2d
Show file tree
Hide file tree
Showing 15 changed files with 207 additions and 126 deletions.
30 changes: 16 additions & 14 deletions electrical_test/robot.py
Expand Up @@ -4,26 +4,28 @@
import magicbot

class MyRobot(magicbot.MagicRobot):

"""For more info on what these functions do, see robot.py."""
def createObjects(self):

self.driveStick = xbox_controller.XboxController(0)

if MyRobot.isSimulation():
self.lf_motor = wpilib.Jaguar(1)
self.lr_motor = wpilib.Jaguar(2)
self.rf_motor = wpilib.Jaguar(3)
self.rr_motor = wpilib.Jaguar(4)
self.drive = wpilib.RobotDrive(self.lf_motor, self.lr_motor, self.rf_motor, self.rr_motor)

self.driveStick = xbox_controller.XboxController(0)

# If the robot is a simulation
if MyRobot.isSimulation():
# Change motors to default jaguars
self.lf_motor = wpilib.Jaguar(1)
self.lr_motor = wpilib.Jaguar(2)
self.rf_motor = wpilib.Jaguar(3)
self.rr_motor = wpilib.Jaguar(4)
# Set up drive train
self.drive = wpilib.RobotDrive(self.lf_motor, self.lr_motor, self.rf_motor, self.rr_motor)

def disabledPeriodic(self):
wpilib.Timer.delay(.01)

def teleopInit(self):
pass

def teleopPeriodic(self):

self.drive.tankDrive(self.driveStick.getLeftY(), self.driveStick.getRightY(), False)

if __name__ == '__main__':
wpilib.run(MyRobot)
31 changes: 24 additions & 7 deletions robot/automations/intakeBall.py
@@ -1,36 +1,53 @@
import wpilib

# Define step numbers for state machine.
START_SPIN = 0
ARM_DOWN = 1
ARM_DOWN = 1
ARM_UP = 2


class intakeBall():
class IntakeBall():
def __init__(self, intake):
self.intake = intake
self.is_running = False
self.state = START_SPIN
self.timer = wpilib.Timer()
self.timer.start()

def get_running(self):
"""Return bool: is intake currently running?"""
return self.is_running

def doit(self):

def go(self):
"""Actually intake ball."""
# Tell program ball intake is running.
self.is_running = True


# If state machine is at 0, start spinning intake motor
if self.state == START_SPIN:
# Reset timer
self.timer.reset()
# Start motor
self.intake.intake()
# If time is up, move on to the next step
if self.timer.hasPeriodPassed(1):
self.state = ARM_DOWN
# If state machine is at 1, put arm down
if self.state == ARM_DOWN:
# Move arm to middle position
self.intake.set_arm_middle()
# Spin intake motor
self.intake.intake()
# If time is up, move on to next step
if self.timer.hasPeriodPassed(2):
self.state = ARM_UP
# If state machine is at 2, put arm back up
if self.state == ARM_UP:
# Move arm to top position
self.intake.set_arm_top()
# If arm has reached desired position
if self.intake.on_target():
# Note that ball intake has finished.
self.is_running = False
self.state = START_SPIN
# Reset statemachine
self.state = 0
28 changes: 18 additions & 10 deletions robot/automations/lightOff.py
Expand Up @@ -2,35 +2,43 @@
from components import light, intake as Intake

class LightSwitch(StateMachine):

# Aliases
light = light.Light
intake = Intake.Arm

def on_enable(self):
"""Runs when light is initiated"""
self.counter = 0

def switch(self):
"""Turn off/on"""
self.counter = 0
self.engage()
@state(first=True, must_finish = True)
self.engage()

@state(first=True, must_finish=True)
def firstState(self):
"""Figure out if it needs to turn on or off"""
if self.light.on:
self.next_state('off')
else:
self.light.turnOn()
self.done()

@timed_state(duration = .25, next_state='on', must_finish = True)
def off(self, initial_call):
"""
Turns light off.
Needs to turn on and off two extra times to switch through the three
flashlight intensities.
"""
if initial_call:
self.counter += 1

self.light.turnOff()
if self.counter > 2:
self.done()

@timed_state(duration = .25, next_state = 'off', must_finish = True)
def on(self):
"""Turns light on. This doesn't need to switch multiple times."""
self.light.turnOn()

51 changes: 38 additions & 13 deletions robot/automations/portcullis.py
@@ -1,54 +1,79 @@
import wpilib

# Define constants
ARM_DOWN = 1
DRIVE_ENC = 2
DRIVE = 3
ARM_MIDDLE = 4
ARM_UP = 5


class PortcullisLift:

def __init__(self, sd, drive, intake, drive_speed=-.5):
self.intake = intake
self.drive = drive
self.sd = sd


# Get set speeds from NetworkTables
self.drive_speed = self.sd.getAutoUpdateValue('Portcullis | Drive Speed', .3)
self.drive_reverse_speed = self.sd.getAutoUpdateValue('Portcullis | Reverse Speed', -.05)
self.drive_speed_2 = self.sd.getAutoUpdateValue('Portcullis | Drive Speed_2', .5)

# By default, not running.
self.is_running = False
# Default state is first.
self.state = ARM_DOWN


# Start timer
self.timer = wpilib.Timer()
self.timer.start()

def get_running(self):
"""Return True if portcullis lift is in progress, False if not"""
return self.is_running


def doit(self):

def go(self):
"""Actually lift portcullis."""
# Note that process is now running.
self.is_running = True
#Add state machine to put the arm at the bottom first
# If state machine is at 1, put arm at the bottom
# Add state machine to put the arm at the bottom first
if self.state == ARM_DOWN:
# Move arm to lowest position
self.intake.set_arm_bottom()
# If arm has moved to desired position
if self.intake.on_target():
# Reset the timer
self.timer.reset()
# Move on to the next state
self.state = DRIVE
# If state machine is at 2, drive forward
if self.state == DRIVE:
# Move forward set amount
self.drive.move(self.drive_speed.value, 0)
# If robot is done moving that amount
if self.timer.hasPeriodPassed(1):
# Reset timer and switch to next state
self.timer.reset()
self.state = ARM_MIDDLE
# If on third state, move arm up halfway and move a bit
if self.state == ARM_MIDDLE:
# Move forward predefined amount
self.drive.move(self.drive_reverse_speed.value, 0)
# Move arm to top
self.intake.set_arm_top()
# If halfway done, move to next state
# (the reason it works like this is complicated and not important)
if self.timer.hasPeriodPassed(.5):
self.state = ARM_UP
# If on final state
if self.state == ARM_UP:
# Move arm to top
self.intake.set_arm_top()
#self.drive.move(self.drive_speed_2.value, 0)
# If everything's done
if self.drive.drive_distance(36):
#if #self.intake.on_target():
# Note that process has finished
self.is_running = False
self.state = ARM_DOWN



# Switch state back to first
self.state = ARM_DOWN
17 changes: 15 additions & 2 deletions robot/automations/shootBall.py
@@ -1,27 +1,40 @@
import components.intake as Intake
from magicbot import StateMachine, state, timed_state


class ShootBall(StateMachine):
# Aliases
intake = Intake.Arm

def on_enable(self):
"""When initiated"""
self.is_running = False

def get_running(self):
"""Is ball being shot?"""
return self.is_running

def shoot(self):
"""When called, engage StateMachine to shoot"""
self.engage()

def stop(self):
"""When called, stop the ball shooting process"""
self.done()

@state(first = True, must_finish = True)
@state(first=True, must_finish=True)
# First state, lower arm.
def lower_arms(self):
"""Lower arm in preparation for shot"""
# Move arm to middle
self.intake.set_arm_middle()
# If close enough to desired position
if self.intake.get_position() > 2000:
# Move to shooting step
self.next_state('fire')

@timed_state(duration = 1, must_finish = True)
@timed_state(duration=1, must_finish=True)
def fire(self):
"""Eject ball"""
# Shoot it yo
self.intake.outtake()
44 changes: 30 additions & 14 deletions robot/automations/targetGoal.py
Expand Up @@ -4,51 +4,67 @@
from automations import shootBall
from magicbot.magic_tunable import tunable


class TargetGoal(StateMachine):


# Aliases
intake = intake.Arm
drive = drive.Drive
shootBall = shootBall.ShootBall


# Fetch NetworkTables values
present = ntproperty('/components/autoaim/present', False)
targetHeight = ntproperty('/components/autoaim/target_height', 0)



idealHeight = tunable(-7)
heightThreshold = tunable(-7)

shoot = False

def target(self):
"""When called, seek target"""
self.engage()
self.shoot = False

def target_shoot(self):
"""When called, seek target then shoot"""
self.engage()
self.shoot = True

@state(first=True)
def align(self, initial_call):
"""First state: turn robot to be facing tower"""
# If it's the first time
if initial_call:
# Then turn on vision system
self.drive.enable_camera_tracking()


# If it's inline with the tower and it's been told to shoot when done aiming,
if self.drive.align_to_tower() and self.shoot:
# Then move on to shooting state.
self.next_state('camera_assisted_drive')

@state
def camera_assisted_drive(self):
if self.targetHeight > self.heightThreshold:#> -12:
"""Second state: go forward to shooting location"""
# If target is close
if self.targetHeight > self.heightThreshold:
# Move forward, guided by threshold
self.drive.move(max(abs(self.idealHeight-self.targetHeight)/55, .5), 0)
# Align to tower. Pretty self explanitory.
self.drive.align_to_tower()
else:
# Otherwise, move on to next step, shooting.
self.next_state('shoot')

@state
def shoot(self):
"""Align to tower and fire ball.""""
self.drive.align_to_tower()
self.shootBall.shoot()

def done(self):
"""Finish up, disable everything."""
# Kill camera tracking
self.drive.disable_camera_tracking()
# Stop state machine
StateMachine.done(self)

0 comments on commit 5685e2d

Please sign in to comment.