mirror of
https://github.com/pybricks/pybricks-api.git
synced 2026-09-11 17:14:41 +00:00
examples/hubs/MoveHub: imu examples
This commit is contained in:
@@ -45,3 +45,18 @@ Making the light blink
|
||||
|
||||
.. literalinclude::
|
||||
../../../examples/pup/hub_movehub/light_blink.py
|
||||
|
||||
IMU examples
|
||||
---------------
|
||||
|
||||
Testing which way is up
|
||||
********************************
|
||||
|
||||
.. literalinclude::
|
||||
../../../examples/pup/hub_movehub/imu_up.py
|
||||
|
||||
Reading acceleration
|
||||
**************************************************
|
||||
|
||||
.. literalinclude::
|
||||
../../../examples/pup/hub_movehub/imu_read_acceleration.py
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
from pybricks.hubs import MoveHub
|
||||
from pybricks.tools import wait
|
||||
|
||||
# Initialize the hub.
|
||||
hub = MoveHub()
|
||||
|
||||
# Get the acceleration tuple.
|
||||
print(hub.imu.acceleration())
|
||||
|
||||
while True:
|
||||
# Get individual acceleration values.
|
||||
x, y, z = hub.imu.acceleration()
|
||||
print(x, y, z)
|
||||
|
||||
# Wait so we can see what we printed.
|
||||
wait(100)
|
||||
@@ -0,0 +1,29 @@
|
||||
from pybricks.hubs import MoveHub
|
||||
from pybricks.parameters import Color, Side
|
||||
from pybricks.tools import wait
|
||||
|
||||
# Initialize the hub.
|
||||
hub = MoveHub()
|
||||
|
||||
# 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)
|
||||
@@ -1,4 +1,4 @@
|
||||
# ExampleHub = TechnicHub PrimeHub
|
||||
# ExampleHub = TechnicHub PrimeHub MoveHub
|
||||
from pybricks.hubs import ExampleHub
|
||||
from pybricks.parameters import Color, Side
|
||||
from pybricks.tools import wait
|
||||
|
||||
Reference in New Issue
Block a user