Difference between revisions of "Programing Bot - Code V3"
From RoboWiki
(Created page with "Return back to project page: Programming Bot - Fedor Agarshev Python code for the Programing Bot project: <syntaxhighlight...") |
|||
Line 9: | Line 9: | ||
− | speaker_volume = | + | speaker_volume = 50 |
motor_pair.unpair(motor_pair.PAIR_1) | motor_pair.unpair(motor_pair.PAIR_1) | ||
motor_pair.pair(motor_pair.PAIR_1, port.A, port.B) | motor_pair.pair(motor_pair.PAIR_1, port.A, port.B) | ||
Line 48: | Line 48: | ||
while force_sensor.is_pressed(forceSensor): | while force_sensor.is_pressed(forceSensor): | ||
self.distance += 1 | self.distance += 1 | ||
− | speaker.beep( | + | speaker.beep(700, 500,speaker_volume) |
time.sleep_ms(10) | time.sleep_ms(10) | ||
− | speaker.beep( | + | speaker.beep(600, 500,speaker_volume) |
− | speaker.beep( | + | speaker.beep(670, 500,speaker_volume) |
− | speaker.beep( | + | speaker.beep(600, 500,speaker_volume) |
Line 115: | Line 115: | ||
while force_sensor.is_pressed(forceSensor): | while force_sensor.is_pressed(forceSensor): | ||
self.angle += 1 | self.angle += 1 | ||
− | speaker.beep( | + | speaker.beep(700, 500,speaker_volume) |
time.sleep_ms(10) | time.sleep_ms(10) | ||
− | speaker.beep( | + | speaker.beep(600, 500,speaker_volume) |
− | speaker.beep( | + | speaker.beep(670, 500,speaker_volume) |
− | speaker.beep( | + | speaker.beep(600, 500,speaker_volume) |
def set_previous_order(self, previous_order): | def set_previous_order(self, previous_order): | ||
Line 151: | Line 151: | ||
while force_sensor.is_pressed(forceSensor): | while force_sensor.is_pressed(forceSensor): | ||
self.angle += 1 | self.angle += 1 | ||
− | speaker.beep( | + | speaker.beep(700, 500,speaker_volume) |
time.sleep_ms(10) | time.sleep_ms(10) | ||
− | speaker.beep( | + | speaker.beep(600, 500,speaker_volume) |
− | speaker.beep( | + | speaker.beep(670, 500,speaker_volume) |
− | speaker.beep( | + | speaker.beep(600, 500,speaker_volume) |
def set_previous_order(self, previous_order): | def set_previous_order(self, previous_order): | ||
Line 235: | Line 235: | ||
if self.check_target(): | if self.check_target(): | ||
light_matrix.write('Good job') | light_matrix.write('Good job') | ||
− | speaker.beep( | + | speaker.beep(800, 500,speaker_volume) |
− | speaker.beep( | + | speaker.beep(870, 500,speaker_volume) |
− | speaker.beep( | + | speaker.beep(800, 500,speaker_volume) |
light_matrix.write(str(self.count)) | light_matrix.write(str(self.count)) | ||
Line 243: | Line 243: | ||
game = Game() | game = Game() | ||
game.play() | game.play() | ||
− | |||
</syntaxhighlight> | </syntaxhighlight> |
Latest revision as of 18:55, 4 June 2023
Return back to project page: Programming Bot - Fedor Agarshev
Python code for the Programing Bot project:
from hub import light_matrix, port, button, light, speaker
import motor,time, color_sensor, motor_pair, force_sensor, distance_sensor, color
speaker_volume = 50
motor_pair.unpair(motor_pair.PAIR_1)
motor_pair.pair(motor_pair.PAIR_1, port.A, port.B)
motorPair = motor_pair.PAIR_1
colorSensor = port.E
motor_pair_speed = 500
forceSensor = port.F
distanceSensor = port.C
def some_button_pressed():
return button.pressed(button.LEFT) or button.pressed(button.RIGHT)
def show_image(image):
for y in range(0,len(image)):
for x in range(0,len(image[y])):
if image[y][x] == 1:
light_matrix.set_pixel(x, y, 100)
else:
light_matrix.set_pixel(x, y, 0)
class Step_forward:
def __init__(self):
image = [[0,0,1,0,0],
[0,1,1,1,0],
[1,0,1,0,1],
[0,0,1,0,0],
[0,0,1,0,0]]
show_image(image)
self.previous_order = None
self.next_order = None
self.distance = 0
def set_variables(self):
while not force_sensor.is_pressed(forceSensor):
pass
while force_sensor.is_pressed(forceSensor):
self.distance += 1
speaker.beep(700, 500,speaker_volume)
time.sleep_ms(10)
speaker.beep(600, 500,speaker_volume)
speaker.beep(670, 500,speaker_volume)
speaker.beep(600, 500,speaker_volume)
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_for_degrees(motorPair, self.distance, 0, velocity= motor_pair_speed)
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.move(motorPair,0, velocity= 300)
while not distance_sensor.distance(distanceSensor) < 110:
pass
motor_pair.stop(motorPair)
class Right_forward:
def __init__(self):
image = [[0,0,1,0,0],
[0,0,0,1,0],
[1,1,1,1,1],
[0,0,0,1,0],
[0,0,1,0,0]]
show_image(image)
self.previous_order = None
self.next_order = None
self.angle = 0
def set_variables(self):
while not force_sensor.is_pressed(forceSensor):
pass
while force_sensor.is_pressed(forceSensor):
self.angle += 1
speaker.beep(700, 500,speaker_volume)
time.sleep_ms(10)
speaker.beep(600, 500,speaker_volume)
speaker.beep(670, 500,speaker_volume)
speaker.beep(600, 500,speaker_volume)
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_for_degrees(motorPair, self.angle, 100, velocity= motor_pair_speed)
class Left_forward:
def __init__(self):
image = [[0,0,1,0,0],
[0,1,0,0,0],
[1,1,1,1,1],
[0,1,0,0,0],
[0,0,1,0,0]]
show_image(image)
self.previous_order = None
self.next_order = None
self.angle = 0
def set_variables(self):
while not force_sensor.is_pressed(forceSensor):
pass
while force_sensor.is_pressed(forceSensor):
self.angle += 1
speaker.beep(700, 500,speaker_volume)
time.sleep_ms(10)
speaker.beep(600, 500,speaker_volume)
speaker.beep(670, 500,speaker_volume)
speaker.beep(600, 500,speaker_volume)
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_for_degrees(motorPair, self.angle, -100, velocity= motor_pair_speed)
class Game:
def __init__(self):
self.count = 0
self.first_order = None
self.current_order = None
#variables for target
self.target_color = color.RED
def create_order(self, order):
while not some_button_pressed():
pass
if button.pressed(button.LEFT):
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) #warning here is ok
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.color(colorSensor) == self.target_color:
return True
return False
def play(self):
light_matrix.write(str(self.count))
while True:
while color_sensor.color == -1:
pass
color1 = color_sensor.color(colorSensor)
if color1 == color.GREEN:
self.create_order(Left_forward())
elif color1 == color.BLUE:
self.create_order(Right_forward())
elif color1 == 1:
self.create_order(Step_forward())
elif color1 == color.YELLOW:
self.create_order(Moving_forward_up_the_wall())
elif color1 == color.RED:
image = [[0,1,1,0,0],
[0,1,1,1,0],
[0,1,1,1,1],
[0,1,1,1,0],
[0,1,1,0,0]]
show_image(image)
while not some_button_pressed():
pass
if button.pressed(button.LEFT):
pass
else:
while self.first_order != None:
self.first_order.execute()
self.first_order = self.first_order.get_next_order()
self.count -= 1
light_matrix.write(str(self.count))
if self.check_target():
light_matrix.write('Good job')
speaker.beep(800, 500,speaker_volume)
speaker.beep(870, 500,speaker_volume)
speaker.beep(800, 500,speaker_volume)
light_matrix.write(str(self.count))
game = Game()
game.play()