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.pupdevices import Motor, ColorSensor
|
||||
from pybricks.parameters import Port, Stop, Color, Direction
|
||||
from pybricks.robotics import DriveBase
|
||||
from pybricks.tools import wait, StopWatch, multitask, run_task
|
||||
|
||||
# Sets all the default values for the runs
|
||||
hub = PrimeHub()
|
||||
left_motor = Motor(Port.A, Direction.COUNTERCLOCKWISE)
|
||||
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
|
||||
color_sensor.detectable_colors()
|
||||
|
||||
# Notes to hertz for nice music
|
||||
cn = {
|
||||
"Cs3": 138.59,
|
||||
"D3": 146.83,
|
||||
@@ -49,7 +52,7 @@ cn = {
|
||||
}
|
||||
|
||||
|
||||
|
||||
# RUNS - contains the mission code that will be executed on color sensor detection
|
||||
async def Run1():
|
||||
left_arm.run_angle(1000, 300)
|
||||
right_arm.run_angle(1000,500)
|
||||
@@ -75,6 +78,7 @@ async def Run1():
|
||||
await drive_base.straight(10)
|
||||
await drive_base.turn(35)
|
||||
await drive_base.straight(-600)
|
||||
|
||||
async def Run2():
|
||||
await drive_base.straight(200)
|
||||
await drive_base.turn(-20)
|
||||
@@ -89,6 +93,7 @@ async def Run2():
|
||||
await drive_base.straight(-100)
|
||||
await drive_base.turn(-100)
|
||||
await drive_base.straight(-600)
|
||||
|
||||
async def Run3():
|
||||
await drive_base.straight(920)
|
||||
await drive_base.turn(-90)
|
||||
@@ -98,6 +103,7 @@ async def Run3():
|
||||
await drive_base.straight(-110)
|
||||
await drive_base.turn(90)
|
||||
await drive_base.straight(2000)
|
||||
|
||||
async def Run4():
|
||||
await drive_base.straight(519)
|
||||
await left_arm.run_angle(300, -100)
|
||||
@@ -121,12 +127,14 @@ async def Run4():
|
||||
await drive_base.straight(-200)
|
||||
await drive_base.turn(-50)
|
||||
await drive_base.straight(-800)
|
||||
|
||||
async def Run5():
|
||||
await drive_base.straight(420)
|
||||
await right_arm.run_angle(300,-100)
|
||||
await drive_base.straight(-100)
|
||||
await right_arm.run_angle(300, 100)
|
||||
await drive_base.straight(-350)
|
||||
|
||||
async def Run6():
|
||||
left_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 drive_base.turn(-60)
|
||||
await drive_base.straight(900)
|
||||
|
||||
# Celebration sound types
|
||||
class CelebrationSound:
|
||||
VICTORY_FANFARE = 0
|
||||
LEVEL_UP = 1
|
||||
WAITING_SOUND = 1
|
||||
SUCCESS_CHIME = 2
|
||||
TA_DA = 3
|
||||
POWER_UP = 4
|
||||
RICKROLL_INSPIRED = 5
|
||||
|
||||
# Sounds functions
|
||||
async def play_victory_fanfare():
|
||||
"""Classic victory fanfare"""
|
||||
notes = [
|
||||
@@ -166,8 +176,9 @@ async def play_victory_fanfare():
|
||||
for freq, duration in notes:
|
||||
await hub.speaker.beep(freq, duration)
|
||||
await wait(50)
|
||||
async def play_level_up():
|
||||
"""Upward scale for level completion"""
|
||||
async def play_waiting_sound():
|
||||
|
||||
# T3rm1na1 V3l0c1ty arpeggio recreated in pybricks
|
||||
notes = [
|
||||
(cn["Cs4"], 100),
|
||||
(cn["E4"], 100),
|
||||
@@ -190,7 +201,6 @@ async def play_level_up():
|
||||
|
||||
for freq, duration in notes:
|
||||
await hub.speaker.beep(freq, duration)
|
||||
await wait(20)
|
||||
async def play_success_chime():
|
||||
"""Simple success notification"""
|
||||
notes = [
|
||||
@@ -239,6 +249,8 @@ async def play_rickroll_inspired():
|
||||
for freq, duration in pattern:
|
||||
await hub.speaker.beep(freq, duration)
|
||||
await wait(50)
|
||||
|
||||
# Basically a big if else statement that calls the functions above
|
||||
async def celebrate_mission_complete(sound_type=CelebrationSound.RICKROLL_INSPIRED):
|
||||
"""
|
||||
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
|
||||
if sound_type == CelebrationSound.VICTORY_FANFARE:
|
||||
await play_victory_fanfare()
|
||||
elif sound_type == CelebrationSound.LEVEL_UP:
|
||||
await play_level_up()
|
||||
elif sound_type == CelebrationSound.WAITING_SOUND:
|
||||
await play_waiting_sound()
|
||||
elif sound_type == CelebrationSound.SUCCESS_CHIME:
|
||||
await play_success_chime()
|
||||
elif sound_type == CelebrationSound.TA_DA:
|
||||
@@ -274,11 +286,12 @@ async def celebrate_mission_complete(sound_type=CelebrationSound.RICKROLL_INSPIR
|
||||
await wait(100)
|
||||
|
||||
hub.light.off()
|
||||
|
||||
|
||||
# This where everything happens
|
||||
async def main():
|
||||
# MAIN LOOP
|
||||
while True:
|
||||
await celebrate_mission_complete(CelebrationSound.LEVEL_UP)
|
||||
await celebrate_mission_complete(CelebrationSound.WAITING_SOUND)
|
||||
color_reflected_percent = await color_sensor.reflection()
|
||||
print(f"Color reflection percentage: {color_reflected_percent}")
|
||||
|
||||
@@ -310,14 +323,14 @@ async def main():
|
||||
elif color_detected == Color.RED:
|
||||
print('Running Mission 6')
|
||||
await Run6()
|
||||
await celebrate_mission_complete(CelebrationSound.LEVEL_UP)
|
||||
await celebrate_mission_complete(CelebrationSound.POWER_UP)
|
||||
else:
|
||||
hub.light.off()
|
||||
left_motor.stop()
|
||||
right_motor.stop()
|
||||
else:
|
||||
print("No color was detected.")
|
||||
await wait(100) #prevent loop from iterating fast
|
||||
await wait(100) # prevent loop from iterating fast
|
||||
|
||||
# Main execution loop
|
||||
run_task(main())
|
||||
Reference in New Issue
Block a user