示例#1
0
def drive_condition(
    lmotor,
    rmotor,
    testFunction,
    state=True
):  # Drives while "testFunction" returns "state" | an example would be: x.drive_condition(50, 50, x.getWait)
    print "driving under condition"
    _clear_ticks()
    if lmotor == 0 or rmotor == 0:
        print "please use pivot instead!"

    elif abs(rmotor) <= abs(lmotor):
        mod = rmotor / (lmotor * 1.0)
        newLeftSpeed = lmotor
        newRightSpeed = int(mod * lmotor)
    elif abs(lmotor) < abs(rmotor):
        mod = (lmotor * 1.0) / rmotor
        newLeftSpeed = int(mod * rmotor)
        newRightSpeed = rmotor
    while testFunction() is state:
        if int(_right_ticks() / mod) == int(_left_ticks() / mod):
            _drive(newLeftSpeed, newRightSpeed)
        if int(_right_ticks() / mod) > int(_left_ticks() / mod):
            _drive(newLeftSpeed, int(newRightSpeed / 1.3))
        if int(_left_ticks() / mod) > int(_right_ticks() / mod):
            _drive(int(newLeftSpeed / 1.3), newRightSpeed)
    freeze_motors()
    print get_motor_position_counter(RMOTOR)
示例#2
0
def arc_radius(angle, turnRadius, speed):  # Turns the robot "angle" degrees by arcing about "turnRadius".
    smallCircRadius = turnRadius - (WHEEL_DISTANCE / 2)
    largeCircRadius = turnRadius + (WHEEL_DISTANCE / 2)
    smallCircum = pi * 2 * smallCircRadius
    largeCircum = pi * 2 * largeCircRadius
    smallCircSeg = (angle / 360.0) * smallCircum
    largeCircSeg = (angle / 360.0) * largeCircum
    if turnRadius < 0:    msleep(300)
    pivot_right(3, -5)
    speed = -speed
    _clear_ticks()
    smallTicks = abs(INCHES_TO_TICKS * smallCircSeg)
    largeTicks = abs(INCHES_TO_TICKS * largeCircSeg)
    if angle > 0:
        smallSpeed = int(speed * (smallTicks / largeTicks))
        largeSpeed = int(speed)
        print smallTicks
        print largeTicks
        print smallTicks / largeTicks
        print smallSpeed
        print largeSpeed
        while _right_ticks() <= largeTicks:
            if (_right_ticks() / largeTicks) == (_left_ticks() / smallTicks):
                _drive(smallSpeed, largeSpeed)
            if (_right_ticks() / largeTicks) > (_left_ticks() / smallTicks):
                _drive(smallSpeed, int(largeSpeed / 1.3))
            if (_left_ticks() / smallTicks) > (_right_ticks() / largeTicks):
                _drive(int(smallSpeed / 1.3), largeSpeed)
    freeze_motors()
    print smallTicks
    print largeTicks
    print get_motor_position_counter(RMOTOR)
示例#3
0
def rotate_spinner(rotations, speed):
    full_rotation = 1400.0
    start = get_motor_position_counter(SPINNER)
    motor_power(SPINNER, speed)

    tries_remaining = 3
    previous = 0
    counter = 0

    while abs(get_motor_position_counter(SPINNER) - start) < abs(
            full_rotation * rotations) and tries_remaining > 0:
        if counter >= 10:
            counter = 0
            if tries_remaining > 0:
                motor_power(SPINNER, int(-speed))
                msleep(300)
                motor_power(SPINNER, speed)
            tries_remaining -= 1
        elif abs(get_motor_position_counter(SPINNER)) == previous:
            counter += 1
        else:
            counter = 0
            previous = abs(get_motor_position_counter(SPINNER))
        msleep(10)
    print "rotated {} out of {}".format(
        get_motor_position_counter(SPINNER) - start,
        abs(full_rotation * rotations))
    freeze(SPINNER)
示例#4
0
def rotate_until_stalled(speed):
    counter = 0
    motor_power(SPINNER, speed)
    previous = abs(get_motor_position_counter(SPINNER))
    while counter < 10:
        if abs(get_motor_position_counter(SPINNER)) == previous:
            counter += 1
        else:
            counter = 0
            previous = abs(get_motor_position_counter(SPINNER))
        msleep(10)
    freeze(SPINNER)
    clear_motor_position_counter(SPINNER)
