86 lines
3.3 KiB
Python
86 lines
3.3 KiB
Python
|
|
import bioglow_helpers as helper
|
||
|
|
import bioglow_settings as robot
|
||
|
|
import bioglow_mission_3_4_5 as Run_1
|
||
|
|
|
||
|
|
# Function to classify color based on HSV
|
||
|
|
def detect_color(h, s, v, reflected):
|
||
|
|
if reflected > 4:
|
||
|
|
if h < 4 or h > 350: # red
|
||
|
|
return "Red"
|
||
|
|
elif 3 < h < 24 and s > 80: # orange
|
||
|
|
return "Orange"
|
||
|
|
elif 30 < h < 60 and v > 50: # yellow
|
||
|
|
return "Yellow"
|
||
|
|
elif 70 < h < 160: # green - do it vertically not horizontally for accuracy
|
||
|
|
return "Green"
|
||
|
|
elif 195 < h < 198: # light blue
|
||
|
|
return "Light_Blue"
|
||
|
|
elif 210 < h < 225: # blue - do it vertically not horizontally for accuracy
|
||
|
|
return "Blue"
|
||
|
|
elif 260 < h < 350: # purple
|
||
|
|
return "Purple"
|
||
|
|
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}")
|
||
|
|
|
||
|
|
|
||
|
|
if color == "Green":
|
||
|
|
print('Running Mission 1, Detect Green')
|
||
|
|
await Run_1.solve_missions()
|
||
|
|
elif color == "Red":
|
||
|
|
print('Running Mission 2, Detect Red')
|
||
|
|
pass # TODO: Run2 not written yet (it crashed with NameError)
|
||
|
|
elif color == "Yellow":
|
||
|
|
print('Running Mission 3, Detect Yellow')
|
||
|
|
pass # TODO: Run3 not written yet (it crashed with NameError)
|
||
|
|
elif color == "Blue":
|
||
|
|
print('Running Mission 4, Detect Blue')
|
||
|
|
pass # TODO: Run4 not written yet (it crashed with NameError)
|
||
|
|
elif color == "Orange":
|
||
|
|
print('Running Mission 5, Detect Orange')
|
||
|
|
pass # TODO: Run5 not written yet (it crashed with NameError)
|
||
|
|
elif color == "Purple":
|
||
|
|
print('Running Mission 6, Detect Purple')
|
||
|
|
pass # TODO: Run6 not written yet (it crashed with NameError)
|
||
|
|
elif color == "Light_Blue":
|
||
|
|
print('Running Mission 7, Detect Cyan')
|
||
|
|
pass # TODO: Run7 not written yet (it crashed with NameError)
|
||
|
|
elif color == "White":
|
||
|
|
print('Detect White')
|
||
|
|
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())
|