Giter Site home page Giter Site logo

brickpi's Introduction

node-brickpi

Node.js bindings for the BrickPi.

Usage

var BrickPi = require('brickpi')

var brickPi = new BrickPi.Board('/dev/ttyAMA0', function() {
  brickPi.led(0).on()
})

Constructor options

All arguments are optional and order is not important.

Pass a string (path to a serial device), an options object and a callback function to invoke when the BrickPi is ready to use.

Default options are as follows:

var brickPi = new BrickPi.Board({
    baudrate: 500000, // the speed of the connection to the BrickPi
    timeout: 10000, // stop the motors if no communication is received in this time period (ms)
    debug: false // whether to print verbose debug output
})

LED

The BrickPi has two LEDs that can be accessed by passing an index to the led function:

var led1 = brickPi.led(0)
var led2 = brickPi.led(1)

An LED supports several methods:

brickPi.led(0).on() // turn the LED on
brickPi.led(0).off() // turn the LED off
brickPi.led(0).toggle() // if the LED is on, turn it off, otherwise turn it on

Motors

var motor = brickPi.addMotor(new BrickPi.Motor(), BrickPi.PORTS.MA)

// later
motor.speed(255) // continuous motion - full speed ahead
motor.speed(0) // stop
motor.speed(-255) // reverse

// rotation
motor.rotate(180) // rotate 180 degrees
motor.rotate(-180) // rotate 180 degrees in the opposite direction

// optionally specify a speed
motor.rotate(180, 255) // rotate 180 degrees at full speed
motor.rotate(180, 128) // rotate 180 degrees at half speed

brickPi.once('emergencyStop', function() {
    console.info('stopped!')
})
brickPi.emergencyStop() // immediately stop all motors

Sensors

Valid sensor types are Distance, Light, Sound and Touch.

// add sensors
var distance = brickPi.addSensor(new BrickPi.Sensors.NXT.Distance(), BrickPi.PORTS.S1)
var light = brickPi.addSensor(new BrickPi.Sensors.NXT.Light(), BrickPi.PORTS.S2)
var sound = brickPi.addSensor(new BrickPi.Sensors.NXT.Sound(), BrickPi.PORTS.S3)
var touch = brickPi.addSensor(new BrickPi.Sensors.NXT.Touch(), BrickPi.PORTS.S4)

// later
distance.value(function(error, value) {
    // value is 0-255 in cm
}) 
light.value(function(error, value) {
  // value is 0-100 in %
})
sound.value(function(error, value) {
  // value is 0-100 in %
})
touch.value(function(error, value) {
  // value is true or false
})

Notes

What about EV3, NXT2, etc?

I only have the NXT kit, sorry. Hardware donations and/or pull requests gratefully accepted.

brickpi's People

Contributors

achingbrain avatar

Stargazers

Michael Tsyganov avatar  avatar

Watchers

James Cloos avatar  avatar

brickpi's Issues

Does it work with a current stack?

Hi there, the brickPi nodes lib from dexterindustries doesn't work anymore so I'm searching for an alternative. Does your library work with a stack like this?:

  • nodejs 7.x
  • brickPi 3
  • NXT servo from lego

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.