This library provides a simple way to control SBrick bluetooth controller for Lego in NodeJS. It focuses on controlling vehicle that can move in four directions: forward, backward, left and right.
npm install sbrick-drive
SBrick Drive is designed to handle instruction for four directional movements: forward, backward, left and right using
the four control channels on a SBrick: 0, 1, 2 and 3. SBrickDrive needs to know which channels are used for forward and
backward instruction (DRIVE_CHANNELS) and which channels are used for left and right steering (STEERING_CHANNEL).
E.g. My 4X4 Crawler uses channel 0 and 1 for driving forward and
backward, and channel 2 for steering.
const DRIVE_CHANNELS = [0, 1],
STEERING_CHANNELS = [2],
SBrickDrive = require('sbrick-drive');
const sbDrive = new SBrickDrive(DRIVE_CHANNELS, STEERING_CHANNELS);
const onConnected = function() {
sbDrive.simpleForward();
};
sbDrive.connect(onConnected);Three simple UIs have been created to control a SBrick-equipped vehicle using this project.
-
Drive by Keyboard
npm run drive-by-keyboardDrives a SBrick-equipped vehicle with arrow keys on keyboard of a computer. -
Drive by Script
npm run drive-by-scriptDrives a SBrick-equipped vehicle with instructions written in a text file, one instruction per line (my daughter loved practicing typing with this). A sample instruction file can be found atsrc/clients/script/instructions.txt. -
Drive by REST
npm run drive-by-restDrives a SBrick-equipped vehicle with RESTful client. This task starts a Express server that provides the following RESTful API:GET /drive/forwardGET /drive/backwardGET /drive/leftGET /drive/rightGET /resolveActionFromText/:commandUse DialogFlow to convert free text/speech command into one of the directional command for SBrick.
A simple web interface was created in
src/clients/rest/web/index.html, which uses HTML5 speech recognition for the four directional commands (my daughter loved this too).
ISC
Drop me a line at [email protected]