Scribbler Code So Far

Posted Below is my Scribbler code that is finalized (experimental things not included) and commented.

QUICKSTART.PY FILE

##  This program was created to connect and
##  calibrate the S2 Robot for easy use.
##  The calibration for walking a certain distance is still in progress.
##
##  Written by Michael Borke

# First, import the myro library and choose the Bluetooth port
# that the Scribbler is connected to
from myro import *
init()

f = open(pickAFile(),'r')       # Choose the file that holds your calibration variables
turnVar = float(f.readline())   # Extract the variables from the file for consistency
walkVar = .16

# This function is used to calibrate the turn function for the Scribbler
# Place the Scribbler with the center of its wheels on a dark black line.
# Then, run this function, making sure to follow the instructions. 
def caliTurn():
    global turnVar
    timeSum = 0
    turnCount = 0
    t = currentTime()
    while turnCount < 5:
        motors(.5,-.5)
        if getLine() != [0,0]:
            timeSum += currentTime() - t
            stop()
            turnCount += 1
            raw_input("Turn me back, then press enter")
            t = currentTime()
    turnVar = (timeSum/5)*2
    f = open(pickAFile(),'w')
    f.write(str(turnVar))
    return turnVar

# This function simplifies the turnLeft() and turnRight() functions
# by using a calibrated time variable to calculate how long it should
# run to turn a given number of degrees.
#
# TL;DR
# You can now turn the Scribbler either direction a given number of
# degrees (instead of speed and time) with this function.
def turn(direction,angle):
    if direction == "left":
        turnLeft(1,(turnVar/360*angle))
    if direction == "right":
        turnRight(1,(turnVar/360*angle))
    stop()

# **IN PROGRESS**
# Adjusts the forward() function so that you can walk a set distance,
# instead of speed and time.
def walk(distance):
    forward(1,(walkVar*distance))
    stop()

# Draws a closed polygon according to the given size and number of sides
def shape(size,sides):
    for s in range(sides):
        walk(size)
        turn("right", 180-((180*(sides-2))/sides))
    stop()

# Draws a simple star according to the given size 
def star(size):
    walk(size)
    turn("right", 144)
    walk(size)
    turn("right", 144)
    walk(size)
    turn("right", 144)
    walk(size)
    turn("right", 144)
    walk(size)
    turn("right", 144)
    stop()

# Draws a simple spiral
def spiral():
    leftSpeed = 1
    rightSpeed = 0
    t = currentTime()
    while timeRemaining(10):
        for t in timer(10):
            rightSpeed = t/30
            motors(leftSpeed, rightSpeed)
    stop()

# Draws a star with no extra lines
def star2(size):
    walk(size)
    turn("left", 72)
    walk(size)
    turn("right", 144)
    walk(size)
    turn("left", 72)
    walk(size)
    turn("right", 144)
    walk(size)
    turn("left", 72)
    walk(size)
    turn("right", 144)
    walk(size)
    turn("left", 72)
    walk(size)
    turn("right", 144)
    walk(size)
    turn("left", 72)
    walk(size)
    turn("right", 144)
    stop()

# Chases a red object until it gets close enough to it,
# or it doesn't see it anymore.
def chase(speed):
    while avgX != 0 and getObstacle("center") < 1000:
        onPixels, avgX, avgY = getBlob()
        if avgX <= 85:
            turnLeft(speed)
        elif avgX <= 170:
            forward(speed)
        else:
            turnRight(speed)
    stop()

# Tired of watching your friends play Twister without
# you because you are the spinner?
# Have the Scribbler Spin for you!
#
# Place the Scribbler on the spinner such that you can
# see the metal spin point though its pen hole. Then,
# enter this function and let the fun begin!
def twister():
    while 1:
        turnRight(1, (12*randomNumber()))
        wait(5)