Move Hub¶
-
class
MoveHub¶ LEGO® BOOST Move Hub.
Using the hub status light
-
light.on(color)¶ Turns on the light at the specified color.
- Parameters
color (Color) – Color of the light.
-
light.off()¶ Turns off the light.
-
light.blink(color, durations)¶ Blinks the light at a given color by turning it on and off for given durations.
The light keeps blinking indefinitely while the rest of your program keeps running.
This method provides a simple way to make basic but useful patterns. For more generic and multi-color patterns, use
animate()instead.
-
light.animate(colors, interval)¶ Animates the light with a list of colors. The next color in the list is shown after the given interval.
The animation runs in the background while the rest of your program keeps running. When the animation completes, it repeats.
Using the IMU
-
imu.up()¶ Checks which side of the hub currently faces upward.
- Returns
Side.TOP,Side.BOTTOM,Side.LEFT,Side.RIGHT,Side.FRONTorSide.BACK.- Return type
-
imu.acceleration()¶ Gets the acceleration of the device.
- Returns
Acceleration along all three axes.
- Return type
tuple of linear acceleration: m/s/s
Using the battery
-
battery.voltage()¶ Gets the voltage of the battery.
- Returns
Battery voltage.
- Return type
-
battery.current()¶ Gets the current supplied by the battery.
- Returns
Battery current.
- Return type
-
Status light examples¶
Turning the light on and off¶
from pybricks.hubs import MoveHub
from pybricks.parameters import Color
from pybricks.tools import wait
# Initialize the hub.
hub = MoveHub()
# Turn the light on and off 5 times.
for i in range(5):
hub.light.on(Color.RED)
wait(1000)
hub.light.off()
wait(500)
Making the light blink¶
from pybricks.hubs import MoveHub
from pybricks.parameters import Color
from pybricks.tools import wait
# Initialize the hub
hub = MoveHub()
# Keep blinking red on and off.
hub.light.blink(Color.RED, [500, 500])
wait(10000)
# Keep blinking green slowly and then quickly.
hub.light.blink(Color.GREEN, [500, 500, 50, 900])
wait(10000)
IMU examples¶
Testing which way is up¶
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)
Reading acceleration¶
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)