Safe - Code

From RoboWiki
Jump to: navigation, search

Return back to project page: Safe - Fedor Agarshev

Python code for the Safe project:

 
from spike import PrimeHub, LightMatrix, Button, StatusLight, ForceSensor, MotionSensor, Speaker, ColorSensor, App, DistanceSensor, Motor, MotorPair
from spike.control import wait_for_seconds, wait_until, Timer
from math import *
import random

hub = PrimeHub()
motor1 = Motor('E')
motor2 = Motor('D')
motorLock = Motor('F')
timer = Timer()

motorLock.set_default_speed(10)
motorLock.run_to_position(0)
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:
                hub.light_matrix.set_pixel(j, i, 100)
            else:
                hub.light_matrix.set_pixel(j, i, 0)




def motor1_position():
    position = motor1.get_position()
    if 12 > position or position > 349:
        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 192 > position and position > 167:
        matrix[4][2] = 1
    else:
        matrix[4][2] = 0
    if 213 > position and position > 191:
        matrix[4][1] = 1
    else:
        matrix[4][1] = 0
    if 236 > position and position > 212:
        matrix[4][0] = 1
    else:
        matrix[4][0] = 0
    if 259 > position and position > 235:
        matrix[3][0] = 1
    else:
        matrix[3][0] = 0
    if 282 > position and position > 258:
        matrix[2][0] = 1
    else:
        matrix[2][0] = 0
    if 303 > position and position > 281:
        matrix[1][0] = 1
    else:
        matrix[1][0] = 0
    if 325 > position and position > 302:
        matrix[0][0] = 1
    else:
        matrix[0][0] = 0
    if 350 > position and position > 325:
        matrix[0][1] = 1
    else:
        matrix[0][1] = 0

def motor2_position():
    position = motor2.get_position()
    if 23 > position or position > 338:
        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 203 > position and position > 157:
        matrix[3][2] = 1
    else:
        matrix[3][2] = 0
    if 248 > position and position > 203:
        matrix[3][1] = 1
    else:
        matrix[3][1] = 0
    if 293 > position and position > 247:
        matrix[2][1] = 1
    else:
        matrix[2][1] = 0
    if 339 > position and position > 292:
        matrix[1][1] = 1
    else:
        matrix[1][1] = 0

def check_for_unlock():
    if matrix[4][1] == 1 and matrix[3][2] == 1:
        if timer.now() > 2:
            return True
    else:
        timer.reset()
        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)
    wait_for_seconds(3)

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)
    wait_for_seconds(3)

def open_chest():
    motorLock.run_to_position(0)
    light_matrix_open_lock()

def close_motors():
    motor1.set_default_speed(80)
    motor2.set_default_speed(80)
    motor1.run_to_position(random.randint(0, 359))
    motor2.run_to_position(random.randint(0, 359))
    motor1.set_default_speed(-80)
    motor2.set_default_speed(-80)
    motor1.run_to_position(random.randint(0, 359))
    motor2.run_to_position(random.randint(0, 359))
    motor1.set_default_speed(80)
    motor2.set_default_speed(80)
    motor1.run_to_position(random.randint(0, 359))
    motor2.run_to_position(random.randint(0, 359))

def close_chest():
    light_matrix_closed_lock()
    show_matrix(matrix)
    motorLock.run_to_position(270)
    close_motors()
    while True:
        motor1_position()
        motor2_position()
        show_matrix(matrix)
        if check_for_unlock():
            break
    open_chest() 

while True:
    close_chest()
    hub.left_button.wait_until_pressed()