Added comments. Good luck for scrimmage y'all!!!
This commit is contained in:
@@ -1,9 +1,11 @@
|
|||||||
|
# Imports... because we kinda need them...
|
||||||
from pybricks.hubs import PrimeHub
|
from pybricks.hubs import PrimeHub
|
||||||
from pybricks.pupdevices import Motor, ColorSensor
|
from pybricks.pupdevices import Motor, ColorSensor
|
||||||
from pybricks.parameters import Port, Stop, Color, Direction
|
from pybricks.parameters import Port, Stop, Color, Direction
|
||||||
from pybricks.robotics import DriveBase
|
from pybricks.robotics import DriveBase
|
||||||
from pybricks.tools import wait, StopWatch, multitask, run_task
|
from pybricks.tools import wait, StopWatch, multitask, run_task
|
||||||
|
|
||||||
|
# Sets all the default values for the runs
|
||||||
hub = PrimeHub()
|
hub = PrimeHub()
|
||||||
left_motor = Motor(Port.A, Direction.COUNTERCLOCKWISE)
|
left_motor = Motor(Port.A, Direction.COUNTERCLOCKWISE)
|
||||||
right_motor = Motor(Port.B)
|
right_motor = Motor(Port.B)
|
||||||
@@ -20,6 +22,7 @@ color_sensor.detectable_colors([Color.YELLOW, Color.BLUE, Color.GREEN, Color.WHI
|
|||||||
hub.speaker.volume(50) # Set the volume of the speaker
|
hub.speaker.volume(50) # Set the volume of the speaker
|
||||||
color_sensor.detectable_colors()
|
color_sensor.detectable_colors()
|
||||||
|
|
||||||
|
# Notes to hertz for nice music
|
||||||
cn = {
|
cn = {
|
||||||
"Cs3": 138.59,
|
"Cs3": 138.59,
|
||||||
"D3": 146.83,
|
"D3": 146.83,
|
||||||
@@ -49,7 +52,7 @@ cn = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# RUNS - contains the mission code that will be executed on color sensor detection
|
||||||
async def Run1():
|
async def Run1():
|
||||||
left_arm.run_angle(1000, 300)
|
left_arm.run_angle(1000, 300)
|
||||||
right_arm.run_angle(1000,500)
|
right_arm.run_angle(1000,500)
|
||||||
@@ -75,6 +78,7 @@ async def Run1():
|
|||||||
await drive_base.straight(10)
|
await drive_base.straight(10)
|
||||||
await drive_base.turn(35)
|
await drive_base.turn(35)
|
||||||
await drive_base.straight(-600)
|
await drive_base.straight(-600)
|
||||||
|
|
||||||
async def Run2():
|
async def Run2():
|
||||||
await drive_base.straight(200)
|
await drive_base.straight(200)
|
||||||
await drive_base.turn(-20)
|
await drive_base.turn(-20)
|
||||||
@@ -89,6 +93,7 @@ async def Run2():
|
|||||||
await drive_base.straight(-100)
|
await drive_base.straight(-100)
|
||||||
await drive_base.turn(-100)
|
await drive_base.turn(-100)
|
||||||
await drive_base.straight(-600)
|
await drive_base.straight(-600)
|
||||||
|
|
||||||
async def Run3():
|
async def Run3():
|
||||||
await drive_base.straight(920)
|
await drive_base.straight(920)
|
||||||
await drive_base.turn(-90)
|
await drive_base.turn(-90)
|
||||||
@@ -98,6 +103,7 @@ async def Run3():
|
|||||||
await drive_base.straight(-110)
|
await drive_base.straight(-110)
|
||||||
await drive_base.turn(90)
|
await drive_base.turn(90)
|
||||||
await drive_base.straight(2000)
|
await drive_base.straight(2000)
|
||||||
|
|
||||||
async def Run4():
|
async def Run4():
|
||||||
await drive_base.straight(519)
|
await drive_base.straight(519)
|
||||||
await left_arm.run_angle(300, -100)
|
await left_arm.run_angle(300, -100)
|
||||||
@@ -121,12 +127,14 @@ async def Run4():
|
|||||||
await drive_base.straight(-200)
|
await drive_base.straight(-200)
|
||||||
await drive_base.turn(-50)
|
await drive_base.turn(-50)
|
||||||
await drive_base.straight(-800)
|
await drive_base.straight(-800)
|
||||||
|
|
||||||
async def Run5():
|
async def Run5():
|
||||||
await drive_base.straight(420)
|
await drive_base.straight(420)
|
||||||
await right_arm.run_angle(300,-100)
|
await right_arm.run_angle(300,-100)
|
||||||
await drive_base.straight(-100)
|
await drive_base.straight(-100)
|
||||||
await right_arm.run_angle(300, 100)
|
await right_arm.run_angle(300, 100)
|
||||||
await drive_base.straight(-350)
|
await drive_base.straight(-350)
|
||||||
|
|
||||||
async def Run6():
|
async def Run6():
|
||||||
left_arm.run_angle(500,200)
|
left_arm.run_angle(500,200)
|
||||||
right_arm.run_angle(500,200)
|
right_arm.run_angle(500,200)
|
||||||
@@ -145,15 +153,17 @@ async def Run6():
|
|||||||
await left_arm.run_angle(300,400)
|
await left_arm.run_angle(300,400)
|
||||||
await drive_base.turn(-60)
|
await drive_base.turn(-60)
|
||||||
await drive_base.straight(900)
|
await drive_base.straight(900)
|
||||||
|
|
||||||
# Celebration sound types
|
# Celebration sound types
|
||||||
class CelebrationSound:
|
class CelebrationSound:
|
||||||
VICTORY_FANFARE = 0
|
VICTORY_FANFARE = 0
|
||||||
LEVEL_UP = 1
|
WAITING_SOUND = 1
|
||||||
SUCCESS_CHIME = 2
|
SUCCESS_CHIME = 2
|
||||||
TA_DA = 3
|
TA_DA = 3
|
||||||
POWER_UP = 4
|
POWER_UP = 4
|
||||||
RICKROLL_INSPIRED = 5
|
RICKROLL_INSPIRED = 5
|
||||||
|
|
||||||
|
# Sounds functions
|
||||||
async def play_victory_fanfare():
|
async def play_victory_fanfare():
|
||||||
"""Classic victory fanfare"""
|
"""Classic victory fanfare"""
|
||||||
notes = [
|
notes = [
|
||||||
@@ -166,8 +176,9 @@ async def play_victory_fanfare():
|
|||||||
for freq, duration in notes:
|
for freq, duration in notes:
|
||||||
await hub.speaker.beep(freq, duration)
|
await hub.speaker.beep(freq, duration)
|
||||||
await wait(50)
|
await wait(50)
|
||||||
async def play_level_up():
|
async def play_waiting_sound():
|
||||||
"""Upward scale for level completion"""
|
|
||||||
|
# T3rm1na1 V3l0c1ty arpeggio recreated in pybricks
|
||||||
notes = [
|
notes = [
|
||||||
(cn["Cs4"], 100),
|
(cn["Cs4"], 100),
|
||||||
(cn["E4"], 100),
|
(cn["E4"], 100),
|
||||||
@@ -190,7 +201,6 @@ async def play_level_up():
|
|||||||
|
|
||||||
for freq, duration in notes:
|
for freq, duration in notes:
|
||||||
await hub.speaker.beep(freq, duration)
|
await hub.speaker.beep(freq, duration)
|
||||||
await wait(20)
|
|
||||||
async def play_success_chime():
|
async def play_success_chime():
|
||||||
"""Simple success notification"""
|
"""Simple success notification"""
|
||||||
notes = [
|
notes = [
|
||||||
@@ -239,6 +249,8 @@ async def play_rickroll_inspired():
|
|||||||
for freq, duration in pattern:
|
for freq, duration in pattern:
|
||||||
await hub.speaker.beep(freq, duration)
|
await hub.speaker.beep(freq, duration)
|
||||||
await wait(50)
|
await wait(50)
|
||||||
|
|
||||||
|
# Basically a big if else statement that calls the functions above
|
||||||
async def celebrate_mission_complete(sound_type=CelebrationSound.RICKROLL_INSPIRED):
|
async def celebrate_mission_complete(sound_type=CelebrationSound.RICKROLL_INSPIRED):
|
||||||
"""
|
"""
|
||||||
Main celebration function to call after completing a mission.
|
Main celebration function to call after completing a mission.
|
||||||
@@ -253,8 +265,8 @@ async def celebrate_mission_complete(sound_type=CelebrationSound.RICKROLL_INSPIR
|
|||||||
# Play the selected celebration sound
|
# Play the selected celebration sound
|
||||||
if sound_type == CelebrationSound.VICTORY_FANFARE:
|
if sound_type == CelebrationSound.VICTORY_FANFARE:
|
||||||
await play_victory_fanfare()
|
await play_victory_fanfare()
|
||||||
elif sound_type == CelebrationSound.LEVEL_UP:
|
elif sound_type == CelebrationSound.WAITING_SOUND:
|
||||||
await play_level_up()
|
await play_waiting_sound()
|
||||||
elif sound_type == CelebrationSound.SUCCESS_CHIME:
|
elif sound_type == CelebrationSound.SUCCESS_CHIME:
|
||||||
await play_success_chime()
|
await play_success_chime()
|
||||||
elif sound_type == CelebrationSound.TA_DA:
|
elif sound_type == CelebrationSound.TA_DA:
|
||||||
@@ -275,10 +287,11 @@ async def celebrate_mission_complete(sound_type=CelebrationSound.RICKROLL_INSPIR
|
|||||||
|
|
||||||
hub.light.off()
|
hub.light.off()
|
||||||
|
|
||||||
|
# This where everything happens
|
||||||
async def main():
|
async def main():
|
||||||
# MAIN LOOP
|
# MAIN LOOP
|
||||||
while True:
|
while True:
|
||||||
await celebrate_mission_complete(CelebrationSound.LEVEL_UP)
|
await celebrate_mission_complete(CelebrationSound.WAITING_SOUND)
|
||||||
color_reflected_percent = await color_sensor.reflection()
|
color_reflected_percent = await color_sensor.reflection()
|
||||||
print(f"Color reflection percentage: {color_reflected_percent}")
|
print(f"Color reflection percentage: {color_reflected_percent}")
|
||||||
|
|
||||||
@@ -310,7 +323,7 @@ async def main():
|
|||||||
elif color_detected == Color.RED:
|
elif color_detected == Color.RED:
|
||||||
print('Running Mission 6')
|
print('Running Mission 6')
|
||||||
await Run6()
|
await Run6()
|
||||||
await celebrate_mission_complete(CelebrationSound.LEVEL_UP)
|
await celebrate_mission_complete(CelebrationSound.POWER_UP)
|
||||||
else:
|
else:
|
||||||
hub.light.off()
|
hub.light.off()
|
||||||
left_motor.stop()
|
left_motor.stop()
|
||||||
|
|||||||
Reference in New Issue
Block a user