Programing Bot - Code

From RoboWiki
Revision as of 19:18, 29 May 2023 by Robot (talk | contribs)
Jump to: navigation, search

Return back to project page: Programming Bot - Fedor Agarshev

Python code for the Spike Pong 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
import math

hub = PrimeHub()
hub.speaker.set_volume(50)
motor_pair = MotorPair('A', 'B')
color_sensor = ColorSensor('E')
motor_pair.set_default_speed(50)
force_sensor = ForceSensor('F')
distance_sensor = DistanceSensor('C')

def some_button_pressed():
    return hub.left_button.is_pressed() or hub.right_button.is_pressed()

def show_image(image):
    for y in range(0,len(image)):
        for x in range(0,len(image[y])):
            if image[y][x] == 1:
                hub.light_matrix.set_pixel(x, y, 100)
            else:
                hub.light_matrix.set_pixel(x, y, 0)



class Step_forward:
    def __init__(self):
        hub.light_matrix.show_image('ARROW_N')
        self.previous_order = None
        self.next_order = None
        self.distance = 0

    def set_variables(self):
        force_sensor.wait_until_pressed()
        while force_sensor.is_pressed():
            self.distance += 1
            hub.speaker.beep(70, 0.2)
        hub.speaker.beep(60, 0.5)
        hub.speaker.beep(67, 0.5)
        hub.speaker.beep(60, 0.5)


    def set_previous_order(self, previous_order):
        self.previous_order = previous_order
    
    def set_next_order(self, next_order):
        self.next_order = next_order

    def get_next_order(self):
        return self.next_order

    def execute(self):
        motor_pair.move(self.distance, 'cm', steering=0)

class Moving_forward_up_the_wall:
    def __init__(self):
        image = [[1,1,1,1,1],
                 [0,0,1,0,0],
                 [0,1,1,1,0],
                 [1,0,1,0,1],
                 [0,0,1,0,0]]
        show_image(image)
        self.previous_order = None
        self.next_order = None

    def set_variables(self):
        pass


    def set_previous_order(self, previous_order):
        self.previous_order = previous_order

    def set_next_order(self, next_order):
        self.next_order = next_order

    def get_next_order(self):
        return self.next_order

    def execute(self):
        motor_pair.start()
        distance_sensor.wait_for_distance_closer_than(10, 'cm')
        motor_pair.stop()

class Right_forward:
    def __init__(self):
        hub.light_matrix.show_image('ARROW_E')
        self.previous_order = None
        self.next_order = None
        self.angle = 0
    
    def set_variables(self):
        force_sensor.wait_until_pressed()
        while force_sensor.is_pressed():
            self.angle += 0.5               # 90 degrees is about 10 units
            hub.speaker.beep(70, 0.2)
        hub.speaker.beep(60, 0.5)
        hub.speaker.beep(67, 0.5)
        hub.speaker.beep(60, 0.5)

    def set_previous_order(self, previous_order):
        self.previous_order = previous_order

    def set_next_order(self, next_order):
        self.next_order = next_order
    
    def get_next_order(self):
        return self.next_order

    def execute(self):
        motor_pair.move(self.angle, 'cm', steering=100)

class Left_forward:
    def __init__(self):
        hub.light_matrix.show_image('ARROW_W')
        self.previous_order = None
        self.next_order = None
        self.angle = 0

    def set_variables(self):
        force_sensor.wait_until_pressed()
        while force_sensor.is_pressed():
            self.angle += 0.5            # 90 degrees is about 10 units
            hub.speaker.beep(70, 0.2)
        hub.speaker.beep(60, 0.5)
        hub.speaker.beep(67, 0.5)
        hub.speaker.beep(60, 0.5)

    def set_previous_order(self, previous_order):
        self.previous_order = previous_order

    def set_next_order(self, next_order):
        self.next_order = next_order
    
    def get_next_order(self):
        return self.next_order

    def execute(self):
        motor_pair.move(self.angle, 'cm', steering=-100)


class Game:
    def __init__(self):
        self.count = 0
        self.first_order = None
        self.current_order = None

        #variables for target
        self.target_color = 'red'

    def create_order(self, order):
        wait_until(some_button_pressed)
        if hub.left_button.is_pressed():
            return
        order.set_variables()
        if self.first_order == None:
            self.first_order = order
            self.current_order = order
        else:
            order.set_previous_order(self.current_order)
            self.current_order.set_next_order(order)
            self.current_order = order
        self.count += 1

    def check_target(self):
        # Conditions for completing the task
        #    You can change them however you want.
        if color_sensor.get_color() == self.target_color:
            return True
        return False

    def play(self):
        hub.light_matrix.write(self.count)
        while True:
            color_sensor.wait_for_new_color()
            color = color_sensor.get_color()
            if color == 'green':
                self.create_order(Left_forward())
            elif color == 'blue':
                self.create_order(Right_forward())
            elif color == 'violet':
                self.create_order(Step_forward())
            elif color == 'yellow':
                self.create_order(Moving_forward_up_the_wall())
            elif color == "red":
                hub.light_matrix.show_image('PACMAN')
                wait_until(some_button_pressed)
                if hub.left_button.is_pressed():
                    pass
                else:
                    while self.first_order != None:
                        self.first_order.execute()
                        self.first_order = self.first_order.get_next_order()
                        self.count -= 1
                        hub.light_matrix.write(self.count)
                    if self.check_target():
                        hub.light_matrix.write('Good job')
                        hub.speaker.beep(80, 0.5)
                        hub.speaker.beep(87, 0.5)
                        hub.speaker.beep(80, 0.5)

            hub.light_matrix.write(self.count)

game = Game()
game.play()