示例#5
0
def rotate_compass(deg, speed):  # Rotates by using both wheels equally.
    if deg < 0:
        speed = -speed
        deg = -deg
    angle = deg / 360.0
    circ = pi * WHEEL_DISTANCE
    inches = angle * circ
    print circ
    print inches
    ticks = int(INCHES_TO_TICKS * inches)
    _clear_ticks()
    _drive(-speed, speed)

    for _ in range(0, 10):
        magneto_x()
        magneto_y()
        msleep(10)

    mx = magneto_x()
    my = magneto_y()

    while abs(mx) > 100 or abs(my) > 100:
        mx = magneto_x()
        my = magneto_y()

    data = {"min_x": mx, "min_y": my, "max_x": mx, "max_y": my}

    while _right_ticks() <= ticks:

        mag_x = magneto_x()
        mag_y = magneto_y()

        print(str(mag_x) + "\t" + str(mag_y))

        if mag_x > data["max_x"] and abs(
                mag_x -
                data["max_x"]) < 5:  #  and abs(mag_x - data["max_x"])< 10
            data["max_x"] = mag_x
            # print("new max")
        if mag_y > data["max_y"] and abs(mag_y - data["max_y"]) < 5:
            data["max_y"] = mag_y
            print("new max y " + str(mag_y))
        if mag_x < data["min_x"] and abs(mag_x - data["min_x"]) < 5:
            data["min_x"] = mag_x
        if mag_y < data["min_y"] and abs(mag_y - data["min_y"]) < 5:
            data["min_y"] = mag_y

    freeze_motors()
    print get_motor_position_counter(RMOTOR)
    return data
示例#6
0
def drive_timed(lmotor, rmotor, time):
    print("driving timed")
    _clear_ticks()
    end = seconds() + time
    if lmotor == 0 or rmotor == 0:
        print("please use pivot instead!")

    elif abs(rmotor) <= abs(lmotor):
        mod = rmotor / (lmotor * 1.0)
        newLeftSpeed = lmotor
        newRightSpeed = int(mod * lmotor)
    elif abs(lmotor) < abs(rmotor):
        mod = (lmotor * 1.0) / rmotor
        newLeftSpeed = int(mod * rmotor)
        newRightSpeed = rmotor
    while seconds() <= end:
        if int(_right_ticks() / mod) == int(_left_ticks() / mod):
            _drive(newLeftSpeed, newRightSpeed)
        if int(_right_ticks() / mod) > int(_left_ticks() / mod):
            _drive(newLeftSpeed, int(newRightSpeed / 1.3))
        if int(_left_ticks() / mod) > int(_right_ticks() / mod):
            _drive(int(newLeftSpeed / 1.3), newRightSpeed)
    freeze_motors()
    print(get_motor_position_counter(RMOTOR))
    ao()
示例#7
0
def rotate(deg, speed):  # Rotates by using both wheels equally.
    if deg < 0:
        speed = -speed
        deg = -deg
    angle = deg / 360.0
    circ = pi * WHEEL_DISTANCE
    inches = angle * circ
    print circ
    print inches
    ticks = int(INCHES_TO_TICKS * inches)
    _clear_ticks()
    _drive(-speed, speed)
    while _right_ticks() <= ticks:
        pass
    freeze_motors()
    print get_motor_position_counter(RMOTOR)
示例#8
0
def drive_speed(inches, speed):  # Drives an exact distance in inches.
    print "driving exact distance"
    if inches < 0:
        speed = -speed
    _clear_ticks()
    ticks = abs(INCHES_TO_TICKS * inches)
    while _right_ticks() <= ticks:
        if _right_ticks() == _left_ticks():
            _drive(speed, speed)
        if _right_ticks() > _left_ticks():
            _drive(speed, int(speed / 1.3))
        if _left_ticks() > _right_ticks():
            _drive(int(speed / 1.3), speed)
    freeze_motors()
    print ticks
    print get_motor_position_counter(RMOTOR)
