diff --git a/doc/api/hubs/movehub.rst b/doc/api/hubs/movehub.rst index 65e77e4..31865e4 100644 --- a/doc/api/hubs/movehub.rst +++ b/doc/api/hubs/movehub.rst @@ -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 diff --git a/examples/pup/hub_movehub/imu_read_acceleration.py b/examples/pup/hub_movehub/imu_read_acceleration.py new file mode 100644 index 0000000..4d76b5a --- /dev/null +++ b/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) diff --git a/examples/pup/hub_movehub/imu_up.py b/examples/pup/hub_movehub/imu_up.py new file mode 100644 index 0000000..abfdc61 --- /dev/null +++ b/examples/pup/hub_movehub/imu_up.py @@ -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) diff --git a/examples/pup/hub_shared/imu_up.py b/examples/pup/hub_shared/imu_up.py index 6698009..b380982 100644 --- a/examples/pup/hub_shared/imu_up.py +++ b/examples/pup/hub_shared/imu_up.py @@ -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