mirror of
https://github.com/pybricks/pybricks-api.git
synced 2026-09-12 01:24:17 +00:00
examples/pup: add imu up() example
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
from pybricks.hubs import PrimeHub
|
||||
from pybricks.parameters import Color, Side
|
||||
from pybricks.tools import wait
|
||||
|
||||
# Initialize the hub.
|
||||
hub = PrimeHub()
|
||||
|
||||
# Define colors for each side in a dictionary.
|
||||
SIDE_COLORS = {
|
||||
Side.TOP: Color.RED,
|
||||
Side.BOTTOM: Color.BLUE,
|
||||
Side.LEFT: Color.GREEN,
|
||||
Side.RIGHT: Color.YELLOW,
|
||||
Side.FRONT: Color.MAGENTA,
|
||||
Side.BACK: Color.BLACK,
|
||||
}
|
||||
|
||||
# Keep updating the color based on detected up side.
|
||||
while True:
|
||||
|
||||
# Check which side of the hub is up.
|
||||
up_side = hub.imu.up()
|
||||
|
||||
# Change the color based on the side.
|
||||
hub.light.on(SIDE_COLORS[up_side])
|
||||
|
||||
# Also print the result.
|
||||
print(up_side)
|
||||
wait(50)
|
||||
@@ -0,0 +1,30 @@
|
||||
# ExampleHub = TechnicHub PrimeHub
|
||||
from pybricks.hubs import ExampleHub
|
||||
from pybricks.parameters import Color, Side
|
||||
from pybricks.tools import wait
|
||||
|
||||
# Initialize the hub.
|
||||
hub = ExampleHub()
|
||||
|
||||
# Define colors for each side in a dictionary.
|
||||
SIDE_COLORS = {
|
||||
Side.TOP: Color.RED,
|
||||
Side.BOTTOM: Color.BLUE,
|
||||
Side.LEFT: Color.GREEN,
|
||||
Side.RIGHT: Color.YELLOW,
|
||||
Side.FRONT: Color.MAGENTA,
|
||||
Side.BACK: Color.BLACK,
|
||||
}
|
||||
|
||||
# Keep updating the color based on detected up side.
|
||||
while True:
|
||||
|
||||
# Check which side of the hub is up.
|
||||
up_side = hub.imu.up()
|
||||
|
||||
# Change the color based on the side.
|
||||
hub.light.on(SIDE_COLORS[up_side])
|
||||
|
||||
# Also print the result.
|
||||
print(up_side)
|
||||
wait(50)
|
||||
@@ -0,0 +1,29 @@
|
||||
from pybricks.hubs import TechnicHub
|
||||
from pybricks.parameters import Color, Side
|
||||
from pybricks.tools import wait
|
||||
|
||||
# Initialize the hub.
|
||||
hub = TechnicHub()
|
||||
|
||||
# Define colors for each side in a dictionary.
|
||||
SIDE_COLORS = {
|
||||
Side.TOP: Color.RED,
|
||||
Side.BOTTOM: Color.BLUE,
|
||||
Side.LEFT: Color.GREEN,
|
||||
Side.RIGHT: Color.YELLOW,
|
||||
Side.FRONT: Color.MAGENTA,
|
||||
Side.BACK: Color.BLACK,
|
||||
}
|
||||
|
||||
# Keep updating the color based on detected up side.
|
||||
while True:
|
||||
|
||||
# Check which side of the hub is up.
|
||||
up_side = hub.imu.up()
|
||||
|
||||
# Change the color based on the side.
|
||||
hub.light.on(SIDE_COLORS[up_side])
|
||||
|
||||
# Also print the result.
|
||||
print(up_side)
|
||||
wait(50)
|
||||
Reference in New Issue
Block a user