undera/pylgbst

Python library for LEGO® PoweredUp devices

What it is

pylgbst is a Python library for talking to the LEGO Boost Move Hub — the central controller block of the LEGO Boost Robotics Set. The Move Hub is a Bluetooth hardware piece, and this library sends commands to it over Bluetooth Low Energy (BLE) so you can control it from a Python program.

What you can do with it

From the README, the library supports:

  • Auto-detecting and connecting to a Move Hub
  • Auto-detecting peripheral devices connected to the Hub
  • Controlling motors with constant, angled, or timed movement, and subscribing to rotation sensor data
  • Using the vision sensor to measure distance, color, and luminosity
  • Using the tilt sensor in 2-axis, 3-axis, and bump-detect modes
  • Changing the RGB LED color
  • Changing headlight brightness
  • Subscribing to push-button status
  • Subscribing to battery voltage and current

Each kind of peripheral has its own methods for actions and/or sensor data.

Getting started

The README points to examples/demo.py as the best starting place. If you have the Vernie robot assembled, there are also scripts in examples/vernie.

Install the library with a Bluetooth backend like this:

pip install -U pylgbst[bleak]

Then create a MoveHub object and use its methods. A minimal example from the README:

from pylgbst.hub import MoveHub

hub = MoveHub()

for device in hub.peripherals:
    print(device)

Bluetooth backends

The library needs one Bluetooth backend installed. The README lists these options:

  • bleak — recommended; supports Linux, Windows, macOS
  • pygatt — works on Windows and Linux; on Windows it requires a Bluegiga BLED112 Bluetooth Smart Dongle
  • gatt — Linux only, not Windows
  • gattlib — Linux only, not Windows, requires sudo
  • bluepy — Linux, including Raspbian, so it can work on Raspberry Pi

Some backends may require sudo on Linux.

Connection options

By default, MoveHub() tries to use whatever get_connection_auto() returns. You can also choose a specific backend connection function, such as:

  • get_connection_bluegiga()
  • get_connection_gatt()
  • get_connection_gattool()
  • get_connection_gattlib()
  • get_connection_bluepy()
  • get_connection_bleak()

These functions can take optional arguments for adapter name and Hub name or MAC address. You can pass a connection object directly to MoveHub, like this:

from pylgbst.hub import MoveHub
from pylgbst import get_connection_gatt

conn = get_connection_gatt(hub_mac='AA:BB:CC:DD:EE:FF')
hub = MoveHub(conn)

Roadmap / not yet done

The README lists these planned items:

  • Validate operations with other Hub types (train, PoweredUp, etc.)
  • Make connections detect hubs by UUID instead of name
  • Document all API methods

Related links from the README

The README links to:

  • Official LEGO BLE wireless protocol docs
  • BOOSTreveng, an early source of protocol knowledge
  • node-poweredup, a JavaScript version of the library
  • A BB-8 joystick example using another Bluetooth approach
  • BrickNil, an alternative async Python library for PoweredUp Hubs

There are also demonstration videos showing projects like a programmed Vernie, laser engraver, color sorter, face tracker, color pin bot, BB-8 joystick, and dancing Vernie.

Related

  • Project
  • Project
  • Project
  • Project