movehub-async
Asynchronous methods for the Lego Boost Move Hub package
movehub async
Simple to use asynchronous methods for the Lego Boost Move Hub
Setup
-
Install Noble prerequisites
-
Install movehub-async
$ npm install movehub-async
Usage
const boost = ; const hub = await boost; // Turn light from red to greenawait hub;await hub;await hub; // Turn A & B motors for 10 seconds with power 20await hub;// Turn motor C 600 degrees with power 5await hub;
It is also possible to wait that motor execution has stopped
await hub;// Continue when led is redawait hub;// Continue 10 sec laterawait hub;// Continue 5 sec laterawait hub;// Continue some time laterawait hub;// 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 forwardawait hub;// After 2 meter drive, turn 90 degrees to the rightawait hub;// Drive 1 meter backwardsawait hub;// Turn 180 degrees to the leftawait hub;
API
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
.
Boost
boost.getHubAsync()
Create a connection to the Hub. Internally calls bleReadyAsync
, hubFoundAsync
and connectAsync
.
const hub = await boost;
boost.bleReadyAsync()
Wait for BLE device to be ready.
await boost;
boost.hubFoundAsync()
Wait for MoveHub found event.
const connectDetails = await boost;
boost.connectAsync(connectDetails)
Initialize and wait for the connection to the Hub.
const hub = await boost;
Hub
hub.ledAsync(color)
Control the LED on the Move Hub.
await hub;
hub.motorTimeAsync(port, seconds, dutyCycle = 100, wait = false)
Run a motor for specific time. Await returns when command is sent to Hub.
await hub;// Continue almost immediately when command is sent to Hub await hub;// Continue 5 seconds later
hub.motorTimeMultiAsync(seconds, dutyCycleA = 100, dutyCycleB = 100, wait = false)
Run both motors (A and B) for specific time. Await returns when command is sent to Hub.
// Drive forward for 10 secondsawait hub;// Continue 10 seconds later
hub.motorAngleAsync(port, angle, dutyCycle = 100, wait = false)
Turn a motor by specific angle. Await returns when command is sent to Hub.
// Turn ~180 degrees await hub;// Continue after the turn
hub.motorAngleMultiAsync(angle, dutyCycleA = 100, dutyCycleB = 100, wait = false)
Turn both motors (A and B) by specific angle. Await returns when command is sent to Hub.
// Drive forwardawait hub;// Continue immediately after command is sent to Hub
hub.drive(centimeters, wait = true)
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 metersawait hub;// Continue after drive is finished
hub.useMetricUnits()
Use metric untis in drive-method. Metric is default.
// Drive forward 200 cmawait hub; hub; // Drive forward 200 inchesawait hub;
hub.useImperialUnits()
Use imperial units with drive-method.
hub.setFrictionModifier(modifier)
If drive method's distance is not correct, friction modifier can be changed.
// Drive forward 100cmawait hub; // Distance was only 90cm, update modifierhub; // Drive 100cmawait hub;
hub.turn(degrees, wait = true)
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;// Drive 1 meter squareawait hub;await hub;await hub;await hub;await hub;await hub;await hub;await hub;
hub.driveUntil(distance = 0, wait = true)
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;
hub.turnUntil(direction = 1, wait = true)
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 rightawait hub;// Turn to the rightawait hub;// Turn to the leftawait hub;
Example project
lego-boost-ai has a simple AI and manual controls for Lego Boost.
Tester
Use development version
It is possible to use development version from GitHub, which may contain unreleased features.
$ npm install git+https://[email protected]/ttu/node-movehub-async.git
Changelog
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
License
Licensed under the MIT License.