Simple to use asynchronous methods for the Move Hub
Move Hub is central controller block of LEGO® Boost Robotics Set.
-
Install Noble prerequisites
-
Install movehub-async
$ npm install movehub-async
const boost = require('movehub-async');
const hub = await boost.getHubAsync();
// Turn light from red to green
await hub.ledAsync('red');
await hub.ledAsync('yellow');
await hub.ledAsync('green');
// Turn A & B motors for 10 seconds with power 20
await hub.motorTimeMultiAsync(10, 20, 20);
// Turn motor C 600 degrees with power 5
await hub.motorAngleAsync('C', 600, 5);
It is also possible to wait that motor execution has stopped
await hub.ledAsync('red');
// Continue when led is red
await hub.motorTimeMultiAsync(10, 20, 20, true);
// Continue 10 sec later
await hub.motorTimeMultiAsync(5, 20, 20, true);
// Continue 5 sec later
await hub.motorAngleAsync('C', 800, 50, true);
// Continue some time later
await hub.ledAsync('green');
// Continue when led is green
Package contains also simple methods to drive for a specified distance and turn a specified angle. By default drive and turn methods will wait the execution has stopped.
// Drive 2 meters forward
await hub.drive(200);
// After 2 meter drive, turn 90 degrees to the right
await hub.turn(90);
// Drive 1 meter backwards
await hub.drive(-100);
// Turn 180 degrees to the left
await hub.turn(-180);
Check complete non-async API definition from Lego Boost Move Hub. Asynchronous version of the method has an Async-suffix in the name, e.g. motorTimeMulti
-> motorTimeMultiAsync
.
Create a connection to the Hub. Internally calls bleReadyAsync
, hubFoundAsync
and connectAsync
.
const hub = await boost.getHubAsync();
Wait for BLE device to be ready.
await boost.bleReadyAsync();
Wait for MoveHub found event.
const connectDetails = await boost.hubFoundAsync();
Initialize and wait for the connection to the Hub.
const hub = await boost.connectAsync(connectDetails);
Control the LED on the Move Hub.
await hub.ledAsync('red');
Run a motor for specific time. Await returns when command is sent to Hub.
await hub.motorTimeAsync('C', 5, 50);
// Continue almost immediately when command is sent to Hub
await hub.motorTimeAsync('C', 5, 50, true);
// Continue 5 seconds later
Run both motors (A and B) for specific time. Await returns when command is sent to Hub.
// Drive forward for 10 seconds
await hub.motorTimeMultiAsync(10, 20, 20, true);
// Continue 10 seconds later
Turn a motor by specific angle. Await returns when command is sent to Hub.
// Turn ~180 degrees
await hub.motorAngleAsync('B', 980, 100, true);
// Continue after the turn
Turn both motors (A and B) by specific angle. Await returns when command is sent to Hub.
// Drive forward
await hub.motorAngleMultiAsync(500, 100, 100);
// Continue immediately after command is sent to Hub
Drive specified distance. By default drive-method's return promise will resolve when the distance has been driven.
Note: Drive method is implemented with Lego Boost Vernie
// Drive forward 2 meters
await hub.drive(200);
// Continue after drive is finished
Use metric untis in drive-method. Metric is default.
// Drive forward 200 cm
await hub.drive(200);
hub.useImperialUnits();
// Drive forward 200 inches
await hub.drive(200);
Use imperial units with drive-method.
If drive method's distance is not correct, friction modifier can be changed.
// Drive forward 100cm
await hub.drive(100);
// Distance was only 90cm, update modifier
hub.setFrictionModifier(1.1);
// Drive 100cm
await hub.drive(100);
Turn specified angle to either right (positive number) or left (negative number). By default turn-method's promise will resolve when the angle has been turned.
Note: Turn method is implemented with Lego Boost Vernie
const hub = await boost.getHubAsync();
// Drive 1 meter square
await hub.drive(100);
await hub.turn(90);
await hub.drive(100);
await hub.turn(90);
await hub.drive(100);
await hub.turn(90);
await hub.drive(100);
await hub.turn(90);
Drive until the sensor shows an object in defined distance. The distance sensor is not very sensitive or accurate. By default the bot will stop when the sensor notices a wall for the first time. Sensor distance values are usualy between 110-50.
await hub.driveUntil();
Turn until sensor doesn't detect any blocking object. 1 or any positive number turns to the right (default) and 0 or any negative number turns to the left.
// Turn to the right
await hub.turnUntil();
// Turn to the right
await hub.turnUntil(1);
// Turn to the left
await hub.turnUntil(0);
lego-boost-ai has a simple AI and manual controls for Lego Boost.
Run ESLint and Mocha tests.
$ npm run test
Run only Mocha tests.
$ npm run mocha
It is possible to use development version from GitHub, which may contain unreleased features.
$ npm install git+https://git@github.com/ttu/node-movehub-async.git
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
LEGO and BOOST are Trademarks from The LEGO Company, which do not support this project.
I'm not responsible for any damage on your LEGO BOOST devices - use it at your own risk.
Licensed under the MIT License.