Added object oriented version of codes
This commit is contained in:
121
regionals/bioglow_main.py
Normal file
121
regionals/bioglow_main.py
Normal file
@@ -0,0 +1,121 @@
|
||||
#####################################
|
||||
# Main.py #
|
||||
# File for running all MISSIONS #
|
||||
# together #
|
||||
# #
|
||||
#####################################
|
||||
|
||||
import bioglow_helpers as helper
|
||||
import robot
|
||||
import bioglow_missions_3_4_5 as Run_1 # import here the name of files containing mission runs Run_1 for run 1,
|
||||
import bioglow_mission_2 as Run_2 # Run_2 for run 2, and so on..
|
||||
import bioglow_mission_1 as Run_3
|
||||
import bioglow_mission_11_experimental as Run_4
|
||||
import bioglow_mission_13 as Run_5
|
||||
import bioglow_missions_6_7_13_14_experimental as Run_6 # FIXED: was bioglow_mission_6_7_13_14 (no such file)
|
||||
import bioglow_missions_15_14_ant as Run_7 # FIXED: was bioglow_missions_14_15 (no such file)
|
||||
import bioglow_mission_8 as Run_8
|
||||
|
||||
async def start(run):
|
||||
"""Run one mission, starting from the normal speeds in robot.py.
|
||||
|
||||
Some runs change speeds with robot.set_default_speed(). Resetting first
|
||||
means each run drives exactly like it did when tested on its own.
|
||||
"""
|
||||
robot.reset_settings()
|
||||
await run.solve_missions()
|
||||
|
||||
|
||||
# Function to classify color based on HSV
|
||||
def detect_color(h, s, v, reflected):
|
||||
if reflected > 4:
|
||||
if 1 < h < 11 and s <50: # brown
|
||||
return "Brown"
|
||||
elif 11 < h < 24 and s > 80: # orange
|
||||
return "Orange"
|
||||
elif 30 < h < 60 and v > 50: # yellow
|
||||
return "Yellow"
|
||||
elif 70 < h < 90 and s > 60: # neon green
|
||||
return "Lime"
|
||||
elif 100 < h < 160: # green - do it vertically not horizontally for accuracy
|
||||
return "Green"
|
||||
elif 195 < h < 198: # light blue
|
||||
return "Cyan"
|
||||
elif 210 < h < 225: # blue - do it vertically not horizontally for accuracy
|
||||
return "Blue"
|
||||
elif 260 < h < 350 and s > 80: # purple
|
||||
return "Purple"
|
||||
elif 350 < h and s > 50: #red
|
||||
return "Red"
|
||||
elif s < 15 and v > 70: # white
|
||||
return "White"
|
||||
else:
|
||||
return "Unknown"
|
||||
return "Unknown"
|
||||
|
||||
|
||||
async def main():
|
||||
while True:
|
||||
pressed = robot.hub.buttons.pressed()
|
||||
h, s, v = await robot.color_sensor.hsv()
|
||||
reflected = await robot.color_sensor.reflection()
|
||||
color = detect_color(h, s, v, reflected)
|
||||
if helper.DEBUG :
|
||||
#print(color_sensor.color())
|
||||
#print(h,s,v)
|
||||
#print(color)
|
||||
print(f"button pressed: {pressed}")
|
||||
|
||||
#TODO: Add the solve_missions() function of the run number for each color here
|
||||
if color == "Green":
|
||||
print('Running Mission 1, Detect Green')
|
||||
await start(Run_1)
|
||||
elif color == "Red":
|
||||
print('Running Mission 2, Detect Red')
|
||||
await start(Run_2)
|
||||
elif color == "Yellow":
|
||||
print('Running Mission 3, Detect Yellow')
|
||||
await start(Run_3)
|
||||
elif color == "Blue":
|
||||
print('Running Mission 4, Detect Blue')
|
||||
await start(Run_4)
|
||||
elif color == "Purple":
|
||||
print('Running Mission 5, Detect Purple')
|
||||
await start(Run_5)
|
||||
elif color == "Orange":
|
||||
print('Running Mission 6, Detect Orange')
|
||||
await start(Run_6)
|
||||
elif color == "Cyan":
|
||||
print('Running Mission 7, Detect Cyan')
|
||||
await start(Run_7)
|
||||
elif color == "Lime":
|
||||
print('Running Mission 8, Detect Lime')
|
||||
await start(Run_8)
|
||||
elif color == "Brown":
|
||||
print('Running Mission Experimental, Detect Brown')
|
||||
await robot.wait(500)
|
||||
elif color == "White":
|
||||
print('Detect White')
|
||||
await robot.wait(500)
|
||||
else:
|
||||
print(f"Unknown color detected (Hue: {h}, Sat: {s}, Val: {v})")
|
||||
#pass
|
||||
|
||||
# Show battery % for debugging
|
||||
if robot.Button.BLUETOOTH in pressed: # using bluetooth button here since away from color sensor
|
||||
# Get the battery voltage in millivolts (mV)
|
||||
battery_voltage_mV = robot.hub.battery.voltage()
|
||||
# Use the function with your voltage reading
|
||||
percentage = await helper.get_battery_percentage(float(battery_voltage_mV))
|
||||
if helper.DEBUG:
|
||||
print(f"Battery voltage: {battery_voltage_mV} mV")
|
||||
print(f"Battery level: {percentage:.3f}%")
|
||||
print("FLL Robot System Ready!")
|
||||
await robot.hub.display.text(f"{percentage:.0f}")
|
||||
break
|
||||
elif pressed == None:
|
||||
continue
|
||||
|
||||
await robot.wait(10)
|
||||
# Run the main function
|
||||
robot.run_task(main())
|
||||
Reference in New Issue
Block a user