Compare commits
19 Commits
6ade2c2dc6
...
dev
| Author | SHA1 | Date | |
|---|---|---|---|
| eb13361f4f | |||
| d09222967b | |||
| 4f23a4d308 | |||
| c4b6ee5d97 | |||
| 9a192d8eb8 | |||
| 8c39974c5a | |||
| 2a555aedbc | |||
| b7bef93301 | |||
| f66438019e | |||
| 5e0925b540 | |||
| 129bd9af93 | |||
| 540ff62836 | |||
| f5b51cb3aa | |||
| 07bbba41e4 | |||
| 8c5e90e0ec | |||
|
|
6beeb837cf | ||
| c8b520c12c | |||
| fab9650a9f | |||
|
|
8489a87ada |
@@ -1,35 +0,0 @@
|
|||||||
from pybricks.tools import wait, StopWatch
|
|
||||||
from pybricks.parameters import Port
|
|
||||||
from pybricks import version
|
|
||||||
import OtherFunctions as debug
|
|
||||||
import usys
|
|
||||||
|
|
||||||
|
|
||||||
class HubDiagnostics:
|
|
||||||
def __init__(self, hub):
|
|
||||||
self.hub = hub
|
|
||||||
self.port_map = {
|
|
||||||
"A": Port.A,
|
|
||||||
"B": Port.B,
|
|
||||||
"C": Port.C,
|
|
||||||
"D": Port.D,
|
|
||||||
"E": Port.E,
|
|
||||||
"F": Port.F,
|
|
||||||
}
|
|
||||||
def printAbout(self):
|
|
||||||
print("Pybricks version information:", version)
|
|
||||||
print("MicroPython information:", usys.implementation)
|
|
||||||
print("MicroPython version:", usys.version)
|
|
||||||
def testLightSources(self, verbose):
|
|
||||||
v = verbose
|
|
||||||
self.hub.display.off()
|
|
||||||
for x in range(5):
|
|
||||||
for y in range(5):
|
|
||||||
debug.log(f"Turning on pixel at position {x}, {y}...", v)
|
|
||||||
self.hub.display.pixel(x, y, brightness=100)
|
|
||||||
wait(100)
|
|
||||||
debug.log(f"Turning off pixel at position {x}, {y}...", v)
|
|
||||||
self.hub.display.pixel(x, y, brightness=0)
|
|
||||||
def printAll(self):
|
|
||||||
self.printAbout()
|
|
||||||
self.testLightSources(False)
|
|
||||||
@@ -4,14 +4,16 @@ from pybricks.parameters import Button, Color, Direction, Port, Side, Stop
|
|||||||
from pybricks.robotics import DriveBase
|
from pybricks.robotics import DriveBase
|
||||||
from pybricks.tools import wait, StopWatch
|
from pybricks.tools import wait, StopWatch
|
||||||
HUB = PrimeHub()
|
HUB = PrimeHub()
|
||||||
from BatteryDiagnostics import BatteryDiagnostics
|
from battery_diagnostics import BatteryDiagnostics
|
||||||
from MotorDiagnostics import MotorDiagnostics
|
from motor_diagnostics import MotorDiagnostics
|
||||||
from ColorSensorDiagnostics import ColorSensorDiagnostics
|
from color_sensor_diagnostics import ColorSensorDiagnostics
|
||||||
from DriveBaseDiagnostics import DriveBaseDiagnostics
|
from drive_base_diagnostics import DriveBaseDiagnostics
|
||||||
|
from hub_diagnostics import HubDiagnostics
|
||||||
battery = BatteryDiagnostics(HUB)
|
battery = BatteryDiagnostics(HUB)
|
||||||
motor = MotorDiagnostics(HUB, Motor)
|
motor = MotorDiagnostics(HUB, Motor)
|
||||||
colorsensor = ColorSensorDiagnostics(HUB, ColorSensor)
|
colorsensor = ColorSensorDiagnostics(HUB, ColorSensor)
|
||||||
drivebase = DriveBaseDiagnostics(HUB, Motor, DriveBase)
|
drivebase = DriveBaseDiagnostics(HUB, Motor, DriveBase)
|
||||||
|
hubdiagnostics = HubDiagnostics(HUB)
|
||||||
CLEARCONFIRM = input("Clear the console before proceeding? Y/N (default: yes): ")
|
CLEARCONFIRM = input("Clear the console before proceeding? Y/N (default: yes): ")
|
||||||
if(CLEARCONFIRM == "Y" or CLEARCONFIRM == "y" or CLEARCONFIRM == "yes" or CLEARCONFIRM == ""):
|
if(CLEARCONFIRM == "Y" or CLEARCONFIRM == "y" or CLEARCONFIRM == "yes" or CLEARCONFIRM == ""):
|
||||||
print("Clearing console... \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n")
|
print("Clearing console... \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n")
|
||||||
@@ -35,6 +37,8 @@ while True:
|
|||||||
print("Enter 'b' for battery diagnostics")
|
print("Enter 'b' for battery diagnostics")
|
||||||
print("Enter 'm' for motor diagnostics")
|
print("Enter 'm' for motor diagnostics")
|
||||||
print("Enter 'cs' for color sensor diagnostics")
|
print("Enter 'cs' for color sensor diagnostics")
|
||||||
|
print("Enter 'db' for drive base diagnostics")
|
||||||
|
print("Enter 'h' for hub diagnostics")
|
||||||
print("Enter 'q' to quit")
|
print("Enter 'q' to quit")
|
||||||
|
|
||||||
choice = input("Your choice: ").strip().lower()
|
choice = input("Your choice: ").strip().lower()
|
||||||
@@ -49,7 +53,7 @@ while True:
|
|||||||
elif choice == "m":
|
elif choice == "m":
|
||||||
print("------------------------MOTOR DIAGNOSTICS------------------------")
|
print("------------------------MOTOR DIAGNOSTICS------------------------")
|
||||||
motor.fullTest()
|
motor.fullTest()
|
||||||
print("Motor diagnostics completed.")
|
print("[Motor Diagnostics] Motor diagnostics completed.")
|
||||||
|
|
||||||
elif choice == "q":
|
elif choice == "q":
|
||||||
print("Diagnostics completed successfully. Exiting program.")
|
print("Diagnostics completed successfully. Exiting program.")
|
||||||
@@ -57,10 +61,13 @@ while True:
|
|||||||
elif choice == "cs":
|
elif choice == "cs":
|
||||||
print("---------------------COLOR SENSOR DIAGNOSTICS---------------------")
|
print("---------------------COLOR SENSOR DIAGNOSTICS---------------------")
|
||||||
colorsensor.printAll()
|
colorsensor.printAll()
|
||||||
print("Color sensor diagnostics completed.")
|
print("[Color Sensor Diagnostics] Color sensor diagnostics completed.")
|
||||||
elif choice == "db":
|
elif choice == "db":
|
||||||
print("----------------------DRIVE BASE DIAGNOSTICS----------------------")
|
print("----------------------DRIVE BASE DIAGNOSTICS----------------------")
|
||||||
drivebase.printAll()
|
drivebase.printAll()
|
||||||
print("Drivebase diagnostics completed.")
|
print("[Drivebase Diagnostics] Drivebase diagnostics completed.")
|
||||||
|
elif choice == "h":
|
||||||
|
print("--------------------------HUB DIAGNOSTICS--------------------------")
|
||||||
|
hubdiagnostics.printAll(False)
|
||||||
else:
|
else:
|
||||||
print("Invalid choice. Please enter 'b', 'm', or 'q'.")
|
print("Invalid choice. Please enter 'b', 'm', or 'q'.")
|
||||||
41
diagnostics/hub_diagnostics.py
Normal file
41
diagnostics/hub_diagnostics.py
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
from pybricks.tools import wait, StopWatch
|
||||||
|
from pybricks import version
|
||||||
|
import other_functions as debug
|
||||||
|
from micropython_diagnostics import MicroPythonDiagnostics
|
||||||
|
from pybricks.parameters import Port
|
||||||
|
class HubDiagnostics:
|
||||||
|
def __init__(self, hub):
|
||||||
|
self.hub = hub
|
||||||
|
self.port_map = {
|
||||||
|
"A": Port.A,
|
||||||
|
"B": Port.B,
|
||||||
|
"C": Port.C,
|
||||||
|
"D": Port.D,
|
||||||
|
"E": Port.E,
|
||||||
|
"F": Port.F,
|
||||||
|
}
|
||||||
|
def testLightSources(self, verbose):
|
||||||
|
v = verbose
|
||||||
|
self.hub.display.off()
|
||||||
|
for x in range(5):
|
||||||
|
for y in range(5):
|
||||||
|
debug.log(f"[Hub Diagnostics - Light Sources] Turning on pixel at position {x}, {y}...", v)
|
||||||
|
self.hub.display.pixel(x, y, brightness=100)
|
||||||
|
wait(100)
|
||||||
|
debug.log(f"[Hub Diagnostics - Light Sources] Turning off pixel at position {x}, {y}...", v)
|
||||||
|
self.hub.display.pixel(x, y, brightness=0)
|
||||||
|
|
||||||
|
def printAll(self, verbose=True):
|
||||||
|
v = verbose
|
||||||
|
debug.log("[Hub Diagnostics] Starting hub diagnostics...", v)
|
||||||
|
while True:
|
||||||
|
choice = input("[Hub Diagnostics] Which hub diagnostic would you like to run?\n[Hub Diagnostics] Enter 'l' for light source test\n[Hub Diagnostics] Enter 'm' for MicroPython diagnostics\n[Hub Diagnostics] Enter 'q' to quit\n[Hub Diagnostics] Your choice: ").strip().lower()
|
||||||
|
if choice == "l":
|
||||||
|
debug.log("[Hub Diagnostics] Running light source test...", v)
|
||||||
|
self.testLightSources(v)
|
||||||
|
if choice == "m":
|
||||||
|
debug.log("[Hub Diagnostics] Running MicroPython diagnostics...", v)
|
||||||
|
MicroPythonDiagnostics.printAll()
|
||||||
|
if choice == "q":
|
||||||
|
print("[Hub Diagnostics] Hub diagnostics completed.")
|
||||||
|
return
|
||||||
42
diagnostics/micropython_diagnostics.py
Normal file
42
diagnostics/micropython_diagnostics.py
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
import usys
|
||||||
|
import micropython
|
||||||
|
from pybricks import version
|
||||||
|
class MicroPythonDiagnostics:
|
||||||
|
def __init__(self, hub):
|
||||||
|
pass
|
||||||
|
def printVersionDiagnostics():
|
||||||
|
print("[Hub Diagnostics - MicroPython - Version] Hub version information:", version)
|
||||||
|
print("[Hub Diagnostics - MicroPython - Version] MicroPython version:", usys.version)
|
||||||
|
print("[Hub Diagnostics - MicroPython - Version] Pybricks version information:", usys.version_info)
|
||||||
|
print("[Hub Diagnostics - MicroPython - Version] MicroPython information:", usys.implementation)
|
||||||
|
def performMemoryDiagnostics():
|
||||||
|
print("[Hub Diagnostics - MicroPython - Memory] Memory information (retrieved from the MicroPython environment):")
|
||||||
|
micropython.mem_info(1)
|
||||||
|
print("[Hub Diagnostics - MicroPython - Memory] Testing heap lock and unlock.")
|
||||||
|
print("[Hub Diagnostics - MicroPython - Memory] Allocating memory while heap is unlocked:")
|
||||||
|
try:
|
||||||
|
x = 5000
|
||||||
|
print("[Hub Diagnostics - MicroPython - Memory] [SUCCESS] There was no MemoryError raised. The value of the new variable x is", x)
|
||||||
|
except MemoryError:
|
||||||
|
print("[Hub Diagnostics - MicroPython - Memory] [FAIL] Allocation failed. Your memory may be faulty.")
|
||||||
|
print("[Hub Diagnostics - MicroPython - Memory] Locking the heap:")
|
||||||
|
micropython.heap_lock()
|
||||||
|
print("[Hub Diagnostics - MicroPython - Memory] Heap was locked. Attempting to allocate memory (this should fail):")
|
||||||
|
try:
|
||||||
|
y = 10000
|
||||||
|
print("[Hub Diagnostics - MicroPython - Memory] [FAIL] There was no MemoryError raised. Heap lock failed.")
|
||||||
|
except MemoryError:
|
||||||
|
print("[Hub Diagnostics - MicroPython - Memory] [SUCCESS] Allocation failed. Test successful. The heap was successfully locked.")
|
||||||
|
# Attempt to add gc to this for memory diagnostics, in addition test machine.soft_reset() and add that first to reset the heap.
|
||||||
|
print("[Hub Diagnostics - MicroPython - Memory] Unlocking the heap:")
|
||||||
|
micropython.heap_unlock()
|
||||||
|
print("[Hub Diagnostics - MicroPython - Memory] Heap was unlocked. Attempting to allocate memory (this should succeed):")
|
||||||
|
try:
|
||||||
|
z = 17000
|
||||||
|
print("[Hub Diagnostics - MicroPython - Memory] [SUCCESS] There was no MemoryError raised. The value of the new variable y is", x)
|
||||||
|
except MemoryError:
|
||||||
|
print("[Hub Diagnostics - MicroPython - Memory] [FAIL] Allocation failed. The heap failed to unlock.")
|
||||||
|
def printAll():
|
||||||
|
printVersionDiagnostics()
|
||||||
|
performMemoryDiagnostics()
|
||||||
|
|
||||||
62
templates/color_sensor_start_logic.py
Normal file
62
templates/color_sensor_start_logic.py
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
from pybricks.pupdevices import ColorSensor
|
||||||
|
from pybricks.parameters import Color, Port
|
||||||
|
from pybricks.tools import run_task
|
||||||
|
from pybricks.tools import wait
|
||||||
|
from pybricks.hubs import PrimeHub
|
||||||
|
hub = PrimeHub()
|
||||||
|
color_sensor = ColorSensor(Port.F) # Change the port to your color sensor's port
|
||||||
|
# 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 < 40 and s > 70: # orange
|
||||||
|
return "Orange"
|
||||||
|
elif 47 < h < 56: # yellow
|
||||||
|
return "Yellow"
|
||||||
|
elif 70 < h < 160: # green - your brick should approach from the top for accuracy
|
||||||
|
return "Green"
|
||||||
|
elif 195 < h < 198: # light blue
|
||||||
|
return "Light_Blue"
|
||||||
|
elif 210 < h < 225: # blue - your brick should approach from the top for accuracy
|
||||||
|
return "Blue"
|
||||||
|
elif 260 < h < 350: # purple
|
||||||
|
return "Purple"
|
||||||
|
|
||||||
|
else:
|
||||||
|
return "Unknown"
|
||||||
|
return "Unknown"
|
||||||
|
async def main():
|
||||||
|
while True:
|
||||||
|
h, s, v = await color_sensor.hsv()
|
||||||
|
reflected = await color_sensor.reflection()
|
||||||
|
color = detect_color(h, s, v, reflected)
|
||||||
|
|
||||||
|
|
||||||
|
if color == "Green":
|
||||||
|
print('Running Task 1')
|
||||||
|
# Run a function with await Function() here
|
||||||
|
elif color == "Red":
|
||||||
|
print('Running Task 2')
|
||||||
|
# Run a function with await Function() here
|
||||||
|
elif color == "Yellow":
|
||||||
|
print('Running Task 3')
|
||||||
|
# Run a function with await Function() here
|
||||||
|
elif color == "Blue":
|
||||||
|
print('Running Task 4')
|
||||||
|
# Run a function with await Function() here
|
||||||
|
elif color == "Orange":
|
||||||
|
print('Running Task 5')
|
||||||
|
# Run a function with await Function() here
|
||||||
|
elif color == "Purple":
|
||||||
|
print('Running Task 6')
|
||||||
|
# Run a function with await Function() here
|
||||||
|
elif color == "Light_Blue":
|
||||||
|
print("Running Task 7")
|
||||||
|
# Run a function with await Function() here
|
||||||
|
else:
|
||||||
|
print(f"Unknown color detected (Hue: {h}, Sat: {s}, Val: {v})")
|
||||||
|
#pass
|
||||||
|
await wait(10)
|
||||||
|
# Run the main function
|
||||||
|
run_task(main())
|
||||||
18
templates/xbox_controller.py
Normal file
18
templates/xbox_controller.py
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
from pybricks.pupdevices import Motor
|
||||||
|
from pybricks.parameters import Button, Direction, Port, Side, Stop
|
||||||
|
from pybricks.tools import run_task, multitask
|
||||||
|
from pybricks.tools import wait, StopWatch
|
||||||
|
from pybricks.robotics import DriveBase
|
||||||
|
from pybricks.iodevices import XboxController
|
||||||
|
from pybricks.hubs import PrimeHub
|
||||||
|
hub = PrimeHub()
|
||||||
|
testmotor = Motor(Port.C)
|
||||||
|
async def main():
|
||||||
|
while True:
|
||||||
|
if(Button.UP in buttons.pressed()):
|
||||||
|
testmotor.run(500)
|
||||||
|
else:
|
||||||
|
testmotor.stop()
|
||||||
|
await wait(10)
|
||||||
|
# Run the main function
|
||||||
|
run_task(main())
|
||||||
Reference in New Issue
Block a user