示例#9
0
def drive_condition(lmotor, rmotor, testFunction, state=True):
    print "driving under condition"
    _clear_ticks()
    if lmotor == 0 or rmotor == 0:
        print "this won't work! please use pivot_right_condition or pivot_left_condition instead!"
        exit(0)

    elif abs(rmotor) <= abs(lmotor):
        mod = rmotor / (lmotor * 1.0)
        newLeftSpeed = lmotor
        newRightSpeed = int(mod * lmotor)
    elif abs(lmotor) < abs(rmotor):
        mod = (lmotor * 1.0) / rmotor
        newLeftSpeed = int(mod * rmotor)
        newRightSpeed = rmotor
    while testFunction() is state:
        if int(_right_ticks() / mod) == int(_left_ticks() / mod):
            _drive(newLeftSpeed, newRightSpeed)
        if int(_right_ticks() / mod) > int(_left_ticks() / mod):
            _drive(newLeftSpeed, int(newRightSpeed / 1.3))
        if int(_left_ticks() / mod) > int(_right_ticks() / mod):
            _drive(int(newLeftSpeed / 1.3), newRightSpeed)
    freeze_motors()
    print get_motor_position_counter(RMOTOR)
示例#10
0
def drive_speed(inches, speed):  # Drives an exact distance in inches.
    print("driving exact distance")
    global lAdjust
    if inches < 0:
        speed = -speed
    if speed < 0:
        lAdjust = lAdjustBack
    else:
        lAdjust = lAdjustForward
    _clear_ticks()
    ticks = abs(INCHES_TO_TICKS * inches)
    while _right_ticks() <= ticks:
        if _right_ticks() == _left_ticks():
            _drive(speed, speed)
        if _right_ticks() > _left_ticks():
            _drive(speed, int(speed / 1.3))
        if _left_ticks() > _right_ticks():
            _drive(int(speed / 1.3), speed)
    freeze_motors()
    print(ticks)
    print(get_motor_position_counter(RMOTOR))
示例#11
0
def _left_ticks():  # Returns the left motor's tick count.
    return abs(get_motor_position_counter(LMOTOR) * lAdjust)
示例#12
0
def _right_ticks():  # Returns the right motor's tick count.
    return abs(get_motor_position_counter(RMOTOR))
示例#13
0
def rotate_to_safe(power):
    full_rotation = 1400.0
    motor_power(SPINNER, power)
    while abs(get_motor_position_counter(SPINNER)) % full_rotation > 50:
        pass
    freeze(SPINNER)
示例#14
0
def wait_for_someone_to_rotate():
    display("please spin me back")
    clear_motor_position_counter(SPINNER)
    while abs(get_motor_position_counter(SPINNER)) < 350:
        pass
    display("good job")
示例#15
0
        newRightSpeed = int(mod * lmotor)
    elif abs(lmotor) < abs(rmotor):
        mod = (lmotor * 1.0) / rmotor
        newLeftSpeed = int(mod * rmotor)
        newRightSpeed = rmotor
    while seconds() <= end:
        if int(_right_ticks() / mod) == int(_left_ticks() / mod):
            _drive(newLeftSpeed, newRightSpeed)
        if int(_right_ticks() / mod) > int(_left_ticks() / mod):
            _drive(newLeftSpeed, int(newRightSpeed / 1.3))
        if int(_left_ticks() / mod) > int(_right_ticks() / mod):
            _drive(int(newLeftSpeed / 1.3), newRightSpeed)
    freeze_motors()


print get_motor_position_counter(RMOTOR)


def drive_condition(lmotor, rmotor, testFunction, state=True):
    # Drives while "testFunction" returns "state" | an example would be: x.drive_condition(50, 50, x.getWait)
    print "driving under condition"
    _clear_ticks()
    if lmotor == 0 or rmotor == 0:
        print "this won't work! please use pivot_right_condition or pivot_left_condition instead!"
        exit(0)

    elif abs(rmotor) <= abs(lmotor):
        mod = rmotor / (lmotor * 1.0)
        newLeftSpeed = lmotor
        newRightSpeed = int(mod * lmotor)
    elif abs(lmotor) < abs(rmotor):