r-pi-brick v1.0.3
r-pi-brick
A Node.js API for the BrickPi.
Getting Started
Installation
npm install r-pi-brick --save
Integration
var brick = require('r-pi-brick');
API
Overview
- Events
- brick.init([maxRetries])
- brick.isMotorEnabled(port)
- brick.setMotorEnabled(enabled, port)
- brick.getMotorPosition(port)
- brick.getMotorPower(port)
- brick.setMotorPower(power, port)
- brick.getMotorSpeed(port)
- brick.MOTOR_PORT_A
- brick.MOTOR_PORT_B
- brick.MOTOR_PORT_C
- brick.MOTOR_PORT_D
Events
The error
Event
brick.on('error', function (error) {
// ...
});
The init
Event
brick.on('init', function () {
// ...
});
The update
Event
brick.on('update', function () {
// ...
});
brick.init(maxRetries)
Initializes the serial connection to the BrickPi. Updates all brick values, every 10 ms, via the serial connection. Retries an update if a serial communication timeout occurs. The number of maximum retries has a default value of 3.
Emits an error if the number of maximum retries is exceeded.
brick.init(3);
brick.isMotorEnabled(port)
Returns the enabled flag of the motor plugged into the specified port.
var enabledA = brick.isMotorEnabled(brick.MOTOR_PORT_A);
var enabledB = brick.isMotorEnabled(brick.MOTOR_PORT_B);
var enabledC = brick.isMotorEnabled(brick.MOTOR_PORT_C);
var enabledD = brick.isMotorEnabled(brick.MOTOR_PORT_D);
brick.setMotorEnabled(enabled, port)
Sets the enabled flag of the motor plugged into the specified port.
brick.setMotorEnabled(true, brick.MOTOR_PORT_A);
brick.setMotorEnabled(true, brick.MOTOR_PORT_B);
brick.setMotorEnabled(false, brick.MOTOR_PORT_C);
brick.setMotorEnabled(false, brick.MOTOR_PORT_D);
brick.getMotorPosition(port)
Returns the position of the motor, in 0.5-degree increments, plugged into the specified port. The position is an integer in the range of -1073741823 to 1073741823. Divide by 2 to get degrees.
var positionA = brick.getMotorPosition(brick.MOTOR_PORT_A);
var positionB = brick.getMotorPosition(brick.MOTOR_PORT_B);
var positionC = brick.getMotorPosition(brick.MOTOR_PORT_C);
var positionD = brick.getMotorPosition(brick.MOTOR_PORT_D);
brick.getMotorPower(port)
Returns the power of the motor plugged into the specified port. The power is an integer in the range of -100 to 100.
var powerA = brick.getMotorPower(brick.MOTOR_PORT_A);
var powerB = brick.getMotorPower(brick.MOTOR_PORT_B);
var powerC = brick.getMotorPower(brick.MOTOR_PORT_C);
var powerD = brick.getMotorPower(brick.MOTOR_PORT_D);
brick.setMotorPower(power, port)
Sets the power of the motor plugged into the specified port. The power must be an integer in the range of -100 to 100.
brick.setMotorPower(100, brick.MOTOR_PORT_A);
brick.setMotorPower(100, brick.MOTOR_PORT_B);
brick.setMotorPower(-100, brick.MOTOR_PORT_C);
brick.setMotorPower(-100, brick.MOTOR_PORT_D);
brick.getMotorSpeed(port)
Returns the speed of the motor, in degrees per second, plugged into the specified port.
var speedA = brick.getMotorSpeed(brick.MOTOR_PORT_A);
var speedB = brick.getMotorSpeed(brick.MOTOR_PORT_B);
var speedC = brick.getMotorSpeed(brick.MOTOR_PORT_C);
var speedD = brick.getMotorSpeed(brick.MOTOR_PORT_D);
brick.MOTOR_PORT_A
This port should be used together with port B.
brick.MOTOR_PORT_B
This port should be used together with port A.
brick.MOTOR_PORT_C
This port should be used together with port D.
brick.MOTOR_PORT_D
This port should be used together with port C.
Example
This example rotates two motors A and B alternately in opposite directions.
node node_modules/r-pi-brick/example/rotator.js
Running Tests
To run the test suite first install the development dependencies:
npm install
then run the tests:
npm test