Safe - Code for version 3

From RoboWiki
Jump to: navigation, search

Return back to project page: Safe - Fedor Agarshev

Python code for the Safe project:

 
from hub import light_matrix, port, button
import motor
import random, time


motor1 = port.E
motor2 = port.D
motorLock = port.F


motor.run_to_relative_position(motorLock, 0, 200)
matrix = [[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]]

def show_matrix(matrix):
    for i in range(len(matrix)):
        for j in range(len(matrix[0])):
            if matrix[i][j] == 1:
                light_matrix.set_pixel(j, i, 100)
            else:
                light_matrix.set_pixel(j, i, 0)




def motor1_position():
    position = motor.absolute_position(motor1)
    if 12 > position and position > 0:
        matrix[0][2] = 1
    else:
        matrix[0][2] = 0
    if 33 > position and position > 11:
        matrix[0][3] = 1
    else:
        matrix[0][3] = 0
    if 56 > position and position > 32:
        matrix[0][4] = 1
    else:
        matrix[0][4] = 0
    if 77 > position and position > 55:
        matrix[1][4] = 1
    else:
        matrix[1][4] = 0
    if 101 > position and position > 76:
        matrix[2][4] = 1
    else:
        matrix[2][4] = 0
    if 123 > position and position > 100:
        matrix[3][4] = 1
    else:
        matrix[3][4] = 0
    if 144 > position and position > 122:
        matrix[4][4] = 1
    else:
        matrix[4][4] = 0
    if 168 > position and position > 143:
        matrix[4][3] = 1
    else:
        matrix[4][3] = 0
    if -168 > position or position > 167:
        matrix[4][2] = 1
    else:
        matrix[4][2] = 0
    if -143 > position and position > -168:
        matrix[4][1] = 1
    else:
        matrix[4][1] = 0
    if -118 > position and position > -143:
        matrix[4][0] = 1
    else:
        matrix[4][0] = 0
    if -93 > position and position > -118:
        matrix[3][0] = 1
    else:
        matrix[3][0] = 0
    if -68 > position and position > -93:
        matrix[2][0] = 1
    else:
        matrix[2][0] = 0
    if -43 > position and position > -68:
        matrix[1][0] = 1
    else:
        matrix[1][0] = 0
    if -18 > position and position > -43:
        matrix[0][0] = 1
    else:
        matrix[0][0] = 0
    if -1 > position and position > -18:
        matrix[0][1] = 1
    else:
        matrix[0][1] = 0

def motor2_position():
    position = motor.absolute_position(motor2)
    if 23 > position and position > 0:
        matrix[1][2] = 1
    else:
        matrix[1][2] = 0
    if 48 > position and position > 23:
        matrix[1][3] = 1
    else:
        matrix[1][3] = 0
    if 113 > position and position > 47:
        matrix[2][3] = 1
    else:
        matrix[2][3] = 0
    if 158 > position and position > 112:
        matrix[3][3] = 1
    else:
        matrix[3][3] = 0
    if -140 > position and position > -180:
        matrix[3][2] = 1
    else:
        matrix[3][2] = 0
    if -100 > position and position > -140:
        matrix[3][1] = 1
    else:
        matrix[3][1] = 0
    if -60 > position and position > -100:
        matrix[2][1] = 1
    else:
        matrix[2][1] = 0
    if -1 > position and position > -60:
        matrix[1][1] = 1
    else:
        matrix[1][1] = 0

def check_for_unlock():
    if matrix[4][1] == 1 and matrix[3][2] == 1:
            return True
    else:
        return False

def light_matrix_open_lock():
    matrix =    [[0,1,1,1,0],
                [0,1,0,1,0],
                [0,0,0,1,0],
                [1,1,1,1,1],
                [1,1,1,1,1]]
    show_matrix(matrix)
    time.sleep_ms(3000)

def light_matrix_closed_lock():
    matrix =    [[0,1,1,1,0],
                [0,1,0,1,0],
                [0,1,0,1,0],
                [1,1,1,1,1],
                [1,1,1,1,1]]
    show_matrix(matrix)
    time.sleep_ms(3000)

def open_chest():
    motor.run_for_degrees(motorLock, 90, 200)
    light_matrix_open_lock()

def close_motors():
    motor.run_to_relative_position(motor1,random.randint(0, 359), 800)
    motor.run_to_relative_position(motor2,random.randint(0, 359), 800)
    motor.run_to_relative_position(motor1,random.randint(0, 359), -800)
    motor.run_to_relative_position(motor2,random.randint(0, 359), -800)
    motor.run_to_relative_position(motor1,random.randint(0, 359), 800)
    motor.run_to_relative_position(motor2,random.randint(0, 359), 800)

def close_chest():
    light_matrix_closed_lock()
    show_matrix(matrix)
    motor.run_for_degrees(motorLock, -90, 200)
    close_motors()
    while True:
        motor1_position()
        motor2_position()
        show_matrix(matrix)
        if check_for_unlock():
            break
    open_chest()

while True:
    close_chest()
    while not button.pressed(button.LEFT):
        pass