Control LEGO(tm) BluetoothLE Hubs, Motors, and Sensors using Async Python
Project description
BrickNil [*] provides an easy way to connect to and program LEGO® Bluetooth hubs (including the PoweredUp Passenger Train 60197 and Cargo Train 60198 sets, and the Lego Duplo Steam Train 10874 and Cargo Train 10875 ) using Python on OS X and Linux. This work was inspired by this EuroBricks thread, and the NodeJS Powered-Up library. It requires modern Python (designed and tested for 3.7) and uses asynchronous event programming built on top of the Curio async library. As an aside, the choice of async library is fairly arbitrary; and enabling another library such as asyncio or Trio should be straightforward.
An example BrickNil program for controlling the Train motor speed is shown below:
from curio import sleep
from bricknil import attach, start
from bricknil.hub import PoweredUpHub
from bricknil.sensor import TrainMotor
@attach(TrainMotor, name='motor')
class Train(PoweredUpHub):
async def run(self):
for i in range(2): # Repeat this control two times
await self.motor.ramp_speed(80,5000) # Ramp speed to 80 over 5 seconds
await sleep(6)
await self.motor.ramp_speed(0,1000) # Brake to 0 over 1 second
await sleep(2)
async def system():
train = Train('My train')
if __name__ == '__main__':
start(system)
Free and open-source software: ASL2 license
Documentation: http://virantha.github.io/bricknil
Features
- Supports the following actuators/sensors:
Internal motors
Train motors
Hub LED color
Boost vision sensor (color, distance)
Internal tilt/orientation/accelerometer
Boost external motor
External light
Hub buttons
- Fully supports Python asynchronous keywords and coroutines
Allows expressive concurrent programming using async/await syntax
The current implementation uses the async library Curio by David Beazley
- Cross-platform
Uses the Adafruit Bluefruit BluetoothLE library for Mac OS X
Uses the Bleak Bluetooth library for Linux and Win10; also tested on Raspberry Pi.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
File details
Details for the file bricknil-0.8.2-py3-none-any.whl
.
File metadata
- Download URL: bricknil-0.8.2-py3-none-any.whl
- Upload date:
- Size: 64.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b6ab83926f11375063e6610bf1a959e3e715d070f6ce95055eb09fe302c3bda6 |
|
MD5 | 93a98a58b50991419008b0a14d145e9d |
|
BLAKE2b-256 | 6142c217d7e93857be3f8676be41d228c091829a4dd87a9c7f74e0874ed14a88 |