Skip to content
项目

关于

用于 LEGO® PoweredUp 设备的 Python 库

科学与工程

这些数字如何统计

可以做什么

  • auto-detect and connect to Move Hub device
  • auto-detects peripheral devices connected to Hub
  • constant, angled and timed movement for motors, rotation sensor subscription
  • vision sensor: several modes to measure distance, color and luminosity

实测星标增长

24小时
0
7天
+1
15天
-1
1个月
正在收集历史

各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。

README.md

原文

Python library to interact with Move Hub / PoweredUp Hubs

Move Hub is central controller block of LEGO® Boost Robotics Set.

In fact, Move Hub is just a Bluetooth hardware piece, and all manipulations with it are made by commands passed through Bluetooth Low Energy (BLE) wireless protocol. One of the ways to issue these commands is to write Python program using this library.

The best way to start is to look into demo.py file, written with the async API, and run it (assuming you have installed library).

If you have Vernie assembled, you might run scripts from examples/vernie directory.

Demonstrational Videos

Vernie Programmed Laser Engraver Color Sorter Face Tracker Color Pin Bot BB-8 Joystick

Dancing Vernie

Features

Usage

Install library like this:

pip install -U pylgbst

Then instantiate MoveHub object and start invoking its methods. Following is example to just print peripherals detected on Hub:

from pylgbst.hub import MoveHub

hub = MoveHub()

for device in hub.peripherals:
    print(device)

Each peripheral kind has own methods to do actions and/or get sensor data. See features list for individual doc pages.

Bluetooth Backend Prerequisites

The library talks Bluetooth through bleak, which supports Linux, Windows and macOS, and gets installed along with pip install -U pylgbst. Other backends (gatt, gattlib, pygatt/BlueGiga, bluepy) were removed, since Bleak covers their platforms.

Please let author know if you have discovered any compatibility/preprequisite details, so we will update this section to help future users

Bluetooth Connection Options

There is an optional parameter for MoveHub class constructor, accepting instance of Connection object. By default, it will try to use whatever get_connection_auto() returns. You have several options to manually control that:

  • use get_connection_auto() - use the relay if it is running, Bleak otherwise
  • use get_connection_bleak() - connect over Bluetooth with Bleak
  • use get_connection_relay() - connect through a running relay

All the functions above have optional arguments to specify Hub name or mac address. Please take a look at functions source code for details. Pass the connection object to MoveHub constructor, like this:

from pylgbst.hub import MoveHub
from pylgbst import get_connection_bleak

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

Keeping the Hub Connected Between Runs

When you run many short experiments, powering the hub up and waiting for it to connect each time gets tedious. Start a relay once; it connects to the hub over Bluetooth (via bleak) and holds the connection while your scripts come and go:

python3 -m pylgbst.aio.relay --name "LEGO Move Hub"   # or --mac AA:BB:CC:DD:EE:FF, or nothing for any LEGO hub

Scripts need no changes: get_connection_auto(), and thus MoveHub() with no arguments, use the relay at 127.0.0.1:9090 when one is running, and fall back to Bluetooth otherwise. To skip the relay, pass an explicit connection, e.g. MoveHub(get_connection_bleak(hub_name=...)).

The relay serves one script at a time; starting another one disconnects the previous. It replays the hub's peripheral announcements to every new script, and when a script exits, it stops the motors and switches off the sensor and button subscriptions that script left on. A script's hub.disconnect() ends its session, while the hub stays connected. If the hub disconnects, the relay rescans for it.

A script that asks for a particular hub (by hub_mac or hub_name, as MoveHub() does with its default name) fails with HubError when the relay holds another one.

Roadmap & TODO

  • validate operations with other Hub types (train, PUP etc)
  • make connections to detect hub by UUID instead of name
  • document all API methods

Links

探索相关项目

加载中…