examples/hubs/MoveHub: imu examples

This commit is contained in:
Laurens Valk
2021-02-17 23:31:19 +01:00
parent c14f1cf473
commit 7679b8b47d
4 changed files with 61 additions and 1 deletions
+15
View File
@@ -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)
+29
View File
@@ -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 -1
View File
@@ -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